Base class for creating a stack of scoped values of type T, identified by Tags.
More...
|
|
| 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.
|
| |
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};
void fun() {
const Config& config = ScopedConfig::top() ? ScopedConfig::top()->value() : default_config;
std::cout << "Path: " << config.path << ", level: " << config.level << std::endl;
}
fun();
{
ScopedConfig config1{"path 1", 1};
fun();
{
fun();
ScopedConfig config2{"path 2", 2};
fun();
}
fun();
}
fun();
}
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
-
| T | Type of the value to be scoped. |
| Tags | Variadic template parameters to uniquely identify different scoped value types. |