NeoFOAM
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
tokenList.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2023 NeoFOAM authors
3#pragma once
4
5#include <vector>
6#include <any>
7
9
10namespace NeoFOAM
11{
12
13
22{
23public:
24
25 TokenList() = default;
26
31 TokenList(const std::vector<std::any>& data);
32
37 TokenList(const std::initializer_list<std::any>& initList);
38
43 void insert(const std::any& value);
44
49 void remove(size_t index);
50
55 [[nodiscard]] bool empty() const;
56
63 template<typename ReturnType>
64 ReturnType popFront()
65 {
66 ReturnType ret {get<ReturnType>(0)};
67 data_.erase(data_.begin());
68 return ret;
69 }
70
75 [[nodiscard]] size_t size() const;
76
85 template<typename ReturnType>
86 [[nodiscard]] ReturnType& get(const size_t& idx)
87 {
88 try
89 {
90 return std::any_cast<ReturnType&>(data_.at(idx));
91 }
92 catch (const std::bad_any_cast& e)
93 {
94 logBadAnyCast<ReturnType>(e, idx, data_);
95 throw e;
96 }
97 }
98
99
108 template<typename ReturnType>
109 [[nodiscard]] const ReturnType& get(const size_t& idx) const
110 {
111 try
112 {
113 return std::any_cast<const ReturnType&>(data_.at(idx));
114 }
115 catch (const std::bad_any_cast& e)
116 {
117 logBadAnyCast<ReturnType>(e, idx, data_);
118 throw e;
119 }
120 }
121
127 [[nodiscard]] std::any& operator[](const size_t& idx);
128
129 [[nodiscard]] std::vector<std::any>& tokens();
130
131
132private:
133
134 std::vector<std::any> data_;
135};
136
137} // namespace NeoFOAM
A class representing a list of tokens.
Definition tokenList.hpp:22
ReturnType popFront()
Removes first entry of TokenList and returns it.
Definition tokenList.hpp:64
size_t size() const
Retrieves the size of the token list.
TokenList(const std::initializer_list< std::any > &initList)
Construct a TokenList object from an initializer list of std::any.
void insert(const std::any &value)
Inserts a value into the token list.
bool empty() const
Checks if the token list is empty.
void remove(size_t index)
Removes a value from the token list based on the specified index.
TokenList(const std::vector< std::any > &data)
Construct a TokenList object from a vector of std::any.
const ReturnType & get(const size_t &idx) const
Retrieves the value associated with the given index, casting it to the specified type.
std::vector< std::any > & tokens()
std::any & operator[](const size_t &idx)
Retrieves the value associated with the given index.
ReturnType & get(const size_t &idx)
Retrieves the value associated with the given index, casting it to the specified type.
Definition tokenList.hpp:86