Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:52

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_DETAIL_SERIALIZATION_HELPER_HPP
0010 #define BOOST_FLYWEIGHT_DETAIL_SERIALIZATION_HELPER_HPP
0011 
0012 #if defined(_MSC_VER)&&(_MSC_VER>=1200)
0013 #pragma once
0014 #endif
0015 
0016 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
0017 #include <boost/multi_index_container.hpp>
0018 #include <boost/multi_index/hashed_index.hpp>
0019 #include <boost/multi_index/random_access_index.hpp>
0020 #include <boost/noncopyable.hpp>
0021 #include <vector>
0022 
0023 namespace boost{
0024 
0025 namespace flyweights{
0026 
0027 namespace detail{
0028 
0029 /* The serialization helpers for flyweight<T> map numerical IDs to
0030  * flyweight exemplars --an exemplar is the flyweight object
0031  * associated to a given value that appears first on the serialization
0032  * stream, so that subsequent equivalent flyweight objects will be made
0033  * to refer to it during the serialization process.
0034  */
0035 
0036 template<typename Flyweight>
0037 struct flyweight_value_address
0038 {
0039   typedef const typename Flyweight::value_type* result_type;
0040 
0041   result_type operator()(const Flyweight& x)const{return &x.get();}
0042 };
0043 
0044 template<typename Flyweight>
0045 class save_helper:private noncopyable
0046 {
0047   typedef multi_index::multi_index_container<
0048     Flyweight,
0049     multi_index::indexed_by<
0050       multi_index::random_access<>,
0051       multi_index::hashed_unique<flyweight_value_address<Flyweight> >
0052     >
0053   > table;
0054 
0055 public:
0056 
0057   typedef typename table::size_type size_type;
0058 
0059   size_type size()const{return t.size();}
0060 
0061   size_type find(const Flyweight& x)const
0062   {
0063     return multi_index::project<0>(t,multi_index::get<1>(t).find(&x.get()))
0064              -t.begin();
0065   }
0066 
0067   void push_back(const Flyweight& x){t.push_back(x);}
0068   
0069 private:
0070   table t;
0071 };
0072 
0073 template<typename Flyweight>
0074 class load_helper:private noncopyable
0075 {
0076   typedef std::vector<Flyweight> table;
0077 
0078 public:
0079 
0080   typedef typename table::size_type size_type;
0081 
0082   size_type size()const{return t.size();}
0083 
0084   Flyweight operator[](size_type n)const{return t[n];}
0085 
0086   void push_back(const Flyweight& x){t.push_back(x);}
0087   
0088 private:
0089   table t;
0090 };
0091 
0092 } /* namespace flyweights::detail */
0093 
0094 } /* namespace flyweights */
0095 
0096 } /* namespace boost */
0097 
0098 #endif