$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 #ifndef FABLE_CONFABLE_HPP_
26 #define FABLE_CONFABLE_HPP_
27 
28 #include <memory> // for unique_ptr<>
29 
30 #include <fable/json.hpp> // for Json
31 #include <fable/conf.hpp> // for Conf
32 
33 #define CONFABLE_FRIENDS(xType) \
34  using ::fable::Confable::to_json; \
35  friend void to_json(::fable::Json& j, const xType& t) { t.to_json(j); } \
36  friend void from_json(const ::fable::Json& j, xType& t) { t.from_conf(::fable::Conf{j}); }
37 
38 #define CONFABLE_SCHEMA(xType) \
39  CONFABLE_FRIENDS(xType) \
40  ::fable::Schema schema_impl() override
41 
42 namespace fable {
43 
44 class Schema;
45 
46 class Confable {
47  public:
48  Confable() noexcept;
49  Confable(const Confable&) noexcept;
50  Confable(Confable&&) noexcept;
51 
52  Confable& operator=(const Confable& other) noexcept;
53  Confable& operator=(Confable&& other) noexcept;
54 
55  virtual ~Confable() noexcept;
56 
62  virtual void reset_schema();
63 
70  Schema& schema();
71 
78  const Schema& schema() const;
79 
90  virtual void validate(const Conf& c) const;
91 
101  virtual void from_conf(const Conf& c);
102 
109  virtual void to_json(Json& j) const;
110 
114  Json to_json() const;
115 
116  protected:
124  virtual Schema schema_impl();
125 
126  private:
127  mutable std::unique_ptr<Schema> schema_;
128 };
129 
130 } // namespace fable
131 
132 namespace nlohmann {
133 
134 template <>
135 struct adl_serializer<fable::Confable> {
136  static void to_json(json& j, const fable::Confable& c) { c.to_json(j); }
137  static void from_json(const json& j, fable::Confable& c) { c.from_conf(fable::Conf{j}); }
138 };
139 
140 } // namespace nlohmann
141 
142 #endif // FABLE_CONFABLE_HPP_
Definition: conf.hpp:74
Definition: confable.hpp:46
nlohmann::json Json
Definition: json.hpp:62
virtual void to_json(Json &j) const
Definition: confable.cpp:72
Definition: conf.hpp:70
Definition: confable.hpp:132
virtual void validate(const Conf &c) const
Definition: confable.cpp:64
Json to_json() const
Definition: confable.cpp:74
Definition: schema.hpp:178
Schema & schema()
Definition: confable.cpp:55
virtual void from_conf(const Conf &c)
Definition: confable.cpp:66
virtual void reset_schema()
Definition: confable.cpp:53
virtual Schema schema_impl()
Definition: confable.cpp:80