libbio
Loading...
Searching...
No Matches
SingletonMacros.h
Go to the documentation of this file.
1/*
2 * This file is a part of the Biology project by eons LLC.
3 * Biology (aka Develop Biology) is a framework for approaching software
4 * development from a natural sciences perspective.
5 *
6 * Copyright (C) 2022 Séon O'Shannon & eons LLC
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include <cstddef>
25#include <new>
26
27// BIO_SINGLETON emits a class deriving `virtual public ::bio::ThreadSafe`, so
28// the singleton retains its own lock regardless of its base. That type must be
29// visible wherever the macro is expanded. Historically this was satisfied only
30// transitively (e.g. via Container -> ThreadSafe); now that Container no longer
31// carries a ThreadSafe base (v8 layout), include it here so the macro is
32// self-sufficient. ThreadSafe.h's only include is Macros.h (pragma-once), and
33// the macros ThreadSafe.h consumes (BIO_THREAD_ENFORCEMENT_LEVEL,
34// BIO_CPP_VERSION, BIO_OS_IS_LINUX) are defined by Optimize/Language/OS macros
35// that Macros.h includes before this header, so there is no ordering hazard.
37
38namespace bio {
39
40namespace pointer_arena {
41void* Allocate(
42 ::std::size_t bytes,
43 ::std::size_t align);
44}
45
46typedef void* (*SingletonCreator)();
47
49 const char* key,
51);
52
53} //bio namespace
54
55//@formatter:off
56#if BIO_COMPACT_POINTER_STORAGE
57 #define BIO_SINGLETON_CREATE_INSTANCE(className) \
58 void* storage = ::bio::pointer_arena::Allocate( \
59 sizeof(className), \
60 __alignof__(className)); \
61 if (!storage) \
62 { \
63 throw ::std::bad_alloc(); \
64 } \
65 return new (storage) className();
66#else
67 #define BIO_SINGLETON_CREATE_INSTANCE(className) \
68 return new className();
69#endif
70//@formatter:on
71
78#define BIO_SINGLETON(className, baseClass) \
79class className : \
80 public baseClass, \
81 virtual public ::bio::ThreadSafe \
82{ \
83public: \
84 static className& Instance() \
85 { \
86 return *static_cast<className*>( \
87 ::bio::GetOrCreateSingleton( \
88 #className, \
89 &className::CreateInstance)); \
90 } \
91private: \
92 static void* CreateInstance() \
93 { \
94 BIO_SINGLETON_CREATE_INSTANCE(className) \
95 } \
96 className() \
97 { \
98 } \
99 className(className const &); \
100 void operator=(className const &); \
101};
Definition Pointer.h:115
void * Allocate(::std::size_t bytes, ::std::size_t align)
Definition PointerArena.cpp:2157
Definition FinalCell.h:29
void *(* SingletonCreator)()
Definition SingletonMacros.h:46
void * GetOrCreateSingleton(const char *key, SingletonCreator creator)
Definition SingletonRegistry.cpp:86