ref_counted_policy.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (c) 2001 Daniel C. Nuffer
  2. // Copyright (c) 2001-2011 Hartmut Kaiser
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #if !defined(BOOST_SPIRIT_ITERATOR_REF_COUNTED_POLICY_MAR_16_2007_1108AM)
  7. #define BOOST_SPIRIT_ITERATOR_REF_COUNTED_POLICY_MAR_16_2007_1108AM
  8. #include <boost/spirit/home/support/iterators/multi_pass_fwd.hpp>
  9. #include <boost/spirit/home/support/iterators/detail/multi_pass.hpp>
  10. #if defined(BOOST_HAS_THREADS)
  11. #include <boost/detail/atomic_count.hpp>
  12. #endif
  13. #include <cstdlib>
  14. namespace boost { namespace spirit { namespace iterator_policies
  15. {
  16. ///////////////////////////////////////////////////////////////////////////
  17. // class ref_counted
  18. // Implementation of an OwnershipPolicy used by multi_pass.
  19. //
  20. // Implementation modified from RefCounted class from the Loki library by
  21. // Andrei Alexandrescu.
  22. ///////////////////////////////////////////////////////////////////////////
  23. struct ref_counted
  24. {
  25. ///////////////////////////////////////////////////////////////////////
  26. struct unique // : detail::default_ownership_policy
  27. {
  28. void swap(unique&) {}
  29. // clone is called when a copy of the iterator is made, so
  30. // increment the ref-count.
  31. template <typename MultiPass>
  32. static void clone(MultiPass& mp)
  33. {
  34. if (0 != mp.shared())
  35. ++mp.shared()->count;
  36. }
  37. // called when a copy is deleted. Decrement the ref-count. Return
  38. // value of true indicates that the last copy has been released.
  39. template <typename MultiPass>
  40. static bool release(MultiPass& mp)
  41. {
  42. return 0 != mp.shared() && 0 == --mp.shared()->count;
  43. }
  44. // returns true if there is only one iterator in existence.
  45. // std_deque StoragePolicy will free it's buffered data if this
  46. // returns true.
  47. template <typename MultiPass>
  48. static bool is_unique(MultiPass const& mp)
  49. {
  50. return 0 == mp.shared() || 1 == mp.shared()->count;
  51. }
  52. template <typename MultiPass>
  53. static void destroy(MultiPass&) {}
  54. };
  55. ////////////////////////////////////////////////////////////////////////
  56. struct shared
  57. {
  58. shared() : count(1) {}
  59. #if defined(BOOST_HAS_THREADS)
  60. boost::detail::atomic_count count;
  61. #else
  62. std::size_t count;
  63. #endif
  64. };
  65. };
  66. }}}
  67. #endif