Sane C++ Libraries
C++ Platform Abstraction Libraries
OpaqueObject.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "AlignedStorage.h"
5namespace SC
6{
9
21
75template <typename Definition>
77{
78 using Object = typename Definition::Object;
79
80 OpaqueObject() { construct(buffer); }
81 ~OpaqueObject() { destruct(get()); }
82 OpaqueObject(OpaqueObject&& other) { moveConstruct(buffer, forward<Object>(other.get())); }
83 OpaqueObject& operator=(OpaqueObject&& other)
84 {
85 moveAssign(get(), forward<Object>(other.get()));
86 return *this;
87 }
88
89 // Disallow copy construction and copy assignment
90 OpaqueObject(const OpaqueObject&) = delete;
91 OpaqueObject& operator=(const OpaqueObject&) = delete;
92
93 Object& get() { return reinterpret_cast<Object&>(buffer); }
94 const Object& get() const { return reinterpret_cast<const Object&>(buffer); }
95
96 private:
97#if SC_PLATFORM_WINDOWS
98 static constexpr int Size = Definition::Windows;
99#elif SC_PLATFORM_APPLE
100 static constexpr int Size = Definition::Apple;
101#elif SC_PLATFORM_LINUX
102 static constexpr int Size = Definition::Linux;
103#else
104 static constexpr int Size = Definition::Default;
105#endif
106 static constexpr int Alignment = Definition::Alignment;
107
109
111 static void construct(Handle& buffer);
112 static void destruct(Object& obj);
113 static void moveConstruct(Handle& buffer, Object&& obj);
114 static void moveAssign(Object& selfPointer, Object&& obj);
115};
116
118} // namespace SC
A buffer of bytes with given alignment.
Definition: AlignedStorage.h:25
Hides implementation details from public headers (static PIMPL).
Definition: OpaqueObject.h:77