aboutsummaryrefslogtreecommitdiff
path: root/include/cru/common
diff options
context:
space:
mode:
Diffstat (limited to 'include/cru/common')
-rw-r--r--include/cru/common/Base.hpp23
-rw-r--r--include/cru/common/Exception.hpp21
-rw-r--r--include/cru/common/String.hpp32
-rw-r--r--include/cru/common/StringUtil.hpp1
4 files changed, 53 insertions, 24 deletions
diff --git a/include/cru/common/Base.hpp b/include/cru/common/Base.hpp
index 93b0008c..5ed1e763 100644
--- a/include/cru/common/Base.hpp
+++ b/include/cru/common/Base.hpp
@@ -36,6 +36,12 @@
classname(classname&&) = delete; \
classname& operator=(classname&&) = delete;
+#define CRU_DEFAULT_DESTRUCTOR(classname) ~classname() override = default;
+
+#define CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(classname) \
+ classname() = default; \
+ ~classname() override = default;
+
namespace cru {
class CRU_BASE_API Object {
public:
@@ -66,21 +72,4 @@ inline void hash_combine(std::size_t& s, const T& v) {
#define CRU_DEFINE_CLASS_LOG_TAG(tag) \
private: \
constexpr static std::u16string_view log_tag = tag;
-
-class CRU_BASE_API Exception {
- public:
- Exception() = default;
- Exception(std::u16string message) : message_(std::move(message)) {}
-
- CRU_DEFAULT_COPY(Exception)
- CRU_DEFAULT_MOVE(Exception)
-
- virtual ~Exception() = default;
-
- public:
- std::u16string GetMessage() const { return message_; }
-
- private:
- std::u16string message_;
-};
} // namespace cru
diff --git a/include/cru/common/Exception.hpp b/include/cru/common/Exception.hpp
new file mode 100644
index 00000000..861bf5e9
--- /dev/null
+++ b/include/cru/common/Exception.hpp
@@ -0,0 +1,21 @@
+#pragma once
+#include "String.hpp"
+
+namespace cru {
+class CRU_BASE_API Exception {
+ public:
+ Exception() = default;
+ Exception(String message) : message_(std::move(message)) {}
+
+ CRU_DEFAULT_COPY(Exception)
+ CRU_DEFAULT_MOVE(Exception)
+
+ virtual ~Exception() = default;
+
+ public:
+ String GetMessage() const { return message_; }
+
+ private:
+ String message_;
+};
+} // namespace cru
diff --git a/include/cru/common/String.hpp b/include/cru/common/String.hpp
index ea9480f0..e779e3a6 100644
--- a/include/cru/common/String.hpp
+++ b/include/cru/common/String.hpp
@@ -9,21 +9,24 @@
namespace cru {
class CRU_BASE_API String {
public:
- static String fromUtf8(const char* str, Index size);
+ static String FromUtf8(std::string_view str) {
+ return FromUtf8(str.data(), str.size());
+ }
+ static String FromUtf8(const char* str, Index size);
- static String fromUtf16(const std::uint16_t* str) { return String(str); }
- static String fromUtf16(const std::uint16_t* str, Index size) {
+ static String FromUtf16(const std::uint16_t* str) { return String(str); }
+ static String FromUtf16(const std::uint16_t* str, Index size) {
return String(str, size);
}
- static String fromUtf16(const char16_t* str) { return String(str); }
- static String fromUtf16(const char16_t* str, Index size) {
+ static String FromUtf16(const char16_t* str) { return String(str); }
+ static String FromUtf16(const char16_t* str, Index size) {
return String(str, size);
}
#ifdef CRU_PLATFORM_WINDOWS
- static String fromUtf16(wchar_t* str) { return String(str); }
- static String fromUtf16(wchar_t* str, Index size) {
+ static String FromUtf16(wchar_t* str) { return String(str); }
+ static String FromUtf16(wchar_t* str, Index size) {
return String(str, size);
}
#endif
@@ -49,10 +52,12 @@ class CRU_BASE_API String {
String(const char16_t* str);
String(const char16_t* str, Index size);
+ String(const std::u16string& str) : String(str.data(), str.size()) {}
#ifdef CRU_PLATFORM_WINDOWS
String(const wchar_t* str);
String(const wchar_t* str, Index size);
+ String(const std::wstring& str) : String(str.data(), str.size()) {}
#endif
String(const String& other);
@@ -121,6 +126,19 @@ class CRU_BASE_API String {
this->insert(cend(), str, size);
}
+ public:
+ const char16_t* Char16CStr() const {
+ return reinterpret_cast<const char16_t*>(c_str());
+ }
+
+#ifdef CRU_PLATFORM_WINDOWS
+ const wchar_t* WinCStr() const {
+ return reinterpret_cast<const wchar_t*>(c_str());
+ }
+#endif
+
+ std::string ToUtf8() const;
+
private:
static std::uint16_t kEmptyBuffer[1];
diff --git a/include/cru/common/StringUtil.hpp b/include/cru/common/StringUtil.hpp
index 481566a6..985f0032 100644
--- a/include/cru/common/StringUtil.hpp
+++ b/include/cru/common/StringUtil.hpp
@@ -1,5 +1,6 @@
#pragma once
#include "Base.hpp"
+#include "Exception.hpp"
#include <functional>
#include <string>