$darkmode
confable.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
24 #pragma once
25 
26 #include <memory> // for unique_ptr<>
27 #include <optional> // for optional<>
28 
29 #include <fable/conf.hpp> // for Conf, Json
30 #include <fable/fable_fwd.hpp> // for Schema, SchemaError
31 
32 #define CONFABLE_FRIENDS(xType) \
33  using ::fable::Confable::to_json; \
34  friend void to_json(::fable::Json& j, const xType& t) { t.to_json(j); } \
35  friend void from_json(const ::fable::Json& j, xType& t) { t.from_conf(::fable::Conf{j}); }
36 
37 #define CONFABLE_SCHEMA(xType) \
38  CONFABLE_FRIENDS(xType) \
39  ::fable::Schema schema_impl() override
40 
41 namespace fable {
42 
98 class Confable {
99  public:
100  Confable() noexcept = default;
101  Confable(const Confable& /*unused*/) noexcept;
102  Confable(Confable&&) noexcept = default;
103  Confable& operator=(const Confable& other) noexcept;
104  Confable& operator=(Confable&& other) noexcept;
105 
106  virtual ~Confable() noexcept = default;
107 
113  virtual void reset_schema();
114 
121  [[nodiscard]] Schema& schema();
122 
129  [[nodiscard]] const Schema& schema() const;
130 
141  virtual void validate_or_throw(const Conf& c) const;
142 
159  virtual bool validate(const Conf& c, std::optional<SchemaError>& err) const;
160 
170  virtual void from_conf(const Conf& c);
171 
178  virtual void to_json(Json& j) const;
179 
183  [[nodiscard]] Json to_json() const;
184 
185  protected:
193  [[nodiscard]] virtual Schema schema_impl();
194 
195  private:
196  mutable std::unique_ptr<Schema> schema_;
197 };
198 
199 } // namespace fable
200 
201 namespace nlohmann {
202 
203 template <>
204 struct adl_serializer<fable::Confable> {
205  static void to_json(json& j, const fable::Confable& c);
206  static void from_json(const json& j, fable::Confable& c);
207 };
208 
209 } // namespace nlohmann
Definition: conf.hpp:81
Definition: confable.hpp:98
Schema & schema()
Definition: confable.cpp:47
virtual Schema schema_impl()
Definition: confable.cpp:84
virtual void validate_or_throw(const Conf &c) const
Definition: confable.cpp:58
virtual void from_conf(const Conf &c)
Definition: confable.cpp:70
virtual void reset_schema()
Definition: confable.cpp:54
virtual bool validate(const Conf &c, std::optional< SchemaError > &err) const
Definition: confable.cpp:66
Json to_json() const
Definition: confable.cpp:78
Definition: schema.hpp:173
nlohmann::json Json
Definition: fable_fwd.hpp:35