Skip to content

Commit

Permalink
rework components system
Browse files Browse the repository at this point in the history
  • Loading branch information
n0lavar committed Jan 1, 2024
1 parent 8416a61 commit ae4cb75
Show file tree
Hide file tree
Showing 13 changed files with 820 additions and 542 deletions.
330 changes: 180 additions & 150 deletions include/qx/containers/components.h

Large diffs are not rendered by default.

376 changes: 218 additions & 158 deletions include/qx/containers/components.inl

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions include/qx/containers/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
**/
#pragma once

#include <qx/patterns/iterator.h>
#include <qx/macros/common.h>
#include <qx/patterns/contiguous_iterator/iterator.h>

/**
@def QX_IMPL_CONTAINER
Expand Down Expand Up @@ -93,14 +94,14 @@
\
const_pointer data() const noexcept \
{ \
return const_cast<container*>(this)->data(); \
return QX_CONST_CAST_THIS()->data(); \
} \
\
/* implement */ reference at(size_type ind) noexcept; \
\
const_reference at(size_type ind) const noexcept \
{ \
return const_cast<container*>(this)->at(ind); \
return QX_CONST_CAST_THIS()->at(ind); \
} \
\
/* implement */ void clear() noexcept;
8 changes: 8 additions & 0 deletions include/qx/meta/concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ concept callable_c = requires(T t, args_t&&... args) {
} -> std::convertible_to<return_t>;
};

// won't work for base_args_t containing template template or type template parameters
template<class derived_t, template<class... base_args_t> class base_t, class... partial_specialization_args_t>
concept derived_from_template_c = requires(const derived_t& derived) {
[]<class... base_args_t>(const base_t<partial_specialization_args_t..., base_args_t...>&)
{
}(derived);
};

namespace details
{

Expand Down
12 changes: 10 additions & 2 deletions include/qx/priority.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
**/
#pragma once

#include <qx/containers/flags.h>
#include <qx/typedefs.h>

namespace qx
Expand All @@ -23,8 +24,7 @@ namespace qx
**/
enum class priority : u8
{
disabled = 0,
lowest = 1,
lowest = 0,
very_low = 32,
low = 64,
normal = 128,
Expand All @@ -33,9 +33,17 @@ enum class priority : u8
highest = 255,
};

enum class status
{
default_value = 0,
disabled = 1 << 0,
};

} // namespace qx

constexpr auto operator<=>(qx::priority eLeft, qx::priority eRight)
{
return static_cast<u8>(eLeft) <=> static_cast<u8>(eRight);
}

QX_FLAGS_ENUM_CLASS(qx::priority);
2 changes: 1 addition & 1 deletion include/qx/rtti/class_identificator.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
namespace qx
{

using class_identificator = int;
using class_identificator = size_t;

}
16 changes: 8 additions & 8 deletions include/qx/rtti/reflection_creator.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace qx
@class reflection_creator
@brief Reflection creator
@details Performs registration of classes inherited from BaseClass
@details Performs registration of classes inherited from base_class_t
and creates their instances by their ID and name.
Allocates memory.
@tparam base_class_t - base class type
Expand Down Expand Up @@ -128,8 +128,8 @@ static std::shared_ptr<base_class_t> create_shared(args_t&&... args)
@details std::unique_ptr version
@param ... - constructor args types
**/
#define QX_REGISTER_UNIQUE_CREATOR(...) \
using CreatorRoot = ThisClass; \
#define QX_REGISTER_UNIQUE_CREATOR(...) \
using CreatorRoot = this_class_type; \
using Creator = qx::reflection_creator<CreatorRoot, std::unique_ptr, __VA_ARGS__>

/**
Expand All @@ -141,7 +141,7 @@ static std::shared_ptr<base_class_t> create_shared(args_t&&... args)
#define QX_REGISTER_UNIQUE_CONSTRUCTOR(...) \
private: \
static inline volatile bool QX_LINE_NAME(s_bRegistered) = Creator::_register_class( \
qx::details::create_unique<CreatorRoot, ThisClass, __VA_ARGS__>, \
qx::details::create_unique<CreatorRoot, this_class_type, __VA_ARGS__>, \
get_class_id_static(), \
get_class_name_static())

Expand All @@ -151,9 +151,9 @@ private:
@details std::shared_ptr version
@param ... - constructor args types
**/
#define QX_REGISTER_SHARED_CREATOR(...) \
protected: \
using CreatorRoot = ThisClass; \
#define QX_REGISTER_SHARED_CREATOR(...) \
protected: \
using CreatorRoot = this_class_type; \
using Creator = qx::reflection_creator<CreatorRoot, std::shared_ptr, __VA_ARGS__>

/**
Expand All @@ -165,7 +165,7 @@ protected: \
#define QX_REGISTER_SHARED_CONSTRUCTOR(...) \
private: \
static inline volatile bool QX_LINE_NAME(s_bRegistered) = Creator::_register_class( \
qx::details::create_shared<CreatorRoot, ThisClass, __VA_ARGS__>, \
qx::details::create_shared<CreatorRoot, this_class_type, __VA_ARGS__>, \
get_class_id_static(), \
get_class_name_static())

Expand Down
134 changes: 81 additions & 53 deletions include/qx/rtti/rtti.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@
**/
#pragma once

#include <qx/containers/string/string_hash.h>
#include <qx/macros/common.h>
#include <qx/meta/tuple_utils.h>
#include <qx/rtti/class_identificator.h>
#include <qx/rtti/rtti_naming_strategy.h>

#include <array>
#include <span>

namespace qx
{

Expand All @@ -43,10 +48,17 @@ inline class_identificator get_class_id() noexcept
}
else
{
return -1;
return std::numeric_limits<class_identificator>::max();
}
}

// utility class to let you use std::derived_from
class rtti_pure_base
{
public:
auto operator<=>(const rtti_pure_base&) const = default;
};

/**
@class rtti_root
Expand All @@ -60,14 +72,19 @@ inline class_identificator get_class_id() noexcept
**/
template<class derived_base_t, template<string_literal> class naming_strategy_t = rtti_naming_strategy_class_name>
class rtti_root
class rtti_root : public rtti_pure_base
{
public:
using BaseClass = derived_base_t;
using SuperClass = derived_base_t;
using ThisClass = derived_base_t;
using base_class_type = derived_base_t;
using super_class_type = derived_base_t;
using this_class_type = derived_base_t;
using inheritance_tuple = std::tuple<rtti_root>;

public:
rtti_root() noexcept = default;
rtti_root(const rtti_root&) noexcept = default;
rtti_root(rtti_root&&) noexcept = default;

template<class rtti_type>
bool is_derived_from() const noexcept
{
Expand All @@ -85,6 +102,11 @@ class rtti_root
return svClassName == get_class_name();
}

bool is(class_identificator id) const noexcept
{
return id == get_class_id();
}

virtual bool is_derived_from_id(class_identificator id) const noexcept
{
return id == get_class_id_static();
Expand All @@ -100,16 +122,18 @@ class rtti_root
return get_class_name_static();
}

static class_identificator get_class_id_static() noexcept
static constexpr class_identificator get_class_id_static() noexcept
{
return 0;
}

virtual int get_class_id() const noexcept
virtual class_identificator get_class_id() const noexcept
{
return get_class_id_static();
}

virtual std::span<const class_identificator> get_inheritance_sequence() const = 0;

auto operator<=>(const rtti_root&) const = default;

protected:
Expand All @@ -120,12 +144,6 @@ class rtti_root
return idBase == get_class_id_static();
}

static class_identificator _get_next_id() noexcept
{
static class_identificator nId = get_class_id_static() + 1;
return nId++;
}

template<string_literal DerivedName>
static constexpr string_view _get_class_name_by_strategy() noexcept
{
Expand All @@ -139,46 +157,56 @@ class rtti_root
@param thisClass - this class name
@param ... - super class name (must implement QX_RTTI_CLASS or QX_RTTI_BASE_CLASS macro)
**/
#define QX_RTTI_CLASS(thisClass, ...) \
\
public: \
using ThisClass = thisClass; \
using SuperClass = __VA_ARGS__; \
using BaseClass = typename SuperClass::BaseClass; \
\
public: \
virtual bool is_derived_from_id(qx::class_identificator id) const noexcept override \
{ \
return id == get_class_id_static() || SuperClass::is_derived_from_id(id); \
} \
\
static constexpr qx::string_view get_class_name_static() noexcept \
{ \
return BaseClass::template _get_class_name_by_strategy<QX_TEXT(QX_STRINGIFY(thisClass))>(); \
} \
\
virtual qx::class_identificator get_class_id() const noexcept override \
{ \
return get_class_id_static(); \
} \
\
static qx::class_identificator get_class_id_static() noexcept \
{ \
static auto id = BaseClass::_get_next_id(); \
return id; \
} \
\
virtual qx::string_view get_class_name() const noexcept override \
{ \
return get_class_name_static(); \
} \
\
protected: \
virtual bool _is_base_id(qx::class_identificator base_id) const noexcept override \
{ \
return base_id == qx::get_class_id<SuperClass>() || SuperClass::_is_base_id(base_id); \
} \
\
#define QX_RTTI_CLASS(thisClass, ...) \
\
public: \
using this_class_type = thisClass; \
using super_class_type = __VA_ARGS__; \
using base_class_type = typename super_class_type::base_class_type; \
using inheritance_tuple = qx::tuple::join_t<typename super_class_type::inheritance_tuple, this_class_type>; \
\
public: \
virtual bool is_derived_from_id(qx::class_identificator id) const noexcept override \
{ \
return id == get_class_id_static() || super_class_type::is_derived_from_id(id); \
} \
static constexpr qx::string_view get_class_name_static() noexcept \
{ \
return base_class_type::template _get_class_name_by_strategy<QX_TEXT(QX_STRINGIFY(thisClass))>(); \
} \
virtual qx::class_identificator get_class_id() const noexcept override \
{ \
return get_class_id_static(); \
} \
static constexpr qx::class_identificator get_class_id_static() noexcept \
{ \
return QX_STRING_HASH(#thisClass); \
} \
virtual qx::string_view get_class_name() const noexcept override \
{ \
return get_class_name_static(); \
} \
virtual std::span<const qx::class_identificator> get_inheritance_sequence() const override \
{ \
static constexpr auto ids = []() \
{ \
std::array<qx::class_identificator, std::tuple_size_v<inheritance_tuple>> ids_; \
qx::tuple::iterate<inheritance_tuple>( \
[&ids_]<class T, size_t I>() \
{ \
ids_[I] = T::get_class_id_static(); \
}); \
return ids_; \
}(); \
return ids; \
} \
\
protected: \
virtual bool _is_base_id(qx::class_identificator base_id) const noexcept override \
{ \
return base_id == qx::get_class_id<super_class_type>() || super_class_type::_is_base_id(base_id); \
} \
\
private:

} // namespace qx
1 change: 0 additions & 1 deletion include/qx/rtti/rtti_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <qx/meta/type_traits.h>

#include <memory>
#include <type_traits>

namespace qx
{
Expand Down
6 changes: 3 additions & 3 deletions include/qx/rtti/rtti_naming_strategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ template<string_literal ClassName>
class rtti_naming_strategy_nullptr
{
public:
static constexpr qx::string_view get_name() noexcept
static constexpr string_view get_name() noexcept
{
return nullptr;
}
Expand All @@ -52,7 +52,7 @@ template<string_literal ClassName>
class rtti_naming_strategy_empty
{
public:
static constexpr qx::string_view get_name() noexcept
static constexpr string_view get_name() noexcept
{
return QX_TEXT("");
}
Expand All @@ -72,7 +72,7 @@ template<string_literal ClassName>
class rtti_naming_strategy_class_name
{
public:
static constexpr qx::string_view get_name() noexcept
static constexpr string_view get_name() noexcept
{
return ClassName.view();
}
Expand Down
Loading

0 comments on commit ae4cb75

Please sign in to comment.