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
9
10#include <vector>
11#include <any>
12
14
15namespace NeoN
16{
17
18
20 const std::out_of_range& e, const sizeType& key, const std::vector<std::any>& data
21);
22
23
32{
33public:
34
36
38
43 TokenList(const std::vector<std::any>& data, sizeType nextIndex = 0);
44
49 TokenList(const std::initializer_list<std::any>& initList);
50
55 void insert(const std::any& value);
56
61 void remove(sizeType index);
62
67 [[nodiscard]] bool empty() const;
68
75 template<typename ReturnType>
76 ReturnType popFront()
77 {
78 ReturnType ret {get<ReturnType>(0)};
79 data_.erase(data_.begin());
80 return ret;
81 }
82
87 [[nodiscard]] sizeType size() const;
88
93 void reset() const { nextIndex_ = 0; }
94
103 template<typename ReturnType>
104 [[nodiscard]] ReturnType& get(const sizeType& idx)
105 {
106 try
107 {
108 return std::any_cast<ReturnType&>(data_.at(idx));
109 }
110 catch (const std::bad_any_cast& e)
111 {
112 logBadAnyCast<ReturnType>(e, idx, data_);
113 throw e;
114 }
115 catch (const std::out_of_range& e)
116 {
117 logOutRange(e, idx, data_);
118 throw e;
119 }
120 }
121
129 template<typename ReturnType>
130 ReturnType& next()
131 {
132 ReturnType& retValue = get<ReturnType&>(nextIndex_);
133 nextIndex_++;
134 return retValue;
135 }
136
145 template<typename ReturnType>
146 [[nodiscard]] const ReturnType& get(const sizeType& idx) const
147 {
148 try
149 {
150 return std::any_cast<const ReturnType&>(data_.at(idx));
151 }
152 catch (const std::bad_any_cast& e)
153 {
154 logBadAnyCast<ReturnType>(e, idx, data_);
155 throw e;
156 }
157 catch (const std::out_of_range& e)
158 {
159 logOutRange(e, idx, data_);
160 throw e;
161 }
162 }
163
171 template<typename ReturnType>
172 const ReturnType& next() const
173 {
174 const ReturnType& retValue = get<ReturnType>(nextIndex_);
175 nextIndex_++;
176 return retValue;
177 }
178
185 template<typename ReturnType>
186 [[nodiscard]] bool peekIs() const
187 {
188 return nextIndex_ < data_.size() && data_[nextIndex_].type() == typeid(ReturnType);
189 }
190
196 [[nodiscard]] std::any& operator[](const sizeType& idx);
197
198 [[nodiscard]] std::vector<std::any>& tokens();
199
200
201private:
202
203 std::vector<std::any> data_;
204 mutable sizeType nextIndex_;
205};
206
207} // namespace NeoN
bool empty() const
Checks if the token list is empty.
sizeType size() const
Retrieves the size of the token list.
void remove(sizeType index)
Removes a value from the token list based on the specified index.
ReturnType & get(const sizeType &idx)
Retrieves the value associated with the given index, casting it to the specified type.
ReturnType popFront()
Removes first entry of TokenList and returns it.
Definition tokenList.hpp:76
std::vector< std::any > & tokens()
const ReturnType & get(const sizeType &idx) const
Retrieves the value associated with the given index, casting it to the specified type.
void reset() const
Rewind the read cursor used by next<>() so the list can be iterated again from the beginning.
Definition tokenList.hpp:93
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.
std::any & operator[](const sizeType &idx)
Retrieves the value associated with the given index.
TokenList(const TokenList &)
TokenList(const std::initializer_list< std::any > &initList)
Construct a TokenList object from an initializer list of std::any.
const ReturnType & next() const
Retrieves the value associated with the nextIndex, casting it to the specified type.
TokenList(const std::vector< std::any > &data, sizeType nextIndex=0)
Construct a TokenList object from a vector of std::any.
bool peekIs() const
Checks whether the next token (the one a subsequent next() would return) holds a value of the given t...
Integer types used throughout NeoN.
Definition array.hpp:18
std::size_t sizeType
Definition label.hpp:56
void logOutRange(const std::out_of_range &e, const std::string &key, const std::unordered_map< std::string, std::any > &data)
void logBadAnyCast(const std::bad_any_cast &e, const Key &key, const Container &data)
Definition demangle.hpp:20
#define NEON_TYPE_VISIBLE