-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathBaseClasses.hpp
117 lines (100 loc) · 2.94 KB
/
BaseClasses.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright (c) 2024 Snowflake Computing, Inc. All rights reserved.
*/
#pragma once
#ifndef BASECLASSES_HPP
#define BASECLASSES_HPP
#include <mutex>
namespace Snowflake
{
namespace Client
{
/**
* Inherit from this class to prohibit assignment and copy construction.
* The inheritance should be private.
*
* Move construction is possible but protected, to allow subclasses
* that cannot be copied, but can still be moved (such as std::unique_ptr).
* Note that there is no move assignment operator. Technically, move
* assignment is perfectly legal on DoNotCopy objects, but providing a public
* or protected operator=(DoNotCopy&&) would make it too easy to
* inadvertently slice a derived class when assigning from an rvalue.
* Derived classes may still define their own move assignment operator,
* skipping the assignment on the DoNotCopy base object.
*
* @example
* class MyComplexClass : private DoNotCopy
* {
* ...
* };
*/
class DoNotCopy
{
protected:
// Allow default construction by subclass constructors
#if defined(WIN32) || defined(_WIN64)
DoNotCopy() {}
// Allow move construction by subclass constructors
DoNotCopy(DoNotCopy &&other)
{
}
// Protected destructor, to disable unsafe polymorphic destruction
~DoNotCopy() {}
#else
// Allow default construction by subclass constructors
inline constexpr DoNotCopy() noexcept = default;
// Allow move construction by subclass constructors
inline DoNotCopy(DoNotCopy &&other) noexcept = default;
// Protected destructor, to disable unsafe polymorphic destruction
inline ~DoNotCopy() noexcept = default;
#endif
private:
#if defined(WIN32) || defined(_WIN64)
// Disallow copy construction and copy assignment
DoNotCopy(const DoNotCopy&);
DoNotCopy& operator=(const DoNotCopy&);
#else
// Disallow copy construction and copy assignment
DoNotCopy(const DoNotCopy&) = delete;
DoNotCopy& operator=(const DoNotCopy&) = delete;
#endif
};
/**
* The singleton template
* This template provides getInstance() function
* creating a singleton instance of the class T
*
* Usage:
* class T : public Singleton<T>
* { ... }
*/
template <typename T>
class Singleton
{
private:
public:
/**
* Get the singleton instance
*/
static T& getInstance()
{
if (!s_singleton)
{
std::lock_guard<std::mutex> guard(s_initLock);
if (!s_singleton)
{
s_singleton = new T;
}
}
return *s_singleton;
}
private:
static std::mutex s_initLock; // lock on the singleton intialization
static T* s_singleton; // the singleton instance of the class T
};
// Static member declarations
template <typename T> std::mutex Singleton<T>::s_initLock;
template <typename T> T* Singleton<T>::s_singleton;
} // namespace Client
} // namespace Snowflake
#endif /* BASECLASSES_HPP */