-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1362fb
commit 04aa71c
Showing
5 changed files
with
145 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#ifndef RFL_YAML_READ_HPP_ | ||
#define RFL_YAML_READ_HPP_ | ||
|
||
#include <yaml-cpp/yaml.h> | ||
|
||
#include <istream> | ||
#include <string> | ||
|
||
#include "Parser.hpp" | ||
#include "Reader.hpp" | ||
|
||
namespace rfl { | ||
namespace yaml { | ||
|
||
using InputVarType = typename Reader::InputVarType; | ||
|
||
/// Parses an object from a YAML var. | ||
template <class T> | ||
Result<T> read(const InputVarType& _var) { | ||
const auto r = Reader(); | ||
return Parser<T>::read(r, _var); | ||
} | ||
|
||
/// Parses an object from YAML using reflection. | ||
template <class T> | ||
Result<T> read(const std::string& _yaml_str) { | ||
try { | ||
const auto var = InputVarType(YAML::Load(_yaml_str)); | ||
return read<T>(var); | ||
} catch (std::exception& e) { | ||
return Error(e.what()); | ||
} | ||
} | ||
|
||
/// Parses an object from a stringstream. | ||
template <class T> | ||
Result<T> read(std::istream& _stream) { | ||
const auto yaml_str = std::string(std::istreambuf_iterator<char>(_stream), | ||
std::istreambuf_iterator<char>()); | ||
return read<T>(yaml_str); | ||
} | ||
|
||
} // namespace yaml | ||
} // namespace rfl | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
#include <iostream> | ||
#include <rfl.hpp> | ||
#include <rfl/yaml.hpp> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "write_and_read.hpp" | ||
|
||
using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, rfl::Maximum<130>>; | ||
|
||
|
@@ -37,7 +40,7 @@ int main() { | |
.email = "[email protected]", | ||
.children = std::vector<Person>({bart, lisa, maggie})}; | ||
|
||
rfl::yaml::write(homer, std::cout) << std::endl << std::endl; | ||
write_and_read(homer); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef WRITE_AND_READ_ | ||
#define WRITE_AND_READ_ | ||
|
||
#include <iostream> | ||
#include <rfl/yaml.hpp> | ||
#include <string> | ||
|
||
template <class T> | ||
void write_and_read(const T& _struct) { | ||
const auto yaml_string1 = rfl::yaml::write(_struct); | ||
const auto res = rfl::yaml::read<T>(yaml_string1); | ||
if (!res) { | ||
std::cout << "Test failed on read. Error: " << res.error().value().what() | ||
<< std::endl | ||
<< "Original string: " << yaml_string1 << std::endl | ||
<< std::endl; | ||
return; | ||
} | ||
const auto yaml_string2 = rfl::yaml::write(res.value()); | ||
if (yaml_string2 != yaml_string1) { | ||
std::cout << "Test failed on read. Expected:" << std::endl | ||
<< yaml_string1 << std::endl | ||
<< "Got: " << std::endl | ||
<< yaml_string2 << std::endl | ||
<< std::endl; | ||
return; | ||
} | ||
std::cout << yaml_string1 << std::endl; | ||
std::cout << "OK" << std::endl << std::endl; | ||
} | ||
|
||
#endif |