named_proxy.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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, std::size_t num) BOOST_OVERRIDE
  55. {
  56. std::size_t constructed = 0;
  57. BOOST_TRY{
  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. BOOST_CATCH(...) {
  65. this->placement_destroy<T>::destroy_n(mem, constructed);
  66. BOOST_RETHROW
  67. } BOOST_CATCH_END
  68. }
  69. private:
  70. template<std::size_t ...IdxPack>
  71. void construct(void *mem, true_, const index_tuple<IdxPack...>&)
  72. { ::new((void*)mem, boost_container_new_t())T(*boost::forward<Args>((get<IdxPack>)(args_))...); }
  73. template<std::size_t ...IdxPack>
  74. void construct(void *mem, false_, const index_tuple<IdxPack...>&)
  75. { ::new((void*)mem, boost_container_new_t())T(boost::forward<Args>((get<IdxPack>)(args_))...); }
  76. template<std::size_t ...IdxPack>
  77. void do_increment(true_, const index_tuple<IdxPack...>&)
  78. {
  79. this->expansion_helper(++(get<IdxPack>)(args_)...);
  80. }
  81. template<class ...ExpansionArgs>
  82. void expansion_helper(ExpansionArgs &&...)
  83. {}
  84. template<std::size_t ...IdxPack>
  85. void do_increment(false_, const index_tuple<IdxPack...>&)
  86. {}
  87. tuple<Args&...> args_;
  88. };
  89. //!Describes a proxy class that implements named
  90. //!allocation syntax.
  91. template
  92. < class SegmentManager //segment manager to construct the object
  93. , class T //type of object to build
  94. , bool is_iterator //passing parameters are normal object or iterators?
  95. >
  96. class named_proxy
  97. {
  98. typedef typename SegmentManager::char_type char_type;
  99. const char_type * mp_name;
  100. SegmentManager * mp_mngr;
  101. mutable std::size_t m_num;
  102. const bool m_find;
  103. const bool m_dothrow;
  104. public:
  105. named_proxy(SegmentManager *mngr, const char_type *name, bool find, bool dothrow)
  106. : mp_name(name), mp_mngr(mngr), m_num(1)
  107. , m_find(find), m_dothrow(dothrow)
  108. {}
  109. template<class ...Args>
  110. T *operator()(Args &&...args) const
  111. {
  112. CtorArgN<T, is_iterator, Args...> &&ctor_obj = CtorArgN<T, is_iterator, Args...>
  113. (boost::forward<Args>(args)...);
  114. return mp_mngr->template
  115. generic_construct<T>(mp_name, m_num, m_find, m_dothrow, ctor_obj);
  116. }
  117. //This operator allows --> named_new("Name")[3]; <-- syntax
  118. const named_proxy &operator[](std::size_t num) const
  119. { m_num *= num; return *this; }
  120. };
  121. #else //#ifdef BOOST_INTERPROCESS_PERFECT_FORWARDING
  122. #define BOOST_INTERPROCESS_NAMED_PROXY_CTORARGN(N)\
  123. \
  124. template<class T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N > \
  125. struct CtorArg##N : placement_destroy<T>\
  126. {\
  127. typedef CtorArg##N self_t;\
  128. \
  129. CtorArg##N ( BOOST_MOVE_UREF##N )\
  130. BOOST_MOVE_COLON##N BOOST_MOVE_FWD_INIT##N{}\
  131. \
  132. virtual void construct_n(void *mem, std::size_t num) BOOST_OVERRIDE\
  133. {\
  134. std::size_t constructed = 0;\
  135. BOOST_TRY{\
  136. T* memory = static_cast<T*>(mem);\
  137. for (constructed = 0; constructed < num; ++constructed) {\
  138. ::new((void*)memory++) T ( BOOST_MOVE_MFWD##N );\
  139. }\
  140. }\
  141. BOOST_CATCH(...) {\
  142. this->placement_destroy<T>::destroy_n(mem, constructed);\
  143. BOOST_RETHROW\
  144. } BOOST_CATCH_END\
  145. }\
  146. \
  147. private:\
  148. BOOST_MOVE_MREF##N\
  149. };\
  150. //!
  151. BOOST_MOVE_ITERATE_0TO9(BOOST_INTERPROCESS_NAMED_PROXY_CTORARGN)
  152. #undef BOOST_INTERPROCESS_NAMED_PROXY_CTORARGN
  153. #define BOOST_INTERPROCESS_NAMED_PROXY_CTORITN(N)\
  154. \
  155. template<class T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N > \
  156. struct CtorIt##N : public placement_destroy<T>\
  157. {\
  158. typedef CtorIt##N self_t;\
  159. \
  160. self_t& operator++()\
  161. { BOOST_MOVE_MINC##N; return *this; }\
  162. \
  163. self_t operator++(int) { return ++*this; *this; }\
  164. \
  165. CtorIt##N ( BOOST_MOVE_VAL##N )\
  166. BOOST_MOVE_COLON##N BOOST_MOVE_VAL_INIT##N{}\
  167. \
  168. virtual void construct_n(void *mem, std::size_t num) BOOST_OVERRIDE\
  169. {\
  170. std::size_t constructed = 0;\
  171. BOOST_TRY{\
  172. T* memory = static_cast<T*>(mem);\
  173. for(constructed = 0; constructed < num; ++constructed){\
  174. ::new((void*)memory++) T( BOOST_MOVE_MITFWD##N );\
  175. ++(*this);\
  176. }\
  177. }\
  178. BOOST_CATCH(...) {\
  179. this->placement_destroy<T>::destroy_n(mem, constructed);\
  180. BOOST_RETHROW\
  181. } BOOST_CATCH_END\
  182. }\
  183. \
  184. private:\
  185. BOOST_MOVE_MEMB##N\
  186. };\
  187. //!
  188. BOOST_MOVE_ITERATE_0TO9(BOOST_INTERPROCESS_NAMED_PROXY_CTORITN)
  189. #undef BOOST_INTERPROCESS_NAMED_PROXY_CTORITN
  190. //!Describes a proxy class that implements named
  191. //!allocation syntax.
  192. template
  193. < class SegmentManager //segment manager to construct the object
  194. , class T //type of object to build
  195. , bool is_iterator //passing parameters are normal object or iterators?
  196. >
  197. class named_proxy
  198. {
  199. typedef typename SegmentManager::char_type char_type;
  200. const char_type * mp_name;
  201. SegmentManager * mp_mngr;
  202. mutable std::size_t m_num;
  203. const bool m_find;
  204. const bool m_dothrow;
  205. public:
  206. named_proxy(SegmentManager *mngr, const char_type *name, bool find, bool dothrow)
  207. : mp_name(name), mp_mngr(mngr), m_num(1)
  208. , m_find(find), m_dothrow(dothrow)
  209. {}
  210. #define BOOST_INTERPROCESS_NAMED_PROXY_CALL_OPERATOR(N)\
  211. \
  212. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  213. T *operator()( BOOST_MOVE_UREF##N ) const\
  214. {\
  215. typedef typename if_c<is_iterator \
  216. , CtorIt##N <T BOOST_MOVE_I##N BOOST_MOVE_TARG##N> \
  217. , CtorArg##N<T BOOST_MOVE_I##N BOOST_MOVE_TARG##N> \
  218. >::type ctor_obj_t;\
  219. ctor_obj_t ctor_obj = ctor_obj_t( BOOST_MOVE_FWD##N );\
  220. return mp_mngr->template generic_construct<T>(mp_name, m_num, m_find, m_dothrow, ctor_obj);\
  221. }\
  222. //
  223. BOOST_MOVE_ITERATE_0TO9(BOOST_INTERPROCESS_NAMED_PROXY_CALL_OPERATOR)
  224. #undef BOOST_INTERPROCESS_NAMED_PROXY_CALL_OPERATOR
  225. ////////////////////////////////////////////////////////////////////////
  226. // What the macro should generate (n == 2)
  227. ////////////////////////////////////////////////////////////////////////
  228. //
  229. // template <class P1, class P2>
  230. // T *operator()(P1 &p1, P2 &p2) const
  231. // {
  232. // typedef CtorArg2
  233. // <T, is_iterator, P1, P2>
  234. // ctor_obj_t;
  235. // ctor_obj_t ctor_obj(p1, p2);
  236. //
  237. // return mp_mngr->template generic_construct<T>
  238. // (mp_name, m_num, m_find, m_dothrow, ctor_obj);
  239. // }
  240. //
  241. //////////////////////////////////////////////////////////////////////////
  242. //This operator allows --> named_new("Name")[3]; <-- syntax
  243. const named_proxy &operator[](std::size_t num) const
  244. { m_num *= num; return *this; }
  245. };
  246. #endif //#ifdef BOOST_INTERPROCESS_PERFECT_FORWARDING
  247. }}} //namespace boost { namespace interprocess { namespace ipcdetail {
  248. #include <boost/interprocess/detail/config_end.hpp>
  249. #endif //#ifndef BOOST_INTERPROCESS_NAMED_PROXY_HPP