Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:27

0001 #ifndef BOOST_ARCHIVE_DETAIL_HELPER_COLLECTION_HPP
0002 #define BOOST_ARCHIVE_DETAIL_HELPER_COLLECTION_HPP
0003 
0004 // MS compatible compilers support #pragma once
0005 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
0006 # pragma once
0007 #endif
0008 
0009 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
0010 // helper_collection.hpp: archive support for run-time helpers
0011 
0012 // (C) Copyright 2002-2008 Robert Ramey and Joaquin M Lopez Munoz
0013 // Use, modification and distribution is subject to the Boost Software
0014 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
0015 // http://www.boost.org/LICENSE_1_0.txt)
0016 
0017 //  See http://www.boost.org for updates, documentation, and revision history.
0018 
0019 #include <cstddef> // NULL
0020 #include <vector>
0021 #include <utility>
0022 #include <memory>
0023 #include <algorithm>
0024 
0025 #include <boost/config.hpp>
0026 
0027 #include <boost/smart_ptr/shared_ptr.hpp>
0028 #include <boost/smart_ptr/make_shared.hpp>
0029 
0030 namespace boost {
0031 
0032 namespace archive {
0033 namespace detail {
0034 
0035 class helper_collection
0036 {
0037     helper_collection(const helper_collection&);              // non-copyable
0038     helper_collection& operator = (const helper_collection&); // non-copyable
0039 
0040     // note: we dont' actually "share" the function object pointer
0041     // we only use shared_ptr to make sure that it get's deleted
0042 
0043     typedef std::pair<
0044         const void *,
0045         boost::shared_ptr<void>
0046     > helper_value_type;
0047     template<class T>
0048     boost::shared_ptr<void> make_helper_ptr(){
0049         // use boost::shared_ptr rather than std::shared_ptr to maintain
0050         // c++03 compatibility
0051         return boost::make_shared<T>();
0052     }
0053 
0054     typedef std::vector<helper_value_type> collection;
0055     collection m_collection;
0056 
0057     struct predicate {
0058         BOOST_DEFAULTED_FUNCTION(predicate(const predicate& rhs), : m_ti(rhs.m_ti) {})
0059         BOOST_DELETED_FUNCTION(predicate & operator=(const predicate & rhs))
0060     public:
0061         const void * const m_ti;
0062         bool operator()(helper_value_type const &rhs){
0063             return m_ti == rhs.first;
0064         }
0065         predicate(const void * ti) :
0066             m_ti(ti)
0067         {}
0068     };
0069 protected:
0070     helper_collection(){}
0071     ~helper_collection(){}
0072 public:
0073     template<typename Helper>
0074     Helper& find_helper(void * const id = 0) {
0075         collection::const_iterator it =
0076             std::find_if(
0077                 m_collection.begin(),
0078                 m_collection.end(),
0079                 predicate(id)
0080             );
0081 
0082         void * rval = 0;
0083         if(it == m_collection.end()){
0084             m_collection.push_back(
0085                 std::make_pair(id, make_helper_ptr<Helper>())
0086             );
0087             rval = m_collection.back().second.get();
0088         }
0089         else{
0090             rval = it->second.get();
0091         }
0092         return *static_cast<Helper *>(rval);
0093     }
0094 };
0095 
0096 } // namespace detail
0097 } // namespace serialization
0098 } // namespace boost
0099 
0100 #endif // BOOST_ARCHIVE_DETAIL_HELPER_COLLECTION_HPP