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

Revision 857, 5.5 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: fixed_mapping.hpp,v $
9//
10//  Version     : $Revision: 1.6 $
11//
12//  Description : fixed sized mapping with specified invalid value
13// ***************************************************************************
14
15#ifndef BOOST_TEST_FIXED_MAPPING_HPP_071894GER
16#define BOOST_TEST_FIXED_MAPPING_HPP_071894GER
17
18// Boost
19#include <boost/preprocessor/repetition/repeat.hpp>
20#include <boost/preprocessor/arithmetic/add.hpp>
21#include <boost/call_traits.hpp>
22#include <boost/detail/binary_search.hpp>
23
24// STL
25#include <vector>
26#include <functional>
27#include <algorithm>
28#include <utility>
29
30#include <boost/test/detail/suppress_warnings.hpp>
31
32//____________________________________________________________________________//
33
34namespace boost {
35
36namespace unit_test {
37
38// configurable maximum fixed sized mapping size supported by this header.
39// You could redefine it before inclusion of this file.
40#ifndef MAX_MAP_SIZE
41#define MAX_MAP_SIZE 15
42#endif
43
44#define CONSTR_DECL_MID( z, i, dummy1 ) key_param_type key##i, value_param_type v##i,
45#define CONSTR_BODY_MID( z, i, dummy1 ) add_pair( key##i, v##i );
46
47#define CONSTR_DECL( z, n, dummy1 )                                 \
48    fixed_mapping( BOOST_PP_REPEAT_ ## z( n, CONSTR_DECL_MID, "" )  \
49                         value_param_type invalid_value )           \
50    : m_invalid_value( invalid_value )                              \
51    {                                                               \
52        BOOST_PP_REPEAT_ ## z( n, CONSTR_BODY_MID, "" )             \
53        init();                                                     \
54    }                                                               \
55/**/
56
57#define CONTRUCTORS( n ) BOOST_PP_REPEAT( n, CONSTR_DECL, "" )
58
59template<typename Key, typename Value, typename Compare = std::less<Key> >
60class fixed_mapping
61{
62    typedef std::pair<Key,Value>                            elem_type;
63    typedef std::vector<elem_type>                          map_type;
64    typedef typename std::vector<elem_type>::const_iterator iterator;
65
66    typedef typename call_traits<Key>::param_type           key_param_type;
67    typedef typename call_traits<Value>::param_type         value_param_type;
68    typedef typename call_traits<Value>::const_reference    value_ref_type;
69
70#if BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042))
71    struct p1; friend struct p1;
72    struct p2; friend struct p2;
73#endif
74
75    // bind( Compare(), bind(select1st<elem_type>(), _1),  bind(identity<Key>(), _2) )
76    struct p1 : public std::binary_function<elem_type,Key,bool>
77    {
78        bool operator()( elem_type const& x, Key const& y ) const { return Compare()( x.first, y ); }
79    };
80
81    // bind( Compare(), bind(select1st<elem_type>(), _1), bind(select1st<elem_type>(), _2) )
82    struct p2 : public std::binary_function<elem_type,elem_type,bool>
83    {
84        bool operator()( elem_type const& x, elem_type const& y ) const { return Compare()( x.first, y.first ); }
85    };
86
87public:
88    // Constructors
89    CONTRUCTORS( BOOST_PP_ADD( MAX_MAP_SIZE, 1 ) )
90
91    // key -> value access
92    value_ref_type  operator[]( key_param_type key ) const
93    {
94#if BOOST_WORKAROUND(__SUNPRO_CC,BOOST_TESTED_AT(0x530))
95        iterator it = std::lower_bound( m_map.begin(), m_map.end(), key, p1() );
96#else
97        iterator it = boost::detail::lower_bound( m_map.begin(), m_map.end(), key, p1() );
98#endif
99        return (it == m_map.end() || Compare()( key, it->first ) ) ? m_invalid_value : it->second;
100    }
101
102private:
103    // Implementation
104    void            init()                                                  { std::sort( m_map.begin(), m_map.end(), p2() ); }
105    void            add_pair( key_param_type key, value_param_type value )  { m_map.push_back( elem_type( key, value ) ); }
106
107    // Data members
108    Value           m_invalid_value;
109    map_type        m_map;
110};
111
112} // namespace unit_test
113
114} // namespace boost
115
116//____________________________________________________________________________//
117
118#include <boost/test/detail/enable_warnings.hpp>
119
120#undef MAX_MAP_SIZE
121#undef CONSTR_DECL_MID
122#undef CONSTR_BODY_MID
123#undef CONSTR_DECL
124#undef CONTRUCTORS
125
126// ***************************************************************************
127//  Revision History :
128// 
129//  $Log: fixed_mapping.hpp,v $
130//  Revision 1.6  2005/06/16 14:33:42  schoepflin
131//  Added workaround for Tru64/CXX which enables boost.test to compile in strict
132//  ansi mode on that compiler.
133//
134//  Revision 1.5  2005/05/08 08:55:09  rogeeff
135//  typos and missing descriptions fixed
136//
137//  Revision 1.4  2005/02/20 08:27:08  rogeeff
138//  This a major update for Boost.Test framework. See release docs for complete list of fixes/updates
139//
140//  Revision 1.3  2005/02/01 06:40:07  rogeeff
141//  copyright update
142//  old log entries removed
143//  minor stilistic changes
144//  depricated tools removed
145//
146//  Revision 1.2  2005/01/31 20:07:19  rogeeff
147//  Sunpro CC 5.3 workarounds
148//
149//  Revision 1.1  2005/01/22 18:21:39  rogeeff
150//  moved sharable staff into utils
151//
152// ***************************************************************************
153
154#endif // BOOST_TEST_FIXED_MAPPING_HPP_071894GER
155
Note: See TracBrowser for help on using the repository browser.