Chris@16: // (C) Copyright Gennadiy Rozental 2001-2008. Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See http://www.boost.org/libs/test for the library home page. Chris@16: // Chris@16: // File : $RCSfile$ Chris@16: // Chris@16: // Version : $Revision: 54633 $ Chris@16: // Chris@16: // Description : simple facility that mimmic notion of read-only read-write Chris@16: // properties in C++ classes. Original idea by Henrik Ravn. Chris@16: // *************************************************************************** Chris@16: Chris@16: #ifndef BOOST_TEST_CLASS_PROPERTIES_HPP_071894GER Chris@16: #define BOOST_TEST_CLASS_PROPERTIES_HPP_071894GER Chris@16: Chris@16: // Boost.Test Chris@16: #include Chris@16: Chris@16: // Boost Chris@16: #if !BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600)) Chris@16: #include Chris@16: #endif Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: // STL Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: //____________________________________________________________________________// Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: namespace unit_test { Chris@16: Chris@16: // ************************************************************************** // Chris@16: // ************** class_property ************** // Chris@16: // ************************************************************************** // Chris@16: Chris@16: template Chris@16: class class_property { Chris@16: protected: Chris@16: typedef typename call_traits::const_reference read_access_t; Chris@16: typedef typename call_traits::param_type write_param_t; Chris@16: typedef typename add_pointer::type>::type address_res_t; Chris@16: public: Chris@16: // Constructor Chris@16: class_property() : value( PropertyType() ) {} Chris@16: explicit class_property( write_param_t init_value ) Chris@16: : value( init_value ) {} Chris@16: Chris@16: // Access methods Chris@16: operator read_access_t() const { return value; } Chris@16: read_access_t get() const { return value; } Chris@16: bool operator!() const { return !value; } Chris@16: address_res_t operator&() const { return &value; } Chris@16: Chris@16: // Data members Chris@16: #ifndef BOOST_TEST_NO_PROTECTED_USING Chris@16: protected: Chris@16: #endif Chris@16: PropertyType value; Chris@16: }; Chris@16: Chris@16: //____________________________________________________________________________// Chris@16: Chris@16: #ifdef BOOST_CLASSIC_IOSTREAMS Chris@16: Chris@16: template Chris@16: inline std::ostream& Chris@16: operator<<( std::ostream& os, class_property const& p ) Chris@16: Chris@16: #else Chris@16: Chris@16: template Chris@16: inline std::basic_ostream& Chris@16: operator<<( std::basic_ostream& os, class_property const& p ) Chris@16: Chris@16: #endif Chris@16: { Chris@16: return os << p.get(); Chris@16: } Chris@16: Chris@16: //____________________________________________________________________________// Chris@16: Chris@16: #define DEFINE_PROPERTY_FREE_BINARY_OPERATOR( op ) \ Chris@16: template \ Chris@16: inline bool \ Chris@16: operator op( PropertyType const& lhs, class_property const& rhs ) \ Chris@16: { \ Chris@16: return lhs op rhs.get(); \ Chris@16: } \ Chris@16: template \ Chris@16: inline bool \ Chris@16: operator op( class_property const& lhs, PropertyType const& rhs ) \ Chris@16: { \ Chris@16: return lhs.get() op rhs; \ Chris@16: } \ Chris@16: template \ Chris@16: inline bool \ Chris@16: operator op( class_property const& lhs, \ Chris@16: class_property const& rhs ) \ Chris@16: { \ Chris@16: return lhs.get() op rhs.get(); \ Chris@16: } \ Chris@16: /**/ Chris@16: Chris@16: DEFINE_PROPERTY_FREE_BINARY_OPERATOR( == ) Chris@16: DEFINE_PROPERTY_FREE_BINARY_OPERATOR( != ) Chris@16: Chris@16: #undef DEFINE_PROPERTY_FREE_BINARY_OPERATOR Chris@16: Chris@16: #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) Chris@16: Chris@16: #define DEFINE_PROPERTY_LOGICAL_OPERATOR( op ) \ Chris@16: template \ Chris@16: inline bool \ Chris@16: operator op( bool b, class_property const& p ) \ Chris@16: { \ Chris@16: return b op p.get(); \ Chris@16: } \ Chris@16: template \ Chris@16: inline bool \ Chris@16: operator op( class_property const& p, bool b ) \ Chris@16: { \ Chris@16: return b op p.get(); \ Chris@16: } \ Chris@16: /**/ Chris@16: Chris@16: DEFINE_PROPERTY_LOGICAL_OPERATOR( && ) Chris@16: DEFINE_PROPERTY_LOGICAL_OPERATOR( || ) Chris@16: Chris@16: #endif Chris@16: Chris@16: // ************************************************************************** // Chris@16: // ************** readonly_property ************** // Chris@16: // ************************************************************************** // Chris@16: Chris@16: template Chris@16: class readonly_property : public class_property { Chris@16: typedef class_property base_prop; Chris@16: typedef typename base_prop::address_res_t arrow_res_t; Chris@16: protected: Chris@16: typedef typename base_prop::write_param_t write_param_t; Chris@16: public: Chris@16: // Constructor Chris@16: readonly_property() {} Chris@16: explicit readonly_property( write_param_t init_value ) : base_prop( init_value ) {} Chris@16: Chris@16: // access methods Chris@16: arrow_res_t operator->() const { return boost::addressof( base_prop::value ); } Chris@16: }; Chris@16: Chris@16: //____________________________________________________________________________// Chris@16: Chris@16: #if BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600)) Chris@16: Chris@16: #define BOOST_READONLY_PROPERTY( property_type, friends ) boost::unit_test::readwrite_property Chris@16: Chris@16: #else Chris@16: Chris@16: #define BOOST_READONLY_PROPERTY_DECLARE_FRIEND(r, data, elem) friend class elem; Chris@16: Chris@16: #define BOOST_READONLY_PROPERTY( property_type, friends ) \ Chris@16: class BOOST_JOIN( readonly_property, __LINE__ ) \ Chris@16: : public boost::unit_test::readonly_property { \ Chris@16: typedef boost::unit_test::readonly_property base_prop; \ Chris@16: BOOST_PP_SEQ_FOR_EACH( BOOST_READONLY_PROPERTY_DECLARE_FRIEND, ' ', friends ) \ Chris@16: typedef base_prop::write_param_t write_param_t; \ Chris@16: public: \ Chris@16: BOOST_JOIN( readonly_property, __LINE__ )() {} \ Chris@16: explicit BOOST_JOIN( readonly_property, __LINE__ )( write_param_t init_v ) \ Chris@16: : base_prop( init_v ) {} \ Chris@16: } \ Chris@16: /**/ Chris@16: Chris@16: #endif Chris@16: Chris@16: // ************************************************************************** // Chris@16: // ************** readwrite_property ************** // Chris@16: // ************************************************************************** // Chris@16: Chris@16: template Chris@16: class readwrite_property : public class_property { Chris@16: typedef class_property base_prop; Chris@16: typedef typename add_pointer::type arrow_res_t; Chris@16: typedef typename base_prop::address_res_t const_arrow_res_t; Chris@16: typedef typename base_prop::write_param_t write_param_t; Chris@16: public: Chris@16: readwrite_property() : base_prop() {} Chris@16: explicit readwrite_property( write_param_t init_value ) : base_prop( init_value ) {} Chris@16: Chris@16: // access methods Chris@16: void set( write_param_t v ) { base_prop::value = v; } Chris@16: arrow_res_t operator->() { return boost::addressof( base_prop::value ); } Chris@16: const_arrow_res_t operator->() const { return boost::addressof( base_prop::value ); } Chris@16: Chris@16: #ifndef BOOST_TEST_NO_PROTECTED_USING Chris@16: using base_prop::value; Chris@16: #endif Chris@16: }; Chris@16: Chris@16: //____________________________________________________________________________// Chris@16: Chris@16: } // unit_test Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: //____________________________________________________________________________// Chris@16: Chris@16: #include Chris@16: Chris@16: #undef BOOST_TEST_NO_PROTECTED_USING Chris@16: Chris@16: #endif // BOOST_TEST_CLASS_PROPERTIES_HPP_071894GER