| 7a956470 |
sago007 |
2016-02-14 17:09 |
1 |
/*! \file static_object.hpp |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
2 |
\brief Internal polymorphism static object support |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
3 |
\ingroup Internal */ |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
4 |
/* |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
5 |
Copyright (c) 2014, Randolph Voorhies, Shane Grant |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
6 |
All rights reserved. |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
7 |
Redistribution and use in source and binary forms, with or without |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
8 |
modification, are permitted provided that the following conditions are met: |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
9 |
* Redistributions of source code must retain the above copyright |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
10 |
notice, this list of conditions and the following disclaimer. |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
11 |
* Redistributions in binary form must reproduce the above copyright |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
12 |
notice, this list of conditions and the following disclaimer in the |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
13 |
documentation and/or other materials provided with the distribution. |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
14 |
* Neither the name of cereal nor the |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
15 |
names of its contributors may be used to endorse or promote products |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
16 |
derived from this software without specific prior written permission. |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
18 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
19 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
20 |
DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES OR SHANE GRANT BE LIABLE FOR ANY |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
21 |
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
22 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
23 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
24 |
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
25 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
26 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
27 |
*/ |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
28 |
#ifndef CEREAL_DETAILS_STATIC_OBJECT_HPP_ |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
29 |
#define CEREAL_DETAILS_STATIC_OBJECT_HPP_ |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
30 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
31 |
#include "cereal/macros.hpp" |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
32 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
33 |
#if CEREAL_THREAD_SAFE |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
34 |
#include <mutex> |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
35 |
#endif |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
36 |
|
| 7a956470 |
sago007 |
2016-02-14 17:09 |
37 |
//! Prevent link optimization from removing non-referenced static objects |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
38 |
/*! Especially for polymorphic support, we create static objects which |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
39 |
may not ever be explicitly referenced. Most linkers will detect this |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
40 |
and remove the code causing various unpleasant runtime errors. These |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
41 |
macros, adopted from Boost (see force_include.hpp) prevent this |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
42 |
(C) Copyright 2002 Robert Ramey - http://www.rrsd.com . |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
43 |
Use, modification and distribution is subject to the Boost Software |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
44 |
License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
45 |
http://www.boost.org/LICENSE_1_0.txt) */ |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
46 |
|
| 7a956470 |
sago007 |
2016-02-14 17:09 |
47 |
#ifdef _MSC_VER |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
48 |
# define CEREAL_DLL_EXPORT __declspec(dllexport) |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
49 |
# define CEREAL_USED |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
50 |
#else // clang or gcc |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
51 |
# define CEREAL_DLL_EXPORT |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
52 |
# define CEREAL_USED __attribute__ ((__used__)) |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
53 |
#endif |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
54 |
|
| 7a956470 |
sago007 |
2016-02-14 17:09 |
55 |
namespace cereal |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
56 |
{ |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
57 |
namespace detail |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
58 |
{ |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
59 |
//! A static, pre-execution object |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
60 |
/*! This class will create a single copy (singleton) of some |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
61 |
type and ensures that merely referencing this type will |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
62 |
cause it to be instantiated and initialized pre-execution. |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
63 |
For example, this is used heavily in the polymorphic pointer |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
64 |
serialization mechanisms to bind various archive types with |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
65 |
different polymorphic classes */ |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
66 |
template <class T> |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
67 |
class CEREAL_DLL_EXPORT StaticObject |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
68 |
{ |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
69 |
private: |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
70 |
//! Forces instantiation at pre-execution time |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
71 |
static void instantiate( T const & ) {} |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
72 |
|
| 7a956470 |
sago007 |
2016-02-14 17:09 |
73 |
static T & create() |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
74 |
{ |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
75 |
static T t; |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
76 |
instantiate(instance); |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
77 |
return t; |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
78 |
} |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
79 |
|
| 7a956470 |
sago007 |
2016-02-14 17:09 |
80 |
StaticObject( StaticObject const & /*other*/ ) {} |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
81 |
|
| 7a956470 |
sago007 |
2016-02-14 17:09 |
82 |
public: |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
83 |
static T & getInstance() |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
84 |
{ |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
85 |
return create(); |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
86 |
} |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
87 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
88 |
//! A class that acts like std::lock_guard |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
89 |
class LockGuard |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
90 |
{ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
91 |
#if CEREAL_THREAD_SAFE |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
92 |
public: |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
93 |
LockGuard(std::mutex & m) : lock(m) {} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
94 |
private: |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
95 |
std::unique_lock<std::mutex> lock; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
96 |
#else |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
97 |
public: |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
98 |
~LockGuard() CEREAL_NOEXCEPT {} // prevents variable not used |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
99 |
#endif |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
100 |
}; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
101 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
102 |
//! Attempts to lock this static object for the current scope |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
103 |
/*! @note This function is a no-op if cereal is not compiled with |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
104 |
thread safety enabled (CEREAL_THREAD_SAFE = 1). |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
105 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
106 |
This function returns an object that holds a lock for |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
107 |
this StaticObject that will release its lock upon destruction. This |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
108 |
call will block until the lock is available. */ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
109 |
static LockGuard lock() |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
110 |
{ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
111 |
#if CEREAL_THREAD_SAFE |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
112 |
static std::mutex instanceMutex; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
113 |
return LockGuard{instanceMutex}; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
114 |
#else |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
115 |
return LockGuard{}; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
116 |
#endif |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
117 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
118 |
|
| 7a956470 |
sago007 |
2016-02-14 17:09 |
119 |
private: |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
120 |
static T & instance; |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
121 |
}; |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
122 |
|
| 7a956470 |
sago007 |
2016-02-14 17:09 |
123 |
template <class T> T & StaticObject<T>::instance = StaticObject<T>::create(); |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
124 |
} // namespace detail |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
125 |
} // namespace cereal |
| 7a956470 |
sago007 |
2016-02-14 17:09 |
126 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
127 |
#endif // CEREAL_DETAILS_STATIC_OBJECT_HPP_ |
|
|
1970-01-01 00:00 |
128 |
|