lucid  0.0.1
Lifting-based Uncertain Control Invariant Dynamics
Loading...
Searching...
No Matches
lucid::BaseScopedValue< T, Tags > Class Template Referenceabstract

Base class for creating a stack of scoped values of type T, identified by Tags. More...

#include <ScopedValue.h>

Public Member Functions

 BaseScopedValue ()
 Construct a new scoped value instance and add it to the scope stack of instances.
 
 BaseScopedValue (const BaseScopedValue &)
 Copy constructor that adds the current instance to the scope stack of instances.
 
virtual ~BaseScopedValue ()
 Destructor that removes the current instance from the scope stack of instances.
 
T & value ()
 Get read-write access to the value of the current scoped value.
 
virtual const T & value () const =0
 Get read-only access to the value of the current scoped value.
 
void swap (BaseScopedValue &other) noexcept
 Swap the values of two scoped value instances.
 
void * operator new (std::size_t)=delete
 Disabled standard new.
 
void * operator new[] (std::size_t)=delete
 Disabled array new.
 
void * operator new (std::size_t, void *)=delete
 Disabled placement new.
 
void * operator new[] (std::size_t, void *)=delete
 Disabled placement array new.
 

Static Public Member Functions

static BaseScopedValuetop ()
 Get read-only access to the most recently created scoped value instance of the scope stack.
 
static BaseScopedValuebottom ()
 Get read-only access to the oldest created scoped value instance of the scope stack.
 
static const std::vector< BaseScopedValue * > & scope_stack ()
 Get read-only access to the all scoped value instances of the scope stack.
 
static void set_scopes (std::vector< BaseScopedValue * > new_scopes)
 Set the entire stack of scoped value instances.
 

Private Member Functions

void detach ()
 Remove the current instance from the scope stack.
 

Static Private Attributes

static thread_local std::vector< BaseScopedValue * > scope_stack_
 Stack of all scoped value instances.
 

Detailed Description

template<class T, class... Tags>
class lucid::BaseScopedValue< T, Tags >

Base class for creating a stack of scoped values of type T, identified by Tags.

This class maintains a linked vector of instances, allowing easy access to the top (most recently created) and bottom (first created) instances.

#include <iostream>
struct Config {
std::string path;
int level = 0;
};
const Config default_config{"default_path", 0};
// The tag avoids conflicts with other ScopedValue<Config> usages.
void fun() { // Note there are no parameters. This could be at any level of the call stack.
// If there is any scoped config on the stack, use it. Otherwise, use the default config.
const Config& config = ScopedConfig::top() ? ScopedConfig::top()->value() : default_config;
std::cout << "Path: " << config.path << ", level: " << config.level << std::endl;
}
int main() {
fun(); // Path: default_path, level: 0
{
ScopedConfig config1{"path 1", 1}; // Push new config onto the stack
fun(); // Path: path 1, level: 1
{
fun(); // Path: path 1, level: 1
ScopedConfig config2{"path 2", 2}; // Push another config onto the stack
fun(); // Path: path 2, level: 2
} // config2 goes out of scope and is popped from the stack
fun(); // Path: path 1, level: 1
} // config1 goes out of scope and is popped from the stack
fun(); // Path: default_path, level: 0
}
int main(const int argc, char *argv[])
Main function.
Definition main.cpp:207
PolymorphicScopedValue< T, T, Tags... > ScopedValue
Simplified scoped value class that holds a value of type T.
Definition ScopedValue.h:175
Template Parameters
TType of the value to be scoped.
TagsVariadic template parameters to uniquely identify different scoped value types.

Constructor & Destructor Documentation

◆ BaseScopedValue()

template<class T, class... Tags>
lucid::BaseScopedValue< T, Tags >::BaseScopedValue ( const BaseScopedValue< T, Tags > & )
inline

Copy constructor that adds the current instance to the scope stack of instances.

The value is copied from the other instance.

Member Function Documentation

◆ bottom()

template<class T, class... Tags>
static BaseScopedValue * lucid::BaseScopedValue< T, Tags >::bottom ( )
inlinestatic

Get read-only access to the oldest created scoped value instance of the scope stack.

Returns
oldest created scoped value instance of the scope stack

◆ scope_stack()

template<class T, class... Tags>
static const std::vector< BaseScopedValue * > & lucid::BaseScopedValue< T, Tags >::scope_stack ( )
inlinestatic

Get read-only access to the all scoped value instances of the scope stack.

Returns
all scoped value instances of the scope stack

◆ set_scopes()

template<class T, class... Tags>
static void lucid::BaseScopedValue< T, Tags >::set_scopes ( std::vector< BaseScopedValue< T, Tags > * > new_scopes)
inlinestatic

Set the entire stack of scoped value instances.

Useful for initialising the scope stack in other threads.

Note
The entire current stack will be replaced.
Parameters
new_scopesnew stack of scoped value instances.

◆ swap()

template<class T, class... Tags>
void lucid::BaseScopedValue< T, Tags >::swap ( BaseScopedValue< T, Tags > & other)
inlinenoexcept

Swap the values of two scoped value instances.

Both instances must be of the same type and identified by the same Tags. Their order in the vector of instances remains unchanged.

Parameters
otherthe other scoped value instance to swap with

◆ top()

template<class T, class... Tags>
static BaseScopedValue * lucid::BaseScopedValue< T, Tags >::top ( )
inlinestatic

Get read-only access to the most recently created scoped value instance of the scope stack.

Returns
most recently created scoped value instance of the scope stack

◆ value() [1/2]

template<class T, class... Tags>
T & lucid::BaseScopedValue< T, Tags >::value ( )
inline

Get read-write access to the value of the current scoped value.

Returns
value of the current scoped value

◆ value() [2/2]

template<class T, class... Tags>
virtual const T & lucid::BaseScopedValue< T, Tags >::value ( ) const
pure virtual

Get read-only access to the value of the current scoped value.

Returns
value of the current scoped value

Implemented in lucid::PolymorphicScopedValue< T, B, Tags >, lucid::PolymorphicScopedValue< T, T, Tags... >, and lucid::PolymorphicScopedValue< T, T, Tags... >.


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