Friendly LWM2M client
wpp::Resource Class Reference

The Resource class in the wpp namespace is a comprehensive and flexible class designed to handle different types of data within a resource-oriented architecture. It offers a range of functionalities including storing, retrieving, and validating various data types, as well as managing multiple instances of a resource. More...

#include <Resource.h>

+ Collaboration diagram for wpp::Resource:

Classes

struct  ResInst
 Type for resource instance. More...
 

Public Types

using DATA_T = std::variant< BOOL_T, INT_T, UINT_T, FLOAT_T, OPAQUE_T, OBJ_LINK_T, STRING_T, EXECUTE_T >
 Universal type for data. More...
 
using DATA_VERIFIER_T = std::variant< VERIFY_INT_T, VERIFY_UINT_T, VERIFY_FLOAT_T, VERIFY_OPAQUE_T, VERIFY_BOOL_T, VERIFY_OBJ_LINK_T, VERIFY_STRING_T, VERIFY_EXECUTE_T >
 Universal type for data validation functions. More...
 

Public Member Functions

 Resource ()
 
 Resource (ID_T id, const ItemOp &operation, IS_SINGLE isSingle, IS_MANDATORY isMandatory, TYPE_ID dataType)
 
 Resource (const Resource &resource)
 
 Resource (Resource &&resource)
 
Resourceoperator= (const Resource &other)
 
Resourceoperator= (Resource &&other)
 
ID_T getId () const
 
TYPE_ID getTypeId () const
 
const ItemOpgetOperation () const
 
bool isMandatory () const
 
bool isOptional () const
 
bool isSingle () const
 
bool isMultiple () const
 
template<typename T >
bool isDataValueValid (const T &data) const
 Check if the data type and value are valid. More...
 
bool isExist (ID_T resInstId) const
 Check if the instance ID is exist. More...
 
size_t instCount () const
 Get the number of resource instances. More...
 
std::vector< ID_TinstIds () const
 Returns vector with available ids of resource instances. More...
 
ID_T newInstId () const
 Find first available instance ID that is not used. More...
 
bool setDataVerifier (const DATA_VERIFIER_T &verifier)
 Set data verifier for the resource. More...
 
template<typename T >
bool set (const T &value, ID_T resInstId=SINGLE_INSTANCE_ID)
 Set data value by copy for the resource (instance) More...
 
template<typename T >
bool set (T &&value, ID_T resInstId=SINGLE_INSTANCE_ID)
 Set data value by move for the resource (instance) More...
 
template<typename T >
const T & get (ID_T resInstId=SINGLE_INSTANCE_ID)
 
template<typename T >
bool add (const T &value)
 Add new instance with data value by copy for the MULTIPLE resource. More...
 
template<typename T >
bool add (T &&value)
 Add new instance with data value by move for the MULTIPLE resource. More...
 
bool remove (ID_T resInstId)
 Remove resource instance if resource is MULTIPLE and instance exists, if the resource is SINGLE remove is not possible. More...
 
bool clear ()
 Remove all instances. More...
 

Detailed Description

The Resource class in the wpp namespace is a comprehensive and flexible class designed to handle different types of data within a resource-oriented architecture. It offers a range of functionalities including storing, retrieving, and validating various data types, as well as managing multiple instances of a resource.

This class provides methods for managing and manipulating a resource, including getting and setting resource metadata, checking resource parameters, managing resource data, and managing verifiers for the resource data.

The Resource class contains public subtypes, such as the DATA_T type for storing data of various types, the ResInst struct for representing a resource instance, and the DATA_VERIFIER_T type for storing data validation functions.

The Resource class also provides public methods for common usage, such as constructors, assignment operators, and methods for retrieving resource metadata.

Additionally, the Resource class provides helpful methods for checking resource parameters, managing resource data, and managing verifiers. These methods include checking the validity of the data type, checking the validity of a data value, checking the validity of a data verifier, checking the validity of an operation, checking the possibility of an instance ID, checking the existence of an instance, checking the compatibility of a type ID, checking if the resource is empty, retrieving the number of resource instances, and getting the available IDs of resource instances.

The Resource class also provides methods for managing resource data, including generating prototypes for getting and setting data of various supported types, disabling implicit conversions, setting data values, getting data values, and getting pointers to data values.

Furthermore, the Resource class provides methods for removing resource instances, clearing all instances, and setting the data verifier for the resource.

Definition at line 49 of file Resource.h.

Member Typedef Documentation

◆ DATA_T

Universal type for data.

Definition at line 54 of file Resource.h.

◆ DATA_VERIFIER_T

Universal type for data validation functions.

Definition at line 67 of file Resource.h.

Constructor & Destructor Documentation

◆ Resource() [1/4]

wpp::Resource::Resource ( )

Definition at line 14 of file Resource.cpp.

◆ Resource() [2/4]

wpp::Resource::Resource ( ID_T  id,
const ItemOp operation,
IS_SINGLE  isSingle,
IS_MANDATORY  isMandatory,
TYPE_ID  dataType 
)

Definition at line 18 of file Resource.cpp.

◆ Resource() [3/4]

wpp::Resource::Resource ( const Resource resource)

Definition at line 22 of file Resource.cpp.

◆ Resource() [4/4]

wpp::Resource::Resource ( Resource &&  resource)

Definition at line 32 of file Resource.cpp.

Member Function Documentation

◆ add() [1/2]

template<typename T >
bool wpp::Resource::add ( const T &  value)

Add new instance with data value by copy for the MULTIPLE resource.

Parameters
valueThe data value to add
Note
If the resource is SINGLE, the method will return false. If the verifier is set, the value will be checked. If the data type is not valid, the method will return false. Instance ID will be generated automatically.The ID is determined according to the following algorithm: if the ID is equal to the number of free instances, then we return it, otherwise, starting with ID 0, we search for the first free index, if no free indexes are found, then method returns false.
Returns
True if the instance is added, false otherwise

Definition at line 306 of file Resource.h.

+ Here is the call graph for this function:

◆ add() [2/2]

template<typename T >
bool wpp::Resource::add ( T &&  value)

Add new instance with data value by move for the MULTIPLE resource.

Parameters
valueThe data value to add
Note
If the resource is SINGLE, the method will return false. If the verifier is set, the value will be checked. If the data type is not valid, the method will return false. Instance ID will be generated automatically.The ID is determined according to the following algorithm: if the ID is equal to the number of free instances, then we return it, otherwise, starting with ID 0, we search for the first free index, if no free indexes are found, then method returns false.
Returns
True if the instance is added, false otherwise

Definition at line 327 of file Resource.h.

+ Here is the call graph for this function:

◆ clear()

bool wpp::Resource::clear ( )

Remove all instances.

Note
If the resource is SINGLE, the method will return false.
Returns
True if all instances are removed, false otherwise.

Definition at line 155 of file Resource.cpp.

+ Here is the call graph for this function:

◆ get()

template<typename T >
const T & wpp::Resource::get ( ID_T  resInstId = SINGLE_INSTANCE_ID)

Definition at line 290 of file Resource.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getId()

ID_T wpp::Resource::getId ( ) const

Definition at line 72 of file Resource.cpp.

+ Here is the caller graph for this function:

◆ getOperation()

const ItemOp & wpp::Resource::getOperation ( ) const

Definition at line 80 of file Resource.cpp.

◆ getTypeId()

TYPE_ID wpp::Resource::getTypeId ( ) const

Definition at line 76 of file Resource.cpp.

◆ instCount()

size_t wpp::Resource::instCount ( ) const

Get the number of resource instances.

Note
If the resource is SINGLE, the number of instances is always 1
Returns
The number of resource instances

Definition at line 123 of file Resource.cpp.

◆ instIds()

std::vector< ID_T > wpp::Resource::instIds ( ) const

Returns vector with available ids of resource instances.

Note
If the resource is SINGLE, the vector will be contain only one element with value SINGLE_INSTANCE_ID

Definition at line 127 of file Resource.cpp.

◆ isDataValueValid()

template<typename T >
bool wpp::Resource::isDataValueValid ( const T &  data) const

Check if the data type and value are valid.

Parameters
dataThe data value to check
Note
Type of data verifies according to the type of the resource, value verifies according to the verifier of the resource that can be set through the setDataVerifier method
Returns
True if the data type and value are valid, false otherwise

Definition at line 349 of file Resource.h.

+ Here is the caller graph for this function:

◆ isExist()

bool wpp::Resource::isExist ( ID_T  resInstId) const

Check if the instance ID is exist.

Parameters
resInstIdThe instance ID to check
Note
If the resource is SINGLE, the instance ID always SINGLE_INSTANCE_ID
Returns
True if the instance ID is exist, false otherwise

Definition at line 104 of file Resource.cpp.

+ Here is the caller graph for this function:

◆ isMandatory()

bool wpp::Resource::isMandatory ( ) const

Definition at line 84 of file Resource.cpp.

◆ isMultiple()

bool wpp::Resource::isMultiple ( ) const

Definition at line 96 of file Resource.cpp.

◆ isOptional()

bool wpp::Resource::isOptional ( ) const

Definition at line 88 of file Resource.cpp.

◆ isSingle()

bool wpp::Resource::isSingle ( ) const

Definition at line 92 of file Resource.cpp.

+ Here is the caller graph for this function:

◆ newInstId()

ID_T wpp::Resource::newInstId ( ) const

Find first available instance ID that is not used.

Note
If the resource is SINGLE, the method will return ID_T_MAX_VAL
Returns
The first available instance ID else ID_T_MAX_VAL

Definition at line 134 of file Resource.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator=() [1/2]

Resource & wpp::Resource::operator= ( const Resource other)

Definition at line 43 of file Resource.cpp.

◆ operator=() [2/2]

Resource & wpp::Resource::operator= ( Resource &&  other)

Definition at line 57 of file Resource.cpp.

◆ remove()

bool wpp::Resource::remove ( ID_T  resInstId)

Remove resource instance if resource is MULTIPLE and instance exists, if the resource is SINGLE remove is not possible.

Parameters
resInstIdThe instance ID to remove
Returns
True if the instance is removed, false otherwise.

Definition at line 144 of file Resource.cpp.

+ Here is the call graph for this function:

◆ set() [1/2]

template<typename T >
bool wpp::Resource::set ( const T &  value,
ID_T  resInstId = SINGLE_INSTANCE_ID 
)

Set data value by copy for the resource (instance)

Parameters
valueThe data value to set
resInstIdThe instance ID to set the value, used only for multiple resources
Note
If set the verifier then the value will be checked, also before set new value its type will be checked. If resource is SINGLE then resInstId should be set to SINGLE_INSTANCE_ID.
Returns
True if the value is set, false otherwise

Definition at line 253 of file Resource.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set() [2/2]

template<typename T >
bool wpp::Resource::set ( T &&  value,
ID_T  resInstId = SINGLE_INSTANCE_ID 
)

Set data value by move for the resource (instance)

Parameters
valueThe data value to set
resInstIdThe instance ID to set the value, used only for multiple resources
Note
If set the verifier then the value will be checked, also before set new value its type will be checked. If resource is SINGLE then resInstId should be set to SINGLE_INSTANCE_ID.
Returns
True if the value is set, false otherwise

Definition at line 271 of file Resource.h.

+ Here is the call graph for this function:

◆ setDataVerifier()

bool wpp::Resource::setDataVerifier ( const DATA_VERIFIER_T verifier)

Set data verifier for the resource.

Parameters
verifierFunction that verifies the data value it will be called each time when the method set is called
Returns
True if the verifier is valid and set, false otherwise

Definition at line 164 of file Resource.cpp.