Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/flyweight/hashed_factory.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* Copyright 2006-2014 Joaquin M Lopez Munoz.
0002  * Distributed under the Boost Software License, Version 1.0.
0003  * (See accompanying file LICENSE_1_0.txt or copy at
0004  * http://www.boost.org/LICENSE_1_0.txt)
0005  *
0006  * See http://www.boost.org/libs/flyweight for library home page.
0007  */
0008 
0009 #ifndef BOOST_FLYWEIGHT_HASHED_FACTORY_HPP
0010 #define BOOST_FLYWEIGHT_HASHED_FACTORY_HPP
0011 
0012 #if defined(_MSC_VER)
0013 #pragma once
0014 #endif
0015 
0016 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
0017 #include <boost/flyweight/factory_tag.hpp>
0018 #include <boost/flyweight/hashed_factory_fwd.hpp>
0019 #include <boost/multi_index_container.hpp>
0020 #include <boost/multi_index/identity.hpp>
0021 #include <boost/multi_index/hashed_index.hpp>
0022 #include <boost/mpl/aux_/lambda_support.hpp>
0023 #include <boost/mpl/if.hpp>
0024 
0025 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
0026 #include <utility>
0027 #endif
0028 
0029 /* Flyweight factory based on a hashed container implemented
0030  * with Boost.MultiIndex.
0031  */
0032 
0033 namespace boost{
0034 
0035 namespace flyweights{
0036 
0037 template<
0038   typename Entry,typename Key,
0039   typename Hash,typename Pred,typename Allocator
0040 >
0041 class hashed_factory_class:public factory_marker
0042 {
0043   struct index_list:
0044     boost::mpl::vector1<
0045       multi_index::hashed_unique<
0046         multi_index::identity<Entry>,
0047         typename boost::mpl::if_<
0048           mpl::is_na<Hash>,
0049           hash<Key>,
0050           Hash
0051         >::type,
0052         typename boost::mpl::if_<
0053           mpl::is_na<Pred>,
0054           std::equal_to<Key>,
0055           Pred
0056         >::type
0057       >
0058     >
0059   {};
0060 
0061   typedef multi_index::multi_index_container<
0062     Entry,
0063     index_list,
0064     typename boost::mpl::if_<
0065       mpl::is_na<Allocator>,
0066       std::allocator<Entry>,
0067       Allocator
0068     >::type
0069   > container_type;
0070 
0071 public:
0072   typedef const Entry* handle_type;
0073   
0074   handle_type insert(const Entry& x)
0075   {
0076     return &*cont.insert(x).first;
0077   }
0078 
0079 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
0080   handle_type insert(Entry&& x)
0081   {
0082     return &*cont.insert(std::move(x)).first;
0083   }
0084 #endif
0085 
0086   void erase(handle_type h)
0087   {
0088     cont.erase(cont.iterator_to(*h));
0089   }
0090 
0091   static const Entry& entry(handle_type h){return *h;}
0092 
0093 private:  
0094   container_type cont;
0095 
0096 public:
0097   typedef hashed_factory_class type;
0098   BOOST_MPL_AUX_LAMBDA_SUPPORT(
0099     5,hashed_factory_class,(Entry,Key,Hash,Pred,Allocator))
0100 };
0101 
0102 /* hashed_factory_class specifier */
0103 
0104 template<
0105   typename Hash,typename Pred,typename Allocator
0106   BOOST_FLYWEIGHT_NOT_A_PLACEHOLDER_EXPRESSION_DEF
0107 >
0108 struct hashed_factory:factory_marker
0109 {
0110   template<typename Entry,typename Key>
0111   struct apply:
0112     mpl::apply2<
0113       hashed_factory_class<
0114         boost::mpl::_1,boost::mpl::_2,Hash,Pred,Allocator
0115       >,
0116       Entry,Key
0117     >
0118   {};
0119 };
0120 
0121 } /* namespace flyweights */
0122 
0123 } /* namespace boost */
0124 
0125 #endif