helper_collection.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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_DELETED_FUNCTION(predicate & operator=(const predicate & rhs))
  45. public:
  46. const void * const m_ti;
  47. bool operator()(helper_value_type const &rhs) const {
  48. return m_ti == rhs.first;
  49. }
  50. predicate(const void * ti) :
  51. m_ti(ti)
  52. {}
  53. };
  54. protected:
  55. helper_collection(){}
  56. ~helper_collection(){}
  57. public:
  58. template<typename Helper>
  59. Helper& find_helper(void * const id = 0) {
  60. collection::const_iterator it =
  61. std::find_if(
  62. m_collection.begin(),
  63. m_collection.end(),
  64. predicate(id)
  65. );
  66. void * rval = 0;
  67. if(it == m_collection.end()){
  68. m_collection.push_back(
  69. std::make_pair(id, make_helper_ptr<Helper>())
  70. );
  71. rval = m_collection.back().second.get();
  72. }
  73. else{
  74. rval = it->second.get();
  75. }
  76. return *static_cast<Helper *>(rval);
  77. }
  78. };
  79. } // namespace detail
  80. } // namespace serialization
  81. } // namespace boost
  82. #endif // BOOST_ARCHIVE_DETAIL_HELPER_COLLECTION_HPP