Module: check_mk
Branch: master
Commit: e8f2aa00285412a6cdeb238131ce99b195790d8b
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=e8f2aa00285412…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Thu Feb 8 09:51:00 2018 +0100
Use pimpl idiom to hide regex implementation.
Change-Id: I2c17da9d678abab031fe48447573abae5ce0c192
---
livestatus/src/RegExp.cc | 31 ++++++++++++++++++++++++++-----
livestatus/src/RegExp.h | 16 +++++++++++++---
2 files changed, 39 insertions(+), 8 deletions(-)
diff --git a/livestatus/src/RegExp.cc b/livestatus/src/RegExp.cc
index 2e9545b..9a6d8bd 100644
--- a/livestatus/src/RegExp.cc
+++ b/livestatus/src/RegExp.cc
@@ -23,19 +23,40 @@
// Boston, MA 02110-1301 USA.
#include "RegExp.h"
+#include <regex>
+
+struct RegExp::Impl {
+ std::regex _regex;
+};
+
+RegExp::RegExp() : _impl(std::make_unique<Impl>()) {}
+
+RegExp::~RegExp() = default;
+
+RegExp::RegExp(const RegExp &rhs) noexcept
+ : _impl(std::make_unique<Impl>(*rhs._impl)) {}
+
+RegExp &RegExp::operator=(const RegExp &rhs) noexcept {
+ *_impl = *rhs._impl;
+ return *this;
+}
+
+RegExp::RegExp(RegExp &&rhs) noexcept = default;
+
+RegExp &RegExp::operator=(RegExp &&rhs) noexcept = default;
void RegExp::assign(const std::string &str, Case c) {
- _regex.assign(str, c == Case::ignore
- ? std::regex::extended | std::regex::icase
- : std::regex::extended);
+ _impl->_regex.assign(str, c == Case::ignore
+ ? std::regex::extended | std::regex::icase
+ : std::regex::extended);
}
std::string RegExp::replace(const std::string &str,
const std::string &replacement) const {
- return std::regex_replace(str, _regex, replacement,
+ return std::regex_replace(str, _impl->_regex, replacement,
std::regex_constants::format_sed);
}
bool RegExp::search(const std::string &str) const {
- return regex_search(str, _regex);
+ return regex_search(str, _impl->_regex);
}
diff --git a/livestatus/src/RegExp.h b/livestatus/src/RegExp.h
index d7b96b4..0d5feea 100644
--- a/livestatus/src/RegExp.h
+++ b/livestatus/src/RegExp.h
@@ -26,21 +26,31 @@
#define RegExp_h
#include "config.h" // IWYU pragma: keep
-#include <regex>
+#include <memory>
#include <string>
class RegExp {
public:
enum class Case { ignore, respect };
+ // Standard pimpl boileplate code, see Scott Meyer's "Effective Modern
C++",
+ // item 22: "When using the Pimpl Idiom, define special member functions in
+ // the implementation file."
+ RegExp();
+ ~RegExp();
+ RegExp(const RegExp &rhs) noexcept;
+ RegExp &operator=(const RegExp &rhs) noexcept;
+ RegExp(RegExp &&rhs) noexcept;
+ RegExp &operator=(RegExp &&rhs) noexcept;
+
void assign(const std::string &str, Case c);
std::string replace(const std::string &str,
const std::string &replacement) const;
-
bool search(const std::string &str) const;
private:
- std::regex _regex;
+ struct Impl;
+ std::unique_ptr<Impl> _impl;
};
#endif // RegExp_h