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