NeoN
A framework for CFD software
Loading...
Searching...
No Matches
tokenList.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2025 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <vector>
8#include <any>
9
11
12namespace NeoN
13{
14
15
17 const std::out_of_range& e, const std::size_t& key, const std::vector<std::any>& data
18);
19
20
29{
30public:
31
33
35
40 TokenList(const std::vector<std::any>& data, size_t nextIndex = 0);
41
46 TokenList(const std::initializer_list<std::any>& initList);
47
52 void insert(const std::any& value);
53
58 void remove(size_t index);
59
64 [[nodiscard]] bool empty() const;
65
72 template<typename ReturnType>
73 ReturnType popFront()
74 {
75 ReturnType ret {get<ReturnType>(0)};
76 data_.erase(data_.begin());
77 return ret;
78 }
79
84 [[nodiscard]] size_t size() const;
85
94 template<typename ReturnType>
95 [[nodiscard]] ReturnType& get(const size_t& idx)
96 {
97 try
98 {
99 return std::any_cast<ReturnType&>(data_.at(idx));
100 }
101 catch (const std::bad_any_cast& e)
102 {
103 logBadAnyCast<ReturnType>(e, idx, data_);
104 throw e;
105 }
106 catch (const std::out_of_range& e)
107 {
108 logOutRange(e, idx, data_);
109 throw e;
110 }
111 }
112
120 template<typename ReturnType>
121 ReturnType& next()
122 {
123 ReturnType& retValue = get<ReturnType&>(nextIndex_);
124 nextIndex_++;
125 return retValue;
126 }
127
136 template<typename ReturnType>
137 [[nodiscard]] const ReturnType& get(const size_t& idx) const
138 {
139 try
140 {
141 return std::any_cast<const ReturnType&>(data_.at(idx));
142 }
143 catch (const std::bad_any_cast& e)
144 {
145 logBadAnyCast<ReturnType>(e, idx, data_);
146 throw e;
147 }
148 catch (const std::out_of_range& e)
149 {
150 logOutRange(e, idx, data_);
151 throw e;
152 }
153 }
154
162 template<typename ReturnType>
163 const ReturnType& next() const
164 {
165 const ReturnType& retValue = get<ReturnType>(nextIndex_);
166 nextIndex_++;
167 return retValue;
168 }
169
175 [[nodiscard]] std::any& operator[](const size_t& idx);
176
177 [[nodiscard]] std::vector<std::any>& tokens();
178
179
180private:
181
182 std::vector<std::any> data_;
183 mutable size_t nextIndex_;
184};
185
186} // namespace NeoN
A class representing a list of tokens.
Definition tokenList.hpp:29
bool empty() const
Checks if the token list is empty.
size_t size() const
Retrieves the size of the token list.
ReturnType popFront()
Removes first entry of TokenList and returns it.
Definition tokenList.hpp:73
ReturnType & get(const size_t &idx)
Retrieves the value associated with the given index, casting it to the specified type.
Definition tokenList.hpp:95
std::vector< std::any > & tokens()
const ReturnType & get(const size_t &idx) const
Retrieves the value associated with the given index, casting it to the specified type.
std::any & operator[](const size_t &idx)
Retrieves the value associated with the given index.
void insert(const std::any &value)
Inserts a value into the token list.
ReturnType & next()
Retrieves the value associated with the nextIndex, casting it to the specified type.
TokenList(const TokenList &)
TokenList(const std::initializer_list< std::any > &initList)
Construct a TokenList object from an initializer list of std::any.
void remove(size_t index)
Removes a value from the token list based on the specified index.
const ReturnType & next() const
Retrieves the value associated with the nextIndex, casting it to the specified type.
TokenList(const std::vector< std::any > &data, size_t nextIndex=0)
Construct a TokenList object from a vector of std::any.
Definition array.hpp:20
void logOutRange(const std::out_of_range &e, const std::string &key, const std::unordered_map< std::string, std::any > &data)