named_creation_functor.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2007-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_INTERPROCESS_SYNC_NAMED_CREATION_FUNCTOR_HPP
  11. #define BOOST_INTERPROCESS_SYNC_NAMED_CREATION_FUNCTOR_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #
  16. #if defined(BOOST_HAS_PRAGMA_ONCE)
  17. # pragma once
  18. #endif
  19. #include <boost/interprocess/creation_tags.hpp>
  20. #include <boost/interprocess/detail/type_traits.hpp>
  21. #include <boost/interprocess/detail/mpl.hpp>
  22. #include <boost/container/detail/placement_new.hpp>
  23. namespace boost {
  24. namespace interprocess {
  25. namespace ipcdetail {
  26. struct named_creation_functor_no_arg{};
  27. template <class T, class Arg = named_creation_functor_no_arg>
  28. class named_creation_functor
  29. {
  30. typedef named_creation_functor_no_arg no_arg_t;
  31. public:
  32. named_creation_functor(create_enum_t type, Arg arg = Arg())
  33. : m_creation_type(type), m_arg(arg){}
  34. template<class ArgType>
  35. void construct(void *address, typename enable_if_c<is_same<ArgType, no_arg_t>::value>::type * = 0) const
  36. { ::new(address, boost_container_new_t())T; }
  37. template<class ArgType>
  38. void construct(void *address, typename enable_if_c<!is_same<ArgType, no_arg_t>::value>::type * = 0) const
  39. { ::new(address, boost_container_new_t())T(m_arg); }
  40. bool operator()(void *address, std::size_t, bool created) const
  41. {
  42. switch(m_creation_type){
  43. case DoOpen:
  44. return true;
  45. break;
  46. case DoCreate:
  47. case DoOpenOrCreate:
  48. if(created){
  49. construct<Arg>(address);
  50. }
  51. return true;
  52. break;
  53. default:
  54. return false;
  55. break;
  56. }
  57. }
  58. static std::size_t get_min_size()
  59. { return sizeof(T); }
  60. private:
  61. create_enum_t m_creation_type;
  62. Arg m_arg;
  63. };
  64. } //namespace ipcdetail {
  65. } //namespace interprocess {
  66. } //namespace boost {
  67. #endif //BOOST_INTERPROCESS_SYNC_NAMED_CREATION_FUNCTOR_HPP