crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
AspellConfig.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  * AspellConfig.hpp
24  *
25  * RAII wrapper for pointer to aspell configurations.
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_ASPELLCONFIG_HPP_
33 #define WRAPPER_ASPELLCONFIG_HPP_
34 
35 #include "../Main/Exception.hpp"
36 
37 #include <aspell.h>
38 
39 #include <string> // std::string, std::to_string
40 
41 namespace crawlservpp::Wrapper {
42 
43  /*
44  * DECLARATION
45  */
46 
48 
58  class AspellConfig {
59  public:
62 
63  AspellConfig();
64  virtual ~AspellConfig();
65 
69 
70  [[nodiscard]] ::AspellConfig * get();
71  [[nodiscard]] const ::AspellConfig * getc() const;
72  [[nodiscard]] bool valid() const;
73 
77 
78  void setOption(const std::string& name, const std::string& value);
79 
83 
84  void clear();
85 
87 
91 
93  AspellConfig(const AspellConfig& other);
94  AspellConfig(AspellConfig&& other) noexcept;
95  AspellConfig& operator=(const AspellConfig& other);
96  AspellConfig& operator=(AspellConfig&& other) noexcept;
97 
99 
102 
103  private:
104  // underlying pointer to the configuration
105  ::AspellConfig * ptr{nullptr};
106  };
107 
108  /*
109  * IMPLEMENTATION
110  */
111 
112  /*
113  * CONSTRUCTION AND DESTRUCTION
114  */
115 
118  this->ptr = new_aspell_config();
119  }
120 
122 
126  this->clear();
127  }
128 
129  /*
130  * GETTERS
131  */
132 
134 
141  inline ::AspellConfig * AspellConfig::get() {
142  return this->ptr;
143  }
144 
146 
154  inline const ::AspellConfig * AspellConfig::getc() const {
155  return this->ptr;
156  }
157 
159 
163  inline bool AspellConfig::valid() const {
164  return this->ptr != nullptr;
165  }
166 
167  /*
168  * SETTER
169  */
170 
172 
186  inline void AspellConfig::setOption(const std::string& name, const std::string& value) {
187  if(this->ptr == nullptr) {
188  throw Exception(
189  "AspellConfig::setOption():"
190  " The configuration is not valid"
191  );
192  }
193 
194  if(aspell_config_replace(this->ptr, name.c_str(), value.c_str()) == 0) {
195  throw Exception(
196  "AspellConfig::setOption():"
197  " Aspell error #"
198  + std::to_string(aspell_config_error_number(this->ptr))
199  + ": "
200  + aspell_config_error_message(this->ptr)
201  );
202  }
203  }
204 
205  /*
206  * CLEANUP
207  */
208 
210  inline void AspellConfig::clear() {
211  if(this->ptr != nullptr) {
212  delete_aspell_config(this->ptr);
213 
214  this->ptr = nullptr;
215  }
216  }
217 
218  /*
219  * COPY AND MOVE
220  */
221 
223 
237  if(other.ptr != nullptr) {
238  aspell_config_assign(this->ptr, other.ptr);
239  }
240  }
241 
243 
256  inline AspellConfig::AspellConfig(AspellConfig&& other) noexcept : ptr(other.ptr) {
257  other.ptr = nullptr;
258  }
259 
261 
279  if(&other != this) {
280  this->clear();
281 
282  aspell_config_assign(this->ptr, other.ptr);
283  }
284 
285  return *this;
286  }
287 
289 
310  if(&other != this) {
311  this->clear();
312 
313  this->ptr = other.ptr;
314 
315  other.ptr = nullptr;
316  }
317 
318  return *this;
319  }
320 
321 } /* namespace crawlservpp::Wrapper */
322 
323 #endif /* WRAPPER_ASPELLCONFIG_HPP_ */
::AspellConfig * get()
Gets a pointer to the underlying configuration.
Definition: AspellConfig.hpp:141
bool valid() const
Gets whether the configuration is valid.
Definition: AspellConfig.hpp:163
RAII wrapper for aspell configurations.
Definition: AspellConfig.hpp:58
#define MAIN_EXCEPTION_CLASS()
Macro used to easily define classes for general exceptions.
Definition: Exception.hpp:50
AspellConfig & operator=(const AspellConfig &other)
Copy assignment operator.
Definition: AspellConfig.hpp:278
const ::AspellConfig * getc() const
Gets a constant pointer to the underlying configuration.
Definition: AspellConfig.hpp:154
Class for aspell configuration-specific exceptions.
Definition: AspellConfig.hpp:101
virtual ~AspellConfig()
Destructor deleting the configuration, if necessary.
Definition: AspellConfig.hpp:125
void clear()
Deletes the configuration, if necessary.
Definition: AspellConfig.hpp:210
Namespace for RAII wrappers and Wrapper::Database.
Definition: Database.hpp:109
void setOption(const std::string &name, const std::string &value)
Sets an option in the configuration.
Definition: AspellConfig.hpp:186
AspellConfig()
Constructor creating a new configuration.
Definition: AspellConfig.hpp:117