1 // (C) Copyright Gennadiy Rozental 2001.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 // See http://www.boost.org/libs/test for the library home page.
10 // Version : $Revision$
12 // Description : simple facility that mimmic notion of read-only read-write
13 // properties in C++ classes. Original idea by Henrik Ravn.
14 // ***************************************************************************
16 #ifndef BOOST_TEST_UTILS_CLASS_PROPERTIES_HPP
17 #define BOOST_TEST_UTILS_CLASS_PROPERTIES_HPP
20 #include <boost/test/detail/config.hpp>
23 #if !BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600))
24 #include <boost/preprocessor/seq/for_each.hpp>
26 #include <boost/call_traits.hpp>
27 #include <boost/type_traits/add_pointer.hpp>
28 #include <boost/type_traits/add_const.hpp>
29 #include <boost/utility/addressof.hpp>
34 #include <boost/test/detail/suppress_warnings.hpp>
36 //____________________________________________________________________________//
41 // ************************************************************************** //
42 // ************** class_property ************** //
43 // ************************************************************************** //
45 template<class PropertyType>
46 class class_property {
48 typedef typename call_traits<PropertyType>::const_reference read_access_t;
49 typedef typename call_traits<PropertyType>::param_type write_param_t;
50 typedef typename add_pointer<typename add_const<PropertyType>::type>::type address_res_t;
53 class_property() : value( PropertyType() ) {}
54 explicit class_property( write_param_t init_value )
55 : value( init_value ) {}
58 operator read_access_t() const { return value; }
59 read_access_t get() const { return value; }
60 bool operator!() const { return !value; }
61 address_res_t operator&() const { return &value; }
64 #ifndef BOOST_TEST_NO_PROTECTED_USING
70 //____________________________________________________________________________//
72 #ifdef BOOST_CLASSIC_IOSTREAMS
74 template<class PropertyType>
76 operator<<( std::ostream& os, class_property<PropertyType> const& p )
80 template<typename CharT1, typename Tr,class PropertyType>
81 inline std::basic_ostream<CharT1,Tr>&
82 operator<<( std::basic_ostream<CharT1,Tr>& os, class_property<PropertyType> const& p )
89 //____________________________________________________________________________//
91 #define DEFINE_PROPERTY_FREE_BINARY_OPERATOR( op ) \
92 template<class PropertyType> \
94 operator op( PropertyType const& lhs, class_property<PropertyType> const& rhs ) \
96 return lhs op rhs.get(); \
98 template<class PropertyType> \
100 operator op( class_property<PropertyType> const& lhs, PropertyType const& rhs ) \
102 return lhs.get() op rhs; \
104 template<class PropertyType> \
106 operator op( class_property<PropertyType> const& lhs, \
107 class_property<PropertyType> const& rhs ) \
109 return lhs.get() op rhs.get(); \
113 DEFINE_PROPERTY_FREE_BINARY_OPERATOR( == )
114 DEFINE_PROPERTY_FREE_BINARY_OPERATOR( != )
116 #undef DEFINE_PROPERTY_FREE_BINARY_OPERATOR
118 // ************************************************************************** //
119 // ************** readonly_property ************** //
120 // ************************************************************************** //
122 template<class PropertyType>
123 class readonly_property : public class_property<PropertyType> {
124 typedef class_property<PropertyType> base_prop;
125 typedef typename base_prop::address_res_t arrow_res_t;
127 typedef typename base_prop::write_param_t write_param_t;
130 readonly_property() {}
131 explicit readonly_property( write_param_t init_value ) : base_prop( init_value ) {}
134 arrow_res_t operator->() const { return boost::addressof( base_prop::value ); }
137 //____________________________________________________________________________//
139 #if BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600))
141 #define BOOST_READONLY_PROPERTY( property_type, friends ) boost::unit_test::readwrite_property<property_type >
145 #define BOOST_READONLY_PROPERTY_DECLARE_FRIEND(r, data, elem) friend class elem;
147 #define BOOST_READONLY_PROPERTY( property_type, friends ) \
148 class BOOST_JOIN( readonly_property, __LINE__ ) \
149 : public boost::unit_test::readonly_property<property_type > { \
150 typedef boost::unit_test::readonly_property<property_type > base_prop; \
151 BOOST_PP_SEQ_FOR_EACH( BOOST_READONLY_PROPERTY_DECLARE_FRIEND, ' ', friends ) \
152 typedef base_prop::write_param_t write_param_t; \
154 BOOST_JOIN( readonly_property, __LINE__ )() {} \
155 explicit BOOST_JOIN( readonly_property, __LINE__ )( write_param_t init_v ) \
156 : base_prop( init_v ) {} \
162 // ************************************************************************** //
163 // ************** readwrite_property ************** //
164 // ************************************************************************** //
166 template<class PropertyType>
167 class readwrite_property : public class_property<PropertyType> {
168 typedef class_property<PropertyType> base_prop;
169 typedef typename add_pointer<PropertyType>::type arrow_res_t;
170 typedef typename base_prop::address_res_t const_arrow_res_t;
171 typedef typename base_prop::write_param_t write_param_t;
173 readwrite_property() : base_prop() {}
174 explicit readwrite_property( write_param_t init_value ) : base_prop( init_value ) {}
177 void set( write_param_t v ) { base_prop::value = v; }
178 arrow_res_t operator->() { return boost::addressof( base_prop::value ); }
179 const_arrow_res_t operator->() const { return boost::addressof( base_prop::value ); }
181 #ifndef BOOST_TEST_NO_PROTECTED_USING
182 using base_prop::value;
186 //____________________________________________________________________________//
191 #include <boost/test/detail/enable_warnings.hpp>
193 #undef BOOST_TEST_NO_PROTECTED_USING
195 #endif // BOOST_TEST_UTILS_CLASS_PROPERTIES_HPP