named_proxy.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-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_NAMED_PROXY_HPP
  11. #define BOOST_INTERPROCESS_NAMED_PROXY_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/detail/config_begin.hpp>
  20. #include <boost/interprocess/detail/workaround.hpp>
  21. // interprocess/detail
  22. #include <boost/interprocess/detail/mpl.hpp>
  23. #include <boost/move/utility_core.hpp>
  24. #ifndef BOOST_INTERPROCESS_PERFECT_FORWARDING
  25. #include <boost/move/detail/fwd_macros.hpp>
  26. #else
  27. #include <boost/move/utility_core.hpp>
  28. #include <boost/interprocess/detail/variadic_templates_tools.hpp>
  29. #endif //#ifdef BOOST_INTERPROCESS_PERFECT_FORWARDING
  30. #include <boost/container/detail/placement_new.hpp>
  31. #include <cstddef>
  32. //!\file
  33. //!Describes a proxy class that implements named allocation syntax.
  34. namespace boost {
  35. namespace interprocess {
  36. namespace ipcdetail {
  37. template<class T>
  38. inline void named_construct_placement_destroy(void *mem, std::size_t num)
  39. {
  40. T* memory = static_cast<T*>(mem); \
  41. for(std::size_t destroyed = 0; destroyed < num; ++destroyed)
  42. (memory++)->~T();
  43. }
  44. #ifdef BOOST_INTERPROCESS_PERFECT_FORWARDING
  45. template<class T, bool is_iterator, class ...Args>
  46. struct CtorArgN
  47. {
  48. typedef T object_type;
  49. typedef bool_<is_iterator> IsIterator;
  50. typedef CtorArgN<T, is_iterator, Args...> self_t;
  51. typedef typename build_number_seq<sizeof...(Args)>::type index_tuple_t;
  52. self_t& operator++()
  53. {
  54. this->do_increment(IsIterator(), index_tuple_t());
  55. return *this;
  56. }
  57. self_t operator++(int) { return ++*this; *this; }
  58. CtorArgN(Args && ...args)
  59. : args_(args...)
  60. {}
  61. virtual void construct_n(void *mem, std::size_t num)
  62. {
  63. std::size_t constructed = 0;
  64. BOOST_INTERPROCESS_TRY{
  65. T* memory = static_cast<T*>(mem);
  66. for(constructed = 0; constructed < num; ++constructed){
  67. this->construct(memory++, IsIterator(), index_tuple_t());
  68. this->do_increment(IsIterator(), index_tuple_t());
  69. }
  70. }
  71. BOOST_INTERPROCESS_CATCH(...) {
  72. named_construct_placement_destroy<T>(mem, constructed);
  73. BOOST_INTERPROCESS_RETHROW
  74. } BOOST_INTERPROCESS_CATCH_END
  75. }
  76. private:
  77. template<std::size_t ...IdxPack>
  78. void construct(void *mem, true_, const index_tuple<IdxPack...>&)
  79. { ::new((void*)mem, boost_container_new_t())T(*boost::forward<Args>((get<IdxPack>)(args_))...); }
  80. template<std::size_t ...IdxPack>
  81. void construct(void *mem, false_, const index_tuple<IdxPack...>&)
  82. { ::new((void*)mem, boost_container_new_t())T(boost::forward<Args>((get<IdxPack>)(args_))...); }
  83. template<std::size_t ...IdxPack>
  84. void do_increment(true_, const index_tuple<IdxPack...>&)
  85. {
  86. this->expansion_helper(++(get<IdxPack>)(args_)...);
  87. }
  88. template<class ...ExpansionArgs>
  89. void expansion_helper(ExpansionArgs &&...)
  90. {}
  91. template<std::size_t ...IdxPack>
  92. void do_increment(false_, const index_tuple<IdxPack...>&)
  93. {}
  94. tuple<Args&...> args_;
  95. };
  96. //!Describes a proxy class that implements named
  97. //!allocation syntax.
  98. template
  99. < class SegmentManager //segment manager to construct the object
  100. , class T //type of object to build
  101. , bool is_iterator //passing parameters are normal object or iterators?
  102. >
  103. class named_proxy
  104. {
  105. typedef typename SegmentManager::char_type char_type;
  106. const char_type * mp_name;
  107. SegmentManager * mp_mngr;
  108. mutable std::size_t m_num;
  109. const bool m_find;
  110. const bool m_dothrow;
  111. public:
  112. named_proxy(SegmentManager *mngr, const char_type *name, bool find, bool dothrow)
  113. : mp_name(name), mp_mngr(mngr), m_num(1)
  114. , m_find(find), m_dothrow(dothrow)
  115. {}
  116. template<class ...Args>
  117. T *operator()(Args &&...args) const
  118. {
  119. CtorArgN<T, is_iterator, Args...> &&ctor_obj = CtorArgN<T, is_iterator, Args...>
  120. (boost::forward<Args>(args)...);
  121. return mp_mngr->generic_construct(ctor_obj, mp_name, m_num, m_find, m_dothrow);
  122. }
  123. //This operator allows --> named_new("Name")[3]; <-- syntax
  124. const named_proxy &operator[](std::size_t num) const
  125. { m_num *= num; return *this; }
  126. };
  127. #else //#ifdef BOOST_INTERPROCESS_PERFECT_FORWARDING
  128. #define BOOST_INTERPROCESS_NAMED_PROXY_CTORARGN(N)\
  129. \
  130. template<class T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N > \
  131. struct CtorArg##N\
  132. {\
  133. typedef T object_type;\
  134. typedef CtorArg##N self_t;\
  135. \
  136. CtorArg##N ( BOOST_MOVE_UREF##N )\
  137. BOOST_MOVE_COLON##N BOOST_MOVE_FWD_INIT##N{}\
  138. \
  139. virtual void construct_n(void *mem, std::size_t num)\
  140. {\
  141. std::size_t constructed = 0;\
  142. BOOST_INTERPROCESS_TRY{\
  143. T* memory = static_cast<T*>(mem);\
  144. for (constructed = 0; constructed < num; ++constructed) {\
  145. ::new((void*)memory++) T ( BOOST_MOVE_MFWD##N );\
  146. }\
  147. }\
  148. BOOST_INTERPROCESS_CATCH(...) {\
  149. named_construct_placement_destroy<T>(mem, constructed);\
  150. BOOST_INTERPROCESS_RETHROW\
  151. } BOOST_INTERPROCESS_CATCH_END\
  152. }\
  153. \
  154. private:\
  155. BOOST_MOVE_MREF##N\
  156. };\
  157. //!
  158. BOOST_MOVE_ITERATE_0TO9(BOOST_INTERPROCESS_NAMED_PROXY_CTORARGN)
  159. #undef BOOST_INTERPROCESS_NAMED_PROXY_CTORARGN
  160. #define BOOST_INTERPROCESS_NAMED_PROXY_CTORITN(N)\
  161. \
  162. template<class T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N > \
  163. struct CtorIt##N\
  164. {\
  165. typedef T object_type;\
  166. typedef CtorIt##N self_t;\
  167. \
  168. self_t& operator++()\
  169. { BOOST_MOVE_MINC##N; return *this; }\
  170. \
  171. self_t operator++(int) { return ++*this; *this; }\
  172. \
  173. CtorIt##N ( BOOST_MOVE_VAL##N )\
  174. BOOST_MOVE_COLON##N BOOST_MOVE_VAL_INIT##N{}\
  175. \
  176. virtual void construct_n(void *mem, std::size_t num)\
  177. {\
  178. std::size_t constructed = 0;\
  179. BOOST_INTERPROCESS_TRY{\
  180. T* memory = static_cast<T*>(mem);\
  181. for(constructed = 0; constructed < num; ++constructed){\
  182. ::new((void*)memory++) T( BOOST_MOVE_MITFWD##N );\
  183. ++(*this);\
  184. }\
  185. }\
  186. BOOST_INTERPROCESS_CATCH(...) {\
  187. named_construct_placement_destroy<T>(mem, constructed);\
  188. BOOST_INTERPROCESS_RETHROW\
  189. } BOOST_INTERPROCESS_CATCH_END\
  190. }\
  191. \
  192. private:\
  193. BOOST_MOVE_MEMB##N\
  194. };\
  195. //!
  196. BOOST_MOVE_ITERATE_0TO9(BOOST_INTERPROCESS_NAMED_PROXY_CTORITN)
  197. #undef BOOST_INTERPROCESS_NAMED_PROXY_CTORITN
  198. //!Describes a proxy class that implements named
  199. //!allocation syntax.
  200. template
  201. < class SegmentManager //segment manager to construct the object
  202. , class T //type of object to build
  203. , bool is_iterator //passing parameters are normal object or iterators?
  204. >
  205. class named_proxy
  206. {
  207. typedef T object_type;
  208. typedef typename SegmentManager::char_type char_type;
  209. const char_type * mp_name;
  210. SegmentManager * mp_mngr;
  211. mutable std::size_t m_num;
  212. const bool m_find;
  213. const bool m_dothrow;
  214. public:
  215. named_proxy(SegmentManager *mngr, const char_type *name, bool find, bool dothrow)
  216. : mp_name(name), mp_mngr(mngr), m_num(1)
  217. , m_find(find), m_dothrow(dothrow)
  218. {}
  219. #define BOOST_INTERPROCESS_NAMED_PROXY_CALL_OPERATOR(N)\
  220. \
  221. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  222. T *operator()( BOOST_MOVE_UREF##N ) const\
  223. {\
  224. typedef typename if_c<is_iterator \
  225. , CtorIt##N <T BOOST_MOVE_I##N BOOST_MOVE_TARG##N> \
  226. , CtorArg##N<T BOOST_MOVE_I##N BOOST_MOVE_TARG##N> \
  227. >::type ctor_obj_t;\
  228. ctor_obj_t ctor_obj = ctor_obj_t( BOOST_MOVE_FWD##N );\
  229. return mp_mngr->generic_construct(ctor_obj, mp_name, m_num, m_find, m_dothrow);\
  230. }\
  231. //
  232. BOOST_MOVE_ITERATE_0TO9(BOOST_INTERPROCESS_NAMED_PROXY_CALL_OPERATOR)
  233. #undef BOOST_INTERPROCESS_NAMED_PROXY_CALL_OPERATOR
  234. ////////////////////////////////////////////////////////////////////////
  235. // What the macro should generate (n == 2)
  236. ////////////////////////////////////////////////////////////////////////
  237. //
  238. // template <class P1, class P2>
  239. // T *operator()(P1 &p1, P2 &p2) const
  240. // {
  241. // typedef CtorArg2
  242. // <T, is_iterator, P1, P2>
  243. // ctor_obj_t;
  244. // ctor_obj_t ctor_obj(p1, p2);
  245. //
  246. // return mp_mngr->(ctor_obj, mp_name, m_num, m_find, m_dothrow);
  247. // }
  248. //
  249. //////////////////////////////////////////////////////////////////////////
  250. //This operator allows --> named_new("Name")[3]; <-- syntax
  251. const named_proxy &operator[](std::size_t num) const
  252. { m_num *= num; return *this; }
  253. };
  254. #endif //#ifdef BOOST_INTERPROCESS_PERFECT_FORWARDING
  255. }}} //namespace boost { namespace interprocess { namespace ipcdetail {
  256. #include <boost/interprocess/detail/config_end.hpp>
  257. #endif //#ifndef BOOST_INTERPROCESS_NAMED_PROXY_HPP