Chris@102: #ifndef BOOST_ARCHIVE_DETAIL_HELPER_COLLECTION_HPP Chris@102: #define BOOST_ARCHIVE_DETAIL_HELPER_COLLECTION_HPP Chris@102: Chris@102: // MS compatible compilers support #pragma once Chris@102: #if defined(_MSC_VER) && (_MSC_VER >= 1020) Chris@102: # pragma once Chris@102: #endif Chris@102: Chris@102: /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 Chris@102: // helper_collection.hpp: archive support for run-time helpers Chris@102: Chris@102: // (C) Copyright 2002-2008 Robert Ramey and Joaquin M Lopez Munoz Chris@102: // Use, modification and distribution is subject to the Boost Software Chris@102: // License, Version 1.0. (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 for updates, documentation, and revision history. Chris@102: Chris@102: #include // NULL Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: Chris@102: #include Chris@102: Chris@102: #include Chris@102: #include Chris@102: Chris@102: namespace boost { Chris@102: Chris@102: namespace archive { Chris@102: namespace detail { Chris@102: Chris@102: class helper_collection Chris@102: { Chris@102: helper_collection(const helper_collection&); // non-copyable Chris@102: helper_collection& operator = (const helper_collection&); // non-copyable Chris@102: Chris@102: // note: we dont' actually "share" the function object pointer Chris@102: // we only use shared_ptr to make sure that it get's deleted Chris@102: Chris@102: typedef std::pair< Chris@102: const void *, Chris@102: boost::shared_ptr Chris@102: > helper_value_type; Chris@102: template Chris@102: boost::shared_ptr make_helper_ptr(){ Chris@102: // use boost::shared_ptr rather than std::shared_ptr to maintain Chris@102: // c++03 compatibility Chris@102: return boost::make_shared(); Chris@102: } Chris@102: Chris@102: typedef std::vector collection; Chris@102: collection m_collection; Chris@102: Chris@102: struct predicate { Chris@102: const void * const m_ti; Chris@102: bool operator()(helper_value_type const &rhs) const { Chris@102: return m_ti == rhs.first; Chris@102: } Chris@102: predicate & operator=(const void * ti); // to suppress warning Chris@102: predicate(const void * ti) : Chris@102: m_ti(ti) Chris@102: {} Chris@102: }; Chris@102: protected: Chris@102: helper_collection(){} Chris@102: ~helper_collection(){} Chris@102: public: Chris@102: template Chris@102: Helper& get_helper(void * const id = 0) { Chris@102: collection::const_iterator it = Chris@102: std::find_if( Chris@102: m_collection.begin(), Chris@102: m_collection.end(), Chris@102: predicate(id) Chris@102: ); Chris@102: Chris@102: void * rval = 0; Chris@102: if(it == m_collection.end()){ Chris@102: m_collection.push_back( Chris@102: std::make_pair(id, make_helper_ptr()) Chris@102: ); Chris@102: rval = m_collection.back().second.get(); Chris@102: } Chris@102: else{ Chris@102: rval = it->second.get(); Chris@102: } Chris@102: return *static_cast(rval); Chris@102: } Chris@102: }; Chris@102: Chris@102: } // namespace detail Chris@102: } // namespace serialization Chris@102: } // namespace boost Chris@102: Chris@102: #endif // BOOST_ARCHIVE_DETAIL_HELPER_COLLECTION_HPP