26 #ifndef FABLE_SCHEMA_OPTIONAL_HPP_ 27 #define FABLE_SCHEMA_OPTIONAL_HPP_ 32 #include <boost/optional.hpp> 40 template <
typename T,
typename P>
43 using Type = boost::optional<T>;
44 using PrototypeSchema = P;
46 Optional(Type* ptr, std::string&& desc);
47 Optional(Type* ptr,
const PrototypeSchema& prototype, std::string&& desc)
48 :
Base<Optional<T, P>>(prototype.type(), std::move(desc)), prototype_(prototype), ptr_(ptr) {
49 prototype_.reset_ptr();
54 Optional(Type* ptr, std::string&& desc)
55 :
Optional(ptr, make_prototype<T>(), std::move(desc)) {}
59 std::string
type_string()
const override {
return prototype_.type_string() +
"?"; }
69 prototype_.json_schema(),
72 this->augment_schema(j);
77 if (c->type() == JsonType::null) {
80 this->validate_type(c);
81 prototype_.validate(c);
86 assert(ptr_ !=
nullptr);
91 assert(ptr_ !=
nullptr);
92 *ptr_ = deserialize(c);
95 Json serialize(
const Type& x)
const {
97 return prototype_.serialize(x.get());
103 Type deserialize(
const Conf& c)
const {
104 if (c->type() == JsonType::null) {
107 return prototype_.deserialize(c);
113 PrototypeSchema prototype_;
117 template <
typename T,
typename P>
118 Optional<T, P> make_schema(boost::optional<T>* ptr,
const P& prototype, std::string&& desc) {
125 #endif // FABLE_SCHEMA_OPTIONAL_HPP_
Definition: optional.hpp:41
std::string type_string() const override
Definition: optional.hpp:59
bool is_variant() const override
Definition: optional.hpp:60
void reset_ptr() override
Definition: optional.hpp:110
nlohmann::json Json
Definition: json.hpp:62
void from_conf(const Conf &c) override
Definition: optional.hpp:90
virtual Json to_json() const
Definition: interface.hpp:220
void validate(const Conf &c) const override
Definition: optional.hpp:76
Json json_schema() const override
Definition: optional.hpp:62
Definition: interface.hpp:354
void to_json(Json &j) const override
Definition: optional.hpp:85