From 05e856f42d09017b5d9835211ea277d1d771718b Mon Sep 17 00:00:00 2001 From: chatlanin Date: Wed, 16 Aug 2023 10:16:30 +0300 Subject: [PATCH] add singleton template --- bin/meson.build | 2 +- bin/utils/main.cpp | 13 +++++++++ src/hack/utils/singleton.hpp | 28 +++++++++++++++++++ src/meson.build | 53 +++++++++++++++++++++++------------- 4 files changed, 76 insertions(+), 20 deletions(-) create mode 100644 src/hack/utils/singleton.hpp diff --git a/bin/meson.build b/bin/meson.build index 6edbfe1..658b058 100755 --- a/bin/meson.build +++ b/bin/meson.build @@ -1,6 +1,6 @@ executable( 'hack', - 'algorithms/main.cpp', + 'utils/main.cpp', dependencies : deps, cpp_args: args, include_directories : inc diff --git a/bin/utils/main.cpp b/bin/utils/main.cpp index eefcefd..ec8f5aa 100644 --- a/bin/utils/main.cpp +++ b/bin/utils/main.cpp @@ -2,6 +2,7 @@ #include "hack/logger/logger.hpp" #include "hack/utils/utils.hpp" +#include "hack/utils/singleton.hpp" #include "hack/utils/func_query.hpp" int f(int a) @@ -32,6 +33,14 @@ struct counter_test_2 int id; }; +struct test_singleton : public hack::utils::singleton +{ + void print() + { + hack::log()("Print singleton"); + } +}; + auto main(int argc, char *argv[]) -> int { {// ex: utils::func_memory @@ -83,6 +92,10 @@ auto main(int argc, char *argv[]) -> int query = hack::utils::make_query("super_function"); hack::log()("query", query); } + + {// ex: singleton + test_singleton::get_instance().print(); + } } diff --git a/src/hack/utils/singleton.hpp b/src/hack/utils/singleton.hpp new file mode 100644 index 0000000..b4ee3ac --- /dev/null +++ b/src/hack/utils/singleton.hpp @@ -0,0 +1,28 @@ +#pragma once + +namespace hack::utils +{ + struct no_copy + { + no_copy() = default; + no_copy(const no_copy&&) = delete; + no_copy operator=(const no_copy&&) = delete; + }; + + struct no_move + { + no_move() = default; + no_move(no_move&&) = delete; + no_move operator=(no_move&&) = delete; + }; + + template + struct singleton : public no_move, no_copy + { + static T& get_instance() + { + static T t; + return t; + } + }; +} diff --git a/src/meson.build b/src/meson.build index 8a0d984..82630ed 100755 --- a/src/meson.build +++ b/src/meson.build @@ -1,27 +1,42 @@ inc += include_directories('.') headers = [ - 'hack/logger/logger.hpp', + 'hack/algorithms/sort.hpp', + + 'hack/concepts/concepts.hpp', + + 'hack/containers/containers.hpp', + 'hack/containers/set.hpp', + 'hack/containers/utils.hpp', 'hack/containers/vector.hpp', - # 'hack/concepts/concepts.hpp', - # 'hack/iterators/associative_ostream_iterator.hpp', - # 'hack/iterators/sequence_ostream_iterator.hpp', - # 'hack/math/matrix.hpp', - # 'hack/math/max.hpp', - # 'hack/math/vector.hpp', - # 'hack/range/range.hpp', - # 'hack/range/range.hpp', - # 'hack/security/is_link.hpp', - # 'hack/security/is_string.hpp', - # 'hack/security/uuid.hpp', - # 'hack/security/validate_email.hpp', - # 'hack/string/string.hpp', - # 'hack/string/string_concat_helper.hpp', - # 'hack/string/utf8_len.hpp', - # 'hack/utils/func_query.hpp', - # 'hack/utils/utils.hpp', - # 'hack/view/color.hpp' + 'hack/iterators/associative_ostream_iterator.hpp', + 'hack/iterators/sequence_ostream_iterator.hpp', + + 'hack/logger/logger.hpp', + + 'hack/macros/macros.hpp', + + 'hack/math/matrix.hpp', + 'hack/math/max.hpp', + 'hack/math/vector.hpp', + + 'hack/memory/make_ptr.hpp', + + 'hack/security/is_link.hpp', + 'hack/security/is_string.hpp', + 'hack/security/uuid.hpp', + 'hack/security/validate_email.hpp', + + 'hack/string/string.hpp', + 'hack/string/string_concat_helper.hpp', + 'hack/string/utf8_len.hpp', + + 'hack/utils/func_query.hpp', + 'hack/utils/singleton.hpp', + 'hack/utils/utils.hpp', + + 'hack/view/color.hpp' ] sources = []