helper_collection.hpp 2.9 KB

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