allow to disable exceptions and RTTI manually

This commit is contained in:
BlackMATov
2023-01-17 16:48:13 +07:00
parent 3a2a3ab984
commit ec05ff9a3d
7 changed files with 110 additions and 74 deletions

View File

@@ -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
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/headers>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_compile_definitions(${PROJECT_NAME} INTERFACE
$<$<BOOL:${META_HPP_NO_EXCEPTIONS}>:META_HPP_NO_EXCEPTIONS>
$<$<BOOL:${META_HPP_NO_RTTI}>:META_HPP_NO_RTTI>)
find_package(Threads REQUIRED)
target_link_libraries(${PROJECT_NAME} INTERFACE Threads::Threads)

View File

@@ -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)

View File

@@ -30,22 +30,18 @@
#include <variant>
#include <vector>
#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<From, To>::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<Array>(); }
@@ -3487,7 +3494,9 @@ namespace meta_hpp::detail
private:
std::recursive_mutex mutex_;
std::map<type_id, any_type, std::less<>> type_by_id_;
#if !defined(META_HPP_NO_RTTI)
std::map<std::type_index, any_type, std::less<>> type_by_rtti_;
#endif
};
}

View File

@@ -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;

View File

@@ -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 <cassert>
#include <cstddef>
#include <cstdint>
@@ -22,31 +30,19 @@
#include <mutex>
#include <set>
#include <span>
#include <stdexcept>
#include <string_view>
#include <string>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <utility>
#include <variant>
#include <vector>
#if !defined(__cpp_exceptions)
# define META_HPP_NO_EXCEPTIONS
#if !defined(META_HPP_NO_EXCEPTIONS)
# include <stdexcept>
#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 <typeindex>
# include <typeinfo>
#endif

View File

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

View File

@@ -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<Array>(); }
@@ -234,6 +235,8 @@ namespace meta_hpp::detail
private:
std::recursive_mutex mutex_;
std::map<type_id, any_type, std::less<>> type_by_id_;
#if !defined(META_HPP_NO_RTTI)
std::map<std::type_index, any_type, std::less<>> type_by_rtti_;
#endif
};
}