diff --git a/CMakeLists.txt b/CMakeLists.txt index bad831d..97e4c7d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,11 @@ cmake_minimum_required(VERSION 3.21 FATAL_ERROR) project(meta.hpp VERSION "0.0.1" DESCRIPTION "C++20 tiny dynamic reflection library" - HOMEPAGE_URL "https://github.com/blackmatov/meta.hpp") + HOMEPAGE_URL "https://github.com/blackmatov/meta.hpp" + LANGUAGES CXX) + +option(META_HPP_NO_EXCEPTIONS "Don't use exceptions" OFF) +option(META_HPP_NO_RTTI "Don't use RTTI" OFF) # # library @@ -22,6 +26,10 @@ target_include_directories(${PROJECT_NAME} INTERFACE $ $) +target_compile_definitions(${PROJECT_NAME} INTERFACE + $<$:META_HPP_NO_EXCEPTIONS> + $<$:META_HPP_NO_RTTI>) + find_package(Threads REQUIRED) target_link_libraries(${PROJECT_NAME} INTERFACE Threads::Threads) diff --git a/develop/CMakeLists.txt b/develop/CMakeLists.txt index af2e74f..16be87b 100644 --- a/develop/CMakeLists.txt +++ b/develop/CMakeLists.txt @@ -1,7 +1,7 @@ option(BUILD_WITH_COVERAGE "Build with coverage" OFF) option(BUILD_WITH_SANITIZERS "Build with sanitizers" OFF) -option(BUILD_WITH_NO_EXCEPTIONS "Build with no exceptions" OFF) -option(BUILD_WITH_NO_RTTI "Build with no RTTI" OFF) +option(BUILD_WITH_NO_EXCEPTIONS "Build with no exceptions" ${META_HPP_NO_EXCEPTIONS}) +option(BUILD_WITH_NO_RTTI "Build with no RTTI" ${META_HPP_NO_RTTI}) enable_testing() set_property(GLOBAL PROPERTY USE_FOLDERS ON) diff --git a/develop/singles/headers/meta.hpp/meta_all.hpp b/develop/singles/headers/meta.hpp/meta_all.hpp index 234fb3e..2372037 100644 --- a/develop/singles/headers/meta.hpp/meta_all.hpp +++ b/develop/singles/headers/meta.hpp/meta_all.hpp @@ -30,22 +30,18 @@ #include #include -#if !defined(__cpp_exceptions) +#if !defined(META_HPP_NO_EXCEPTIONS) && !defined(__cpp_exceptions) # define META_HPP_NO_EXCEPTIONS #endif -#if !defined(__cpp_rtti) +#if !defined(META_HPP_NO_RTTI) && !defined(__cpp_rtti) # define META_HPP_NO_RTTI #endif -#if defined(META_HPP_NO_EXCEPTIONS) -# define META_HPP_TRY if ( true ) -# define META_HPP_CATCH(e) if ( false ) -# define META_HPP_RETHROW() std::abort() -#else -# define META_HPP_TRY try -# define META_HPP_CATCH(e) catch(e) -# define META_HPP_RETHROW() throw +#if !defined(META_HPP_NO_EXCEPTIONS) +#endif + +#if !defined(META_HPP_NO_RTTI) #endif namespace meta_hpp::detail @@ -287,6 +283,33 @@ namespace meta_hpp::detail using copy_cvref_t = typename copy_cvref::type; } +#if !defined(META_HPP_NO_EXCEPTIONS) +# define META_HPP_TRY try +# define META_HPP_CATCH(e) catch(e) +# define META_HPP_RETHROW() throw +# define META_HPP_THROW_AS(e, m) throw e{m} +#else +# define META_HPP_TRY if ( true ) +# define META_HPP_CATCH(e) if ( false ) +# define META_HPP_RETHROW() std::abort() +# define META_HPP_THROW_AS(e, m) std::terminate() +#endif + +namespace meta_hpp::detail +{ +#if !defined(META_HPP_NO_EXCEPTIONS) + class exception final : public std::runtime_error { + public: + explicit exception(const char* what) + : std::runtime_error(what) {} + }; +#endif + + inline void throw_exception_with [[noreturn]] ([[maybe_unused]] const char* what) { + META_HPP_THROW_AS(exception, what); + } +} + namespace meta_hpp::detail { template < typename Function > @@ -954,6 +977,10 @@ namespace meta_hpp::detail namespace meta_hpp { +#if !defined(META_HPP_NO_EXCEPTIONS) + using detail::exception; +#endif + using detail::hashed_string; using detail::memory_buffer; @@ -966,27 +993,6 @@ namespace meta_hpp using detail::type_list; } -namespace meta_hpp -{ - class exception final : public std::runtime_error { - public: - explicit exception(const char* what) - : std::runtime_error(what) {} - }; - - namespace detail - { - inline void throw_exception_with [[noreturn]] (const char* what) { - #if !defined(META_HPP_NO_EXCEPTIONS) - throw ::meta_hpp::exception(what); - #else - (void)what; - std::abort(); - #endif - } - } -} - namespace meta_hpp { class uvalue; @@ -3294,7 +3300,7 @@ namespace meta_hpp::detail return any_type{}; } - + #if !defined(META_HPP_NO_RTTI) [[nodiscard]] any_type get_type_by_rtti(const std::type_index& index) const noexcept { const locker lock; @@ -3304,6 +3310,7 @@ namespace meta_hpp::detail return any_type{}; } + #endif public: template < array_kind Array > [[nodiscard]] array_type resolve_type() { return resolve_array_type(); } @@ -3487,7 +3494,9 @@ namespace meta_hpp::detail private: std::recursive_mutex mutex_; std::map> type_by_id_; + #if !defined(META_HPP_NO_RTTI) std::map> type_by_rtti_; + #endif }; } diff --git a/headers/meta.hpp/meta_base.hpp b/headers/meta.hpp/meta_base.hpp index c8c621f..4fa0f1d 100644 --- a/headers/meta.hpp/meta_base.hpp +++ b/headers/meta.hpp/meta_base.hpp @@ -10,6 +10,7 @@ #include "meta_base/bitflags.hpp" #include "meta_base/cv_traits.hpp" #include "meta_base/cvref_traits.hpp" +#include "meta_base/exceptions.hpp" #include "meta_base/fixed_function.hpp" #include "meta_base/hash_combiner.hpp" #include "meta_base/hashed_string.hpp" @@ -26,6 +27,10 @@ namespace meta_hpp { +#if !defined(META_HPP_NO_EXCEPTIONS) + using detail::exception; +#endif + using detail::hashed_string; using detail::memory_buffer; @@ -38,27 +43,6 @@ namespace meta_hpp using detail::type_list; } -namespace meta_hpp -{ - class exception final : public std::runtime_error { - public: - explicit exception(const char* what) - : std::runtime_error(what) {} - }; - - namespace detail - { - inline void throw_exception_with [[noreturn]] (const char* what) { - #if !defined(META_HPP_NO_EXCEPTIONS) - throw ::meta_hpp::exception(what); - #else - (void)what; - std::abort(); - #endif - } - } -} - namespace meta_hpp { class uvalue; diff --git a/headers/meta.hpp/meta_base/base.hpp b/headers/meta.hpp/meta_base/base.hpp index c853b32..2dea187 100644 --- a/headers/meta.hpp/meta_base/base.hpp +++ b/headers/meta.hpp/meta_base/base.hpp @@ -6,6 +6,14 @@ #pragma once +#if !defined(META_HPP_NO_EXCEPTIONS) && !defined(__cpp_exceptions) +# define META_HPP_NO_EXCEPTIONS +#endif + +#if !defined(META_HPP_NO_RTTI) && !defined(__cpp_rtti) +# define META_HPP_NO_RTTI +#endif + #include #include #include @@ -22,31 +30,19 @@ #include #include #include -#include #include #include #include #include -#include -#include #include #include #include -#if !defined(__cpp_exceptions) -# define META_HPP_NO_EXCEPTIONS +#if !defined(META_HPP_NO_EXCEPTIONS) +# include #endif -#if !defined(__cpp_rtti) -# define META_HPP_NO_RTTI -#endif - -#if defined(META_HPP_NO_EXCEPTIONS) -# define META_HPP_TRY if ( true ) -# define META_HPP_CATCH(e) if ( false ) -# define META_HPP_RETHROW() std::abort() -#else -# define META_HPP_TRY try -# define META_HPP_CATCH(e) catch(e) -# define META_HPP_RETHROW() throw +#if !defined(META_HPP_NO_RTTI) +# include +# include #endif diff --git a/headers/meta.hpp/meta_base/exceptions.hpp b/headers/meta.hpp/meta_base/exceptions.hpp new file mode 100644 index 0000000..0abe5b8 --- /dev/null +++ b/headers/meta.hpp/meta_base/exceptions.hpp @@ -0,0 +1,36 @@ +/******************************************************************************* + * This file is part of the "https://github.com/blackmatov/meta.hpp" + * For conditions of distribution and use, see copyright notice in LICENSE.md + * Copyright (C) 2021-2023, by Matvey Cherevko (blackmatov@gmail.com) + ******************************************************************************/ + +#pragma once + +#include "base.hpp" + +#if !defined(META_HPP_NO_EXCEPTIONS) +# define META_HPP_TRY try +# define META_HPP_CATCH(e) catch(e) +# define META_HPP_RETHROW() throw +# define META_HPP_THROW_AS(e, m) throw e{m} +#else +# define META_HPP_TRY if ( true ) +# define META_HPP_CATCH(e) if ( false ) +# define META_HPP_RETHROW() std::abort() +# define META_HPP_THROW_AS(e, m) std::terminate() +#endif + +namespace meta_hpp::detail +{ +#if !defined(META_HPP_NO_EXCEPTIONS) + class exception final : public std::runtime_error { + public: + explicit exception(const char* what) + : std::runtime_error(what) {} + }; +#endif + + inline void throw_exception_with [[noreturn]] ([[maybe_unused]] const char* what) { + META_HPP_THROW_AS(exception, what); + } +} diff --git a/headers/meta.hpp/meta_detail/type_registry.hpp b/headers/meta.hpp/meta_detail/type_registry.hpp index 0f2a1fc..67b1c88 100644 --- a/headers/meta.hpp/meta_detail/type_registry.hpp +++ b/headers/meta.hpp/meta_detail/type_registry.hpp @@ -41,7 +41,7 @@ namespace meta_hpp::detail return any_type{}; } - + #if !defined(META_HPP_NO_RTTI) [[nodiscard]] any_type get_type_by_rtti(const std::type_index& index) const noexcept { const locker lock; @@ -51,6 +51,7 @@ namespace meta_hpp::detail return any_type{}; } + #endif public: template < array_kind Array > [[nodiscard]] array_type resolve_type() { return resolve_array_type(); } @@ -234,6 +235,8 @@ namespace meta_hpp::detail private: std::recursive_mutex mutex_; std::map> type_by_id_; + #if !defined(META_HPP_NO_RTTI) std::map> type_by_rtti_; + #endif }; }