$darkmode
fable::schema::Array< T, N, P > Class Template Reference

#include <array.hpp>

Inheritance diagram for fable::schema::Array< T, N, P >:
Collaboration diagram for fable::schema::Array< T, N, P >:

Public Types

using Type = std::array< T, N >
 
using PrototypeSchema = P
 

Public Member Functions

 Array (Type *ptr, std::string desc)
 
 Array (Type *ptr, PrototypeSchema prototype)
 
 Array (Type *ptr, PrototypeSchema prototype, std::string desc)
 
bool require_all () const
 
void set_require_all (bool value)
 
Array< T, N, P > require_all (bool value) &&
 
std::string type_string () const override
 
Json json_schema () const override
 
bool validate (const Conf &c, std::optional< SchemaError > &err) const override
 
void to_json (Json &j) const override
 
void from_conf (const Conf &c) override
 
Json serialize (const Type &xs) const
 
void serialize_into (Json &j, const Type &v) const
 
Type deserialize (const Conf &c) const
 
void deserialize_into (const Conf &c, Type &v) const
 
void reset_ptr () override
 
virtual Json to_json () const
 
virtual void to_json (Json &) const=0
 
- Public Member Functions inherited from fable::schema::Base< Array< T, N, P > >
std::unique_ptr< Interfaceclone () const override
 
 operator Box () const
 
JsonType type () const override
 
std::string type_string () const override
 
Json usage () const override
 
bool is_required () const override
 
Array< T, N, P > require () &&
 
Array< T, N, P > required (bool value) &&
 
Array< T, N, P > reset_pointer () &&
 
bool has_description () const
 
void set_description (std::string s) override
 
const std::string & description () const override
 
Array< T, N, P > description (std::string desc) &&
 
- Public Member Functions inherited from fable::schema::Interface
virtual bool is_variant () const
 
virtual void validate_or_throw (const Conf &c) const final
 
virtual std::optional< SchemaErrorfail (const Conf &c) const final
 
virtual bool is_valid (const Conf &c) const final
 
virtual Json to_json () const
 

Additional Inherited Members

- Protected Member Functions inherited from fable::schema::Base< Array< T, N, P > >
 Base (const Base< Array< T, N, P > > &)=default
 
 Base (Base< Array< T, N, P > > &&) noexcept=default
 
 Base (JsonType t, std::string desc)
 
 Base (JsonType t)
 
 Base (std::string desc)
 
Base< Array< T, N, P > > & operator= (const Base< Array< T, N, P > > &)=default
 
Base< Array< T, N, P > > & operator= (Base< Array< T, N, P > > &&) noexcept=default
 
bool validate_type (const Conf &c, std::optional< SchemaError > &err) const
 
SchemaError error (const Conf &c, std::string_view format, Args &&... args) const
 
SchemaError error (const ConfError &e) const
 
SchemaError wrong_type (const Conf &c) const
 
bool set_error (std::optional< SchemaError > &err, const Conf &c, std::string_view format, Args &&... args) const
 
bool set_error (std::optional< SchemaError > &err, const ConfError &e) const
 
bool set_error (std::optional< SchemaError > &err, SchemaError &&e) const
 
bool set_wrong_type (std::optional< SchemaError > &err, const Conf &c) const
 
void augment_schema (Json &j) const
 
- Protected Member Functions inherited from fable::schema::Interface
 Interface (const Interface &)=default
 
 Interface (Interface &&) noexcept=default
 
Interfaceoperator= (const Interface &)=default
 
Interfaceoperator= (Interface &&) noexcept=default
 
- Protected Attributes inherited from fable::schema::Base< Array< T, N, P > >
JsonType type_
 
bool required_
 
std::string desc_
 

Detailed Description

template<typename T, size_t N, typename P>
class fable::schema::Array< T, N, P >

The Array schema provides support for using the std::array<> schema.

Because arrays are fixed size, there are two approaches to setting data in them:

  1. Setting the entire array, e.g. for arrays representing object vectors.
  2. Setting individual elements in array, e.g. for setting a boolean in a bit field.

Example:

using Vec3d = std::array<double, 3>;
Vec3d target;
auto sma = fable::make_schema(&target, "position in 3d space");

This will serialize to a JSON array:

[ 0.0, 0.0, 0.0 ]

And it will deserialize from a JSON array:

[ 45.0, 22.0, 0.0 ]

If you want to set the z-axis, you can use the object deserialization:

{ "2": 5.0 }

Note that, because we are using a JSON object, we need to specify the index as string. The first index is "0" and the last is "N-1", where N is the size of the array.

Also note that setting a single element in the array does not modify any of the other fields in the array, since the array exists outside of this schema. In that regard, you are responsible for initializing your array.

C Arrays

If you have control over the definition of the variable, please use std::array<>. Otherwise it is possible (though potentially undefined behavior) to re-interpret the array as a std::array<>, since memory layout is identical.

For example:

int c_array[] = {0, 0, 0};
auto array_p = reinterpret_cast<std::array<int, 3>*>(c_array);
static_assert(sizeof(c_array) == sizeof(array_p));
auto schema = fable::make_schema(
  reintpret_cast<std::array<int, 3>*>(c_array),
  "schema description"
);

Any linter will probably tell you not to do this, so proceed at your own risk.

See also
https://stackoverflow.com/questions/11205186/treat-c-cstyle-array-as-stdarray

Member Function Documentation

◆ deserialize()

template<typename T , size_t N, typename P >
Type fable::schema::Array< T, N, P >::deserialize ( const Conf c) const
inline

Deserialize the Conf into a new object.

Because it's not pre-existing, we can't guarantee that it will be initialized and therefore only support setting the full array.

◆ from_conf()

template<typename T , size_t N, typename P >
void fable::schema::Array< T, N, P >::from_conf ( const Conf )
inlineoverridevirtual

Apply the input JSON configuration.

This does not validate the input.

Implements fable::schema::Interface.

◆ json_schema()

template<typename T , size_t N, typename P >
Json fable::schema::Array< T, N, P >::json_schema ( ) const
inlineoverridevirtual

Return the JSON schema.

Example output:

{ "$schema": "http://json-schema.org/draft-07/schema#", "description": "stand-in no-operation simulator", "properties": { "vehicles": { "description": "list of vehicle names to make available", "items": { "type": "string" }, "type": "array" } }, "title": "nop", "additionalProperties": false, "type": "object" }

See the following links for the specification:

Implements fable::schema::Interface.

◆ require_all() [1/2]

template<typename T , size_t N, typename P >
bool fable::schema::Array< T, N, P >::require_all ( ) const
inline

Return whether deserialization must set all fields, in which case only the full array syntax is supported.

By default, this is false.

◆ require_all() [2/2]

template<typename T , size_t N, typename P >
Array<T, N, P> fable::schema::Array< T, N, P >::require_all ( bool  value) &&
inline

Set whether deserialization requires setting the entire array.

See also
set_require_all(bool)
Here is the call graph for this function:

◆ reset_ptr()

template<typename T , size_t N, typename P >
void fable::schema::Array< T, N, P >::reset_ptr ( )
inlineoverridevirtual

Reset the internal pointer to nullptr, protecting against invalid access.

This should be used when a schema is used after the backing data has been deleted.

Implements fable::schema::Interface.

◆ serialize_into()

template<typename T , size_t N, typename P >
void fable::schema::Array< T, N, P >::serialize_into ( Json j,
const Type &  v 
) const
inline

Serialize contents of v into j.

◆ set_require_all()

template<typename T , size_t N, typename P >
void fable::schema::Array< T, N, P >::set_require_all ( bool  value)
inline

Set whether deserialization requires setting the entire array.

If set to true, then only the array syntax is supported:

[ 1, 3, 4]

When set to false, then the object syntax is also supported:

{ "1": 3 }

The object syntax only sets the specified elements and leaves the others untouched.

Whether you choose one or the other depends on the semantics of your data.

Here is the caller graph for this function:

◆ to_json() [1/3]

template<typename T , size_t N, typename P >
virtual Json fable::schema::Interface::to_json
inline

Return the current value of the destination.

Warning: This is NOT an efficient operation, but it can be useful for cases where speed is not important.

◆ to_json() [2/3]

template<typename T , size_t N, typename P >
virtual void fable::schema::Interface::to_json

Return the current value of the destination.

Warning: This is NOT an efficient operation, but it can be useful for cases where speed is not important.

◆ to_json() [3/3]

template<typename T , size_t N, typename P >
void fable::schema::Array< T, N, P >::to_json ( Json ) const
inlineoverridevirtual

Return the current value of the destination.

Warning: This is NOT an efficient operation, but it can be useful for cases where speed is not important.

Implements fable::schema::Interface.

◆ type_string()

template<typename T , size_t N, typename P >
std::string fable::schema::Array< T, N, P >::type_string ( ) const
inlineoverridevirtual

Return the type as a string.

The format of the string is:

"[array of] TYPE"

where TYPE is one of: null, object, boolean, float, integer, unsigned, string, and unknown.

Example output:

"object"
"array of boolean"
"array of object"

Implements fable::schema::Interface.

◆ validate()

template<typename T , size_t N, typename P >
bool fable::schema::Array< T, N, P >::validate ( const Conf c,
std::optional< SchemaError > &  error 
) const
inlineoverridevirtual

Validate the input JSON configuration for correctness.

  • This method should only set error if there is an error. This method should not reset error if there is no error. Therefore, the content of error is only valid if the method returns false. This allows you to chain validates and check at the end if there was an error.
  • This method should not throw if there is a schema error!
Parameters
cJSON to check
errorreference to store error if occurred
Returns
true if valid

Implements fable::schema::Interface.

Here is the call graph for this function:

The documentation for this class was generated from the following file: