Cppcheck
tokenrange.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 //---------------------------------------------------------------------------
20 #ifndef tokenrangeH
21 #define tokenrangeH
22 //---------------------------------------------------------------------------
23 
24 #include "config.h"
25 
26 #include <cstddef>
27 #include <iterator>
28 #include <type_traits>
29 
30 class Token;
31 
32 template<typename T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
34  T* mFront;
35  T* mBack;
36 
37 public:
38  TokenRangeBase(T* front, T* back) : mFront(front), mBack(back) {}
39 
40  struct TokenIterator {
41  using iterator_category = std::forward_iterator_tag;
42  using value_type = T*;
43  using difference_type = std::ptrdiff_t;
44  using pointer = void;
45  using reference = T*;
46 
47  T* mt;
48  TokenIterator() : mt(nullptr) {}
49  explicit TokenIterator(T* t) : mt(t) {}
51  mt = mt->next();
52  return *this;
53  }
54  bool operator==(const TokenIterator& b) const {
55  return mt == b.mt;
56  }
57  bool operator!=(const TokenIterator& b) const {
58  return mt != b.mt;
59  }
60  T* operator*() const {
61  return mt;
62  }
63  };
64 
65  TokenIterator begin() const {
66  return TokenIterator(mFront);
67  }
68  TokenIterator end() const {
69  return TokenIterator(mBack);
70  }
71 };
72 
73 class TokenRange : public TokenRangeBase<Token> {
74 public:
75  TokenRange(Token* front, Token* back) : TokenRangeBase<Token>(front, back) {}
76 };
77 
78 class ConstTokenRange : public TokenRangeBase<const Token> {
79 public:
80  ConstTokenRange(const Token* front, const Token* back) : TokenRangeBase<const Token>(front, back) {}
81 };
82 
83 #endif // tokenrangeH
ConstTokenRange(const Token *front, const Token *back)
Definition: tokenrange.h:80
TokenIterator end() const
Definition: tokenrange.h:68
TokenRangeBase(T *front, T *back)
Definition: tokenrange.h:38
TokenIterator begin() const
Definition: tokenrange.h:65
TokenRange(Token *front, Token *back)
Definition: tokenrange.h:75
The token list that the TokenList generates is a linked-list of this class.
Definition: token.h:150
#define REQUIRES(msg,...)
Definition: config.h:124
TokenIterator & operator++()
Definition: tokenrange.h:50
bool operator!=(const TokenIterator &b) const
Definition: tokenrange.h:57
std::ptrdiff_t difference_type
Definition: tokenrange.h:43
bool operator==(const TokenIterator &b) const
Definition: tokenrange.h:54
std::forward_iterator_tag iterator_category
Definition: tokenrange.h:41