1 // (C) Copyright Gennadiy Rozental 2001-2014. 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 : fixed sized mapping with specified invalid value 13 // *************************************************************************** 14 15 #ifndef BOOST_TEST_UTILS_FIXED_MAPPING_HPP 16 #define BOOST_TEST_UTILS_FIXED_MAPPING_HPP 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 34 namespace boost { 35 namespace unit_test { 36 37 // configurable maximum fixed sized mapping size supported by this header. 38 // You can redefine it before inclusion of this file. 39 #ifndef MAX_MAP_SIZE 40 #define MAX_MAP_SIZE 20 41 #endif 42 43 #define CONSTR_DECL_MID( z, i, dummy1 ) key_param_type key##i, value_param_type v##i, 44 #define CONSTR_BODY_MID( z, i, dummy1 ) add_pair( key##i, v##i ); 45 46 #define CONSTR_DECL( z, n, dummy1 ) \ 47 fixed_mapping( BOOST_PP_REPEAT_ ## z( n, CONSTR_DECL_MID, "" ) \ 48 value_param_type invalid_value ) \ 49 : m_invalid_value( invalid_value ) \ 50 { \ 51 BOOST_PP_REPEAT_ ## z( n, CONSTR_BODY_MID, "" ) \ 52 init(); \ 53 } \ 54 /**/ 55 56 #define CONTRUCTORS( n ) BOOST_PP_REPEAT( n, CONSTR_DECL, "" ) 57 58 template<typename Key, typename Value, typename Compare = std::less<Key> > 59 class fixed_mapping 60 { 61 typedef std::pair<Key,Value> elem_type; 62 typedef std::vector<elem_type> map_type; 63 typedef typename std::vector<elem_type>::const_iterator iterator; 64 65 typedef typename call_traits<Key>::param_type key_param_type; 66 typedef typename call_traits<Value>::param_type value_param_type; 67 typedef typename call_traits<Value>::const_reference value_ref_type; 68 69 #if BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042)) 70 struct p1; friend struct p1; 71 struct p2; friend struct p2; 72 #endif 73 74 // bind( Compare(), bind(select1st<elem_type>(), _1), bind(identity<Key>(), _2) ) 75 struct p1 : public std::binary_function<elem_type,Key,bool> 76 { operator ()boost::unit_test::fixed_mapping::p177 bool operator()( elem_type const& x, Key const& y ) const { return Compare()( x.first, y ); } 78 }; 79 80 // bind( Compare(), bind(select1st<elem_type>(), _1), bind(select1st<elem_type>(), _2) ) 81 struct p2 : public std::binary_function<elem_type,elem_type,bool> 82 { operator ()boost::unit_test::fixed_mapping::p283 bool operator()( elem_type const& x, elem_type const& y ) const { return Compare()( x.first, y.first ); } 84 }; 85 86 public: 87 // Constructors 88 CONTRUCTORS( BOOST_PP_ADD( MAX_MAP_SIZE, 1 ) ) 89 90 // key -> value access operator [](key_param_type key) const91 value_ref_type operator[]( key_param_type key ) const 92 { 93 iterator it = boost::detail::lower_bound( m_map.begin(), m_map.end(), key, p1() ); 94 95 return (it == m_map.end() || Compare()( key, it->first ) ) ? m_invalid_value : it->second; 96 } 97 98 private: 99 // Implementation init()100 void init() { std::sort( m_map.begin(), m_map.end(), p2() ); } add_pair(key_param_type key,value_param_type value)101 void add_pair( key_param_type key, value_param_type value ) { m_map.push_back( elem_type( key, value ) ); } 102 103 // Data members 104 Value m_invalid_value; 105 map_type m_map; 106 }; 107 108 } // namespace unit_test 109 } // namespace boost 110 111 #include <boost/test/detail/enable_warnings.hpp> 112 113 #undef MAX_MAP_SIZE 114 #undef CONSTR_DECL_MID 115 #undef CONSTR_BODY_MID 116 #undef CONSTR_DECL 117 #undef CONTRUCTORS 118 119 #endif // BOOST_TEST_UTILS_FIXED_MAPPING_HPP 120 121