throw in incorrect merge

This commit is contained in:
BlackMATov
2021-07-02 00:55:11 +07:00
parent ebfc3ddfc7
commit 9495c2a69d
7 changed files with 70 additions and 9 deletions

View File

@@ -111,6 +111,9 @@ namespace meta_hpp
friend class namespace_;
private:
void merge_with_(const class_info& other) {
if ( fid() != other.fid() ) {
throw std::logic_error("class_info::merge failed");
}
detail::merge_with(classes_, other.classes_, &class_info::merge_with_);
detail::merge_with(datas_, other.datas_, &data_info::merge_with_);
detail::merge_with(fields_, other.fields_, &field_info::merge_with_);

View File

@@ -105,8 +105,9 @@ namespace meta_hpp
template < auto Field > friend class field_;
private:
void merge_with_(const field_info& other) {
getter_ = other.getter_;
setter_ = other.setter_;
if ( fid() != other.fid() ) {
throw std::logic_error("field_info::merge failed");
}
detail::merge_with(datas_, other.datas_, &data_info::merge_with_);
}
private:

View File

@@ -113,7 +113,9 @@ namespace meta_hpp
template < auto Function > friend class function_;
private:
void merge_with_(const function_info& other) {
invoke_ = other.invoke_;
if ( fid() != other.fid() ) {
throw std::logic_error("function_info::merge failed");
}
detail::merge_with(datas_, other.datas_, &data_info::merge_with_);
}
private:

View File

@@ -55,6 +55,14 @@ namespace meta_hpp
friend bool operator<(family_id l, family_id r) noexcept {
return l.id < r.id;
}
friend bool operator==(family_id l, family_id r) noexcept {
return l.id == r.id;
}
friend bool operator!=(family_id l, family_id r) noexcept {
return l.id != r.id;
}
};
namespace family_id_detail

View File

@@ -171,8 +171,9 @@ namespace meta_hpp
template < auto Method > friend class method_;
private:
void merge_with_(const method_info& other) {
invoke_ = other.invoke_;
cinvoke_ = other.cinvoke_;
if ( fid() != other.fid() ) {
throw std::logic_error("method_info::merge failed");
}
detail::merge_with(datas_, other.datas_, &data_info::merge_with_);
}
private:

View File

@@ -103,8 +103,9 @@ namespace meta_hpp
template < auto Variable > friend class variable_;
private:
void merge_with_(const variable_info& other) {
getter_ = other.getter_;
setter_ = other.setter_;
if ( fid() != other.fid() ) {
throw std::logic_error("variable_info::merge failed");
}
detail::merge_with(datas_, other.datas_, &data_info::merge_with_);
}
private:

View File

@@ -17,6 +17,15 @@ namespace
class clazz {
public:
class clazz2 {
public:
int field{21};
const int cfield{22};
};
class clazz3 {
public:
int field{31};
const int cfield{32};
};
int field{1};
@@ -39,8 +48,7 @@ TEST_CASE("meta/class") {
namespace meta = meta_hpp;
meta::class_<clazz> class_{"clazz"};
const meta::class_<clazz>& cclass_ = class_;
const meta::class_info& clazz_info = cclass_;
const meta::class_info& clazz_info = class_;
CHECK_FALSE(clazz_info.get_class("clazz2"));
CHECK_FALSE(clazz_info.get_field("field"));
@@ -110,3 +118,40 @@ TEST_CASE("meta/class") {
CHECK(cvariable_info.id() == "cvariable");
}
}
TEST_CASE("meta/class/merge") {
namespace meta = meta_hpp;
meta::class_<clazz> clazz_{"clazz"};
const meta::class_info& clazz_info = clazz_;
SUBCASE("merge") {
CHECK_NOTHROW(clazz_(
meta::class_<clazz::clazz2>("child")(
meta::field_<&clazz::clazz2::field>("field")
)
));
CHECK_NOTHROW(clazz_(
meta::class_<clazz::clazz2>("child")(
meta::field_<&clazz::clazz2::cfield>("cfield")
)
));
CHECK_THROWS_AS(clazz_(
meta::class_<clazz::clazz3>("child")(
meta::field_<&clazz::clazz3::field>("field")
)
), std::logic_error);
CHECK(clazz_info.get_class("child"));
CHECK(clazz_info.get_class("child")->get_field("field"));
CHECK(clazz_info.get_class("child")->get_field("cfield"));
{
clazz::clazz2 instance{};
CHECK(clazz_info.get_class("child")->get_field("field")->get(&instance).to_int() == 21);
CHECK(clazz_info.get_class("child")->get_field("cfield")->get(&instance).to_int() == 22);
}
}
}