From c91e2c08d69f374cb52cafffc0c00ef2829b5b32 Mon Sep 17 00:00:00 2001 From: chatlanin Date: Fri, 5 Sep 2025 18:29:24 +0300 Subject: [PATCH] end concepts examples --- bin/examples/concepts/main.cpp | 375 ++++++++++++++------------------- src/hack/concepts/concepts.hpp | 20 +- src/hack/logger/logger.hpp | 4 + 3 files changed, 167 insertions(+), 232 deletions(-) diff --git a/bin/examples/concepts/main.cpp b/bin/examples/concepts/main.cpp index ca5366d..53a5f16 100644 --- a/bin/examples/concepts/main.cpp +++ b/bin/examples/concepts/main.cpp @@ -54,244 +54,179 @@ void example_has_allocator_type(const T& container) hack::log()("Allocator obtained successfully"); } -// template -// void example_is_string(const T& str) { -// std::cout << "String content: " << str << " (length: " << str.length() << ")" << std::endl; -// } -// -// template -// void example_is_sequence_container(const T& container) { -// std::cout << "Sequence container with " << container.size() << " elements" << std::endl; -// } -// -// template -// void example_is_random_access_container(T& container) { -// if (!container.empty()) { -// std::cout << "First element: " << container[0] << std::endl; -// } -// } -// -// template -// void example_is_container_adapter(T& adapter) { -// std::cout << "Container adapter with " << adapter.size() << " elements" << std::endl; -// } -// -// template -// void example_is_associative_container(const T& container) { -// std::cout << "Associative container with " << container.size() << " elements" << std::endl; -// } -// -// template -// void example_is_unordered_associative_container(const T& container) { -// std::cout << "Unordered associative container with " << container.size() << " elements" << std::endl; -// } -// -// template -// void example_is_tuple_like(const T& tuple) { -// std::cout << "Tuple-like with " << std::tuple_size_v << " elements" << std::endl; -// } -// -// template -// void example_is_fixed_array(const T& array) { -// std::cout << "Fixed array with " << std::extent_v << " elements" << std::endl; -// } -// -// template> T> -// void example_is_std_array(const T& array) { -// std::cout << "std::array with " << array.size() << " elements" << std::endl; -// } -// -// template -// void example_is_any_container(const T& container) { -// std::cout << "Any container with " << container.size() << " elements" << std::endl; -// } -// -// template -// void example_is_iterable(const T& iterable) { -// std::cout << "Iterable object: "; -// for (const auto& item : iterable) { -// std::cout << item << " "; -// } -// std::cout << std::endl; -// } -// -// template -// void example_is_sized(const T& sized) { -// if constexpr (hack::concepts::modern::has_size) { -// std::cout << "Sized object: " << sized.size() << " elements" << std::endl; -// } else { -// std::cout << "Sized object (compile-time size)" << std::endl; -// } -// } -// -// template -// void example_has_key_value_semantics(T& container) { -// if constexpr (hack::concepts::modern::has_mapped_type) { -// std::cout << "Key-value container, sample access demonstrated" << std::endl; -// } else { -// std::cout << "Set-like container" << std::endl; -// } -// } -// -// template -// void example_is_contiguous_container(const T& container) { -// std::cout << "Contiguous memory container" << std::endl; -// } -// -// template, int> Container> -// void example_can_emplace(Container& container, int value) { -// container.emplace_back(value); -// std::cout << "Emplaced value: " << value << std::endl; -// } -// -// template, int> Container> -// void example_can_push_back(Container& container, int value) { -// container.push_back(value); -// std::cout << "Pushed back value: " << value << std::endl; -// } -// -// template, int> Container> -// void example_can_push_front(Container& container, int value) { -// container.push_front(value); -// std::cout << "Pushed front value: " << value << std::endl; -// } -// -// template, int> Container> -// void example_can_find(Container& container, int value) { -// auto it = container.find(value); -// if (it != container.end()) { -// std::cout << "Found value: " << value << std::endl; -// } else { -// std::cout << "Value not found: " << value << std::endl; -// } -// } -// -// // Функция для демонстрации not_supported -// template -// void check_support(const T& value) { -// if constexpr (modern::concepts::not_supported) { -// std::cout << "Type is NOT supported by this library" << std::endl; -// } else { -// std::cout << "Type is supported" << std::endl; -// } -// } +template +void example_is_string(const T& str) +{ + hack::log()("String content: ", str, " (length: ", str.length(), ")"); +} + +template +void example_is_sequence_container(const T& container) +{ + hack::log()("Sequence container with ", container.size(), " elements"); +} + +template +void example_is_random_access_container(T& container) +{ + if (!container.empty()) + hack::log()("First element: ", container[0]); +} + +template +void example_is_container_adapter(T& adapter) +{ + hack::log()("Container adapter with ", adapter.size(), " elements"); +} + +template +void example_is_associative_container(const T& container) +{ + hack::log()("Associative container with ", container.size(), " elements"); +} + +template +void example_is_unordered_associative_container(const T& container) +{ + hack::log()("Unordered associative container with ", container.size(), " elements"); +} + +template +void example_is_tuple_like(const T& tuple) +{ + hack::log()("Tuple-like with ", std::tuple_size_v, " elements"); +} + +template +void example_is_fixed_array(const T& array) +{ + hack::log()("Fixed array with ", std::extent_v, " elements"); +} + +template +void example_is_std_array(const T& array) +{ + hack::log()("std::array with ", array.size(), " elements"); +} + +template +void example_is_any_container(const T& container) +{ + hack::log()("Any container with ", container.size(), " elements"); +} + +template +void example_is_iterable(const T& iterable) +{ + hack::log()("Iterable object"); +} + +template +void example_is_sized(const T& sized) +{ + if constexpr (hack::concepts::modern::has_size) + hack::log()("Sized object: ", sized.size(), " elements"); + else + hack::log()("Sized object (compile-time size)"); +} + +template +void check_support(const T& value) +{ + if constexpr (hack::concepts::modern::not_supported) + hack::log()("Type is NOT supported by this library"); + else + hack::log()("Type is supported"); +} + +template +void example_has_key_value_semantics(T& container) +{ + if constexpr (hack::concepts::modern::has_mapped_type) + hack::log()("Key-value container, sample access demonstrated"); + else + hack::log()("Set-like container"); +} + +template +void example_is_contiguous_container(const T& container) +{ + hack::log()("Contiguous memory container"); +} + +template +requires hack::concepts::modern::can_push_front +void example_can_push_front(Container& container, Value&& value) +{ + container.push_front(std::forward(value)); + hack::log()("Pushed front value: ", value); +} + +template +requires hack::concepts::modern::can_push_back +void example_can_push_back(Container& container, Value&& value) +{ + container.push_back(std::forward(value)); + hack::log()("Pushed back value: ", value); +} + +template +requires hack::concepts::modern::can_find +void example_can_find(Container& container, Key&& key) +{ + auto it = container.find(std::forward(key)); + if (it != container.end()) + hack::log()("Found value: ", *it); + else + hack::log()("Value not found: ", key); +} auto main(int argc, char *argv[]) -> int { - // Базовые концепты std::vector vec = { 1, 2, 3 }; std::map map = {{ 1, "one" }, { 2, "two" }}; std::set set = { 5, 3, 1, 4, 2 }; std::unordered_map umap = {{ 1, "uno" }, { 2, "dos" }}; + std::list list = { 1.1, 2.2, 3.3 }; + std::deque deque = { 'a', 'b', 'c' }; + std::stack stack; stack.push(1); stack.push(2); + std::string str = "Hello"; + std::tuple tuple = {1, "test", 3.14}; + int fixed_array[5] = {1, 2, 3, 4, 5}; + std::array std_array = {1.0f, 2.0f, 3.0f}; int a = 10; + struct custom_type { int x; }; + custom_type custom; - hack::log()("has_value_type:"); example_has_value_type(vec); // example_has_value_type(a); // выдаст ошибку т.к. у a нет встроенного value_type - - hack::log()("has_key_type:"); example_has_key_type(map); - - hack::log()("has_mapped_type:"); example_has_mapped_type(map); // example_has_mapped_type(set); // выдаст ошибку т.к. у set нет типа mapped_type. Этот тип характерен для контейнеров, которые хранят пары "ключ-значение". - - hack::log()("has_iterator:"); example_has_iterator(vec); - - hack::log()("has_size:"); example_has_size(vec); - - hack::log()("has_key_compare:"); example_has_key_compare(map); - - hack::log()("has_allocator_type:"); example_has_allocator_type(map); - - // // Строки и контейнеры - // std::string str = "Hello"; - // std::list list = {1.1, 2.2, 3.3}; - // std::deque deque = {'a', 'b', 'c'}; - // - // std::cout << "6. is_string: "; - // example_is_string(str); - // - // std::cout << "7. is_sequence_container (vector): "; - // example_is_sequence_container(vec); - // - // std::cout << "8. is_sequence_container (list): "; - // example_is_sequence_container(list); - // - // std::cout << "9. is_random_access_container: "; - // example_is_random_access_container(vec); - // - // // Адаптеры и ассоциативные контейнеры - // std::stack stack; - // stack.push(1); stack.push(2); - // - // std::cout << "10. is_container_adapter: "; - // example_is_container_adapter(stack); - // - // std::cout << "11. is_associative_container: "; - // example_is_associative_container(set); - // - // std::cout << "12. is_unordered_associative_container: "; - // example_is_unordered_associative_container(umap); - // - // // Кортежи и массивы - // std::tuple tuple = {1, "test", 3.14}; - // int fixed_array[5] = {1, 2, 3, 4, 5}; - // std::array std_array = {1.0f, 2.0f, 3.0f}; - // - // std::cout << "13. is_tuple_like: "; - // example_is_tuple_like(tuple); - // - // std::cout << "14. is_fixed_array: "; - // example_is_fixed_array(fixed_array); - // - // std::cout << "15. is_std_array: "; - // example_is_std_array(std_array); - // - // // Универсальные концепты - // std::cout << "16. is_any_container: "; - // example_is_any_container(vec); - // - // std::cout << "17. is_iterable: "; - // example_is_iterable(vec); - // - // std::cout << "18. is_sized: "; - // example_is_sized(vec); - // - // std::cout << "19. has_key_value_semantics: "; - // example_has_key_value_semantics(map); - // - // std::cout << "20. is_contiguous_container: "; - // example_is_contiguous_container(vec); - // - // // Концепты для алгоритмов - // std::cout << "21. can_emplace: "; - // example_can_emplace(vec, 42); - // - // std::cout << "22. can_push_back: "; - // example_can_push_back(vec, 99); - // - // std::cout << "23. can_push_front: "; - // example_can_push_front(list, 0); - // - // std::cout << "24. can_find: "; - // example_can_find(set, 3); - // - // // Проверка неподдерживаемых типов - // struct CustomType { int x; }; - // CustomType custom; - // - // std::cout << "25. not_supported check (int): "; - // check_support(42); - // - // std::cout << "26. not_supported check (CustomType): "; - // check_support(custom); - // - // std::cout << "\n=== Все примеры выполнены ===" << std::endl; + example_is_string(str); + example_is_sequence_container(vec); + example_is_sequence_container(list); + example_is_random_access_container(vec); + example_is_container_adapter(stack); + example_is_associative_container(set); + example_is_unordered_associative_container(umap); + example_is_tuple_like(tuple); + example_is_fixed_array(fixed_array); + example_is_std_array(std_array); + example_is_any_container(vec); + example_is_iterable(vec); + example_is_sized(vec); + check_support(a); + check_support(custom); + example_has_key_value_semantics(map); + example_is_contiguous_container(vec); + example_can_push_front(list, 0); + example_can_push_back(vec, 99); + example_can_find(set, 3); return 0; } diff --git a/src/hack/concepts/concepts.hpp b/src/hack/concepts/concepts.hpp index e8ad017..09701ab 100755 --- a/src/hack/concepts/concepts.hpp +++ b/src/hack/concepts/concepts.hpp @@ -204,8 +204,12 @@ namespace hack::concepts::modern // @brief Проверяет, является ли тип std::array // @details Обнаруживает std::array с конкретным размером // Полезно для алгоритмов, которые требуют знания размера на этапе компиляции - template - concept is_std_array = std::same_as>; + template + concept is_std_array = requires(T arr) { + requires std::same_as>>; + arr.size(); + arr[0]; + }; // Универсальные концепты для категоризации // @brief Проверяет, является ли тип любым контейнером @@ -221,7 +225,7 @@ namespace hack::concepts::modern is_unordered_associative_container || is_container_adapter || is_fixed_array || - is_std_array>; + is_std_array; // @brief Проверяет, является ли тип итерируемым // @details Обнаруживает любые типы, по которым можно итерироваться: @@ -276,15 +280,7 @@ namespace hack::concepts::modern template concept is_contiguous_container = std::same_as> || is_fixed_array || - is_std_array>; - - // Концепты для алгоритмов - // @brief Проверяет, поддерживает ли контейнер emplace-операцию - // @details Обнаруживает контейнеры, которые могут создавать элементы на месте: - // - vector::emplace_back(), map::emplace(), etc. - // Позволяет избежать лишних копирований и перемещений - template - concept can_emplace = requires(Container c, Value&& v) { c.emplace(std::forward(v)); }; + is_std_array; // @brief Проверяет, поддерживает ли контейнер добавление в начало // @details Обнаруживает контейнеры с push_front(): diff --git a/src/hack/logger/logger.hpp b/src/hack/logger/logger.hpp index 132e669..2792d32 100755 --- a/src/hack/logger/logger.hpp +++ b/src/hack/logger/logger.hpp @@ -10,6 +10,10 @@ #include "hack/patterns/ring_buffer.hpp" +// HERE +// и нужно сделать реализацию где выводлится все в одной линии но при помощи цикла +// типа такого: +// for (auto i : range) hack::log(hack::log::line)(i); namespace hack { class log