Skip to content

Commit 66bdaac

Browse files
feat: theme abstractions
1 parent 4a5126b commit 66bdaac

63 files changed

Lines changed: 5363 additions & 119 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* @file base/include/base/singleton/simple_singleton.hpp
3+
* @brief Simple thread-safe singleton using Meyer's singleton pattern.
4+
*
5+
* Provides a minimal singleton implementation that leverages C++11 guarantees
6+
* for thread-safe static local variable initialization.
7+
*
8+
* @author N/A
9+
* @date N/A
10+
* @version N/A
11+
* @since N/A
12+
* @ingroup none
13+
*/
14+
15+
#pragma once
16+
17+
namespace cf {
18+
19+
/**
20+
* @brief Simple thread-safe singleton using Meyer's singleton pattern.
21+
*
22+
* Provides a minimal singleton implementation that leverages C++11 guarantees
23+
* for thread-safe static local variable initialization. The instance is created
24+
* on first access and destroyed on application exit.
25+
*
26+
* @tparam SingleTargetClass Type of the singleton instance.
27+
*
28+
* @ingroup none
29+
*
30+
* @note Thread-safe. C++11 guarantees thread-safe initialization of function-local
31+
* static variables.
32+
*
33+
* @warning None
34+
*
35+
* @code
36+
* class MyClass {
37+
* public:
38+
* void doSomething() {}
39+
* };
40+
*
41+
* using MySingleton = SimpleSingleton<MyClass>;
42+
* MySingleton::instance().doSomething();
43+
* @endcode
44+
*/
45+
template <typename SingleTargetClass> class SimpleSingleton {
46+
public:
47+
/**
48+
* @brief Returns the singleton instance.
49+
*
50+
* Creates the instance on first call, subsequent calls return the same
51+
* instance.
52+
*
53+
* @return Reference to the singleton instance.
54+
* @throws None
55+
* @note Thread-safe initialization guaranteed by C++11 standard.
56+
* @warning None
57+
* @since N/A
58+
* @ingroup none
59+
*/
60+
static SingleTargetClass& instance() {
61+
static SingleTargetClass target;
62+
return target;
63+
}
64+
65+
private:
66+
SimpleSingleton(const SimpleSingleton&) = delete;
67+
SimpleSingleton& operator=(const SimpleSingleton&) = delete;
68+
SimpleSingleton(SimpleSingleton&&) = delete;
69+
SimpleSingleton& operator=(SimpleSingleton&&) = delete;
70+
};
71+
72+
} // namespace cf
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* @file base/include/base/singleton/singleton.hpp
3+
* @brief Thread-safe singleton with explicit initialization.
4+
*
5+
* Provides a singleton implementation that requires explicit initialization
6+
* via init() before accessing the instance. Uses std::call_once for
7+
* thread-safe initialization.
8+
*
9+
* @author N/A
10+
* @date N/A
11+
* @version N/A
12+
* @since N/A
13+
* @ingroup none
14+
*/
15+
16+
#pragma once
17+
#include <memory>
18+
#include <mutex>
19+
#include <stdexcept>
20+
21+
namespace cf {
22+
23+
/**
24+
* @brief Thread-safe singleton with explicit initialization.
25+
*
26+
* Provides a singleton implementation that requires explicit initialization
27+
* via init() before accessing the instance. This pattern allows for
28+
* parameterized construction and explicit control over initialization timing.
29+
*
30+
* @tparam T Type of the singleton instance.
31+
*
32+
* @ingroup none
33+
*
34+
* @note Thread-safe. Uses std::call_once for initialization.
35+
*
36+
* @warning Accessing instance() before calling init() throws std::logic_error.
37+
*
38+
* @code
39+
* class MyClass {
40+
* public:
41+
* MyClass(int value) : value_(value) {}
42+
* void doSomething() {}
43+
* private:
44+
* int value_;
45+
* };
46+
*
47+
* using MySingleton = Singleton<MyClass>;
48+
* MySingleton::init(42);
49+
* MySingleton::instance().doSomething();
50+
* @endcode
51+
*/
52+
template <typename T> class Singleton {
53+
public:
54+
/**
55+
* @brief Initializes the singleton with the provided arguments.
56+
*
57+
* Constructs the singleton instance once, subsequent calls return the
58+
* existing instance. Thread-safe via std::call_once.
59+
*
60+
* @tparam Args Types of constructor arguments.
61+
* @param[in] args Arguments to forward to T's constructor.
62+
* @return Reference to the initialized singleton instance.
63+
* @throws None
64+
* @note Thread-safe initialization guaranteed by std::call_once.
65+
* @warning Calling init() twice has no effect; the first instance is kept.
66+
* @since N/A
67+
* @ingroup none
68+
*/
69+
template <typename... Args>
70+
static T& init(Args&&... args) {
71+
std::call_once(flag_, [&] {
72+
instance_.reset(new T(std::forward<Args>(args)...));
73+
});
74+
return *instance_;
75+
}
76+
77+
/**
78+
* @brief Returns the singleton instance.
79+
*
80+
* Requires that init() has been called first.
81+
*
82+
* @return Reference to the singleton instance.
83+
* @throws std::logic_error if init() has not been called.
84+
* @note None
85+
* @warning Must call init() before accessing instance().
86+
* @since N/A
87+
* @ingroup none
88+
*/
89+
static T& instance() {
90+
if (!instance_) {
91+
throw std::logic_error("Singleton not initialized. Call init() first.");
92+
}
93+
return *instance_;
94+
}
95+
96+
/**
97+
* @brief Resets the singleton instance.
98+
*
99+
* Destroys the current instance and allows re-initialization via init().
100+
*
101+
* @throws None
102+
* @note After calling reset(), init() must be called again before
103+
* accessing instance().
104+
* @warning None
105+
* @since N/A
106+
* @ingroup none
107+
*/
108+
static void reset() { instance_.reset(); }
109+
110+
Singleton(const Singleton&) = delete;
111+
Singleton& operator=(const Singleton&) = delete;
112+
113+
protected:
114+
Singleton() = default;
115+
virtual ~Singleton() = default;
116+
117+
private:
118+
inline static std::unique_ptr<T> instance_ = nullptr;
119+
inline static std::once_flag flag_;
120+
};
121+
122+
} // namespace cf
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* @file base/include/base/weak_ptr/private/weak_ptr_internals.h
3+
* @brief Internal implementation details for WeakPtr system.
4+
*
5+
* This header contains the internal implementation details for the WeakPtr
6+
* system. External code should not directly include or depend on this file.
7+
* WeakPtr and WeakPtrFactory share the WeakReferenceFlag to track liveness.
8+
*
9+
* @author N/A
10+
* @date N/A
11+
* @version N/A
12+
* @since N/A
13+
* @ingroup none
14+
*/
15+
16+
#pragma once
17+
18+
#include <atomic>
19+
#include <memory>
20+
21+
namespace cf {
22+
namespace internal {
23+
24+
/**
25+
* @brief Shared flag tracking WeakPtr liveness state.
26+
*
27+
* The Owner (WeakPtrFactory) and all WeakPtr instances share the same
28+
* WeakReferenceFlag instance. When the owner is destroyed (or explicitly
29+
* calls InvalidateWeakPtrs), the alive_ flag is set to false, causing all
30+
* WeakPtr::Get() calls to return nullptr.
31+
*
32+
* @ingroup none
33+
*
34+
* @note The alive_ flag uses std::atomic<bool> for thread-safety.
35+
* Invalidate() and IsAlive() are thread-safe operations.
36+
*
37+
* @warning WeakPtr should only be used on the same thread where it was
38+
* created. The "check + dereference" two-step operation between
39+
* IsAlive() and actual access is not atomic.
40+
*
41+
* @code
42+
* // Internal use only - created by WeakPtrFactory
43+
* auto flag = std::make_shared<WeakReferenceFlag>();
44+
* if (flag->IsAlive()) {
45+
* // Safe to access
46+
* }
47+
* flag->Invalidate(); // All WeakPtrs now return nullptr
48+
* @endcode
49+
*/
50+
class WeakReferenceFlag {
51+
public:
52+
/// @brief Default constructor: creates a flag in the alive state.
53+
WeakReferenceFlag() = default;
54+
55+
/// @brief Copy constructor: deleted. Flag is shared via shared_ptr.
56+
WeakReferenceFlag(const WeakReferenceFlag&) = delete;
57+
58+
/// @brief Copy assignment: deleted.
59+
WeakReferenceFlag& operator=(const WeakReferenceFlag&) = delete;
60+
61+
/// @brief Move constructor: deleted.
62+
WeakReferenceFlag(WeakReferenceFlag&&) = delete;
63+
64+
/// @brief Move assignment: deleted.
65+
WeakReferenceFlag& operator=(WeakReferenceFlag&&) = delete;
66+
67+
/**
68+
* @brief Checks if the owner is still alive.
69+
*
70+
* @return true if the owner is alive, false if invalidated.
71+
* @throws None
72+
* @note Uses memory_order_acquire for proper synchronization.
73+
* @warning None
74+
* @since N/A
75+
* @ingroup none
76+
*/
77+
[[nodiscard]] bool IsAlive() const noexcept {
78+
return alive_.load(std::memory_order_acquire);
79+
}
80+
81+
/**
82+
* @brief Marks the flag as invalid.
83+
*
84+
* After calling this method, all existing and future IsAlive() calls
85+
* returns false.
86+
*
87+
* @throws None
88+
* @note Uses memory_order_release for proper synchronization.
89+
* @warning None
90+
* @since N/A
91+
* @ingroup none
92+
*/
93+
void Invalidate() noexcept { alive_.store(false, std::memory_order_release); }
94+
95+
private:
96+
std::atomic<bool> alive_{true};
97+
};
98+
99+
/// @brief Shared pointer type for WeakReferenceFlag.
100+
using WeakReferenceFlagPtr = std::shared_ptr<WeakReferenceFlag>;
101+
102+
} // namespace internal
103+
} // namespace cf

0 commit comments

Comments
 (0)