-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Allowing partial specialization of the convert struct #922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| #include "yaml-cpp/emitterstyle.h" | ||
| #include "yaml-cpp/eventhandler.h" | ||
| #include "yaml-cpp/yaml.h" // IWYU pragma: keep | ||
| #include "gtest/gtest.h" | ||
|
|
||
| // Base class | ||
| class A { | ||
| public: | ||
| A() = default; | ||
| A(int a) : a{a} {} | ||
|
|
||
| // virtual load/emit methods | ||
| virtual void load(const YAML::Node &node) { a = node["a"].as<int>(); } | ||
|
|
||
| virtual YAML::Node emit() const { | ||
| YAML::Node node; | ||
| node["a"] = a; | ||
| return node; | ||
| } | ||
|
|
||
| int a{}; | ||
| }; | ||
|
|
||
| // Derived class | ||
| class B : public A { | ||
| public: | ||
| B() = default; | ||
| B(int a, int b) : A{a}, b{b} {} | ||
|
|
||
| // override virtual load/emit methods | ||
| virtual void load(const YAML::Node &node) override { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even though it's a test case, I think the constructors are needed, like #310. It is also convenient for later usage.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about updating the Tutorial.md at the same time. And use this in testcase. A a(1);
B b(1, 2);
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated the test cases to use the constructors and added them in the tutorial. |
||
| A::load(node); | ||
| b = node["b"].as<int>(); | ||
| } | ||
|
|
||
| virtual YAML::Node emit() const override { | ||
| YAML::Node node = A::emit(); | ||
| node["b"] = b; | ||
| return node; | ||
| } | ||
|
|
||
| int b{}; | ||
| }; | ||
|
|
||
| // Implementation of convert::{encode,decode} for all classes derived from or | ||
| // being A | ||
| namespace YAML { | ||
| template <typename T> | ||
| struct convert<T, typename std::enable_if<std::is_base_of<A, T>::value>::type> { | ||
| static Node encode(const T &rhs) { | ||
| Node node = rhs.emit(); | ||
| return node; | ||
| } | ||
|
|
||
| static bool decode(const Node &node, T &rhs) { | ||
| rhs.load(node); | ||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| namespace { | ||
|
|
||
| TEST(ConvertPartialSpecializationTest, EncodeBaseClass) { | ||
| Node n(Load("{a: 1}")); | ||
| A a = n.as<A>(); | ||
| EXPECT_EQ(a.a, 1); | ||
| } | ||
|
|
||
| TEST(ConvertPartialSpecializationTest, EncodeDerivedClass) { | ||
| Node n(Load("{a: 1, b: 2}")); | ||
| B b = n.as<B>(); | ||
| EXPECT_EQ(b.a, 1); | ||
| EXPECT_EQ(b.b, 2); | ||
| } | ||
|
|
||
| TEST(ConvertPartialSpecializationTest, DecodeBaseClass) { | ||
| A a(1); | ||
| Node n; | ||
| n = a; | ||
| EXPECT_EQ(a.a, n["a"].as<int>()); | ||
| } | ||
|
|
||
| TEST(ConvertPartialSpecializationTest, DecodeDerivedClass) { | ||
| B b(1, 2); | ||
| Node n; | ||
| n = b; | ||
| EXPECT_EQ(b.a, n["a"].as<int>()); | ||
| EXPECT_EQ(b.b, n["b"].as<int>()); | ||
| } | ||
|
|
||
| } // namespace | ||
| } // namespace YAML | ||
Uh oh!
There was an error while loading. Please reload this page.