4_function
Store definition
// SPDX-FileCopyrightText: 2020-2023 Jochem Rutgers
//
// SPDX-License-Identifier: CC0-1.0
// All previous examples had normal variables, which do not have side effects
// when read or written. If a side effect is required, one can define a
// function. For this, surround the type with ( and ). Read it as a function
// that has an argument of the given type. Functions can be an array, but
// cannot have an initializer.
// The side effect is to be defined in your application. See main.cpp.
(uint64) time (s) // The number of seconds since the Epoch.
(int32) rand // A pseudo-random number.
(int32)[2] echo // Print every value written to it.
Application
// SPDX-FileCopyrightText: 2020-2023 Jochem Rutgers
//
// SPDX-License-Identifier: CC0-1.0
/*!
* \file
* \brief Example to show how functions work.
*/
#include <stored>
#include "ExampleFunction.h"
#include <cstdio>
#include <cstdlib>
#include <ctime>
#ifdef STORED_COMPILER_MSVC
# pragma warning(disable : 4996)
#endif
// Create a subclass of stored::ExampleFunctionBase to define the side effects of the functions.
class MyExample : public STORE_T(MyExample, stored::ExampleFunctionBase) {
STORE_CLASS(MyExample, stored::ExampleFunctionBase)
public:
MyExample()
: m_echo()
{}
// Override the default functions from base. Even though they are not
// virtual, these are called as expected. To do this, you had to specify
// the class as the template parameter of ExampleFunctionBase :)
// The function gets the parameter set, which indicates if the value is
// written (true) or should be returned (false).
void __time_s(bool set, uint64_t& value)
{
if(set)
return; // read-only
value = (uint64_t)time(nullptr);
}
void __rand(bool set, int32_t& value)
{
if(!set) {
uint32_t a = 48271;
uint32_t m = 2147483647;
static uint32_t seed = 42;
value = (int32_t)(seed = (a * seed) % m);
}
}
void __echo_0(bool set, int32_t& value)
{
__echo(0, set, value);
}
void __echo_1(bool set, int32_t& value)
{
__echo(1, set, value);
}
private:
void __echo(int i, bool set, int32_t& value)
{
assert(i >= 0 && (size_t)i < sizeof(m_echo) / sizeof(m_echo[0]));
if(set)
m_echo[i] = value;
else
value = m_echo[i];
printf("%s echo[%d] = %" PRId32 "\n", set ? "set" : "get", i, value);
}
private:
int32_t m_echo[2];
};
int main()
{
MyExample e;
#if 0
time_t now = (time_t)e.time_s.get();
printf("time = %s\n", ctime(&now));
#endif
printf("rand = %" PRId32 "\n", e.rand.get());
printf("rand = %" PRId32 "\n", e.rand.get());
printf("rand = %" PRId32 "\n", e.rand.get());
e.echo_0.set(10);
e.echo_1.set(11);
printf("echo[0] returned %" PRId32 "\n", e.echo_0.get());
printf("echo[1] returned %" PRId32 "\n", e.find("/echo[1]").function<int32_t>().get());
return 0;
}
Output
rand = 2027382
rand = 1226992363
rand = 549342533
set echo[0] = 10
set echo[1] = 11
get echo[0] = 10
echo[0] returned 10
get echo[1] = 11
echo[1] returned 11
Store reference
-
template<typename Base_, typename Implementation_>
class ExampleFunctionObjects All ExampleFunctionBase’s objects.
Subclassed by stored::ExampleFunctionBase< MyExample >
Public Members
-
impl::StoreFunction<Base, Implementation, ExampleFunctionFunctionMap, 3u> echo_0
echo[0]
-
impl::StoreFunction<Base, Implementation, ExampleFunctionFunctionMap, 4u> echo_1
echo[1]
-
impl::StoreFunction<Base, Implementation, ExampleFunctionFunctionMap, 2u> rand
rand
-
impl::StoreFunction<Base, Implementation, ExampleFunctionFunctionMap, 1u> time_s
time (s)
-
impl::StoreFunction<Base, Implementation, ExampleFunctionFunctionMap, 3u> echo_0
-
template<typename Implementation_>
class ExampleFunctionBase : public stored::ExampleFunctionObjects<ExampleFunctionBase<Implementation_>, Implementation_> Base class with default interface of all ExampleFunction 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::ExampleFunction. 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 ExampleFunction : public stored::store<ExampleFunction, ExampleFunctionBase>::type { STORE_CLASS(ExampleFunction, ExampleFunctionBase) public: // Your class implementation, such as: ExampleFunction() 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
stored::ExampleFunction
See also
stored::ExampleFunctionData
Subclassed by stored::ExampleFunctionDefaultFunctions< ExampleFunctionBase< ExampleFunction > >
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 ExampleFunctionObjects<ExampleFunctionBase, Implementation_> Objects
-
typedef ExampleFunctionBase root
We are the root, as used by
STORE_CLASS
.
-
typedef ExampleFunctionBase self
Define
self
forstored::store
.
Public Functions
-
inline ~ExampleFunctionBase()
-
void __echo_0(bool set, int32_t &value)
Callback for echo[0].
-
void __echo_1(bool set, int32_t &value)
Callback for echo[1].
-
void __rand(bool set, int32_t &value)
Callback for rand.
-
void __time_s(bool set, uint64_t &value)
Callback for time (s)
-
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> echo_a(int a) noexcept
Array-lookup accessor for echo[a].
-
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.
-
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::StoreFunction<Base, Implementation, ExampleFunctionFunctionMap, 3u> echo_0
echo[0]
-
impl::StoreFunction<Base, Implementation, ExampleFunctionFunctionMap, 4u> echo_1
echo[1]
-
impl::StoreFunction<Base, Implementation, ExampleFunctionFunctionMap, 2u> rand
rand
-
impl::StoreFunction<Base, Implementation, ExampleFunctionFunctionMap, 1u> time_s
time (s)
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 MyExample : public stored::ExampleFunctionBase<MyExample>