2_basic
Store definition
// SPDX-FileCopyrightText: 2020-2023 Jochem Rutgers
//
// SPDX-License-Identifier: CC0-1.0
// Did you enable syntax highlighting in Vim? See scripts/st.vim.
// The following types are supported:
bool b
int8 i8
uint8 u8
int16 i16
uint16 u16
int32 i32
uint32 u32
int64 i64
uint64 u64
float f
double d
// A string is a UTF-8 string. Indicate the length of the string in bytes,
// excluding the \0 terminator, which is added implicitly.
string:4 s
// A blob is just a piece of memory. You can use it for any other buffer.
// Indicate the size of the blob in bytes.
blob:16 blob
// To override the default-initialized value (which is zero), specify it like
// this:
int8=42 test42
// An initializer can be an int, as shown above, but also hex (0x12ab), binary
// (0b01101), and float (1.4, -3.2e-4, inf, nan). For a string, wrap it in
// double quotes, and escape quotes appropriately. Escaping works like python
// strings.
string:8="hello" string hello
// Initializers don't work on a blob, though.
// Note that there should not be any whitespace around the =, because after
// from the first whitespace, the name is taken. So, this is not what you would
// expect, although the syntax highlighting might give you a hint:
int8 = 42 test42
// If you make this error a lot, you may prefer writing the name on the next
// line like this:
int8=42
another 42
// The name of a variable consists of ASCII 0x20 (space) till 0x7e (~). Spaces
// at the start and end of the named are stripped off, and consecutive spaces
// inside the name are merged. Otherwise, the name is as you type.
bool=true So, this is a perfectly fine name, even if it includes characters like ~!@#$%; and so on.
// In the C++ interface, all invalid characters are mapped and merged to _.
// Check the generated Example.h!
// Every type can be an array by adding [size]:
int8[4] four ints
int16[3]=2 three ints // All initialized to two.
string:4[2] two strings
// The generated store does not have arrays, however. The generator will expand
// the array by adding [n] to the name. So, this is identical as the arrays
// above:
//int8 four ints[0]
//int8 four ints[1]
//int8 four ints[2]
//int8 four ints[3]
//int16=2 three ints[0]
//int16=2 three ints[1]
//int16=2 three ints[2]
//string:4 two strings[0]
//string:4 two strings[1]
Application
// SPDX-FileCopyrightText: 2020-2023 Jochem Rutgers
//
// SPDX-License-Identifier: CC0-1.0
/*!
* \file
* \brief Basic variable example.
*/
#include "ExampleBasic.h"
#include <cstdio>
int main()
{
stored::ExampleBasic e;
// Initialized value.
printf("test42=%" PRId8 "\n", e.test42.get());
// The thing with the space around the =
printf("_42_test42=%" PRId8 "\n", e._42_test42.get());
// The initialized array element.
printf("three ints[1]=%" PRId16 "\n", e.three_ints_1.get());
return 0;
}
Output
test42=42
_42_test42=0
three ints[1]=2
Store reference
-
template<typename Base_, typename Implementation_>
class ExampleBasicObjects All ExampleBasicBase’s objects.
Public Members
-
impl::StoreVariable<Base, Implementation, int8_t, 115u, 1> _42_test42
= 42 test42
-
impl::StoreVariable<Base, Implementation, int8_t, 23u, 1> another_42
another 42
-
impl::StoreVariable<Base, Implementation, bool, 112u, 1> b
b
-
impl::StoreVariantV<Base, Implementation, Type::Blob, 32u, 16u> blob
blob
-
impl::StoreVariable<Base, Implementation, double, 64u, 8> d
d
-
impl::StoreVariable<Base, Implementation, float, 80u, 4> f
f
-
impl::StoreVariable<Base, Implementation, int8_t, 116u, 1> four_ints_0
four ints[0]
-
impl::StoreVariable<Base, Implementation, int8_t, 117u, 1> four_ints_1
four ints[1]
-
impl::StoreVariable<Base, Implementation, int8_t, 118u, 1> four_ints_2
four ints[2]
-
impl::StoreVariable<Base, Implementation, int8_t, 119u, 1> four_ints_3
four ints[3]
-
impl::StoreVariable<Base, Implementation, int16_t, 108u, 2> i16
i16
-
impl::StoreVariable<Base, Implementation, int32_t, 72u, 4> i32
i32
-
impl::StoreVariable<Base, Implementation, int64_t, 48u, 8> i64
i64
-
impl::StoreVariable<Base, Implementation, int8_t, 113u, 1> i8
i8
-
impl::StoreVariantV<Base, Implementation, Type::String, 84u, 4u> s
s
-
impl::StoreVariable<Base, Implementation, bool, 24u, 1> So_this_is_a_perfectly_fine_name_even_if_it_includes_characters_like_and_so_on
So, this is a perfectly fine name, even if it includes characters like ~!#$%; and so on.
-
impl::StoreVariantV<Base, Implementation, Type::String, 0u, 8u> string_hello
string hello
-
impl::StoreVariable<Base, Implementation, int8_t, 22u, 1> test42
test42
-
impl::StoreVariable<Base, Implementation, int16_t, 16u, 2> three_ints_0
three ints[0]
-
impl::StoreVariable<Base, Implementation, int16_t, 18u, 2> three_ints_1
three ints[1]
-
impl::StoreVariable<Base, Implementation, int16_t, 20u, 2> three_ints_2
three ints[2]
-
impl::StoreVariantV<Base, Implementation, Type::String, 92u, 4u> two_strings_0
two strings[0]
-
impl::StoreVariantV<Base, Implementation, Type::String, 100u, 4u> two_strings_1
two strings[1]
-
impl::StoreVariable<Base, Implementation, uint16_t, 110u, 2> u16
u16
-
impl::StoreVariable<Base, Implementation, uint32_t, 76u, 4> u32
u32
-
impl::StoreVariable<Base, Implementation, uint64_t, 56u, 8> u64
u64
-
impl::StoreVariable<Base, Implementation, uint8_t, 114u, 1> u8
u8
-
impl::StoreVariable<Base, Implementation, int8_t, 115u, 1> _42_test42
-
template<typename Implementation_>
class ExampleBasicBase : public stored::ExampleBasicObjects<ExampleBasicBase<Implementation_>, Implementation_> Base class with default interface of all ExampleBasic implementations.
Although there are no virtual functions in the base class, subclasses can override them. The (lowest) subclass must pass the
Implementation_
template paramater to its base, such that all calls from the base class can be directed to the proper overridden implementation.The base class cannot be instantiated. If a default implementation is required, which does not have side effects to functions, instantiate stored::ExampleBasic. This class contains all data of all variables, so it can be large. So, be aware when instantiating it on the stack. Heap is fine. Static allocations is fine too, as the constructor and destructor are trivial.
To inherit the base class, you can use the following template:
class ExampleBasic : public stored::store<ExampleBasic, ExampleBasicBase>::type { STORE_CLASS(ExampleBasic, ExampleBasicBase) public: // Your class implementation, such as: ExampleBasic() is_default // ... };
Some compilers or tools may get confused by the inheritance using
stored::store
orstored::store_t
. Alternatively, useSTORE_T(...)
instead, providing the template parameters ofstored::store
as macro arguments.See also
See also
stored::ExampleBasicData
Subclassed by stored::ExampleBasicDefaultFunctions< ExampleBasicBase< ExampleBasic > >
Public Types
-
enum [anonymous]
Values:
-
enumerator ObjectCount
Number of objects in the store.
-
enumerator VariableCount
Number of variables in the store.
-
enumerator FunctionCount
Number of functions in the store.
-
enumerator BufferSize
Buffer size.
-
enumerator ObjectCount
-
typedef Implementation_ Implementation
Type of the actual implementation, which is the (lowest) subclass.
-
typedef uintptr_t Key
Type of a key.
See also
-
typedef Map<String::type, Variant<Implementation>>::type ObjectMap
Map as generated by map().
-
typedef ExampleBasicObjects<ExampleBasicBase, Implementation_> Objects
-
typedef ExampleBasicBase root
We are the root, as used by
STORE_CLASS
.
-
typedef ExampleBasicBase self
Define
self
forstored::store
.
Public Functions
-
inline ~ExampleBasicBase()
-
inline Key bufferToKey(void const *buffer) const noexcept
Converts a variable’s buffer to a key.
A key is unique for all variables of the same store, but identical for the same variables across different instances of the same store class. Therefore, the key can be used to synchronize between instances of the same store. A key does not contain meta data, such as type or length. It is up to the synchronization library to make sure that these properties are handled well.
For synchronization, when hookEntryX() or hookEntryRO() is invoked, one can compute the key of the object that is accessed. The key can be used, for example, in a key-to-Variant map. When data arrives from another party, the key can be used to find the proper Variant in the map.
This way, data exchange is type-safe, as the Variant can check if the data format matches the expected type. However, one cannot process data if the key is not set yet in the map.
-
inline Type::type bufferToType(void const *buffer) noexcept
Return the type of the variable, given its buffer.
-
inline Variant<Implementation> find(char const *name, size_t len = std::numeric_limits<size_t>::max()) noexcept
Finds an object with the given name.
- Returns:
the object, or an invalid stored::Variant if not found.
-
inline Variant<Implementation> four_ints_a(int a) noexcept
Array-lookup accessor for four ints[a].
-
template<typename T>
inline Function<T, Implementation> function(char const *name, size_t len = std::numeric_limits<size_t>::max()) noexcept Finds a function with the given name.
The function, when it exists, must have the given (fixed) type.
-
inline Implementation const &implementation() const noexcept
Returns the reference to the implementation.
-
inline Implementation &implementation() noexcept
Returns the reference to the implementation.
-
template<typename F>
inline void list(F &&f) noexcept Calls a callback for every object in the longDirectory().
See also
-
template<typename F>
inline void list(F f, void *arg, char const *prefix, String::type *nameBuffer) noexcept Calls a callback for every object in the longDirectory().
See also
-
template<typename F>
inline void list(F f, void *arg, char const *prefix = nullptr) noexcept Calls a callback for every object in the longDirectory().
See also
-
inline uint8_t const *longDirectory() const noexcept
Retuns the long directory.
When not available, the short directory is returned.
-
inline ObjectMap map(char const *prefix = nullptr)
Create a name to Variant map for the store.
Generating the map may be expensive and the result is not cached.
-
inline char const *name() const noexcept
Returns the name of store, which can be used as prefix for stored::Debugger.
-
inline uint8_t const *shortDirectory() const noexcept
Returns the short directory.
-
inline Variant<Implementation> three_ints_a(int a) noexcept
Array-lookup accessor for three ints[a].
-
inline Variant<Implementation> two_strings_a(int a) noexcept
Array-lookup accessor for two strings[a].
-
template<typename T>
inline Variable<T, Implementation> variable(char const *name, size_t len = std::numeric_limits<size_t>::max()) noexcept Finds a variable with the given name.
The variable, when it exists, must have the given (fixed) type.
Public Members
-
impl::StoreVariable<Base, Implementation, int8_t, 115u, 1> _42_test42
= 42 test42
-
impl::StoreVariable<Base, Implementation, int8_t, 23u, 1> another_42
another 42
-
impl::StoreVariable<Base, Implementation, bool, 112u, 1> b
b
-
impl::StoreVariantV<Base, Implementation, Type::Blob, 32u, 16u> blob
blob
-
impl::StoreVariable<Base, Implementation, double, 64u, 8> d
d
-
impl::StoreVariable<Base, Implementation, float, 80u, 4> f
f
-
impl::StoreVariable<Base, Implementation, int8_t, 116u, 1> four_ints_0
four ints[0]
-
impl::StoreVariable<Base, Implementation, int8_t, 117u, 1> four_ints_1
four ints[1]
-
impl::StoreVariable<Base, Implementation, int8_t, 118u, 1> four_ints_2
four ints[2]
-
impl::StoreVariable<Base, Implementation, int8_t, 119u, 1> four_ints_3
four ints[3]
-
impl::StoreVariable<Base, Implementation, int16_t, 108u, 2> i16
i16
-
impl::StoreVariable<Base, Implementation, int32_t, 72u, 4> i32
i32
-
impl::StoreVariable<Base, Implementation, int64_t, 48u, 8> i64
i64
-
impl::StoreVariable<Base, Implementation, int8_t, 113u, 1> i8
i8
-
impl::StoreVariantV<Base, Implementation, Type::String, 84u, 4u> s
s
-
impl::StoreVariable<Base, Implementation, bool, 24u, 1> So_this_is_a_perfectly_fine_name_even_if_it_includes_characters_like_and_so_on
So, this is a perfectly fine name, even if it includes characters like ~!#$%; and so on.
-
impl::StoreVariantV<Base, Implementation, Type::String, 0u, 8u> string_hello
string hello
-
impl::StoreVariable<Base, Implementation, int8_t, 22u, 1> test42
test42
-
impl::StoreVariable<Base, Implementation, int16_t, 16u, 2> three_ints_0
three ints[0]
-
impl::StoreVariable<Base, Implementation, int16_t, 18u, 2> three_ints_1
three ints[1]
-
impl::StoreVariable<Base, Implementation, int16_t, 20u, 2> three_ints_2
three ints[2]
-
impl::StoreVariantV<Base, Implementation, Type::String, 92u, 4u> two_strings_0
two strings[0]
-
impl::StoreVariantV<Base, Implementation, Type::String, 100u, 4u> two_strings_1
two strings[1]
-
impl::StoreVariable<Base, Implementation, uint16_t, 110u, 2> u16
u16
-
impl::StoreVariable<Base, Implementation, uint32_t, 76u, 4> u32
u32
-
impl::StoreVariable<Base, Implementation, uint64_t, 56u, 8> u64
u64
-
impl::StoreVariable<Base, Implementation, uint8_t, 114u, 1> u8
u8
Public Static Functions
-
template<typename T>
static inline constexpr FreeFunction<T, Implementation> freeFunction(char const *name, size_t len = std::numeric_limits<size_t>::max()) noexcept Finds a function with the given name.
The function, when it exists, must have the given (fixed) type. It is returned as a free function; it is not bound yet to a specific store instance. This function is constexpr for C++14.
-
template<typename T>
static inline constexpr FreeVariable<T, Implementation> freeVariable(char const *name, size_t len = std::numeric_limits<size_t>::max()) noexcept Finds a variable with the given name.
The variable, when it exists, must have the given (fixed) type. It is returned as a free variable; it is not bound yet to a specific store instance. This function is constexpr for C++14.
-
static inline constexpr char const *hash() noexcept
Returns a unique hash of the store.
Friends
- friend class impl::StoreFunction
- friend class impl::StoreVariable
- friend class impl::StoreVariantF
- friend class impl::StoreVariantV
- friend class stored::FreeVariable
- friend class stored::Variant< void >
-
enum [anonymous]
-
class ExampleBasic : public stored::ExampleBasicDefaultFunctions<ExampleBasicBase<ExampleBasic>>
Default ExampleBasicBase implementation.
Public Functions
-
ExampleBasic() = default
Default constructor.
-
ExampleBasic() = default