Skip to content

Commit

Permalink
fable: Simplify make_prototype definition file
Browse files Browse the repository at this point in the history
- 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"));
  • Loading branch information
cassava committed Apr 22, 2024
1 parent 8f9875a commit 7c9b960
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 107 deletions.
6 changes: 3 additions & 3 deletions engine/src/stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ struct SimulatorConf : public Confable {
CONFABLE_SCHEMA(SimulatorConf) {
using namespace schema; // NOLINT(build/namespaces)
return Struct{
{"binding", make_const_str(binding, "name of simulator binding").require()},
{"binding", make_const_schema(binding, "name of simulator binding").require()},
{"name", make_schema(&name, id_prototype(), "identifier override for binding")},
{"args", make_schema(&args, factory->schema(), "factory-specific arguments")},
};
Expand Down Expand Up @@ -555,7 +555,7 @@ struct ControllerConf : public Confable {
// clang-format off
using namespace schema; // NOLINT(build/namespaces)
return Struct{
{"binding", make_const_str(binding, "name of controller binding").require()},
{"binding", make_const_schema(binding, "name of controller binding").require()},
{"name", make_schema(&name, id_prototype(), "identifier override for binding")},
{"vehicle", make_schema(&vehicle, "vehicle controller is assigned to").c_identifier().require()},
{"args", make_schema(&args, factory->schema(), "factory-specific arguments")},
Expand Down Expand Up @@ -639,7 +639,7 @@ struct ComponentConf : public Confable {
// clang-format off
using namespace schema; // NOLINT(build/namespaces)
return Struct{
{"binding", make_const_str(binding, "name of binding").require()},
{"binding", make_const_schema(binding, "name of binding").require()},
{"name", make_schema(&name, id_prototype(), "globally unique identifier for component")},
{"from", Variant{
make_schema(&from, "component inputs for binding"),
Expand Down
3 changes: 1 addition & 2 deletions fable/include/fable/schema.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,12 @@

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

namespace fable {

// Bring all make_* functions into the fable namespace.
using schema::make_const_schema;
using schema::make_const_str;
using schema::make_prototype;
using schema::make_schema;

Expand Down
19 changes: 10 additions & 9 deletions fable/include/fable/schema/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
/**
* \file fable/schema/array_fixed.hpp
* \see fable/schema/magic.hpp
* \file fable/schema/array.hpp
* \see fable/schema.hpp
* \see fable/schema_test.cpp
*/
Expand Down Expand Up @@ -100,18 +99,15 @@ class Array : public Base<Array<T, N, P>> {
using Type = std::array<T, N>;
using PrototypeSchema = P;

Array(Type* ptr, std::string desc);
Array(Type* ptr, std::string desc)
: Array<T, N, P>(ptr, make_prototype<T>(), std::move(desc)) {}

Array(Type* ptr, PrototypeSchema prototype)
: Base<Array<T, N, P>>(), prototype_(std::move(prototype)), ptr_(ptr) {}

Array(Type* ptr, PrototypeSchema prototype, std::string desc)
: Base<Array<T, N, P>>(std::move(desc)), prototype_(std::move(prototype)), ptr_(ptr) {}

#if 0
// This is defined in: fable/schema/magic.hpp
Array(Type* ptr, std::string desc)
: Array(ptr, make_prototype<T>(), std::move(desc)) {}
#endif

public: // Specials
/**
* Return whether deserialization must set all fields, in which case
Expand Down Expand Up @@ -383,5 +379,10 @@ Array<T, N, P> make_schema(std::array<T, N>* ptr, P&& prototype, S&& desc) {
return Array<T, N, P>(ptr, std::forward<P>(prototype), std::forward<S>(desc));
}

template <typename T, size_t N, typename S>
Array<T, N, decltype(make_prototype<T>())> make_schema(std::array<T, N>* ptr, S&& desc) {
return Array<T, N, decltype(make_prototype<T>())>(ptr, std::forward<S>(desc));
}

} // namespace schema
} // namespace fable
5 changes: 1 addition & 4 deletions fable/include/fable/schema/confable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@
#include <string> // for string
#include <utility> // for move

#include <fable/fable_fwd.hpp> // for Confable
#include <fable/schema/interface.hpp> // for Interface

namespace fable {

// Forward declarations:
class Confable;

namespace schema {

template <typename T, std::enable_if_t<std::is_base_of_v<Confable, T>, int> = 0>
Expand Down
25 changes: 9 additions & 16 deletions fable/include/fable/schema/const.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
/**
* \file fable/schema/const.hpp
* \see fable/schema/xmagic.hpp
* \see fable/schema/const_test.cpp
* \see fable/schema_test.cpp
*/
Expand All @@ -36,23 +35,19 @@ namespace schema {
template <typename T, typename P>
class Const : public Base<Const<T, P>> {
public: // Types and Constructors
using Type = T;
using Type = std::remove_cv_t<std::remove_reference_t<T>>;
using PrototypeSchema = std::remove_cv_t<std::remove_reference_t<P>>;

Const(Type constant, std::string desc);
Const(Type constant, std::string desc)
: Const(std::move(constant), make_prototype<Type>(), std::move(desc)) {}

Const(Type constant, PrototypeSchema prototype, std::string desc)
: Base<Const<T, P>>(prototype.type(), std::move(desc))
, prototype_(std::move(prototype))
, constant_(std::move(constant)) {
prototype_.reset_ptr();
}

#if 0
// This is defined in: fable/schema/xmagic.hpp
Const(const T& constant, std::string desc)
: Const(constant, make_prototype<T>(), std::move(desc)) {}
#endif

public: // Overrides
Json json_schema() const override {
Json j{
Expand Down Expand Up @@ -81,9 +76,7 @@ class Const : public Base<Const<T, P>> {
return constant_;
}

void serialize_into(Json& j, const Type& x) const {
prototype_.serialize_into(j, x);
}
void serialize_into(Json& j, const Type& x) const { prototype_.serialize_into(j, x); }

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

template <typename T, typename P, typename S>
Const<T, P> make_const_schema(T&& constant, P&& prototype, S&& desc) {
return Const<T, P>(std::forward<T>(constant), std::forward<P>(prototype), std::forward<S>(desc));
return {std::forward<T>(constant), std::forward<P>(prototype), std::forward<S>(desc)};
}

template <typename S1, typename S2>
inline Const<std::string, String> make_const_str(S1&& constant, S2&& desc) {
return Const<std::string, String>(std::forward<S1>(constant), std::forward<S2>(desc));
template <typename T, typename S>
Const<T, decltype(make_prototype<std::remove_cv_t<std::remove_reference_t<T>>>())> make_const_schema(T&& constant, S&& desc) {
return {std::forward<T>(constant), std::forward<S>(desc)};
}

} // namespace schema
Expand Down
2 changes: 1 addition & 1 deletion fable/include/fable/schema/factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ class FactoryBase : public Base<CRTP> {
out.reserve(available_.size());
for (auto& kv : available_) {
Struct base{
{factory_key_, make_const_str(kv.first, "name of factory").require()},
{factory_key_, make_const_schema(kv.first, "name of factory").require()},
};
if (args_key_ == "") {
base.set_properties_from(kv.second.schema);
Expand Down
16 changes: 8 additions & 8 deletions fable/include/fable/schema/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#include <limits> // for numeric_limits<>
#include <map> // for map<>
#include <memory> // for shared_ptr<>
#include <optional> // for optional<>
#include <regex> // for regex, regex_match
#include <string> // for string
Expand All @@ -51,22 +50,18 @@ class Map : public Base<Map<T, P>> {
using Type = std::map<std::string, T>;
using PrototypeSchema = std::remove_cv_t<std::remove_reference_t<P>>;

Map(Type* ptr, std::string desc);
Map(Type* ptr, std::string desc) : Map(ptr, make_prototype<T>(), std::move(desc)) {}

Map(Type* ptr, PrototypeSchema prototype)
: Base<Map<T, P>>(JsonType::object), prototype_(std::move(prototype)), ptr_(ptr) {
prototype_.reset_ptr();
}

Map(Type* ptr, PrototypeSchema prototype, std::string desc)
: Base<Map<T, P>>(JsonType::object, std::move(desc)), prototype_(std::move(prototype)), ptr_(ptr) {
prototype_.reset_ptr();
}

#if 0
// This is defined in: fable/schema/xmagic.hpp
Map(Type* ptr, std::string desc)
: Map(ptr, make_prototype<T>(), std::move(desc)) {}
#endif

public: // Special
bool unique_properties() const { return unique_properties_; }
Map<T, P> unique_properties(bool value) && {
Expand Down Expand Up @@ -200,5 +195,10 @@ Map<T, P> make_schema(std::map<std::string, T>* ptr, P&& prototype, S&& desc) {
return Map<T, P>(ptr, std::forward<P>(prototype), std::forward<S>(desc));
}

template <typename T, typename S>
Map<T, decltype(make_prototype<T>())> make_schema(std::map<std::string, T>* ptr, S&& desc) {
return Map<T, decltype(make_prototype<T>())>(ptr, std::forward<S>(desc));
}

} // namespace schema
} // namespace fable
15 changes: 8 additions & 7 deletions fable/include/fable/schema/optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,14 @@ class Optional : public Base<Optional<T, P>> {
using ValueType = typename Type::value_type;
using PrototypeSchema = std::remove_cv_t<std::remove_reference_t<P>>;

Optional(Type* ptr, std::string desc);
Optional(Type* ptr, std::string desc)
: Optional(ptr, make_prototype<typename T::value_type>(), std::move(desc)) {}

Optional(Type* ptr, PrototypeSchema prototype, std::string desc)
: Base<Optional<T, P>>(prototype.type(), std::move(desc)), prototype_(std::move(prototype)), ptr_(ptr) {
prototype_.reset_ptr();
}

#if 0
// This is defined in: fable/schema/xmagic.hpp
Optional(T* ptr, std::string desc)
: Optional<T, P>(ptr, make_prototype<typename T::value_type>(), std::move(desc)) {}
#endif

public: // Overrides
std::string type_string() const override { return prototype_.type_string() + "?"; }
bool is_variant() const override { return true; }
Expand Down Expand Up @@ -153,5 +149,10 @@ inline Optional<T, P> make_schema(T* ptr, P&& prototype, S&& desc) {
return Optional<T, P>(ptr, std::forward<P>(prototype), std::forward<S>(desc));
}

template <typename T, typename S, std::enable_if_t<is_optional_v<T>, bool> = true>
Optional<T, decltype(make_prototype<typename T::value_type>())> make_schema(T* ptr, S&& desc) {
return Optional<T, decltype(make_prototype<typename T::value_type>())>(ptr, std::forward<S>(desc));
}

} // namespace schema
} // namespace fable
16 changes: 8 additions & 8 deletions fable/include/fable/schema/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
/**
* \file fable/schema/vector.hpp
* \see fable/schema/xmagic.hpp
* \see fable/schema.hpp
* \see fable/schema_test.cpp
*/
Expand All @@ -42,18 +41,14 @@ class Vector : public Base<Vector<T, P>> {
using Type = std::vector<T>;
using PrototypeSchema = std::remove_cv_t<std::remove_reference_t<P>>;

Vector(Type* ptr, std::string desc);
Vector(Type* ptr, std::string desc) : Vector(ptr, make_prototype<T>(), std::move(desc)) {}

Vector(Type* ptr, PrototypeSchema prototype)
: Base<Vector<T, P>>(JsonType::array), prototype_(std::move(prototype)), ptr_(ptr) {}

Vector(Type* ptr, PrototypeSchema prototype, std::string desc)
: Base<Vector<T, P>>(JsonType::array, std::move(desc)), prototype_(std::move(prototype)), ptr_(ptr) {}

#if 0
// This is defined in: fable/schema/xmagic.hpp
Vector(Type* ptr, std::string desc)
: Vector(ptr, make_prototype<T>(), std::move(desc)) {}
#endif

public: // Specials
/**
* Return whether deserialization extends the underlying array (true) or
Expand Down Expand Up @@ -190,5 +185,10 @@ Vector<T, P> make_schema(std::vector<T>* ptr, P&& prototype, S&& desc) {
return Vector<T, P>(ptr, std::forward<P>(prototype), std::forward<S>(desc));
}

template <typename T, typename S>
Vector<T, decltype(make_prototype<T>())> make_schema(std::vector<T>* ptr, S&& desc) {
return Vector<T, decltype(make_prototype<T>())>(ptr, std::forward<S>(desc));
}

} // namespace schema
} // namespace fable
46 changes: 0 additions & 46 deletions fable/include/fable/schema/xmagic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,52 +52,6 @@
namespace fable {
namespace schema {

template <typename T, typename P>
Vector<T, P>::Vector(std::vector<T>* ptr, std::string desc)
: Vector<T, P>(ptr, make_prototype<T>(), std::move(desc)) {}

template <typename T, typename S>
Vector<T, decltype(make_prototype<T>())> make_schema(std::vector<T>* ptr, S&& desc) {
return Vector<T, decltype(make_prototype<T>())>(ptr, std::forward<S>(desc));
}

template <typename T, size_t N, typename P>
Array<T, N, P>::Array(std::array<T, N>* ptr, std::string desc)
: Array<T, N, P>(ptr, make_prototype<T>(), std::move(desc)) {}

template <typename T, size_t N, typename S>
Array<T, N, decltype(make_prototype<T>())> make_schema(std::array<T, N>* ptr, S&& desc) {
return Array<T, N, decltype(make_prototype<T>())>(ptr, std::forward<S>(desc));
}

template <typename T, typename P>
Const<T, P>::Const(T constant, std::string desc)
: Const<T, P>(constant, make_prototype<T>(), std::move(desc)) {}

template <typename T, typename S>
Const<T, decltype(make_prototype<T>())> make_const_schema(T&& constant, S&& desc) {
return Const<T, decltype(make_prototype<T>())>(std::forward<T>(constant), std::forward<S>(desc));
}

template <typename T, typename P>
Map<T, P>::Map(std::map<std::string, T>* ptr, std::string desc)
: Map<T, P>(ptr, make_prototype<T>(), std::move(desc)) {}

template <typename T, typename S>
Map<T, decltype(make_prototype<T>())> make_schema(std::map<std::string, T>* ptr,
S&& desc) {
return Map<T, decltype(make_prototype<T>())>(ptr, std::forward<S>(desc));
}

template <typename T, typename P>
Optional<T, P>::Optional(T* ptr, std::string desc)
: Optional<T, P>(ptr, make_prototype<typename T::value_type>(), std::move(desc)) {}

template <typename T, typename S, std::enable_if_t<is_optional_v<T>, bool> = true>
Optional<T, decltype(make_prototype<typename T::value_type>())> make_schema(T* ptr, S&& desc) {
return Optional<T, decltype(make_prototype<typename T::value_type>())>(ptr, std::forward<S>(desc));
}

template <typename T, typename S, std::enable_if_t<std::is_base_of_v<Confable, T>, int>>
auto make_prototype(S&& desc) {
return FromConfable<T>(std::forward<S>(desc));
Expand Down
3 changes: 2 additions & 1 deletion fable/src/fable/schema/const_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ namespace {

struct MyConstStruct : public fable::Confable {
CONFABLE_SCHEMA(MyConstStruct) {
using namespace std::literals;
using namespace fable::schema; // NOLINT(build/namespaces)
return Struct{
{"release", make_const_str("2", "constant string").require()},
{"release", make_const_schema("2"s, "constant string").require()},
{"major", Const<int, Number<int>>(2, "constant number")},
};
}
Expand Down
3 changes: 2 additions & 1 deletion fable/src/fable/schema_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ struct MyStruct : public fable::Confable {
TEST(fable_schema, schema_wrapper) {
using namespace fable; // NOLINT(build/namespaces)
using namespace fable::schema; // NOLINT(build/namespaces)
using namespace std::literals; // NOLINT(build/namespaces)

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

auto s1 = Schema{
{"author", make_const_str("me", "author of this code")},
{"author", make_const_schema("me"s, "author of this code")},
{"required", make_schema(&my_required, "my required boolean, should be true").require()},
{"string", Schema(&my_string, "my string")},
{"int", make_schema(&my_int, "my integer").minimum(0)},
Expand Down
1 change: 0 additions & 1 deletion runtime/include/cloe/core/fable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ using fable::Schema;
using fable::SchemaError;

using fable::make_const_schema;
using fable::make_const_str;
using fable::make_prototype;
using fable::make_schema;
using fable::schema_type;
Expand Down

0 comments on commit 7c9b960

Please sign in to comment.