Chris@16: // (C) Copyright Gennadiy Rozental 2001-2008. Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See http://www.boost.org/libs/test for the library home page. Chris@16: // Chris@16: // File : $RCSfile$ Chris@16: // Chris@101: // Version : $Revision$ Chris@16: // Chris@16: // Description : fixed sized mapping with specified invalid value Chris@16: // *************************************************************************** Chris@16: Chris@16: #ifndef BOOST_TEST_FIXED_MAPPING_HPP_071894GER Chris@16: #define BOOST_TEST_FIXED_MAPPING_HPP_071894GER Chris@16: Chris@16: // Boost Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: // STL Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: //____________________________________________________________________________// Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: namespace unit_test { Chris@16: Chris@16: // configurable maximum fixed sized mapping size supported by this header. Chris@16: // You can redefine it before inclusion of this file. Chris@16: #ifndef MAX_MAP_SIZE Chris@16: #define MAX_MAP_SIZE 20 Chris@16: #endif Chris@16: Chris@16: #define CONSTR_DECL_MID( z, i, dummy1 ) key_param_type key##i, value_param_type v##i, Chris@16: #define CONSTR_BODY_MID( z, i, dummy1 ) add_pair( key##i, v##i ); Chris@16: Chris@16: #define CONSTR_DECL( z, n, dummy1 ) \ Chris@16: fixed_mapping( BOOST_PP_REPEAT_ ## z( n, CONSTR_DECL_MID, "" ) \ Chris@16: value_param_type invalid_value ) \ Chris@16: : m_invalid_value( invalid_value ) \ Chris@16: { \ Chris@16: BOOST_PP_REPEAT_ ## z( n, CONSTR_BODY_MID, "" ) \ Chris@16: init(); \ Chris@16: } \ Chris@16: /**/ Chris@16: Chris@16: #define CONTRUCTORS( n ) BOOST_PP_REPEAT( n, CONSTR_DECL, "" ) Chris@16: Chris@16: template > Chris@16: class fixed_mapping Chris@16: { Chris@16: typedef std::pair elem_type; Chris@16: typedef std::vector map_type; Chris@16: typedef typename std::vector::const_iterator iterator; Chris@16: Chris@16: typedef typename call_traits::param_type key_param_type; Chris@16: typedef typename call_traits::param_type value_param_type; Chris@16: typedef typename call_traits::const_reference value_ref_type; Chris@16: Chris@16: #if BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042)) Chris@16: struct p1; friend struct p1; Chris@16: struct p2; friend struct p2; Chris@16: #endif Chris@16: Chris@16: // bind( Compare(), bind(select1st(), _1), bind(identity(), _2) ) Chris@16: struct p1 : public std::binary_function Chris@16: { Chris@16: bool operator()( elem_type const& x, Key const& y ) const { return Compare()( x.first, y ); } Chris@16: }; Chris@16: Chris@16: // bind( Compare(), bind(select1st(), _1), bind(select1st(), _2) ) Chris@16: struct p2 : public std::binary_function Chris@16: { Chris@16: bool operator()( elem_type const& x, elem_type const& y ) const { return Compare()( x.first, y.first ); } Chris@16: }; Chris@16: Chris@16: public: Chris@16: // Constructors Chris@16: CONTRUCTORS( BOOST_PP_ADD( MAX_MAP_SIZE, 1 ) ) Chris@16: Chris@16: // key -> value access Chris@16: value_ref_type operator[]( key_param_type key ) const Chris@16: { Chris@16: iterator it = boost::detail::lower_bound( m_map.begin(), m_map.end(), key, p1() ); Chris@16: Chris@16: return (it == m_map.end() || Compare()( key, it->first ) ) ? m_invalid_value : it->second; Chris@16: } Chris@16: Chris@16: private: Chris@16: // Implementation Chris@16: void init() { std::sort( m_map.begin(), m_map.end(), p2() ); } Chris@16: void add_pair( key_param_type key, value_param_type value ) { m_map.push_back( elem_type( key, value ) ); } Chris@16: Chris@16: // Data members Chris@16: Value m_invalid_value; Chris@16: map_type m_map; Chris@16: }; Chris@16: Chris@16: } // namespace unit_test Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: //____________________________________________________________________________// Chris@16: Chris@16: #include Chris@16: Chris@16: #undef MAX_MAP_SIZE Chris@16: #undef CONSTR_DECL_MID Chris@16: #undef CONSTR_BODY_MID Chris@16: #undef CONSTR_DECL Chris@16: #undef CONTRUCTORS Chris@16: Chris@16: #endif // BOOST_TEST_FIXED_MAPPING_HPP_071894GER Chris@16: