From 7f5d41812db5aa569bcca078d004228a7b9828d9 Mon Sep 17 00:00:00 2001 From: taynpg Date: Sat, 7 Dec 2024 22:31:27 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9A=E6=B7=BB=E5=8A=A0=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=8D=95=E4=BE=8B=E7=94=9F=E6=88=90=E7=B1=BB=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- include/of_util.h | 31 +++++++++++++++++++++++++++++++ src/of_util.cpp | 1 + 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 include/of_util.h create mode 100644 src/of_util.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a6188e..9314923 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,7 @@ message(STATUS "Compiler CXX ID: ${CMAKE_CXX_COMPILER_ID}") set(SRC_FILES src/of_path.cpp src/of_str.cpp - src/of_win.cpp + src/of_win.cpp src/of_util.cpp ) include_directories(include) diff --git a/include/of_util.h b/include/of_util.h new file mode 100644 index 0000000..20a542a --- /dev/null +++ b/include/of_util.h @@ -0,0 +1,31 @@ +#include "of_def.hpp" +#include +#include +#include +#include + +namespace ofen { +template class OfSingleton +{ +public: + OfSingleton(const OfSingleton&) = delete; + OfSingleton& operator=(const OfSingleton&) = delete; + + static std::shared_ptr getInstance() + { + std::call_once(init_flag, []() { instance.reset(new T()); }); + return instance; + } + +protected: + OfSingleton() = default; + virtual ~OfSingleton() = default; + +private: + static std::shared_ptr instance; + static std::once_flag init_flag; +}; + +template std::shared_ptr OfSingleton::instance = nullptr; +template std::once_flag OfSingleton::init_flag; +} // namespace ofen diff --git a/src/of_util.cpp b/src/of_util.cpp new file mode 100644 index 0000000..e282d86 --- /dev/null +++ b/src/of_util.cpp @@ -0,0 +1 @@ +#include "of_util.h" \ No newline at end of file