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