cereal
A C++11 library for serialization
static_object.hpp
Go to the documentation of this file.
1 
4 /*
5  Copyright (c) 2014, Randolph Voorhies, Shane Grant
6  All rights reserved.
7  Redistribution and use in source and binary forms, with or without
8  modification, are permitted provided that the following conditions are met:
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in the
13  documentation and/or other materials provided with the distribution.
14  * Neither the name of cereal nor the
15  names of its contributors may be used to endorse or promote products
16  derived from this software without specific prior written permission.
17  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES OR SHANE GRANT BE LIABLE FOR ANY
21  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 #ifndef CEREAL_DETAILS_STATIC_OBJECT_HPP_
29 #define CEREAL_DETAILS_STATIC_OBJECT_HPP_
30 
31 #include "cereal/macros.hpp"
32 
33 #if CEREAL_THREAD_SAFE
34 #include <mutex>
35 #endif
36 
38 
47 #ifdef _MSC_VER
48 # define CEREAL_DLL_EXPORT __declspec(dllexport)
49 # define CEREAL_USED
50 #else // clang or gcc
51 # define CEREAL_DLL_EXPORT
52 # define CEREAL_USED __attribute__ ((__used__))
53 #endif
54 
55 namespace cereal
56 {
57  namespace detail
58  {
60 
66  template <class T>
68  {
69  private:
71  static void instantiate( T const & ) {}
72 
73  static T & create()
74  {
75  static T t;
76  instantiate(instance);
77  return t;
78  }
79 
80  StaticObject( StaticObject const & /*other*/ ) {}
81 
82  public:
83  static T & getInstance()
84  {
85  return create();
86  }
87 
89  class LockGuard
90  {
91  #if CEREAL_THREAD_SAFE
92  public:
93  LockGuard(std::mutex & m) : lock(m) {}
94  private:
95  std::unique_lock<std::mutex> lock;
96  #else
97  public:
98  ~LockGuard() CEREAL_NOEXCEPT {} // prevents variable not used
99  #endif
100  };
101 
103 
109  static LockGuard lock()
110  {
111  #if CEREAL_THREAD_SAFE
112  static std::mutex instanceMutex;
113  return LockGuard{instanceMutex};
114  #else
115  return LockGuard{};
116  #endif
117  }
118 
119  private:
120  static T & instance;
121  };
122 
123  template <class T> T & StaticObject<T>::instance = StaticObject<T>::create();
124  } // namespace detail
125 } // namespace cereal
126 
127 #endif // CEREAL_DETAILS_STATIC_OBJECT_HPP_
static LockGuard lock()
Attempts to lock this static object for the current scope.
Definition: static_object.hpp:109
Definition: access.hpp:40
Preprocessor macros that can customise the cereal library.
#define CEREAL_NOEXCEPT
Defines the CEREAL_NOEXCEPT macro to use instead of noexcept.
Definition: macros.hpp:130
#define CEREAL_DLL_EXPORT
Prevent link optimization from removing non-referenced static objects.
Definition: static_object.hpp:51
A static, pre-execution object.
Definition: static_object.hpp:67
A class that acts like std::lock_guard.
Definition: static_object.hpp:89