crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
PCREMatch.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2021 Anselm Schmidt (ans[ät]ohai.su)
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version in addition to the terms of any
11  * licences already herein identified.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * ---
22  *
23  * PCREMatch.hpp
24  *
25  * RAII wrapper for pointer to Perl-Compatible Regular Expression match.
26  * Does NOT have ownership of the pointer, but takes care of its deletion!
27  *
28  * Created on: Feb 7, 2019
29  * Author: ans
30  */
31 
32 #ifndef WRAPPER_PCREMATCH_HPP_
33 #define WRAPPER_PCREMATCH_HPP_
34 
35 #include <pcre2.h>
36 
37 namespace crawlservpp::Wrapper {
38 
39  /*
40  * DECLARATION
41  */
42 
44 
61  class PCREMatch {
62  public:
65 
66  explicit PCREMatch(pcre2_match_data * setPtr) noexcept;
67  virtual ~PCREMatch();
68 
72 
73  [[nodiscard]] pcre2_match_data * get() noexcept;
74  [[nodiscard]] const pcre2_match_data * getc() const noexcept;
75  [[nodiscard]] bool valid() const noexcept;
76 
80 
81  void clear() noexcept;
82 
84 
88 
91  PCREMatch(PCREMatch&) = delete;
92 
94  PCREMatch& operator=(PCREMatch&) = delete;
95 
96  PCREMatch(PCREMatch&& other) noexcept;
97  PCREMatch& operator=(PCREMatch&& other) noexcept;
98 
100 
101  private:
102  // underlying pointer to regular expression match
103  pcre2_match_data * ptr{nullptr};
104  };
105 
106  /*
107  * IMPLEMENTATION
108  */
109 
110  /*
111  * CONSTRUCTION AND DESTRUCTION
112  */
113 
115 
124  inline PCREMatch::PCREMatch(pcre2_match_data * setPtr) noexcept : ptr(setPtr) {}
125 
128  this->clear();
129  }
130 
131  /*
132  * GETTERS
133  */
134 
136 
140  inline pcre2_match_data * PCREMatch::get() noexcept {
141  return this->ptr;
142  }
143 
145 
150  inline const pcre2_match_data * PCREMatch::getc() const noexcept {
151  return this->ptr;
152  }
153 
155 
160  inline bool PCREMatch::valid() const noexcept {
161  return this->ptr != nullptr;
162  }
163 
164  /*
165  * CLEANUP
166  */
167 
169  inline void PCREMatch::clear() noexcept {
170  if(this->ptr != nullptr) {
171  pcre2_match_data_free(this->ptr);
172 
173  this->ptr = nullptr;
174  }
175  }
176 
177  /*
178  * COPY AND MOVE
179  */
180 
182 
195  inline PCREMatch::PCREMatch(PCREMatch&& other) noexcept : ptr(other.ptr) {
196  other.ptr = nullptr;
197  }
198 
200 
220  inline PCREMatch& PCREMatch::operator=(PCREMatch&& other) noexcept {
221  if(&other != this) {
222  this->clear();
223 
224  this->ptr = other.ptr;
225 
226  other.ptr = nullptr;
227  }
228 
229  return *this;
230  }
231 
232 } /* namespace crawlservpp::Wrapper */
233 
234 #endif /* WRAPPER_PCREMATCH_HPP_ */
void clear() noexcept
Clears the underlying regular expression match if necessary.
Definition: PCREMatch.hpp:169
PCREMatch & operator=(PCREMatch &)=delete
Deleted copy assignment operator.
bool valid() const noexcept
Checks whether the underlying regular expression match is valid.
Definition: PCREMatch.hpp:160
const pcre2_match_data * getc() const noexcept
Gets a const pointer to the underlying regular expression match.
Definition: PCREMatch.hpp:150
RAII wrapper for Perl-compatible regular expression matches.
Definition: PCREMatch.hpp:61
PCREMatch(pcre2_match_data *setPtr) noexcept
Constructor setting the underlying regular expression.
Definition: PCREMatch.hpp:124
Namespace for RAII wrappers and Wrapper::Database.
Definition: Database.hpp:109
virtual ~PCREMatch()
Destructor clearing the underlying regular expression if necessary.
Definition: PCREMatch.hpp:127
pcre2_match_data * get() noexcept
Gets a pointer to the underlying regular expression match.
Definition: PCREMatch.hpp:140