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)
5
6// See http://www.boost.org/libs/test for the library home page.
7//
8// File : $RCSfile$
9//
10// Version : $Revision$
11//
12// Description : simple facility that mimmic notion of read-only read-write
13// properties in C++ classes. Original idea by Henrik Ravn.
14// ***************************************************************************
15
16#ifndef BOOST_TEST_UTILS_CLASS_PROPERTIES_HPP
17#define BOOST_TEST_UTILS_CLASS_PROPERTIES_HPP
18
19// Boost.Test
20#include <boost/test/detail/config.hpp>
21
22// Boost
23#if !BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600))
24#include <boost/preprocessor/seq/for_each.hpp>
25#endif
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>
30
31// STL
32#include <iosfwd>
33
34#include <boost/test/detail/suppress_warnings.hpp>
35
36//____________________________________________________________________________//
37
38namespace boost {
39namespace unit_test {
40
41// ************************************************************************** //
42// ************** class_property ************** //
43// ************************************************************************** //
44
45template<class PropertyType>
46class class_property {
47protected:
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;
51public:
52 // Constructor
53 class_property() : value( PropertyType() ) {}
54 explicit class_property( write_param_t init_value )
55 : value( init_value ) {}
56
57 // Access methods
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; }
62
63 // Data members
64#ifndef BOOST_TEST_NO_PROTECTED_USING
65protected:
66#endif
67 PropertyType value;
68};
69
70//____________________________________________________________________________//
71
72#ifdef BOOST_CLASSIC_IOSTREAMS
73
74template<class PropertyType>
75inline std::ostream&
76operator<<( std::ostream& os, class_property<PropertyType> const& p )
77
78#else
79
80template<typename CharT1, typename Tr,class PropertyType>
81inline std::basic_ostream<CharT1,Tr>&
82operator<<( std::basic_ostream<CharT1,Tr>& os, class_property<PropertyType> const& p )
83
84#endif
85{
86 return os << p.get();
87}
88
89//____________________________________________________________________________//
90
91#define DEFINE_PROPERTY_FREE_BINARY_OPERATOR( op ) \
92template<class PropertyType> \
93inline bool \
94operator op( PropertyType const& lhs, class_property<PropertyType> const& rhs ) \
95{ \
96 return lhs op rhs.get(); \
97} \
98template<class PropertyType> \
99inline bool \
100operator op( class_property<PropertyType> const& lhs, PropertyType const& rhs ) \
101{ \
102 return lhs.get() op rhs; \
103} \
104template<class PropertyType> \
105inline bool \
106operator op( class_property<PropertyType> const& lhs, \
107 class_property<PropertyType> const& rhs ) \
108{ \
109 return lhs.get() op rhs.get(); \
110} \
111/**/
112
113DEFINE_PROPERTY_FREE_BINARY_OPERATOR( == )
114DEFINE_PROPERTY_FREE_BINARY_OPERATOR( != )
115
116#undef DEFINE_PROPERTY_FREE_BINARY_OPERATOR
117
118// ************************************************************************** //
119// ************** readonly_property ************** //
120// ************************************************************************** //
121
122template<class PropertyType>
123class readonly_property : public class_property<PropertyType> {
124 typedef class_property<PropertyType> base_prop;
125 typedef typename base_prop::address_res_t arrow_res_t;
126protected:
127 typedef typename base_prop::write_param_t write_param_t;
128public:
129 // Constructor
130 readonly_property() {}
131 explicit readonly_property( write_param_t init_value ) : base_prop( init_value ) {}
132
133 // access methods
134 arrow_res_t operator->() const { return boost::addressof( base_prop::value ); }
135};
136
137//____________________________________________________________________________//
138
139#if BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600))
140
141#define BOOST_READONLY_PROPERTY( property_type, friends ) boost::unit_test::readwrite_property<property_type >
142
143#else
144
145#define BOOST_READONLY_PROPERTY_DECLARE_FRIEND(r, data, elem) friend class elem;
146
147#define BOOST_READONLY_PROPERTY( property_type, friends ) \
148class 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; \
153public: \
154 BOOST_JOIN( readonly_property, __LINE__ )() {} \
155 explicit BOOST_JOIN( readonly_property, __LINE__ )( write_param_t init_v ) \
156 : base_prop( init_v ) {} \
157} \
158/**/
159
160#endif
161
162// ************************************************************************** //
163// ************** readwrite_property ************** //
164// ************************************************************************** //
165
166template<class PropertyType>
167class 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;
172public:
173 readwrite_property() : base_prop() {}
174 explicit readwrite_property( write_param_t init_value ) : base_prop( init_value ) {}
175
176 // access methods
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 ); }
180
181#ifndef BOOST_TEST_NO_PROTECTED_USING
182 using base_prop::value;
183#endif
184};
185
186//____________________________________________________________________________//
187
188} // unit_test
189} // namespace boost
190
191#include <boost/test/detail/enable_warnings.hpp>
192
193#undef BOOST_TEST_NO_PROTECTED_USING
194
195#endif // BOOST_TEST_UTILS_CLASS_PROPERTIES_HPP
196

source code of boost/boost/test/utils/class_properties.hpp