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
15 const std::out_of_range& e, const std::size_t& key, const std::vector<std::any>& data
16);
17
18
27{
28public:
29
31
33
38 TokenList(const std::vector<std::any>& data, size_t nextIndex = 0);
39
44 TokenList(const std::initializer_list<std::any>& initList);
45
50 void insert(const std::any& value);
51
56 void remove(size_t index);
57
62 [[nodiscard]] bool empty() const;
63
70 template<typename ReturnType>
71 ReturnType popFront()
72 {
73 ReturnType ret {get<ReturnType>(0)};
74 data_.erase(data_.begin());
75 return ret;
76 }
77
82 [[nodiscard]] size_t size() const;
83
92 template<typename ReturnType>
93 [[nodiscard]] ReturnType& get(const size_t& idx)
94 {
95 try
96 {
97 return std::any_cast<ReturnType&>(data_.at(idx));
98 }
99 catch (const std::bad_any_cast& e)
100 {
101 logBadAnyCast<ReturnType>(e, idx, data_);
102 throw e;
103 }
104 catch (const std::out_of_range& e)
105 {
106 logOutRange(e, idx, data_);
107 throw e;
108 }
109 }
110
118 template<typename ReturnType>
119 ReturnType& next()
120 {
121 ReturnType& retValue = get<ReturnType&>(nextIndex_);
122 nextIndex_++;
123 return retValue;
124 }
125
134 template<typename ReturnType>
135 [[nodiscard]] const ReturnType& get(const size_t& idx) const
136 {
137 try
138 {
139 return std::any_cast<const ReturnType&>(data_.at(idx));
140 }
141 catch (const std::bad_any_cast& e)
142 {
143 logBadAnyCast<ReturnType>(e, idx, data_);
144 throw e;
145 }
146 catch (const std::out_of_range& e)
147 {
148 logOutRange(e, idx, data_);
149 throw e;
150 }
151 }
152
160 template<typename ReturnType>
161 const ReturnType& next() const
162 {
163 const ReturnType& retValue = get<ReturnType>(nextIndex_);
164 nextIndex_++;
165 return retValue;
166 }
167
173 [[nodiscard]] std::any& operator[](const size_t& idx);
174
175 [[nodiscard]] std::vector<std::any>& tokens();
176
177
178private:
179
180 std::vector<std::any> data_;
181 mutable size_t nextIndex_;
182};
183
184} // namespace NeoFOAM
A class representing a list of tokens.
Definition tokenList.hpp:27
ReturnType popFront()
Removes first entry of TokenList and returns it.
Definition tokenList.hpp:71
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.
TokenList(const std::vector< std::any > &data, size_t nextIndex=0)
Construct a TokenList object from a vector of std::any.
const ReturnType & next() const
Retrieves the value associated with the nextIndex, casting it to the specified type.
ReturnType & next()
Retrieves the value associated with the nextIndex, casting it to the specified type.
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.
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()
TokenList(const TokenList &)
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:93
void logOutRange(const std::out_of_range &e, const std::string &key, const std::unordered_map< std::string, std::any > &data)