named_creation_template.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2004-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_TEST_NAMED_RESOURCE_TEMPLATE_HEADER
  11. #define BOOST_INTERPROCESS_TEST_NAMED_RESOURCE_TEMPLATE_HEADER
  12. #include <boost/interprocess/detail/config_begin.hpp>
  13. #include <boost/interprocess/exceptions.hpp>
  14. #include "boost_interprocess_check.hpp"
  15. #include "get_process_id_name.hpp"
  16. #include <iostream>
  17. #include <typeinfo>
  18. #include <boost/interprocess/creation_tags.hpp>
  19. namespace boost { namespace interprocess { namespace test {
  20. template <class NamedResource>
  21. inline void create_then_open_then_open_or_create()
  22. {
  23. try{
  24. //Create it and open it twice
  25. NamedResource nresource1(create_only);
  26. NamedResource nresource2(open_only);
  27. NamedResource nresource3(open_or_create);
  28. }
  29. catch(...){
  30. //This shouldn't throw so show the error
  31. BOOST_INTERPROCESS_CHECK( false );
  32. }
  33. }
  34. template <class NamedResource>
  35. inline void open_or_create_then_create()
  36. {
  37. //Create it with open_or_create and try to create it twice
  38. NamedResource nresource1(open_or_create);
  39. try{
  40. NamedResource nresource2(create_only);
  41. }
  42. catch(interprocess_exception &err){
  43. BOOST_INTERPROCESS_CHECK(err.get_error_code() == already_exists_error);
  44. }
  45. }
  46. template <class NamedResource>
  47. inline void dont_create_and_open()
  48. {
  49. //Try to open it without creating
  50. try{
  51. NamedResource nresource1(open_only);
  52. }
  53. catch(interprocess_exception &err){
  54. BOOST_INTERPROCESS_CHECK(err.get_error_code() == not_found_error);
  55. return;
  56. }
  57. //The mutex should not exist
  58. BOOST_INTERPROCESS_CHECK(false);
  59. }
  60. template <class NamedResource>
  61. inline void test_named_creation()
  62. {
  63. std::cout << "create_then_open_then_open_or_create<"
  64. << typeid(NamedResource).name() << ">" << std::endl;
  65. create_then_open_then_open_or_create<NamedResource>();
  66. std::cout << "open_or_create_then_create<"
  67. << typeid(NamedResource).name() << ">" << std::endl;
  68. open_or_create_then_create<NamedResource>();
  69. std::cout << "dont_create_and_open<"
  70. << typeid(NamedResource).name() << ">" << std::endl;
  71. dont_create_and_open<NamedResource>();
  72. }
  73. template<class NamedSync>
  74. class named_sync_wrapper
  75. : public NamedSync
  76. {
  77. public:
  78. named_sync_wrapper()
  79. : NamedSync(open_or_create, test::get_process_id_ptr_name(this))
  80. {}
  81. ~named_sync_wrapper()
  82. {
  83. NamedSync::remove(test::get_process_id_ptr_name(this));
  84. }
  85. };
  86. template<class NamedSync>
  87. struct named_sync_deleter
  88. {
  89. ~named_sync_deleter()
  90. { NamedSync::remove(test::get_process_id_name()); }
  91. };
  92. //This wrapper is necessary to have a common constructor
  93. //in generic named_creation_template functions
  94. template<class NamedSync>
  95. class named_sync_creation_test_wrapper
  96. : public test::named_sync_deleter<NamedSync>, public NamedSync
  97. {
  98. public:
  99. named_sync_creation_test_wrapper(create_only_t)
  100. : NamedSync(create_only, test::get_process_id_name())
  101. {}
  102. named_sync_creation_test_wrapper(open_only_t)
  103. : NamedSync(open_only, test::get_process_id_name())
  104. {}
  105. named_sync_creation_test_wrapper(open_or_create_t)
  106. : NamedSync(open_or_create, test::get_process_id_name())
  107. {}
  108. ~named_sync_creation_test_wrapper()
  109. {}
  110. };
  111. }}} //namespace boost { namespace interprocess { namespace test {
  112. #include <boost/interprocess/detail/config_end.hpp>
  113. #endif //BOOST_INTERPROCESS_TEST_NAMED_RESOURCE_TEMPLATE_HEADER