Chris@102
|
1 #ifndef BOOST_ARCHIVE_DETAIL_HELPER_COLLECTION_HPP
|
Chris@102
|
2 #define BOOST_ARCHIVE_DETAIL_HELPER_COLLECTION_HPP
|
Chris@102
|
3
|
Chris@102
|
4 // MS compatible compilers support #pragma once
|
Chris@102
|
5 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
Chris@102
|
6 # pragma once
|
Chris@102
|
7 #endif
|
Chris@102
|
8
|
Chris@102
|
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
Chris@102
|
10 // helper_collection.hpp: archive support for run-time helpers
|
Chris@102
|
11
|
Chris@102
|
12 // (C) Copyright 2002-2008 Robert Ramey and Joaquin M Lopez Munoz
|
Chris@102
|
13 // Use, modification and distribution is subject to the Boost Software
|
Chris@102
|
14 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@102
|
15 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@102
|
16
|
Chris@102
|
17 // See http://www.boost.org for updates, documentation, and revision history.
|
Chris@102
|
18
|
Chris@102
|
19 #include <cstddef> // NULL
|
Chris@102
|
20 #include <vector>
|
Chris@102
|
21 #include <utility>
|
Chris@102
|
22 #include <memory>
|
Chris@102
|
23 #include <algorithm>
|
Chris@102
|
24
|
Chris@102
|
25 #include <boost/config.hpp>
|
Chris@102
|
26
|
Chris@102
|
27 #include <boost/smart_ptr/shared_ptr.hpp>
|
Chris@102
|
28 #include <boost/smart_ptr/make_shared.hpp>
|
Chris@102
|
29
|
Chris@102
|
30 namespace boost {
|
Chris@102
|
31
|
Chris@102
|
32 namespace archive {
|
Chris@102
|
33 namespace detail {
|
Chris@102
|
34
|
Chris@102
|
35 class helper_collection
|
Chris@102
|
36 {
|
Chris@102
|
37 helper_collection(const helper_collection&); // non-copyable
|
Chris@102
|
38 helper_collection& operator = (const helper_collection&); // non-copyable
|
Chris@102
|
39
|
Chris@102
|
40 // note: we dont' actually "share" the function object pointer
|
Chris@102
|
41 // we only use shared_ptr to make sure that it get's deleted
|
Chris@102
|
42
|
Chris@102
|
43 typedef std::pair<
|
Chris@102
|
44 const void *,
|
Chris@102
|
45 boost::shared_ptr<void>
|
Chris@102
|
46 > helper_value_type;
|
Chris@102
|
47 template<class T>
|
Chris@102
|
48 boost::shared_ptr<void> make_helper_ptr(){
|
Chris@102
|
49 // use boost::shared_ptr rather than std::shared_ptr to maintain
|
Chris@102
|
50 // c++03 compatibility
|
Chris@102
|
51 return boost::make_shared<T>();
|
Chris@102
|
52 }
|
Chris@102
|
53
|
Chris@102
|
54 typedef std::vector<helper_value_type> collection;
|
Chris@102
|
55 collection m_collection;
|
Chris@102
|
56
|
Chris@102
|
57 struct predicate {
|
Chris@102
|
58 const void * const m_ti;
|
Chris@102
|
59 bool operator()(helper_value_type const &rhs) const {
|
Chris@102
|
60 return m_ti == rhs.first;
|
Chris@102
|
61 }
|
Chris@102
|
62 predicate & operator=(const void * ti); // to suppress warning
|
Chris@102
|
63 predicate(const void * ti) :
|
Chris@102
|
64 m_ti(ti)
|
Chris@102
|
65 {}
|
Chris@102
|
66 };
|
Chris@102
|
67 protected:
|
Chris@102
|
68 helper_collection(){}
|
Chris@102
|
69 ~helper_collection(){}
|
Chris@102
|
70 public:
|
Chris@102
|
71 template<typename Helper>
|
Chris@102
|
72 Helper& get_helper(void * const id = 0) {
|
Chris@102
|
73 collection::const_iterator it =
|
Chris@102
|
74 std::find_if(
|
Chris@102
|
75 m_collection.begin(),
|
Chris@102
|
76 m_collection.end(),
|
Chris@102
|
77 predicate(id)
|
Chris@102
|
78 );
|
Chris@102
|
79
|
Chris@102
|
80 void * rval = 0;
|
Chris@102
|
81 if(it == m_collection.end()){
|
Chris@102
|
82 m_collection.push_back(
|
Chris@102
|
83 std::make_pair(id, make_helper_ptr<Helper>())
|
Chris@102
|
84 );
|
Chris@102
|
85 rval = m_collection.back().second.get();
|
Chris@102
|
86 }
|
Chris@102
|
87 else{
|
Chris@102
|
88 rval = it->second.get();
|
Chris@102
|
89 }
|
Chris@102
|
90 return *static_cast<Helper *>(rval);
|
Chris@102
|
91 }
|
Chris@102
|
92 };
|
Chris@102
|
93
|
Chris@102
|
94 } // namespace detail
|
Chris@102
|
95 } // namespace serialization
|
Chris@102
|
96 } // namespace boost
|
Chris@102
|
97
|
Chris@102
|
98 #endif // BOOST_ARCHIVE_DETAIL_HELPER_COLLECTION_HPP
|