Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / interprocess / sync / shm / named_creation_functor.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2007-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_SYNC_NAMED_CREATION_FUNCTOR_HPP
12 #define BOOST_INTERPROCESS_SYNC_NAMED_CREATION_FUNCTOR_HPP
13
14 #if defined(_MSC_VER)
15 #  pragma once
16 #endif
17
18 #include <boost/interprocess/creation_tags.hpp>
19 #include <boost/interprocess/detail/type_traits.hpp>
20 #include <boost/interprocess/detail/mpl.hpp>
21
22 namespace boost {
23 namespace interprocess {
24 namespace ipcdetail {
25
26 struct named_creation_functor_no_arg{};
27
28 template <class T, class Arg = named_creation_functor_no_arg>
29 class named_creation_functor
30 {
31    typedef named_creation_functor_no_arg no_arg_t;
32    public:
33    named_creation_functor(create_enum_t type, Arg arg = Arg())
34       :  m_creation_type(type), m_arg(arg){}
35
36    template<class ArgType>
37    void construct(void *address, typename enable_if_c<is_same<ArgType, no_arg_t>::value>::type * = 0) const
38    {  new(address)T; }
39
40    template<class ArgType>
41    void construct(void *address, typename enable_if_c<!is_same<ArgType, no_arg_t>::value>::type * = 0) const
42    {  new(address)T(m_arg); }
43
44    bool operator()(void *address, std::size_t, bool created) const
45    {
46       switch(m_creation_type){
47          case DoOpen:
48             return true;
49          break;
50          case DoCreate:
51          case DoOpenOrCreate:
52             if(created){
53                construct<Arg>(address);
54             }
55             return true;
56          break;
57
58          default:
59             return false;
60          break;
61       }
62    }
63
64    std::size_t get_min_size() const
65    {  return sizeof(T);  }
66
67    private:
68    create_enum_t m_creation_type;
69    Arg m_arg;
70 };
71
72 }  //namespace ipcdetail {
73 }  //namespace interprocess {
74 }  //namespace boost {
75
76 #endif   //BOOST_INTERPROCESS_SYNC_NAMED_CREATION_FUNCTOR_HPP