'align' property for number and class types

This commit is contained in:
BlackMATov
2022-08-19 06:14:39 +07:00
parent 35c312ed4a
commit cbc38caf0d
7 changed files with 30 additions and 4 deletions

View File

@@ -52,6 +52,7 @@ namespace meta_hpp::detail
template < class_kind Class >
struct class_traits : impl::class_traits_base<Class> {
static constexpr std::size_t size{sizeof(Class)};
static constexpr std::size_t align{alignof(Class)};
[[nodiscard]] static constexpr class_bitflags make_flags() noexcept {
class_bitflags flags{};

View File

@@ -26,6 +26,7 @@ namespace meta_hpp::detail
template < number_kind Number >
struct number_traits {
static constexpr std::size_t size{sizeof(Number)};
static constexpr std::size_t align{alignof(Number)};
[[nodiscard]] static constexpr number_bitflags make_flags() noexcept {
number_bitflags flags{};

View File

@@ -150,6 +150,7 @@ namespace meta_hpp
[[nodiscard]] const metadata_map& get_metadata() const noexcept;
[[nodiscard]] std::size_t get_size() const noexcept;
[[nodiscard]] std::size_t get_align() const noexcept;
[[nodiscard]] std::size_t get_arity() const noexcept;
[[nodiscard]] any_type get_argument_type(std::size_t position) const noexcept;
@@ -367,6 +368,7 @@ namespace meta_hpp
[[nodiscard]] const metadata_map& get_metadata() const noexcept;
[[nodiscard]] std::size_t get_size() const noexcept;
[[nodiscard]] std::size_t get_align() const noexcept;
private:
detail::number_type_data* data_{};
friend auto detail::type_access<number_type>(const number_type&);
@@ -493,6 +495,7 @@ namespace meta_hpp::detail
struct class_type_data final : type_data_base {
const class_bitflags flags;
const std::size_t size;
const std::size_t align;
const std::vector<any_type> argument_types;
class_set bases;
@@ -579,6 +582,7 @@ namespace meta_hpp::detail
struct number_type_data final : type_data_base {
const number_bitflags flags;
const std::size_t size;
const std::size_t align;
template < number_kind Number >
explicit number_type_data(type_list<Number>);

View File

@@ -29,6 +29,7 @@ namespace meta_hpp::detail
: type_data_base{type_id{type_list<class_tag<Class>>{}}, type_kind::class_}
, flags{class_traits<Class>::make_flags()}
, size{class_traits<Class>::size}
, align{class_traits<Class>::align}
, argument_types{resolve_types(typename class_traits<Class>::argument_types{})} {}
}
@@ -61,6 +62,10 @@ namespace meta_hpp
return data_->size;
}
inline std::size_t class_type::get_align() const noexcept {
return data_->align;
}
inline std::size_t class_type::get_arity() const noexcept {
return data_->argument_types.size();
}

View File

@@ -22,7 +22,8 @@ namespace meta_hpp::detail
number_type_data::number_type_data(type_list<Number>)
: type_data_base{type_id{type_list<number_tag<Number>>{}}, type_kind::number_}
, flags{number_traits<Number>::make_flags()}
, size{number_traits<Number>::size} {}
, size{number_traits<Number>::size}
, align{number_traits<Number>::align} {}
}
namespace meta_hpp
@@ -53,4 +54,8 @@ namespace meta_hpp
inline std::size_t number_type::get_size() const noexcept {
return data_->size;
}
inline std::size_t number_type::get_align() const noexcept {
return data_->align;
}
}

View File

@@ -126,6 +126,13 @@ TEST_CASE("meta/meta_types/class_type") {
CHECK(final_derived_clazz_type.get_size() == sizeof(final_derived_clazz));
}
SUBCASE("get_align") {
CHECK(base_clazz_1_type.get_align() == alignof(base_clazz_1));
CHECK(base_clazz_2_type.get_align() == alignof(base_clazz_2));
CHECK(derived_clazz_type.get_align() == alignof(derived_clazz));
CHECK(final_derived_clazz_type.get_align() == alignof(final_derived_clazz));
}
SUBCASE("get_arity") {
{
const meta::class_type type = meta::resolve_type<derived_clazz>();

View File

@@ -24,6 +24,7 @@ TEST_CASE("meta/meta_types/number_type") {
REQUIRE(type);
CHECK(type.get_size() == sizeof(int));
CHECK(type.get_align() == alignof(int));
CHECK(type.get_flags() == (
meta::number_flags::is_signed |
meta::number_flags::is_integral));
@@ -34,16 +35,18 @@ TEST_CASE("meta/meta_types/number_type") {
REQUIRE(type);
CHECK(type.get_size() == sizeof(float));
CHECK(type.get_align() == alignof(float));
CHECK(type.get_flags() == (
meta::number_flags::is_signed |
meta::number_flags::is_floating_point));
}
SUBCASE("const unsigned") {
const meta::number_type type = meta::resolve_type<const unsigned>();
SUBCASE("const unsigned long long") {
const meta::number_type type = meta::resolve_type<const unsigned long long>();
REQUIRE(type);
CHECK(type.get_size() == sizeof(unsigned));
CHECK(type.get_size() == sizeof(unsigned long long));
CHECK(type.get_align() == alignof(unsigned long long));
CHECK(type.get_flags() == (
meta::number_flags::is_unsigned |
meta::number_flags::is_integral));