userver: utils::AnyStorage< StorageTag > Class Template Reference
Loading...
Searching...
No Matches
utils::AnyStorage< StorageTag > Class Template Reference

#include <userver/utils/any_storage.hpp>

Detailed Description

template<typename StorageTag>
class utils::AnyStorage< StorageTag >

map-like heterogeneous data storage

Usage example

struct MyStorage {};
TEST(AnyStorage, Simple) {
EXPECT_EQ(storage.GetOptional(kInt), nullptr);
storage.Emplace(kInt, 1);
storage.Emplace(kStr, std::string{"1"});
EXPECT_EQ(*storage.GetOptional(kInt), 1);
EXPECT_EQ(storage.Get(kInt), 1);
EXPECT_EQ(storage.Get(kStr), "1");
storage.Set(kInt, 2);
storage.Set(kStr, std::string{"2"});
EXPECT_EQ(storage.Get(kInt), 2);
EXPECT_EQ(storage.Get(kStr), "2");
storage.Emplace(kStr, std::string{"3"});
storage.Emplace(kStr, std::string{"4"});
EXPECT_EQ(storage.Get(kStr), "4");
}

Definition at line 79 of file any_storage.hpp.

Public Member Functions

template<typename Data >
const Data & Get (const AnyStorageDataTag< StorageTag, Data > &tag) const
 
template<typename Data >
Data & Get (const AnyStorageDataTag< StorageTag, Data > &tag)
 
template<typename Data >
Data & Set (AnyStorageDataTag< StorageTag, Data > tag, Data data)
 Stores the data.
 
template<typename Data , typename... Args>
Data & Emplace (const AnyStorageDataTag< StorageTag, Data > &tag, Args &&... args)
 Emplaces the data. The data is rewritten if already stored.
 
template<typename Data >
Data * GetOptional (const AnyStorageDataTag< StorageTag, Data > &tag) noexcept
 
template<typename Data >
const Data * GetOptional (const AnyStorageDataTag< StorageTag, Data > &tag) const noexcept
 
template<typename Data >
void Erase (const AnyStorageDataTag< StorageTag, Data > &tag)
 Erase data.
 

Constructor & Destructor Documentation

◆ AnyStorage()

template<typename StorageTag >
utils::AnyStorage< StorageTag >::AnyStorage ( )

Definition at line 130 of file any_storage.hpp.

◆ ~AnyStorage()

Definition at line 144 of file any_storage.hpp.

Member Function Documentation

◆ Emplace()

template<typename StorageTag >
template<typename Data , typename... Args>
Data & utils::AnyStorage< StorageTag >::Emplace ( const AnyStorageDataTag< StorageTag, Data > & tag,
Args &&... args )

Emplaces the data. The data is rewritten if already stored.

Definition at line 165 of file any_storage.hpp.

◆ Get() [1/2]

template<typename StorageTag >
template<typename Data >
Data & utils::AnyStorage< StorageTag >::Get ( const AnyStorageDataTag< StorageTag, Data > & tag)
Returns
Stored data.
Exceptions
std::runtime_errorif no data was stored

Definition at line 179 of file any_storage.hpp.

◆ Get() [2/2]

template<typename StorageTag >
template<typename Data >
const Data & utils::AnyStorage< StorageTag >::Get ( const AnyStorageDataTag< StorageTag, Data > & tag) const
Returns
Stored data.

Definition at line 188 of file any_storage.hpp.

◆ GetOptional() [1/2]

template<typename StorageTag >
template<typename Data >
const Data * utils::AnyStorage< StorageTag >::GetOptional ( const AnyStorageDataTag< StorageTag, Data > & tag) const
noexcept
Returns
Pointer to stored data or nullptr if no data found.

Definition at line 206 of file any_storage.hpp.

◆ GetOptional() [2/2]

template<typename StorageTag >
template<typename Data >
Data * utils::AnyStorage< StorageTag >::GetOptional ( const AnyStorageDataTag< StorageTag, Data > & tag)
noexcept
Returns
Pointer to stored data or nullptr if no data is found.

Definition at line 196 of file any_storage.hpp.

◆ Set()

template<typename StorageTag >
template<typename Data >
Data & utils::AnyStorage< StorageTag >::Set ( AnyStorageDataTag< StorageTag, Data > tag,
Data data )

Stores the data.

Definition at line 154 of file any_storage.hpp.


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