Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
initializable_object.h
1// SPDX-FileCopyrightText: © 2024-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#ifndef UTILS_INITIALIZABLE_OBJECT_H
5#define UTILS_INITIALIZABLE_OBJECT_H
6
7#include <memory>
8
9#include "utils/traits.h"
10#include "utils/types.h"
11
12namespace zrythm::utils
13{
14
15namespace InitializableObjectDetail
16{
18template <typename T>
19concept Initializable = requires (T t) {
20 { t.initialize () } -> std::convertible_to<bool>;
21};
22}
23
26template <typename Derived> class InitializableObject
27{
28protected:
30 InitializableObject () = default;
31
32public:
34 template <typename... Args>
35 static std::shared_ptr<Derived> create_shared (Args &&... args)
36 {
37 auto obj =
38 std::shared_ptr<Derived> (new Derived (std::forward<Args> (args)...));
39 return obj->initialize () ? obj : nullptr;
40 }
41
43 template <typename... Args>
44 static std::unique_ptr<Derived> create_unique (Args &&... args)
45 {
46 auto obj =
47 std::unique_ptr<Derived> (new Derived (std::forward<Args> (args)...));
48 return obj->initialize () ? std::move (obj) : nullptr;
49 }
50
51 Z_DISABLE_COPY_MOVE (InitializableObject);
52};
53
54template <typename T>
56
57} // namespace zrythm::utils
58
59#endif // UTILS_OBJECT_FACTORY_H
A factory class for creating initializable objects using static polymorphism.
InitializableObject()=default
Protected constructor to prevent instantiation.
static std::shared_ptr< Derived > create_shared(Args &&... args)
Creates a shared pointer to initialized object.
static std::unique_ptr< Derived > create_unique(Args &&... args)
Creates a unique pointer to initialized object.
Concept to ensure a type has an initialize() method returning bool.
String utilities.
Definition algorithms.h:12