NeoN
A framework for CFD software
Loading...
Searching...
No Matches
tokenList.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2026 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8
9#include <vector>
10#include <any>
11
13
14namespace NeoN
15{
16
17
19 const std::out_of_range& e, const std::size_t& key, const std::vector<std::any>& data
20);
21
22
31{
32public:
33
35
37
42 TokenList(const std::vector<std::any>& data, size_t nextIndex = 0);
43
48 TokenList(const std::initializer_list<std::any>& initList);
49
54 void insert(const std::any& value);
55
60 void remove(size_t index);
61
66 [[nodiscard]] bool empty() const;
67
74 template<typename ReturnType>
75 ReturnType popFront()
76 {
77 ReturnType ret {get<ReturnType>(0)};
78 data_.erase(data_.begin());
79 return ret;
80 }
81
86 [[nodiscard]] size_t size() const;
87
92 void reset() const { nextIndex_ = 0; }
93
102 template<typename ReturnType>
103 [[nodiscard]] ReturnType& get(const size_t& idx)
104 {
105 try
106 {
107 return std::any_cast<ReturnType&>(data_.at(idx));
108 }
109 catch (const std::bad_any_cast& e)
110 {
111 logBadAnyCast<ReturnType>(e, idx, data_);
112 throw e;
113 }
114 catch (const std::out_of_range& e)
115 {
116 logOutRange(e, idx, data_);
117 throw e;
118 }
119 }
120
128 template<typename ReturnType>
129 ReturnType& next()
130 {
131 ReturnType& retValue = get<ReturnType&>(nextIndex_);
132 nextIndex_++;
133 return retValue;
134 }
135
144 template<typename ReturnType>
145 [[nodiscard]] const ReturnType& get(const size_t& idx) const
146 {
147 try
148 {
149 return std::any_cast<const ReturnType&>(data_.at(idx));
150 }
151 catch (const std::bad_any_cast& e)
152 {
153 logBadAnyCast<ReturnType>(e, idx, data_);
154 throw e;
155 }
156 catch (const std::out_of_range& e)
157 {
158 logOutRange(e, idx, data_);
159 throw e;
160 }
161 }
162
170 template<typename ReturnType>
171 const ReturnType& next() const
172 {
173 const ReturnType& retValue = get<ReturnType>(nextIndex_);
174 nextIndex_++;
175 return retValue;
176 }
177
184 template<typename ReturnType>
185 [[nodiscard]] bool peekIs() const
186 {
187 return nextIndex_ < data_.size() && data_[nextIndex_].type() == typeid(ReturnType);
188 }
189
195 [[nodiscard]] std::any& operator[](const size_t& idx);
196
197 [[nodiscard]] std::vector<std::any>& tokens();
198
199
200private:
201
202 std::vector<std::any> data_;
203 mutable size_t nextIndex_;
204};
205
206} // namespace NeoN
A class representing a list of tokens.
Definition tokenList.hpp:31
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:75
ReturnType & get(const size_t &idx)
Retrieves the value associated with the given index, casting it to the specified type.
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 reset() const
Rewind the read cursor used by next<>() so the list can be iterated again from the beginning.
Definition tokenList.hpp:92
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.
bool peekIs() const
Checks whether the next token (the one a subsequent next() would return) holds a value of the given t...
TokenList(const std::vector< std::any > &data, size_t nextIndex=0)
Construct a TokenList object from a vector of std::any.
Integer types used throughout NeoN.
Definition array.hpp:18
void logOutRange(const std::out_of_range &e, const std::string &key, const std::unordered_map< std::string, std::any > &data)
#define NEON_TYPE_VISIBLE