Skip to content

Commit 7c9b960

Browse files
committed
fable: Simplify make_prototype definition file
- All outlined definitions that use make_prototype are inlined again in their respective header files. BREAKING CHANGE: The function `make_const_str` is removed in favor of `make_const_schema`. This is orthogonal with `make_schema` and works the same as `make_const_str`, except that you need to make sure a `std::string` is actually used instead of something else, such as `char *` or `std::string_view`. For example, the following statement: make_const_str("constant"); should become: use namespace std::literals; make_const_schema("constant"s); or: make_const_schema(std::string("constant"));
1 parent 8f9875a commit 7c9b960

File tree

13 files changed

+53
-107
lines changed

13 files changed

+53
-107
lines changed

engine/src/stack.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ struct SimulatorConf : public Confable {
521521
CONFABLE_SCHEMA(SimulatorConf) {
522522
using namespace schema; // NOLINT(build/namespaces)
523523
return Struct{
524-
{"binding", make_const_str(binding, "name of simulator binding").require()},
524+
{"binding", make_const_schema(binding, "name of simulator binding").require()},
525525
{"name", make_schema(&name, id_prototype(), "identifier override for binding")},
526526
{"args", make_schema(&args, factory->schema(), "factory-specific arguments")},
527527
};
@@ -555,7 +555,7 @@ struct ControllerConf : public Confable {
555555
// clang-format off
556556
using namespace schema; // NOLINT(build/namespaces)
557557
return Struct{
558-
{"binding", make_const_str(binding, "name of controller binding").require()},
558+
{"binding", make_const_schema(binding, "name of controller binding").require()},
559559
{"name", make_schema(&name, id_prototype(), "identifier override for binding")},
560560
{"vehicle", make_schema(&vehicle, "vehicle controller is assigned to").c_identifier().require()},
561561
{"args", make_schema(&args, factory->schema(), "factory-specific arguments")},
@@ -639,7 +639,7 @@ struct ComponentConf : public Confable {
639639
// clang-format off
640640
using namespace schema; // NOLINT(build/namespaces)
641641
return Struct{
642-
{"binding", make_const_str(binding, "name of binding").require()},
642+
{"binding", make_const_schema(binding, "name of binding").require()},
643643
{"name", make_schema(&name, id_prototype(), "globally unique identifier for component")},
644644
{"from", Variant{
645645
make_schema(&from, "component inputs for binding"),

fable/include/fable/schema.hpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,12 @@
141141

142142
// It is important that this include comes after all the other ones,
143143
// so that it has access to ALL the previous definitions.
144-
#include <fable/schema/xmagic.hpp> // for make_prototype, ...
144+
#include <fable/schema/xmagic.hpp> // for make_prototype
145145

146146
namespace fable {
147147

148148
// Bring all make_* functions into the fable namespace.
149149
using schema::make_const_schema;
150-
using schema::make_const_str;
151150
using schema::make_prototype;
152151
using schema::make_schema;
153152

fable/include/fable/schema/array.hpp

+10-9
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
* SPDX-License-Identifier: Apache-2.0
1717
*/
1818
/**
19-
* \file fable/schema/array_fixed.hpp
20-
* \see fable/schema/magic.hpp
19+
* \file fable/schema/array.hpp
2120
* \see fable/schema.hpp
2221
* \see fable/schema_test.cpp
2322
*/
@@ -100,18 +99,15 @@ class Array : public Base<Array<T, N, P>> {
10099
using Type = std::array<T, N>;
101100
using PrototypeSchema = P;
102101

103-
Array(Type* ptr, std::string desc);
102+
Array(Type* ptr, std::string desc)
103+
: Array<T, N, P>(ptr, make_prototype<T>(), std::move(desc)) {}
104+
104105
Array(Type* ptr, PrototypeSchema prototype)
105106
: Base<Array<T, N, P>>(), prototype_(std::move(prototype)), ptr_(ptr) {}
107+
106108
Array(Type* ptr, PrototypeSchema prototype, std::string desc)
107109
: Base<Array<T, N, P>>(std::move(desc)), prototype_(std::move(prototype)), ptr_(ptr) {}
108110

109-
#if 0
110-
// This is defined in: fable/schema/magic.hpp
111-
Array(Type* ptr, std::string desc)
112-
: Array(ptr, make_prototype<T>(), std::move(desc)) {}
113-
#endif
114-
115111
public: // Specials
116112
/**
117113
* Return whether deserialization must set all fields, in which case
@@ -383,5 +379,10 @@ Array<T, N, P> make_schema(std::array<T, N>* ptr, P&& prototype, S&& desc) {
383379
return Array<T, N, P>(ptr, std::forward<P>(prototype), std::forward<S>(desc));
384380
}
385381

382+
template <typename T, size_t N, typename S>
383+
Array<T, N, decltype(make_prototype<T>())> make_schema(std::array<T, N>* ptr, S&& desc) {
384+
return Array<T, N, decltype(make_prototype<T>())>(ptr, std::forward<S>(desc));
385+
}
386+
386387
} // namespace schema
387388
} // namespace fable

fable/include/fable/schema/confable.hpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,10 @@
2828
#include <string> // for string
2929
#include <utility> // for move
3030

31+
#include <fable/fable_fwd.hpp> // for Confable
3132
#include <fable/schema/interface.hpp> // for Interface
3233

3334
namespace fable {
34-
35-
// Forward declarations:
36-
class Confable;
37-
3835
namespace schema {
3936

4037
template <typename T, std::enable_if_t<std::is_base_of_v<Confable, T>, int> = 0>

fable/include/fable/schema/const.hpp

+9-16
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818
/**
1919
* \file fable/schema/const.hpp
20-
* \see fable/schema/xmagic.hpp
2120
* \see fable/schema/const_test.cpp
2221
* \see fable/schema_test.cpp
2322
*/
@@ -36,23 +35,19 @@ namespace schema {
3635
template <typename T, typename P>
3736
class Const : public Base<Const<T, P>> {
3837
public: // Types and Constructors
39-
using Type = T;
38+
using Type = std::remove_cv_t<std::remove_reference_t<T>>;
4039
using PrototypeSchema = std::remove_cv_t<std::remove_reference_t<P>>;
4140

42-
Const(Type constant, std::string desc);
41+
Const(Type constant, std::string desc)
42+
: Const(std::move(constant), make_prototype<Type>(), std::move(desc)) {}
43+
4344
Const(Type constant, PrototypeSchema prototype, std::string desc)
4445
: Base<Const<T, P>>(prototype.type(), std::move(desc))
4546
, prototype_(std::move(prototype))
4647
, constant_(std::move(constant)) {
4748
prototype_.reset_ptr();
4849
}
4950

50-
#if 0
51-
// This is defined in: fable/schema/xmagic.hpp
52-
Const(const T& constant, std::string desc)
53-
: Const(constant, make_prototype<T>(), std::move(desc)) {}
54-
#endif
55-
5651
public: // Overrides
5752
Json json_schema() const override {
5853
Json j{
@@ -81,9 +76,7 @@ class Const : public Base<Const<T, P>> {
8176
return constant_;
8277
}
8378

84-
void serialize_into(Json& j, const Type& x) const {
85-
prototype_.serialize_into(j, x);
86-
}
79+
void serialize_into(Json& j, const Type& x) const { prototype_.serialize_into(j, x); }
8780

8881
void deserialize_into(const Conf& c, Type& x) const {
8982
validate(c);
@@ -99,12 +92,12 @@ class Const : public Base<Const<T, P>> {
9992

10093
template <typename T, typename P, typename S>
10194
Const<T, P> make_const_schema(T&& constant, P&& prototype, S&& desc) {
102-
return Const<T, P>(std::forward<T>(constant), std::forward<P>(prototype), std::forward<S>(desc));
95+
return {std::forward<T>(constant), std::forward<P>(prototype), std::forward<S>(desc)};
10396
}
10497

105-
template <typename S1, typename S2>
106-
inline Const<std::string, String> make_const_str(S1&& constant, S2&& desc) {
107-
return Const<std::string, String>(std::forward<S1>(constant), std::forward<S2>(desc));
98+
template <typename T, typename S>
99+
Const<T, decltype(make_prototype<std::remove_cv_t<std::remove_reference_t<T>>>())> make_const_schema(T&& constant, S&& desc) {
100+
return {std::forward<T>(constant), std::forward<S>(desc)};
108101
}
109102

110103
} // namespace schema

fable/include/fable/schema/factory.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ class FactoryBase : public Base<CRTP> {
322322
out.reserve(available_.size());
323323
for (auto& kv : available_) {
324324
Struct base{
325-
{factory_key_, make_const_str(kv.first, "name of factory").require()},
325+
{factory_key_, make_const_schema(kv.first, "name of factory").require()},
326326
};
327327
if (args_key_ == "") {
328328
base.set_properties_from(kv.second.schema);

fable/include/fable/schema/map.hpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
#include <limits> // for numeric_limits<>
2727
#include <map> // for map<>
28-
#include <memory> // for shared_ptr<>
2928
#include <optional> // for optional<>
3029
#include <regex> // for regex, regex_match
3130
#include <string> // for string
@@ -51,22 +50,18 @@ class Map : public Base<Map<T, P>> {
5150
using Type = std::map<std::string, T>;
5251
using PrototypeSchema = std::remove_cv_t<std::remove_reference_t<P>>;
5352

54-
Map(Type* ptr, std::string desc);
53+
Map(Type* ptr, std::string desc) : Map(ptr, make_prototype<T>(), std::move(desc)) {}
54+
5555
Map(Type* ptr, PrototypeSchema prototype)
5656
: Base<Map<T, P>>(JsonType::object), prototype_(std::move(prototype)), ptr_(ptr) {
5757
prototype_.reset_ptr();
5858
}
59+
5960
Map(Type* ptr, PrototypeSchema prototype, std::string desc)
6061
: Base<Map<T, P>>(JsonType::object, std::move(desc)), prototype_(std::move(prototype)), ptr_(ptr) {
6162
prototype_.reset_ptr();
6263
}
6364

64-
#if 0
65-
// This is defined in: fable/schema/xmagic.hpp
66-
Map(Type* ptr, std::string desc)
67-
: Map(ptr, make_prototype<T>(), std::move(desc)) {}
68-
#endif
69-
7065
public: // Special
7166
bool unique_properties() const { return unique_properties_; }
7267
Map<T, P> unique_properties(bool value) && {
@@ -200,5 +195,10 @@ Map<T, P> make_schema(std::map<std::string, T>* ptr, P&& prototype, S&& desc) {
200195
return Map<T, P>(ptr, std::forward<P>(prototype), std::forward<S>(desc));
201196
}
202197

198+
template <typename T, typename S>
199+
Map<T, decltype(make_prototype<T>())> make_schema(std::map<std::string, T>* ptr, S&& desc) {
200+
return Map<T, decltype(make_prototype<T>())>(ptr, std::forward<S>(desc));
201+
}
202+
203203
} // namespace schema
204204
} // namespace fable

fable/include/fable/schema/optional.hpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,14 @@ class Optional : public Base<Optional<T, P>> {
7272
using ValueType = typename Type::value_type;
7373
using PrototypeSchema = std::remove_cv_t<std::remove_reference_t<P>>;
7474

75-
Optional(Type* ptr, std::string desc);
75+
Optional(Type* ptr, std::string desc)
76+
: Optional(ptr, make_prototype<typename T::value_type>(), std::move(desc)) {}
77+
7678
Optional(Type* ptr, PrototypeSchema prototype, std::string desc)
7779
: Base<Optional<T, P>>(prototype.type(), std::move(desc)), prototype_(std::move(prototype)), ptr_(ptr) {
7880
prototype_.reset_ptr();
7981
}
8082

81-
#if 0
82-
// This is defined in: fable/schema/xmagic.hpp
83-
Optional(T* ptr, std::string desc)
84-
: Optional<T, P>(ptr, make_prototype<typename T::value_type>(), std::move(desc)) {}
85-
#endif
86-
8783
public: // Overrides
8884
std::string type_string() const override { return prototype_.type_string() + "?"; }
8985
bool is_variant() const override { return true; }
@@ -153,5 +149,10 @@ inline Optional<T, P> make_schema(T* ptr, P&& prototype, S&& desc) {
153149
return Optional<T, P>(ptr, std::forward<P>(prototype), std::forward<S>(desc));
154150
}
155151

152+
template <typename T, typename S, std::enable_if_t<is_optional_v<T>, bool> = true>
153+
Optional<T, decltype(make_prototype<typename T::value_type>())> make_schema(T* ptr, S&& desc) {
154+
return Optional<T, decltype(make_prototype<typename T::value_type>())>(ptr, std::forward<S>(desc));
155+
}
156+
156157
} // namespace schema
157158
} // namespace fable

fable/include/fable/schema/vector.hpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818
/**
1919
* \file fable/schema/vector.hpp
20-
* \see fable/schema/xmagic.hpp
2120
* \see fable/schema.hpp
2221
* \see fable/schema_test.cpp
2322
*/
@@ -42,18 +41,14 @@ class Vector : public Base<Vector<T, P>> {
4241
using Type = std::vector<T>;
4342
using PrototypeSchema = std::remove_cv_t<std::remove_reference_t<P>>;
4443

45-
Vector(Type* ptr, std::string desc);
44+
Vector(Type* ptr, std::string desc) : Vector(ptr, make_prototype<T>(), std::move(desc)) {}
45+
4646
Vector(Type* ptr, PrototypeSchema prototype)
4747
: Base<Vector<T, P>>(JsonType::array), prototype_(std::move(prototype)), ptr_(ptr) {}
48+
4849
Vector(Type* ptr, PrototypeSchema prototype, std::string desc)
4950
: Base<Vector<T, P>>(JsonType::array, std::move(desc)), prototype_(std::move(prototype)), ptr_(ptr) {}
5051

51-
#if 0
52-
// This is defined in: fable/schema/xmagic.hpp
53-
Vector(Type* ptr, std::string desc)
54-
: Vector(ptr, make_prototype<T>(), std::move(desc)) {}
55-
#endif
56-
5752
public: // Specials
5853
/**
5954
* Return whether deserialization extends the underlying array (true) or
@@ -190,5 +185,10 @@ Vector<T, P> make_schema(std::vector<T>* ptr, P&& prototype, S&& desc) {
190185
return Vector<T, P>(ptr, std::forward<P>(prototype), std::forward<S>(desc));
191186
}
192187

188+
template <typename T, typename S>
189+
Vector<T, decltype(make_prototype<T>())> make_schema(std::vector<T>* ptr, S&& desc) {
190+
return Vector<T, decltype(make_prototype<T>())>(ptr, std::forward<S>(desc));
191+
}
192+
193193
} // namespace schema
194194
} // namespace fable

fable/include/fable/schema/xmagic.hpp

-46
Original file line numberDiff line numberDiff line change
@@ -52,52 +52,6 @@
5252
namespace fable {
5353
namespace schema {
5454

55-
template <typename T, typename P>
56-
Vector<T, P>::Vector(std::vector<T>* ptr, std::string desc)
57-
: Vector<T, P>(ptr, make_prototype<T>(), std::move(desc)) {}
58-
59-
template <typename T, typename S>
60-
Vector<T, decltype(make_prototype<T>())> make_schema(std::vector<T>* ptr, S&& desc) {
61-
return Vector<T, decltype(make_prototype<T>())>(ptr, std::forward<S>(desc));
62-
}
63-
64-
template <typename T, size_t N, typename P>
65-
Array<T, N, P>::Array(std::array<T, N>* ptr, std::string desc)
66-
: Array<T, N, P>(ptr, make_prototype<T>(), std::move(desc)) {}
67-
68-
template <typename T, size_t N, typename S>
69-
Array<T, N, decltype(make_prototype<T>())> make_schema(std::array<T, N>* ptr, S&& desc) {
70-
return Array<T, N, decltype(make_prototype<T>())>(ptr, std::forward<S>(desc));
71-
}
72-
73-
template <typename T, typename P>
74-
Const<T, P>::Const(T constant, std::string desc)
75-
: Const<T, P>(constant, make_prototype<T>(), std::move(desc)) {}
76-
77-
template <typename T, typename S>
78-
Const<T, decltype(make_prototype<T>())> make_const_schema(T&& constant, S&& desc) {
79-
return Const<T, decltype(make_prototype<T>())>(std::forward<T>(constant), std::forward<S>(desc));
80-
}
81-
82-
template <typename T, typename P>
83-
Map<T, P>::Map(std::map<std::string, T>* ptr, std::string desc)
84-
: Map<T, P>(ptr, make_prototype<T>(), std::move(desc)) {}
85-
86-
template <typename T, typename S>
87-
Map<T, decltype(make_prototype<T>())> make_schema(std::map<std::string, T>* ptr,
88-
S&& desc) {
89-
return Map<T, decltype(make_prototype<T>())>(ptr, std::forward<S>(desc));
90-
}
91-
92-
template <typename T, typename P>
93-
Optional<T, P>::Optional(T* ptr, std::string desc)
94-
: Optional<T, P>(ptr, make_prototype<typename T::value_type>(), std::move(desc)) {}
95-
96-
template <typename T, typename S, std::enable_if_t<is_optional_v<T>, bool> = true>
97-
Optional<T, decltype(make_prototype<typename T::value_type>())> make_schema(T* ptr, S&& desc) {
98-
return Optional<T, decltype(make_prototype<typename T::value_type>())>(ptr, std::forward<S>(desc));
99-
}
100-
10155
template <typename T, typename S, std::enable_if_t<std::is_base_of_v<Confable, T>, int>>
10256
auto make_prototype(S&& desc) {
10357
return FromConfable<T>(std::forward<S>(desc));

fable/src/fable/schema/const_test.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ namespace {
3030

3131
struct MyConstStruct : public fable::Confable {
3232
CONFABLE_SCHEMA(MyConstStruct) {
33+
using namespace std::literals;
3334
using namespace fable::schema; // NOLINT(build/namespaces)
3435
return Struct{
35-
{"release", make_const_str("2", "constant string").require()},
36+
{"release", make_const_schema("2"s, "constant string").require()},
3637
{"major", Const<int, Number<int>>(2, "constant number")},
3738
};
3839
}

fable/src/fable/schema_test.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ struct MyStruct : public fable::Confable {
114114
TEST(fable_schema, schema_wrapper) {
115115
using namespace fable; // NOLINT(build/namespaces)
116116
using namespace fable::schema; // NOLINT(build/namespaces)
117+
using namespace std::literals; // NOLINT(build/namespaces)
117118

118119
bool my_required = false;
119120
std::string my_string = "";
@@ -126,7 +127,7 @@ TEST(fable_schema, schema_wrapper) {
126127
std::optional<std::string> middlename;
127128

128129
auto s1 = Schema{
129-
{"author", make_const_str("me", "author of this code")},
130+
{"author", make_const_schema("me"s, "author of this code")},
130131
{"required", make_schema(&my_required, "my required boolean, should be true").require()},
131132
{"string", Schema(&my_string, "my string")},
132133
{"int", make_schema(&my_int, "my integer").minimum(0)},

runtime/include/cloe/core/fable.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ using fable::Schema;
3636
using fable::SchemaError;
3737

3838
using fable::make_const_schema;
39-
using fable::make_const_str;
4039
using fable::make_prototype;
4140
using fable::make_schema;
4241
using fable::schema_type;

0 commit comments

Comments
 (0)