crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
PCRE.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  * PCRE.hpp
24  *
25  * Wrapper for pointer to Perl-Compatible Regular Expression.
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_PCRE_HPP_
33 #define WRAPPER_PCRE_HPP_
34 
35 #include <pcre2.h>
36 
37 namespace crawlservpp::Wrapper {
38 
39  /*
40  * DECLARATION
41  */
42 
44 
62  class PCRE {
63  public:
66 
68  PCRE() = default;
69 
70  explicit PCRE(pcre2_code * regExPtr) noexcept;
71  virtual ~PCRE();
72 
76 
77  [[nodiscard]] pcre2_code * get() noexcept;
78  [[nodiscard]] const pcre2_code * getc() const noexcept;
79  [[nodiscard]] bool valid() const noexcept;
80 
84 
85  void set(pcre2_code * regExPtr);
86 
90 
91  void clear() noexcept;
92 
94 
98 
100  PCRE(const PCRE& other);
101  PCRE& operator=(const PCRE& other);
102  PCRE(PCRE&& other) noexcept;
103  PCRE& operator=(PCRE&& other) noexcept;
104 
106 
107  private:
108  // underlying pointer to regular expression
109  pcre2_code * ptr{nullptr};
110  };
111 
112  /*
113  * IMPLEMENTATION
114  */
115 
116  /*
117  * CONSTRUCTION AND DESTRUCTION
118  */
119 
121 
133  inline PCRE::PCRE(pcre2_code * regExPtr) noexcept : ptr(regExPtr) {}
134 
136  inline PCRE::~PCRE() {
137  this->clear();
138  }
139 
140  /*
141  * GETTERS
142  */
143 
145 
150  inline pcre2_code * PCRE::get() noexcept {
151  return this->ptr;
152  }
153 
155 
160  inline const pcre2_code * PCRE::getc() const noexcept {
161  return this->ptr;
162  }
163 
165 
173  inline bool PCRE::valid() const noexcept {
174  return this->ptr != nullptr;
175  }
176 
177  /*
178  * SETTER
179  */
180 
182 
195  inline void PCRE::set(pcre2_code * regExPtr) {
196  this->clear();
197 
198  this->ptr = regExPtr;
199  }
200 
201  /*
202  * CLEANUP
203  */
204 
206 
216  inline void PCRE::clear() noexcept {
217  if(this->ptr != nullptr) {
218  pcre2_code_free(this->ptr);
219 
220  this->ptr = nullptr;
221  }
222  }
223 
224  /*
225  * COPY AND MOVE
226  */
227 
229 
252  inline PCRE::PCRE(const PCRE& other) {
253  this->ptr = pcre2_code_copy_with_tables(other.getc());
254  }
255 
257 
281  inline PCRE& PCRE::operator=(const PCRE& other) {
282  if(&other != this) {
283  this->clear();
284 
285  this->ptr = pcre2_code_copy_with_tables(other.getc());
286  }
287 
288  return *this;
289  }
290 
292 
305  inline PCRE::PCRE(PCRE&& other) noexcept : ptr(other.ptr) {
306  other.ptr = nullptr;
307  }
308 
310 
330  inline PCRE& PCRE::operator=(PCRE&& other) noexcept {
331  if(&other != this) {
332  this->clear();
333 
334  this->ptr = other.ptr;
335 
336  other.ptr = nullptr;
337  }
338 
339  return *this;
340  }
341 
342 } /* namespace crawlservpp::Wrapper */
343 
344 #endif /* WRAPPER_PCRE_HPP_ */
PCRE()=default
Default constructor.
const pcre2_code * getc() const noexcept
Gets a const pointer to the underlying regular expression.
Definition: PCRE.hpp:160
PCRE & operator=(const PCRE &other)
Copy assignment operator.
Definition: PCRE.hpp:281
RAII wrapper for Perl-compatible regular expressions.
Definition: PCRE.hpp:62
bool valid() const noexcept
Checks whether the underlying regular expression is valid.
Definition: PCRE.hpp:173
pcre2_code * get() noexcept
Gets a pointer to the underlying regular expression.
Definition: PCRE.hpp:150
virtual ~PCRE()
Destructor freeing the underlying regular expression if necessary.
Definition: PCRE.hpp:136
void set(pcre2_code *regExPtr)
Sets a PERL-compatibe regular expression.
Definition: PCRE.hpp:195
Namespace for RAII wrappers and Wrapper::Database.
Definition: Database.hpp:109
void clear() noexcept
Clears the underlying regular expression if necessary.
Definition: PCRE.hpp:216