crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
AspellList.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  * AspellList.hpp
24  *
25  * RAII wrapper for pointer to aspell word lists.
26  * Does NOT have ownership of the pointer, but takes care of its deletion!
27  *
28  * Created on: Feb 27, 2021
29  * Author: ans
30  */
31 
32 #ifndef WRAPPER_ASPELLLIST_HPP_
33 #define WRAPPER_ASPELLLIST_HPP_
34 
35 #include <aspell.h>
36 
37 #include <string> // std::string
38 
39 namespace crawlservpp::Wrapper {
40 
41  /*
42  * DECLARATION
43  */
44 
46 
56  class AspellList {
57  public:
60 
61  explicit AspellList(const AspellWordList * source);
62  virtual ~AspellList();
63 
67 
68  [[nodiscard]] bool valid() const;
69 
73 
74  bool next(std::string& nextTo);
75 
79 
80  void clear();
81 
83 
87 
89  AspellList(const AspellList& other);
90  AspellList(AspellList&& other) noexcept;
91  AspellList& operator=(const AspellList& other);
92  AspellList& operator=(AspellList&& other) noexcept;
93 
95 
98 
99  private:
100  // underlying pointer to the word list
101  AspellStringEnumeration * ptr{nullptr};
102  };
103 
104  /*
105  * IMPLEMENTATION
106  */
107 
108  /*
109  * CONSTRUCTION AND DESTRUCTION
110  */
111 
113 
118  inline AspellList::AspellList(const AspellWordList * source) {
119  if(source != nullptr) {
120  this->ptr = aspell_word_list_elements(source);
121  }
122  }
123 
125 
129  this->clear();
130  }
131 
132  /*
133  * GETTERS
134  */
135 
137 
141  inline bool AspellList::valid() const {
142  return this->ptr != nullptr;
143  }
144 
145  /*
146  * LIST ITERATION
147  */
148 
150 
161  inline bool AspellList::next(std::string& nextTo) {
162  const auto * next{aspell_string_enumeration_next(this->ptr)};
163 
164  if(next == nullptr) {
165  return false;
166  }
167 
168  nextTo = std::string(next);
169 
170  return true;
171  }
172 
173  /*
174  * CLEANUP
175  */
176 
178  inline void AspellList::clear() {
179  if(this->ptr != nullptr) {
180  delete_aspell_string_enumeration(this->ptr);
181 
182  this->ptr = nullptr;
183  }
184  }
185 
186  /*
187  * COPY AND MOVE
188  */
189 
191 
203  inline AspellList::AspellList(const AspellList& other) {
204  if(other.ptr != nullptr) {
205  aspell_string_enumeration_assign(this->ptr, other.ptr);
206  }
207  }
208 
210 
223  inline AspellList::AspellList(AspellList&& other) noexcept : ptr(other.ptr) {
224  other.ptr = nullptr;
225  }
226 
228 
246  if(&other != this) {
247  this->clear();
248 
249  aspell_string_enumeration_assign(this->ptr, other.ptr);
250  }
251 
252  return *this;
253  }
254 
256 
276  inline AspellList& AspellList::operator=(AspellList&& other) noexcept {
277  if(&other != this) {
278  this->clear();
279 
280  this->ptr = other.ptr;
281 
282  other.ptr = nullptr;
283  }
284 
285  return *this;
286  }
287 
288 } /* namespace crawlservpp::Wrapper */
289 
290 #endif /* WRAPPER_ASPELLLIST_HPP_ */
bool valid() const
Gets whether the word list is valid.
Definition: AspellList.hpp:141
AspellList(const AspellWordList *source)
Constructor creating a new word list.
Definition: AspellList.hpp:118
AspellList & operator=(const AspellList &other)
Copy assignment operator.
Definition: AspellList.hpp:245
MAIN_EXCEPTION_CLASS()
Class for aspell word list-specific exceptions.
Namespace for RAII wrappers and Wrapper::Database.
Definition: Database.hpp:109
RAII wrapper for aspell word lists.
Definition: AspellList.hpp:56
void clear()
Deletes the word list, if necessary.
Definition: AspellList.hpp:178
bool next(std::string &nextTo)
Checks for the next list element.
Definition: AspellList.hpp:161
virtual ~AspellList()
Destructor deleting the word list, if necessary.
Definition: AspellList.hpp:128