$darkmode
magic.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  */
36 #pragma once
37 #ifndef FABLE_SCHEMA_MAGIC_HPP_
38 #define FABLE_SCHEMA_MAGIC_HPP_
39 
40 #include <map> // for map<>
41 #include <string> // for string
42 #include <utility> // for move
43 #include <vector> // for vector<>
44 
45 #include <fable/schema/array.hpp> // for Array<>
46 #include <fable/schema/confable.hpp> // for FromConfable<>
47 #include <fable/schema/const.hpp> // for Const<>
48 #include <fable/schema/map.hpp> // for Map<>
49 #include <fable/schema/optional.hpp> // for Optional<>
50 
51 namespace fable {
52 
53 // Forward declarations:
54 class Confable;
55 
56 namespace schema {
57 
58 template <typename T, typename P>
59 Array<T, P>::Array(std::vector<T>* ptr, std::string&& desc)
60  : Array<T, P>(ptr, make_prototype<T>(), std::move(desc)) {}
61 
62 template <typename T>
63 Array<T, decltype(make_prototype<T>())> make_schema(std::vector<T>* ptr, std::string&& desc) {
64  return Array<T, decltype(make_prototype<T>())>(ptr, std::move(desc));
65 }
66 
67 template <typename T, typename P>
68 Const<T, P>::Const(const T& constant, std::string&& desc)
69  : Const<T, P>(constant, make_prototype<T>(), std::move(desc)) {}
70 
71 template <typename T>
72 Const<T, decltype(make_prototype<T>())> make_const_schema(const T& constant, std::string&& desc) {
73  return Const<T, decltype(make_prototype<T>())>(constant, std::move(desc));
74 }
75 
76 template <typename T, typename P>
77 Map<T, P>::Map(std::map<std::string, T>* ptr, std::string&& desc)
78  : Map<T, P>(ptr, make_prototype<T>(), std::move(desc)) {}
79 
80 template <typename T>
81 Map<T, decltype(make_prototype<T>())> make_schema(std::map<std::string, T>* ptr,
82  std::string&& desc) {
83  return Map<T, decltype(make_prototype<T>())>(ptr, std::move(desc));
84 }
85 
86 template <typename T, typename P>
87 Optional<T, P>::Optional(boost::optional<T>* ptr, std::string&& desc)
88  : Optional<T, P>(ptr, make_prototype<T>(), std::move(desc)) {}
89 
90 template <typename T>
91 Optional<T, decltype(make_prototype<T>())> make_schema(boost::optional<T>* ptr,
92  std::string&& desc) {
93  return Optional<T, decltype(make_prototype<T>())>(ptr, std::move(desc));
94 }
95 
96 template <typename T, std::enable_if_t<std::is_base_of<Confable, T>::value, int>>
97 auto make_prototype(std::string&& desc) {
98  return FromConfable<T>(std::move(desc));
99 }
100 
101 template <typename T, std::enable_if_t<!std::is_base_of<Confable, T>::value, int>>
102 auto make_prototype(std::string&& desc) {
103  return make_schema(static_cast<T*>(nullptr), std::move(desc));
104 }
105 
106 } // namespace schema
107 } // namespace fable
108 
109 #endif // FABLE_SCHEMA_MAGIC_HPP_
Definition: conf.hpp:70