Chris@16
|
1 // (C) Copyright Gennadiy Rozental 2001-2008.
|
Chris@16
|
2 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
3 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
4 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5
|
Chris@16
|
6 // See http://www.boost.org/libs/test for the library home page.
|
Chris@16
|
7 //
|
Chris@16
|
8 // File : $RCSfile$
|
Chris@16
|
9 //
|
Chris@101
|
10 // Version : $Revision$
|
Chris@16
|
11 //
|
Chris@16
|
12 // Description : simple facility that mimmic notion of read-only read-write
|
Chris@16
|
13 // properties in C++ classes. Original idea by Henrik Ravn.
|
Chris@16
|
14 // ***************************************************************************
|
Chris@16
|
15
|
Chris@16
|
16 #ifndef BOOST_TEST_CLASS_PROPERTIES_HPP_071894GER
|
Chris@16
|
17 #define BOOST_TEST_CLASS_PROPERTIES_HPP_071894GER
|
Chris@16
|
18
|
Chris@16
|
19 // Boost.Test
|
Chris@16
|
20 #include <boost/test/detail/config.hpp>
|
Chris@16
|
21
|
Chris@16
|
22 // Boost
|
Chris@16
|
23 #if !BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600))
|
Chris@16
|
24 #include <boost/preprocessor/seq/for_each.hpp>
|
Chris@16
|
25 #endif
|
Chris@16
|
26 #include <boost/call_traits.hpp>
|
Chris@16
|
27 #include <boost/type_traits/add_pointer.hpp>
|
Chris@16
|
28 #include <boost/type_traits/add_const.hpp>
|
Chris@16
|
29 #include <boost/utility/addressof.hpp>
|
Chris@16
|
30
|
Chris@16
|
31 // STL
|
Chris@16
|
32 #include <iosfwd>
|
Chris@16
|
33
|
Chris@16
|
34 #include <boost/test/detail/suppress_warnings.hpp>
|
Chris@16
|
35
|
Chris@16
|
36 //____________________________________________________________________________//
|
Chris@16
|
37
|
Chris@16
|
38 namespace boost {
|
Chris@16
|
39
|
Chris@16
|
40 namespace unit_test {
|
Chris@16
|
41
|
Chris@16
|
42 // ************************************************************************** //
|
Chris@16
|
43 // ************** class_property ************** //
|
Chris@16
|
44 // ************************************************************************** //
|
Chris@16
|
45
|
Chris@16
|
46 template<class PropertyType>
|
Chris@16
|
47 class class_property {
|
Chris@16
|
48 protected:
|
Chris@16
|
49 typedef typename call_traits<PropertyType>::const_reference read_access_t;
|
Chris@16
|
50 typedef typename call_traits<PropertyType>::param_type write_param_t;
|
Chris@16
|
51 typedef typename add_pointer<typename add_const<PropertyType>::type>::type address_res_t;
|
Chris@16
|
52 public:
|
Chris@16
|
53 // Constructor
|
Chris@16
|
54 class_property() : value( PropertyType() ) {}
|
Chris@16
|
55 explicit class_property( write_param_t init_value )
|
Chris@16
|
56 : value( init_value ) {}
|
Chris@16
|
57
|
Chris@16
|
58 // Access methods
|
Chris@16
|
59 operator read_access_t() const { return value; }
|
Chris@16
|
60 read_access_t get() const { return value; }
|
Chris@16
|
61 bool operator!() const { return !value; }
|
Chris@16
|
62 address_res_t operator&() const { return &value; }
|
Chris@16
|
63
|
Chris@16
|
64 // Data members
|
Chris@16
|
65 #ifndef BOOST_TEST_NO_PROTECTED_USING
|
Chris@16
|
66 protected:
|
Chris@16
|
67 #endif
|
Chris@16
|
68 PropertyType value;
|
Chris@16
|
69 };
|
Chris@16
|
70
|
Chris@16
|
71 //____________________________________________________________________________//
|
Chris@16
|
72
|
Chris@16
|
73 #ifdef BOOST_CLASSIC_IOSTREAMS
|
Chris@16
|
74
|
Chris@16
|
75 template<class PropertyType>
|
Chris@16
|
76 inline std::ostream&
|
Chris@16
|
77 operator<<( std::ostream& os, class_property<PropertyType> const& p )
|
Chris@16
|
78
|
Chris@16
|
79 #else
|
Chris@16
|
80
|
Chris@16
|
81 template<typename CharT1, typename Tr,class PropertyType>
|
Chris@16
|
82 inline std::basic_ostream<CharT1,Tr>&
|
Chris@16
|
83 operator<<( std::basic_ostream<CharT1,Tr>& os, class_property<PropertyType> const& p )
|
Chris@16
|
84
|
Chris@16
|
85 #endif
|
Chris@16
|
86 {
|
Chris@16
|
87 return os << p.get();
|
Chris@16
|
88 }
|
Chris@16
|
89
|
Chris@16
|
90 //____________________________________________________________________________//
|
Chris@16
|
91
|
Chris@16
|
92 #define DEFINE_PROPERTY_FREE_BINARY_OPERATOR( op ) \
|
Chris@16
|
93 template<class PropertyType> \
|
Chris@16
|
94 inline bool \
|
Chris@16
|
95 operator op( PropertyType const& lhs, class_property<PropertyType> const& rhs ) \
|
Chris@16
|
96 { \
|
Chris@16
|
97 return lhs op rhs.get(); \
|
Chris@16
|
98 } \
|
Chris@16
|
99 template<class PropertyType> \
|
Chris@16
|
100 inline bool \
|
Chris@16
|
101 operator op( class_property<PropertyType> const& lhs, PropertyType const& rhs ) \
|
Chris@16
|
102 { \
|
Chris@16
|
103 return lhs.get() op rhs; \
|
Chris@16
|
104 } \
|
Chris@16
|
105 template<class PropertyType> \
|
Chris@16
|
106 inline bool \
|
Chris@16
|
107 operator op( class_property<PropertyType> const& lhs, \
|
Chris@16
|
108 class_property<PropertyType> const& rhs ) \
|
Chris@16
|
109 { \
|
Chris@16
|
110 return lhs.get() op rhs.get(); \
|
Chris@16
|
111 } \
|
Chris@16
|
112 /**/
|
Chris@16
|
113
|
Chris@16
|
114 DEFINE_PROPERTY_FREE_BINARY_OPERATOR( == )
|
Chris@16
|
115 DEFINE_PROPERTY_FREE_BINARY_OPERATOR( != )
|
Chris@16
|
116
|
Chris@16
|
117 #undef DEFINE_PROPERTY_FREE_BINARY_OPERATOR
|
Chris@16
|
118
|
Chris@16
|
119 #if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
|
Chris@16
|
120
|
Chris@16
|
121 #define DEFINE_PROPERTY_LOGICAL_OPERATOR( op ) \
|
Chris@16
|
122 template<class PropertyType> \
|
Chris@16
|
123 inline bool \
|
Chris@16
|
124 operator op( bool b, class_property<PropertyType> const& p ) \
|
Chris@16
|
125 { \
|
Chris@16
|
126 return b op p.get(); \
|
Chris@16
|
127 } \
|
Chris@16
|
128 template<class PropertyType> \
|
Chris@16
|
129 inline bool \
|
Chris@16
|
130 operator op( class_property<PropertyType> const& p, bool b ) \
|
Chris@16
|
131 { \
|
Chris@16
|
132 return b op p.get(); \
|
Chris@16
|
133 } \
|
Chris@16
|
134 /**/
|
Chris@16
|
135
|
Chris@16
|
136 DEFINE_PROPERTY_LOGICAL_OPERATOR( && )
|
Chris@16
|
137 DEFINE_PROPERTY_LOGICAL_OPERATOR( || )
|
Chris@16
|
138
|
Chris@16
|
139 #endif
|
Chris@16
|
140
|
Chris@16
|
141 // ************************************************************************** //
|
Chris@16
|
142 // ************** readonly_property ************** //
|
Chris@16
|
143 // ************************************************************************** //
|
Chris@16
|
144
|
Chris@16
|
145 template<class PropertyType>
|
Chris@16
|
146 class readonly_property : public class_property<PropertyType> {
|
Chris@16
|
147 typedef class_property<PropertyType> base_prop;
|
Chris@16
|
148 typedef typename base_prop::address_res_t arrow_res_t;
|
Chris@16
|
149 protected:
|
Chris@16
|
150 typedef typename base_prop::write_param_t write_param_t;
|
Chris@16
|
151 public:
|
Chris@16
|
152 // Constructor
|
Chris@16
|
153 readonly_property() {}
|
Chris@16
|
154 explicit readonly_property( write_param_t init_value ) : base_prop( init_value ) {}
|
Chris@16
|
155
|
Chris@16
|
156 // access methods
|
Chris@16
|
157 arrow_res_t operator->() const { return boost::addressof( base_prop::value ); }
|
Chris@16
|
158 };
|
Chris@16
|
159
|
Chris@16
|
160 //____________________________________________________________________________//
|
Chris@16
|
161
|
Chris@16
|
162 #if BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600))
|
Chris@16
|
163
|
Chris@16
|
164 #define BOOST_READONLY_PROPERTY( property_type, friends ) boost::unit_test::readwrite_property<property_type >
|
Chris@16
|
165
|
Chris@16
|
166 #else
|
Chris@16
|
167
|
Chris@16
|
168 #define BOOST_READONLY_PROPERTY_DECLARE_FRIEND(r, data, elem) friend class elem;
|
Chris@16
|
169
|
Chris@16
|
170 #define BOOST_READONLY_PROPERTY( property_type, friends ) \
|
Chris@16
|
171 class BOOST_JOIN( readonly_property, __LINE__ ) \
|
Chris@16
|
172 : public boost::unit_test::readonly_property<property_type > { \
|
Chris@16
|
173 typedef boost::unit_test::readonly_property<property_type > base_prop; \
|
Chris@16
|
174 BOOST_PP_SEQ_FOR_EACH( BOOST_READONLY_PROPERTY_DECLARE_FRIEND, ' ', friends ) \
|
Chris@16
|
175 typedef base_prop::write_param_t write_param_t; \
|
Chris@16
|
176 public: \
|
Chris@16
|
177 BOOST_JOIN( readonly_property, __LINE__ )() {} \
|
Chris@16
|
178 explicit BOOST_JOIN( readonly_property, __LINE__ )( write_param_t init_v ) \
|
Chris@16
|
179 : base_prop( init_v ) {} \
|
Chris@16
|
180 } \
|
Chris@16
|
181 /**/
|
Chris@16
|
182
|
Chris@16
|
183 #endif
|
Chris@16
|
184
|
Chris@16
|
185 // ************************************************************************** //
|
Chris@16
|
186 // ************** readwrite_property ************** //
|
Chris@16
|
187 // ************************************************************************** //
|
Chris@16
|
188
|
Chris@16
|
189 template<class PropertyType>
|
Chris@16
|
190 class readwrite_property : public class_property<PropertyType> {
|
Chris@16
|
191 typedef class_property<PropertyType> base_prop;
|
Chris@16
|
192 typedef typename add_pointer<PropertyType>::type arrow_res_t;
|
Chris@16
|
193 typedef typename base_prop::address_res_t const_arrow_res_t;
|
Chris@16
|
194 typedef typename base_prop::write_param_t write_param_t;
|
Chris@16
|
195 public:
|
Chris@16
|
196 readwrite_property() : base_prop() {}
|
Chris@16
|
197 explicit readwrite_property( write_param_t init_value ) : base_prop( init_value ) {}
|
Chris@16
|
198
|
Chris@16
|
199 // access methods
|
Chris@16
|
200 void set( write_param_t v ) { base_prop::value = v; }
|
Chris@16
|
201 arrow_res_t operator->() { return boost::addressof( base_prop::value ); }
|
Chris@16
|
202 const_arrow_res_t operator->() const { return boost::addressof( base_prop::value ); }
|
Chris@16
|
203
|
Chris@16
|
204 #ifndef BOOST_TEST_NO_PROTECTED_USING
|
Chris@16
|
205 using base_prop::value;
|
Chris@16
|
206 #endif
|
Chris@16
|
207 };
|
Chris@16
|
208
|
Chris@16
|
209 //____________________________________________________________________________//
|
Chris@16
|
210
|
Chris@16
|
211 } // unit_test
|
Chris@16
|
212
|
Chris@16
|
213 } // namespace boost
|
Chris@16
|
214
|
Chris@16
|
215 //____________________________________________________________________________//
|
Chris@16
|
216
|
Chris@16
|
217 #include <boost/test/detail/enable_warnings.hpp>
|
Chris@16
|
218
|
Chris@16
|
219 #undef BOOST_TEST_NO_PROTECTED_USING
|
Chris@16
|
220
|
Chris@16
|
221 #endif // BOOST_TEST_CLASS_PROPERTIES_HPP_071894GER
|