Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:52:42

0001 //  (C) Copyright Gennadiy Rozental 2001.
0002 //  Distributed under the Boost Software License, Version 1.0.
0003 //  (See accompanying file LICENSE_1_0.txt or copy at
0004 //  http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 //  See http://www.boost.org/libs/test for the library home page.
0007 //
0008 //  File        : $RCSfile$
0009 //
0010 //  Version     : $Revision$
0011 //
0012 //  Description : model of actual argument (both typed and abstract interface)
0013 // ***************************************************************************
0014 
0015 #ifndef BOOST_TEST_UTILS_RUNTIME_ARGUMENT_HPP
0016 #define BOOST_TEST_UTILS_RUNTIME_ARGUMENT_HPP
0017 
0018 // Boost.Test Runtime parameters
0019 #include <boost/test/utils/runtime/fwd.hpp>
0020 #include <boost/test/utils/runtime/errors.hpp>
0021 
0022 // Boost.Test
0023 #include <boost/test/utils/class_properties.hpp>
0024 #include <boost/test/utils/rtti.hpp>
0025 #include <boost/test/utils/basic_cstring/compare.hpp>
0026 #include <boost/test/detail/throw_exception.hpp>
0027 
0028 // STL
0029 #include <cassert>
0030 
0031 #include <boost/test/detail/suppress_warnings.hpp>
0032 
0033 namespace boost {
0034 namespace runtime {
0035 
0036 // ************************************************************************** //
0037 // **************              runtime::argument               ************** //
0038 // ************************************************************************** //
0039 
0040 class argument {
0041 public:
0042     // Constructor
0043     argument( rtti::id_t value_type )
0044     : p_value_type( value_type )
0045     {}
0046 
0047     // Destructor
0048     virtual     ~argument()  {}
0049 
0050     // Public properties
0051     rtti::id_t const    p_value_type;
0052 };
0053 
0054 // ************************************************************************** //
0055 // **************             runtime::typed_argument          ************** //
0056 // ************************************************************************** //
0057 
0058 template<typename T>
0059 class typed_argument : public argument {
0060 public:
0061     // Constructor
0062     explicit typed_argument( T const& v )
0063     : argument( rtti::type_id<T>() )
0064     , p_value( v )
0065     {}
0066 
0067     unit_test::readwrite_property<T>    p_value;
0068 };
0069 
0070 // ************************************************************************** //
0071 // **************           runtime::arguments_store          ************** //
0072 // ************************************************************************** //
0073 
0074 class arguments_store {
0075 public:
0076     typedef std::map<cstring, argument_ptr> storage_type;
0077 
0078     /// Returns number of arguments in the store; mostly used for testing
0079     std::size_t size() const        { return m_arguments.size(); }
0080 
0081     /// Clears the store for reuse
0082     void        clear()             { m_arguments.clear(); }
0083 
0084     /// Returns true if there is an argument corresponding to the specified parameter name
0085     bool        has( cstring parameter_name ) const
0086     {
0087         return m_arguments.find( parameter_name ) != m_arguments.end();
0088     }
0089 
0090     /// Provides types access to argument value by parameter name
0091     template<typename T>
0092     T const&    get( cstring parameter_name ) const {
0093         return const_cast<arguments_store*>(this)->get<T>( parameter_name );
0094     }
0095 
0096     template<typename T>
0097     T&          get( cstring parameter_name ) {
0098         storage_type::const_iterator found = m_arguments.find( parameter_name );
0099         BOOST_TEST_I_ASSRT( found != m_arguments.end(),
0100                             access_to_missing_argument() 
0101                                 << "There is no argument provided for parameter "
0102                                 << parameter_name );
0103 
0104         argument_ptr arg = found->second;
0105 
0106         BOOST_TEST_I_ASSRT( arg->p_value_type == rtti::type_id<T>(),
0107                             arg_type_mismatch()
0108                                 << "Access with invalid type for argument corresponding to parameter "
0109                                 << parameter_name );
0110 
0111         return static_cast<typed_argument<T>&>( *arg ).p_value.value;
0112     }
0113 
0114     /// Set's the argument value for specified parameter name
0115     template<typename T>
0116     void        set( cstring parameter_name, T const& value )
0117     {
0118         m_arguments[parameter_name] = argument_ptr( new typed_argument<T>( value ) );
0119     }
0120 
0121 private:
0122     // Data members
0123     storage_type            m_arguments;
0124 };
0125 
0126 } // namespace runtime
0127 } // namespace boost
0128 
0129 #include <boost/test/detail/enable_warnings.hpp>
0130 
0131 #endif // BOOST_TEST_UTILS_RUNTIME_ARGUMENT_HPP