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
74template <typename Definition>
76{
77 using Object = typename Definition::Object;
78
79 OpaqueObject() { construct(buffer); }
80 ~OpaqueObject() { destruct(get()); }
81 OpaqueObject(OpaqueObject&& other) { moveConstruct(buffer, forward<Object>(other.get())); }
82 OpaqueObject& operator=(OpaqueObject&& other)
83 {
84 moveAssign(get(), forward<Object>(other.get()));
85 return *this;
86 }
87
88 // Disallow copy construction and copy assignment
89 OpaqueObject(const OpaqueObject&) = delete;
90 OpaqueObject& operator=(const OpaqueObject&) = delete;
91
92 Object& get() { return reinterpret_cast<Object&>(buffer); }
93 const Object& get() const { return reinterpret_cast<const Object&>(buffer); }
94
95 private:
96#if SC_PLATFORM_WINDOWS
97 static constexpr int Size = Definition::Windows;
98#elif SC_PLATFORM_APPLE
99 static constexpr int Size = Definition::Apple;
100#else
101 static constexpr int Size = Definition::Default;
102#endif
103 static constexpr int Alignment = Definition::Alignment;
104
106
108 static void construct(Handle& buffer);
109 static void destruct(Object& obj);
110 static void moveConstruct(Handle& buffer, Object&& obj);
111 static void moveAssign(Object& selfPointer, Object&& obj);
112};
113
115} // namespace SC
A buffer of bytes with given alignment.
Definition: AlignedStorage.h:25
Hides implementation details from public headers (static PIMPL).
Definition: OpaqueObject.h:76