source: NonGTP/Boost/boost/test/utils/class_properties.hpp @ 857

Revision 857, 10.7 KB checked in by igarcia, 18 years ago (diff)
Line 
1//  (C) Copyright Gennadiy Rozental 2001-2005.
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: class_properties.hpp,v $
9//
10//  Version     : $Revision: 1.7 $
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_CLASS_PROPERTIES_HPP_071894GER
17#define BOOST_TEST_CLASS_PROPERTIES_HPP_071894GER
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 {
39
40namespace unit_test {
41
42// ************************************************************************** //
43// **************                 class_property               ************** //
44// ************************************************************************** //
45
46template<class PropertyType>
47class class_property {
48protected:
49    typedef typename call_traits<PropertyType>::const_reference     read_access_t;
50    typedef typename call_traits<PropertyType>::param_type          write_param_t;
51#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570))
52    typedef typename add_pointer<PropertyType const>::type address_res_t;
53#else
54    typedef typename add_pointer<typename add_const<PropertyType>::type>::type address_res_t;
55#endif
56public:
57    // Constructor
58                    class_property() : value( PropertyType() ) {}
59    explicit        class_property( write_param_t init_value )
60    : value( init_value ) {}
61
62    // Access methods
63    operator        read_access_t() const   { return value; }
64    read_access_t   get() const             { return value; }
65    bool            operator!() const       { return !value; }
66    address_res_t   operator&() const       { return &value; }
67
68    // Data members
69#ifndef BOOST_TEST_NO_PROTECTED_USING
70protected:
71#endif
72    PropertyType        value;
73};
74
75//____________________________________________________________________________//
76
77#ifdef BOOST_CLASSIC_IOSTREAMS
78
79template<class PropertyType>
80inline std::ostream&
81operator<<( std::ostream& os, class_property<PropertyType> const& p )
82
83#else
84
85template<typename CharT1, typename Tr,class PropertyType>
86inline std::basic_ostream<CharT1,Tr>&
87operator<<( std::basic_ostream<CharT1,Tr>& os, class_property<PropertyType> const& p )
88
89#endif
90{
91    return os << p.get();
92}
93
94//____________________________________________________________________________//
95
96#define DEFINE_PROPERTY_FREE_BINARY_OPERATOR( op )                              \
97template<class PropertyType>                                                    \
98inline bool                                                                     \
99operator op( PropertyType const& lhs, class_property<PropertyType> const& rhs ) \
100{                                                                               \
101    return lhs op rhs.get();                                                    \
102}                                                                               \
103template<class PropertyType>                                                    \
104inline bool                                                                     \
105operator op( class_property<PropertyType> const& lhs, PropertyType const& rhs ) \
106{                                                                               \
107    return lhs.get() op rhs;                                                    \
108}                                                                               \
109template<class PropertyType>                                                    \
110inline bool                                                                     \
111operator op( class_property<PropertyType> const& lhs,                           \
112             class_property<PropertyType> const& rhs )                          \
113{                                                                               \
114    return lhs.get() op rhs.get();                                              \
115}                                                                               \
116/**/
117
118DEFINE_PROPERTY_FREE_BINARY_OPERATOR( == )
119DEFINE_PROPERTY_FREE_BINARY_OPERATOR( != )
120
121#undef DEFINE_PROPERTY_FREE_BINARY_OPERATOR
122
123#if BOOST_WORKAROUND(BOOST_MSVC, <= 1200)
124
125#define DEFINE_PROPERTY_LOGICAL_OPERATOR( op )                                  \
126template<class PropertyType>                                                    \
127inline bool                                                                     \
128operator op( bool b, class_property<PropertyType> const& p )                    \
129{                                                                               \
130    return b op p.get();                                                        \
131}                                                                               \
132template<class PropertyType>                                                    \
133inline bool                                                                     \
134operator op( class_property<PropertyType> const& p, bool b )                    \
135{                                                                               \
136    return b op p.get();                                                        \
137}                                                                               \
138/**/
139
140DEFINE_PROPERTY_LOGICAL_OPERATOR( && )
141DEFINE_PROPERTY_LOGICAL_OPERATOR( || )
142
143#endif
144
145// ************************************************************************** //
146// **************               readonly_property              ************** //
147// ************************************************************************** //
148
149template<class PropertyType>
150class readonly_property : public class_property<PropertyType> {
151    typedef class_property<PropertyType>         base_prop;
152    typedef typename base_prop::address_res_t    arrow_res_t;
153protected:
154    typedef typename base_prop::write_param_t    write_param_t;
155public:
156    // Constructor
157                    readonly_property() {}
158    explicit        readonly_property( write_param_t init_value ) : base_prop( init_value ) {}
159
160    // access methods
161    arrow_res_t     operator->() const      { return boost::addressof( base_prop::value ); }
162};
163
164//____________________________________________________________________________//
165
166#if BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600))
167
168#define BOOST_READONLY_PROPERTY( property_type, friends ) boost::unit_test::readwrite_property<property_type >
169
170#else
171
172#define BOOST_READONLY_PROPERTY_DECLARE_FRIEND(r, data, elem) friend class elem;
173
174#define BOOST_READONLY_PROPERTY( property_type, friends )                           \
175class BOOST_JOIN( readonly_property, __LINE__ )                                     \
176: public boost::unit_test::readonly_property<property_type > {                      \
177    typedef boost::unit_test::readonly_property<property_type > base_prop;          \
178    BOOST_PP_SEQ_FOR_EACH( BOOST_READONLY_PROPERTY_DECLARE_FRIEND, ' ', friends )   \
179    typedef base_prop::write_param_t  write_param_t;                                \
180public:                                                                             \
181                BOOST_JOIN( readonly_property, __LINE__ )() {}                      \
182    explicit    BOOST_JOIN( readonly_property, __LINE__ )( write_param_t init_v  )  \
183    : base_prop( init_v ) {}                                                        \
184}                                                                                   \
185/**/
186
187#endif
188
189// ************************************************************************** //
190// **************              readwrite_property              ************** //
191// ************************************************************************** //
192
193template<class PropertyType>
194class readwrite_property : public class_property<PropertyType> {
195    typedef class_property<PropertyType>                base_prop;
196    typedef typename add_pointer<PropertyType>::type    arrow_res_t;
197    typedef typename base_prop::address_res_t           const_arrow_res_t;
198    typedef typename base_prop::write_param_t           write_param_t;
199public:
200                    readwrite_property() : base_prop() {}
201    explicit        readwrite_property( write_param_t init_value ) : base_prop( init_value ) {}
202
203    // access methods
204    void            set( write_param_t v )  { base_prop::value = v; }
205    arrow_res_t     operator->()            { return boost::addressof( base_prop::value ); }
206    const_arrow_res_t operator->() const    { return boost::addressof( base_prop::value ); }
207
208#ifndef BOOST_TEST_NO_PROTECTED_USING
209    using           base_prop::value;
210#endif
211};
212
213//____________________________________________________________________________//
214
215} // unit_test
216
217} // namespace boost
218
219//____________________________________________________________________________//
220
221#include <boost/test/detail/enable_warnings.hpp>
222
223#undef BOOST_TEST_NO_PROTECTED_USING
224
225// ***************************************************************************
226//  Revision History :
227// 
228//  $Log: class_properties.hpp,v $
229//  Revision 1.7  2005/05/11 05:04:58  rogeeff
230//  borland portability fix
231//
232//  Revision 1.6  2005/04/12 06:46:17  rogeeff
233//  use add_const
234//
235//  Revision 1.5  2005/02/21 10:17:27  rogeeff
236//  base reference renamed (borland bug fix)
237//
238//  Revision 1.4  2005/02/20 08:27:08  rogeeff
239//  This a major update for Boost.Test framework. See release docs for complete list of fixes/updates
240//
241//  Revision 1.3  2005/02/01 06:40:07  rogeeff
242//  copyright update
243//  old log entries removed
244//  minor stilistic changes
245//  depricated tools removed
246//
247//  Revision 1.2  2005/01/22 19:22:13  rogeeff
248//  implementation moved into headers section to eliminate dependency of included/minimal component on src directory
249//
250//  Revision 1.1  2005/01/22 18:21:39  rogeeff
251//  moved sharable staff into utils
252//
253// ***************************************************************************
254
255#endif // BOOST_TEST_CLASS_PROPERTIES_HPP_071894GER
Note: See TracBrowser for help on using the repository browser.