Skip to content

Commit

Permalink
update name and documentation
Browse files Browse the repository at this point in the history
Co-authored-by: Tobias Ribizel <ribizel@kit.edu>
  • Loading branch information
yhmtsai and upsj committed Apr 2, 2024
1 parent a176934 commit b445707
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 86 deletions.
60 changes: 28 additions & 32 deletions core/config/property_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,84 +12,80 @@ namespace gko {
namespace config {


pnode::pnode() : status_(status_t::empty) {}
pnode::pnode() : tag_(tag_t::empty) {}


pnode::pnode(bool boolean) : status_(status_t::boolean)
pnode::pnode(bool boolean) : tag_(tag_t::boolean)
{
union_data_.boolean_ = boolean;
}


pnode::pnode(const std::string& str) : status_(status_t::string) { str_ = str; }
pnode::pnode(const std::string& str) : tag_(tag_t::string) { str_ = str; }


pnode::pnode(double real) : status_(status_t::real)
{
union_data_.real_ = real;
}
pnode::pnode(double real) : tag_(tag_t::real) { union_data_.real_ = real; }


pnode::pnode(const char* str) : pnode(std::string(str)) {}


pnode::pnode(const array_type& array) : status_(status_t::array), array_(array)
{}
pnode::pnode(const array_type& array) : tag_(tag_t::array), array_(array) {}


pnode::pnode(const map_type& map) : status_(status_t::map), map_(map) {}
pnode::pnode(const map_type& map) : tag_(tag_t::map), map_(map) {}


pnode::operator bool() const noexcept { return status_ != status_t::empty; }
pnode::operator bool() const noexcept { return tag_ != tag_t::empty; }


pnode::status_t pnode::get_status() const { return status_; }
pnode::tag_t pnode::get_tag() const { return tag_; }

const pnode::array_type& pnode::get_array() const
{
this->throw_if_not_contain(status_t::array);
this->throw_if_not_contain(tag_t::array);
return array_;
}


const pnode::map_type& pnode::get_map() const
{
this->throw_if_not_contain(status_t::map);
this->throw_if_not_contain(tag_t::map);
return map_;
}


bool pnode::get_boolean() const
{
this->throw_if_not_contain(status_t::boolean);
this->throw_if_not_contain(tag_t::boolean);
return union_data_.boolean_;
}


std::int64_t pnode::get_integer() const
{
this->throw_if_not_contain(status_t::integer);
this->throw_if_not_contain(tag_t::integer);
return union_data_.integer_;
}


double pnode::get_real() const
{
this->throw_if_not_contain(status_t::real);
this->throw_if_not_contain(tag_t::real);
return union_data_.real_;
}


std::string pnode::get_string() const
{
this->throw_if_not_contain(status_t::string);
this->throw_if_not_contain(tag_t::string);
return str_;
}


const pnode& pnode::get(const std::string& key) const
{
this->throw_if_not_contain(status_t::map);
this->throw_if_not_contain(tag_t::map);
auto it = map_.find(key);
if (it != map_.end()) {
return map_.at(key);
Expand All @@ -100,35 +96,35 @@ const pnode& pnode::get(const std::string& key) const

const pnode& pnode::get(int index) const
{
this->throw_if_not_contain(status_t::array);
this->throw_if_not_contain(tag_t::array);
return array_.at(index);
}


void pnode::throw_if_not_contain(status_t status) const
void pnode::throw_if_not_contain(tag_t tag) const
{
static auto str_status = [](status_t status) -> std::string {
if (status == status_t::empty) {
static auto str_tag = [](tag_t tag) -> std::string {
if (tag == tag_t::empty) {
return "empty";
} else if (status == status_t::array) {
} else if (tag == tag_t::array) {
return "array";
} else if (status == status_t::map) {
} else if (tag == tag_t::map) {
return "map";
} else if (status == status_t::real) {
} else if (tag == tag_t::real) {
return "real";
} else if (status == status_t::boolean) {
} else if (tag == tag_t::boolean) {
return "boolean";
} else if (status == status_t::integer) {
} else if (tag == tag_t::integer) {
return "integer";
} else if (status == status_t::string) {
} else if (tag == tag_t::string) {
return "string";
} else {
return "unknown";
}
};
bool is_valid = (status_ == status);
std::string msg = "Contains " + str_status(status_) + ", but try to get " +
str_status(status);
bool is_valid = (tag_ == tag);
std::string msg =
"Contains " + str_tag(tag_) + ", but try to get " + str_tag(tag);
GKO_THROW_IF_INVALID(is_valid, msg);
}

Expand Down
34 changes: 17 additions & 17 deletions core/test/config/property_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ using namespace gko::config;

void assert_others_throw(const pnode& node)
{
auto status = node.get_status();
if (status != pnode::status_t::array) {
auto tag = node.get_tag();
if (tag != pnode::tag_t::array) {
ASSERT_THROW(node.get_array(), gko::InvalidStateError);
ASSERT_THROW(node.get(0), gko::InvalidStateError);
}
if (status != pnode::status_t::map) {
if (tag != pnode::tag_t::map) {
ASSERT_THROW(node.get_map(), gko::InvalidStateError);
ASSERT_THROW(node.get("random"), gko::InvalidStateError);
}
if (status != pnode::status_t::boolean) {
if (tag != pnode::tag_t::boolean) {
ASSERT_THROW(node.get_boolean(), gko::InvalidStateError);
}
if (status != pnode::status_t::integer) {
if (tag != pnode::tag_t::integer) {
ASSERT_THROW(node.get_integer(), gko::InvalidStateError);
}
if (status != pnode::status_t::real) {
if (tag != pnode::tag_t::real) {
ASSERT_THROW(node.get_real(), gko::InvalidStateError);
}
if (status != pnode::status_t::string) {
if (tag != pnode::tag_t::string) {
ASSERT_THROW(node.get_string(), gko::InvalidStateError);
}
}
Expand All @@ -50,7 +50,7 @@ TEST(PropertyTree, CreateEmpty)
{
pnode root;

ASSERT_EQ(root.get_status(), pnode::status_t::empty);
ASSERT_EQ(root.get_tag(), pnode::tag_t::empty);
assert_others_throw(root);
}

Expand All @@ -60,10 +60,10 @@ TEST(PropertyTree, CreateStringData)
pnode str(std::string("test_name"));
pnode char_str("test_name");

ASSERT_EQ(str.get_status(), pnode::status_t::string);
ASSERT_EQ(str.get_tag(), pnode::tag_t::string);
ASSERT_EQ(str.get_string(), "test_name");
assert_others_throw(str);
ASSERT_EQ(char_str.get_status(), pnode::status_t::string);
ASSERT_EQ(char_str.get_tag(), pnode::tag_t::string);
ASSERT_EQ(char_str.get_string(), "test_name");
assert_others_throw(char_str);
}
Expand All @@ -73,7 +73,7 @@ TEST(PropertyTree, CreateBoolData)
{
pnode boolean(true);

ASSERT_EQ(boolean.get_status(), pnode::status_t::boolean);
ASSERT_EQ(boolean.get_tag(), pnode::tag_t::boolean);
ASSERT_EQ(boolean.get_boolean(), true);
assert_others_throw(boolean);
}
Expand All @@ -94,7 +94,7 @@ TEST(PropertyTree, CreateIntegerData)

for (auto& node : {integer, integer_8, integer_16, integer_32, integer_64,
integer_u8, integer_u16, integer_u32, integer_u64}) {
ASSERT_EQ(node.get_status(), pnode::status_t::integer);
ASSERT_EQ(node.get_tag(), pnode::tag_t::integer);
ASSERT_EQ(node.get_integer(), 1);
assert_others_throw(node);
}
Expand All @@ -111,7 +111,7 @@ TEST(PropertyTree, CreateRealData)
pnode real_double(double(1.0));

for (auto& node : {real, real_double, real_float}) {
ASSERT_EQ(node.get_status(), pnode::status_t::real);
ASSERT_EQ(node.get_tag(), pnode::tag_t::real);
ASSERT_EQ(node.get_real(), 1.0);
assert_others_throw(node);
}
Expand All @@ -124,10 +124,10 @@ TEST(PropertyTree, CreateMap)
{"p1", pnode{1}},
{"p2", pnode{pnode::map_type{{"p0", pnode{"test"}}}}}});

ASSERT_EQ(root.get_status(), pnode::status_t::map);
ASSERT_EQ(root.get_tag(), pnode::tag_t::map);
ASSERT_EQ(root.get("p0").get_real(), 1.0);
ASSERT_EQ(root.get("p1").get_integer(), 1);
ASSERT_EQ(root.get("p2").get_status(), pnode::status_t::map);
ASSERT_EQ(root.get("p2").get_tag(), pnode::tag_t::map);
ASSERT_EQ(root.get("p2").get("p0").get_string(), "test");
assert_others_throw(root);
}
Expand All @@ -137,7 +137,7 @@ TEST(PropertyTree, CreateArray)
{
pnode root(pnode::array_type{pnode{"123"}, pnode{"456"}, pnode{"789"}});

ASSERT_EQ(root.get_status(), pnode::status_t::array);
ASSERT_EQ(root.get_tag(), pnode::tag_t::array);
ASSERT_EQ(root.get(0).get_string(), "123");
ASSERT_EQ(root.get(1).get_string(), "456");
ASSERT_EQ(root.get(2).get_string(), "789");
Expand All @@ -163,7 +163,7 @@ TEST(PropertyTree, ReturnEmptyIfNotFound)

auto obj = ptree.get("na");

ASSERT_EQ(obj.get_status(), pnode::status_t::empty);
ASSERT_EQ(obj.get_tag(), pnode::tag_t::empty);
}


Expand Down
Loading

0 comments on commit b445707

Please sign in to comment.