named_proxy.hpp 9.7 KB

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