add singleton template

This commit is contained in:
chatlanin 2023-08-16 10:16:30 +03:00
parent 86ac6b451d
commit 05e856f42d
4 changed files with 76 additions and 20 deletions

View File

@ -1,6 +1,6 @@
executable(
'hack',
'algorithms/main.cpp',
'utils/main.cpp',
dependencies : deps,
cpp_args: args,
include_directories : inc

View File

@ -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<test_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();
}
}

View File

@ -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<typename T>
struct singleton : public no_move, no_copy
{
static T& get_instance()
{
static T t;
return t;
}
};
}

View File

@ -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 = []