named_construct_test.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2008-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. #include <boost/interprocess/detail/config_begin.hpp>
  11. #include <boost/interprocess/detail/workaround.hpp>
  12. #include <boost/interprocess/managed_shared_memory.hpp>
  13. // intrusive/detail
  14. #include <boost/intrusive/detail/minimal_pair_header.hpp>
  15. typedef std::pair<double, int> simple_pair;
  16. using namespace boost::interprocess;
  17. struct array_pair : public simple_pair
  18. {
  19. array_pair(double d, int i)
  20. : simple_pair(d, i) {}
  21. };
  22. struct array_it_pair : public array_pair
  23. {
  24. array_it_pair(double d, int i)
  25. : array_pair(d, i) {}
  26. };
  27. struct named_name_generator
  28. {
  29. static const bool searchable = true;
  30. typedef simple_pair simple_type;
  31. typedef array_pair array_type;
  32. typedef array_it_pair array_it_type;
  33. static const char *get_simple_name()
  34. { return "MyType instance"; }
  35. static const char *get_array_name()
  36. { return "MyType array"; }
  37. static const char *get_array_it_name()
  38. { return "MyType array from it"; }
  39. };
  40. struct unique_name_generator
  41. {
  42. static const bool searchable = true;
  43. typedef simple_pair simple_type;
  44. typedef array_pair array_type;
  45. typedef array_it_pair array_it_type;
  46. static const ipcdetail::unique_instance_t *get_simple_name()
  47. { return 0; }
  48. static const ipcdetail::unique_instance_t *get_array_name()
  49. { return 0; }
  50. static const ipcdetail::unique_instance_t *get_array_it_name()
  51. { return 0; }
  52. };
  53. struct anonymous_name_generator
  54. {
  55. static const bool searchable = false;
  56. typedef simple_pair simple_type;
  57. typedef array_pair array_type;
  58. typedef array_it_pair array_it_type;
  59. static const ipcdetail::anonymous_instance_t *get_simple_name()
  60. { return 0; }
  61. static const ipcdetail::anonymous_instance_t *get_array_name()
  62. { return 0; }
  63. static const ipcdetail::anonymous_instance_t *get_array_it_name()
  64. { return 0; }
  65. };
  66. template<class NameGenerator>
  67. int construct_test()
  68. {
  69. typedef typename NameGenerator::simple_type simple_type;
  70. typedef typename NameGenerator::array_type array_type;
  71. typedef typename NameGenerator::array_it_type array_it_type;
  72. remove_shared_memory_on_destroy remover("MySharedMemory");
  73. shared_memory_object::remove("MySharedMemory");
  74. {
  75. //A special shared memory where we can
  76. //construct objects associated with a name.
  77. //First remove any old shared memory of the same name, create
  78. //the shared memory segment and initialize needed resources
  79. managed_shared_memory segment
  80. //create segment name segment size
  81. (create_only, "MySharedMemory", 65536);
  82. //Create an object of MyType initialized to {0.0, 0}
  83. simple_type *s = segment.construct<simple_type>
  84. (NameGenerator::get_simple_name())//name of the object
  85. (1.0, 2); //ctor first argument
  86. assert(s->first == 1.0 && s->second == 2);
  87. if(!(s->first == 1.0 && s->second == 2))
  88. return 1;
  89. //Create an array of 10 elements of MyType initialized to {0.0, 0}
  90. array_type *a = segment.construct<array_type>
  91. (NameGenerator::get_array_name()) //name of the object
  92. [10] //number of elements
  93. (3.0, 4); //Same two ctor arguments for all objects
  94. assert(a->first == 3.0 && a->second == 4);
  95. if(!(a->first == 3.0 && a->second == 4))
  96. return 1;
  97. //Create an array of 3 elements of MyType initializing each one
  98. //to a different value {0.0, 3}, {1.0, 4}, {2.0, 5}...
  99. float float_initializer[3] = { 0.0, 1.0, 2.0 };
  100. int int_initializer[3] = { 3, 4, 5 };
  101. array_it_type *a_it = segment.construct_it<array_it_type>
  102. (NameGenerator::get_array_it_name()) //name of the object
  103. [3] //number of elements
  104. ( &float_initializer[0] //Iterator for the 1st ctor argument
  105. , &int_initializer[0]); //Iterator for the 2nd ctor argument
  106. {
  107. const array_it_type *a_it_ptr = a_it;
  108. for(unsigned int i = 0, max = 3; i != max; ++i, ++a_it_ptr){
  109. assert(a_it_ptr->first == float_initializer[i]);
  110. if(a_it_ptr->first != float_initializer[i]){
  111. return 1;
  112. }
  113. assert(a_it_ptr->second == int_initializer[i]);
  114. if(a_it_ptr->second != int_initializer[i]){
  115. return 1;
  116. }
  117. }
  118. }
  119. if(NameGenerator::searchable){
  120. {
  121. std::pair<simple_type*, managed_shared_memory::size_type> res;
  122. //Find the object
  123. res = segment.find<simple_type> (NameGenerator::get_simple_name());
  124. //Length should be 1
  125. assert(res.second == 1);
  126. if(res.second != 1)
  127. return 1;
  128. assert(res.first == s);
  129. if(res.first != s)
  130. return 1;
  131. }
  132. {
  133. std::pair<array_type*, managed_shared_memory::size_type> res;
  134. //Find the array
  135. res = segment.find<array_type> (NameGenerator::get_array_name());
  136. //Length should be 10
  137. assert(res.second == 10);
  138. if(res.second != 10)
  139. return 1;
  140. assert(res.first == a);
  141. if(res.first != a)
  142. return 1;
  143. }
  144. {
  145. std::pair<array_it_type*, managed_shared_memory::size_type> res;
  146. //Find the array constructed from iterators
  147. res = segment.find<array_it_type> (NameGenerator::get_array_it_name());
  148. //Length should be 3
  149. assert(res.second == 3);
  150. if(res.second != 3)
  151. return 1;
  152. assert(res.first == a_it);
  153. if(res.first != a_it)
  154. return 1;
  155. }
  156. }
  157. //We're done, delete all the objects
  158. segment.destroy_ptr<simple_type>(s);
  159. segment.destroy_ptr<array_type>(a);
  160. segment.destroy_ptr<array_it_type>(a_it);
  161. }
  162. return 0;
  163. }
  164. int main ()
  165. {
  166. if(0 != construct_test<named_name_generator>())
  167. return 1;
  168. if(0 != construct_test<unique_name_generator>())
  169. return 1;
  170. if(0 != construct_test<anonymous_name_generator>())
  171. return 1;
  172. return 0;
  173. }
  174. //]
  175. #include <boost/interprocess/detail/config_end.hpp>