3_scope
Store definition
// SPDX-FileCopyrightText: 2020-2023 Jochem Rutgers
//
// SPDX-License-Identifier: CC0-1.0
// Scopes are surrounded by { } and have a name. You can see the scope as a
// special type. Within the scope, any variable can exist like at the
// top-level, including subscopes.
{
bool some bool
int8=123 an int
{
bool another bool
} subscope
} scope
// Scopes can be an array too, like all other variables.
{
int8 i
}[2] two scopes
// When accessed variables by string using find(), separate scopes by /, like a
// Unix filesystem. Now the quirky part: a / can also be part of a name, which
// implicitly creates a scope. In the following example, /top/level1/b and
// /top/level1/b2 are normal siblings in the generated code.
{
{
bool b
} level1
bool level1/b2
} top
// If you create the following variable, it will add a scope, but that's fine.
double=9.81 gravitational constant (m/s^2)
Application
// SPDX-FileCopyrightText: 2020-2023 Jochem Rutgers
//
// SPDX-License-Identifier: CC0-1.0
/*!
* \file
* \brief Example to show scoping in the store's objects.
*/
#include "ExampleScope.h"
#include <cstdio>
int main()
{
stored::ExampleScope e;
// Notice how the scope separator gets replaced by two underscores.
printf("/scope/an int by API: %" PRId8 "\n", e.scope__an_int.get());
printf("/scope/an int by find(): %" PRId8 "\n",
e.find("/scope/an int").variable<int8_t>().get());
printf("G = %g\n", e.gravitational_constant_m__s_2.get());
return 0;
}
Output
/scope/an int by API: 123
/scope/an int by find(): 123
G = 9.81
Store reference
-
template<typename Base_, typename Implementation_>
class ExampleScopeObjects All ExampleScopeBase’s objects.
Public Members
-
impl::StoreVariable<Base, Implementation, double, 0u, 8> gravitational_constant_m__s_2
gravitational constant (m/s^2)
-
impl::StoreVariable<Base, Implementation, int8_t, 8u, 1> scope__an_int
scope/an int
-
impl::StoreVariable<Base, Implementation, bool, 16u, 1> scope__some_bool
scope/some bool
-
impl::StoreVariable<Base, Implementation, bool, 17u, 1> scope__subscope__another_bool
scope/subscope/another bool
-
impl::StoreVariable<Base, Implementation, bool, 20u, 1> top__level1__b
top/level1/b
-
impl::StoreVariable<Base, Implementation, bool, 21u, 1> top__level1__b2
top/level1/b2
-
impl::StoreVariable<Base, Implementation, int8_t, 18u, 1> two_scopes_0__i
two scopes[0]/i
-
impl::StoreVariable<Base, Implementation, int8_t, 19u, 1> two_scopes_1__i
two scopes[1]/i
-
impl::StoreVariable<Base, Implementation, double, 0u, 8> gravitational_constant_m__s_2
-
template<typename Implementation_>
class ExampleScopeBase : public stored::ExampleScopeObjects<ExampleScopeBase<Implementation_>, Implementation_> Base class with default interface of all ExampleScope 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::ExampleScope. 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 ExampleScope : public stored::store<ExampleScope, ExampleScopeBase>::type { STORE_CLASS(ExampleScope, ExampleScopeBase) public: // Your class implementation, such as: ExampleScope() 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::ExampleScopeData
Subclassed by stored::ExampleScopeDefaultFunctions< ExampleScopeBase< ExampleScope > >
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 ExampleScopeObjects<ExampleScopeBase, Implementation_> Objects
-
typedef ExampleScopeBase root
We are the root, as used by
STORE_CLASS
.
-
typedef ExampleScopeBase self
Define
self
forstored::store
.
Public Functions
-
inline ~ExampleScopeBase()
-
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.
-
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> two_scopes_a__i(int a) noexcept
Array-lookup accessor for two scopes[a]/i.
-
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, double, 0u, 8> gravitational_constant_m__s_2
gravitational constant (m/s^2)
-
impl::StoreVariable<Base, Implementation, int8_t, 8u, 1> scope__an_int
scope/an int
-
impl::StoreVariable<Base, Implementation, bool, 16u, 1> scope__some_bool
scope/some bool
-
impl::StoreVariable<Base, Implementation, bool, 17u, 1> scope__subscope__another_bool
scope/subscope/another bool
-
impl::StoreVariable<Base, Implementation, bool, 20u, 1> top__level1__b
top/level1/b
-
impl::StoreVariable<Base, Implementation, bool, 21u, 1> top__level1__b2
top/level1/b2
-
impl::StoreVariable<Base, Implementation, int8_t, 18u, 1> two_scopes_0__i
two scopes[0]/i
-
impl::StoreVariable<Base, Implementation, int8_t, 19u, 1> two_scopes_1__i
two scopes[1]/i
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 ExampleScope : public stored::ExampleScopeDefaultFunctions<ExampleScopeBase<ExampleScope>>
Default ExampleScopeBase implementation.
Public Functions
-
ExampleScope() = default
Default constructor.
-
ExampleScope() = default