Chris@102: /* Copyright 2006-2014 Joaquin M Lopez Munoz. Chris@102: * Distributed under the Boost Software License, Version 1.0. Chris@102: * (See accompanying file LICENSE_1_0.txt or copy at Chris@102: * http://www.boost.org/LICENSE_1_0.txt) Chris@102: * Chris@102: * See http://www.boost.org/libs/flyweight for library home page. Chris@102: */ Chris@102: Chris@102: #ifndef BOOST_FLYWEIGHT_DETAIL_SERIALIZATION_HELPER_HPP Chris@102: #define BOOST_FLYWEIGHT_DETAIL_SERIALIZATION_HELPER_HPP Chris@102: Chris@102: #if defined(_MSC_VER)&&(_MSC_VER>=1200) Chris@102: #pragma once Chris@102: #endif Chris@102: Chris@102: #include /* keep it first to prevent nasty warns in MSVC */ Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: Chris@102: namespace boost{ Chris@102: Chris@102: namespace flyweights{ Chris@102: Chris@102: namespace detail{ Chris@102: Chris@102: /* The serialization helpers for flyweight map numerical IDs to Chris@102: * flyweight exemplars --an exemplar is the flyweight object Chris@102: * associated to a given value that appears first on the serialization Chris@102: * stream, so that subsequent equivalent flyweight objects will be made Chris@102: * to refer to it during the serialization process. Chris@102: */ Chris@102: Chris@102: template Chris@102: struct flyweight_value_address Chris@102: { Chris@102: typedef const typename Flyweight::value_type* result_type; Chris@102: Chris@102: result_type operator()(const Flyweight& x)const{return &x.get();} Chris@102: }; Chris@102: Chris@102: template Chris@102: class save_helper:private noncopyable Chris@102: { Chris@102: typedef multi_index::multi_index_container< Chris@102: Flyweight, Chris@102: multi_index::indexed_by< Chris@102: multi_index::random_access<>, Chris@102: multi_index::hashed_unique > Chris@102: > Chris@102: > table; Chris@102: Chris@102: public: Chris@102: Chris@102: typedef typename table::size_type size_type; Chris@102: Chris@102: size_type size()const{return t.size();} Chris@102: Chris@102: size_type find(const Flyweight& x)const Chris@102: { Chris@102: return multi_index::project<0>(t,multi_index::get<1>(t).find(&x.get())) Chris@102: -t.begin(); Chris@102: } Chris@102: Chris@102: void push_back(const Flyweight& x){t.push_back(x);} Chris@102: Chris@102: private: Chris@102: table t; Chris@102: }; Chris@102: Chris@102: template Chris@102: class load_helper:private noncopyable Chris@102: { Chris@102: typedef std::vector table; Chris@102: Chris@102: public: Chris@102: Chris@102: typedef typename table::size_type size_type; Chris@102: Chris@102: size_type size()const{return t.size();} Chris@102: Chris@102: Flyweight operator[](size_type n)const{return t[n];} Chris@102: Chris@102: void push_back(const Flyweight& x){t.push_back(x);} Chris@102: Chris@102: private: Chris@102: table t; Chris@102: }; Chris@102: Chris@102: } /* namespace flyweights::detail */ Chris@102: Chris@102: } /* namespace flyweights */ Chris@102: Chris@102: } /* namespace boost */ Chris@102: Chris@102: #endif