mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-07 03:06:18 +08:00
Added XML support; #21
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
#ifndef RFL_PARSING_PARSER_SHARED_PTR_HPP_
|
||||
#define RFL_PARSING_PARSER_SHARED_PTR_HPP_
|
||||
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/Result.hpp"
|
||||
#include "rfl/always_false.hpp"
|
||||
#include "rfl/parsing/Parser_base.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class R, class W, class T>
|
||||
requires AreReaderAndWriter<R, W, std::shared_ptr<T>>
|
||||
struct Parser<R, W, std::shared_ptr<T>> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<std::shared_ptr<T>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
if (_r.is_empty(*_var)) {
|
||||
return std::shared_ptr<T>();
|
||||
}
|
||||
const auto to_ptr = [](auto&& _t) {
|
||||
return std::make_shared<T>(std::move(_t));
|
||||
};
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_ptr);
|
||||
}
|
||||
|
||||
/// Expresses the variable a a JSON.
|
||||
static OutputVarType write(const W& _w,
|
||||
const std::shared_ptr<T>& _s) noexcept {
|
||||
if (!_s) {
|
||||
return _w.empty_var();
|
||||
}
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, *_s);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user