aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-09-15 19:17:38 +0800
committercrupest <crupest@outlook.com>2021-09-15 19:17:38 +0800
commitcb981f5a337f3a8fc9d450b891c358c2b8dc29d3 (patch)
tree5216a09c127e534edfebca22f8b91d876812f6b4 /include
parent105e4ad880a810300bf4b3a0a0752ae58924667b (diff)
downloadcru-cb981f5a337f3a8fc9d450b891c358c2b8dc29d3.tar.gz
cru-cb981f5a337f3a8fc9d450b891c358c2b8dc29d3.tar.bz2
cru-cb981f5a337f3a8fc9d450b891c358c2b8dc29d3.zip
...
Diffstat (limited to 'include')
-rw-r--r--include/cru/common/String.hpp54
-rw-r--r--include/cru/platform/GraphicsBase.hpp (renamed from include/cru/platform/GraphBase.hpp)0
-rw-r--r--include/cru/platform/Matrix.hpp2
-rw-r--r--include/cru/platform/graphics/Base.hpp3
-rw-r--r--include/cru/platform/gui/InputMethod.hpp4
-rw-r--r--include/cru/platform/gui/Keyboard.hpp6
-rw-r--r--include/cru/ui/render/MeasureRequirement.hpp5
-rw-r--r--include/cru/ui/render/RenderObject.hpp1
8 files changed, 48 insertions, 27 deletions
diff --git a/include/cru/common/String.hpp b/include/cru/common/String.hpp
index 47b20147..ed3deb4c 100644
--- a/include/cru/common/String.hpp
+++ b/include/cru/common/String.hpp
@@ -7,9 +7,11 @@
#include <algorithm>
#include <array>
#include <charconv>
+#include <initializer_list>
#include <iterator>
#include <limits>
#include <stdexcept>
+#include <string>
#include <system_error>
#include <type_traits>
#include <vector>
@@ -62,12 +64,25 @@ class CRU_BASE_API String {
public:
String() = default;
- String(const std::uint16_t* str);
+ explicit String(const std::uint16_t* str);
String(const std::uint16_t* str, Index size);
- String(const char16_t* str);
+ explicit String(const char16_t* str);
String(const char16_t* str, Index size);
- String(const std::u16string& str) : String(str.data(), str.size()) {}
+ String(const std::u16string_view& str) : String(str.data(), str.size()) {}
+
+ template <Index size>
+ constexpr String(const char16_t (&str)[size]) : String(str, size) {}
+
+ template <typename Iter>
+ String(Iter start, Iter end) {
+ for (; start != end; start++) {
+ append(*start);
+ }
+ }
+
+ String(const std::initializer_list<std::uint16_t>& l)
+ : String(l.begin(), l.end()) {}
#ifdef CRU_PLATFORM_WINDOWS
String(const wchar_t* str);
@@ -147,14 +162,10 @@ class CRU_BASE_API String {
void append(const std::uint16_t* str, Index size) {
this->insert(cend(), str, size);
}
- void append(const String& other) { append(other.data(), other.size()); }
inline void append(StringView str);
public:
- String& operator+=(const String& other) {
- append(other);
- return *this;
- }
+ String& operator+=(const String& other);
public:
Utf16CodePointIterator CodePointIterator() const {
@@ -201,8 +212,12 @@ inline String operator+(const String& left, const String& right) {
return result;
}
-template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
-String ToString(T value) {
+inline String ToString(bool value) {
+ return value ? String(u"true") : String(u"false");
+}
+
+template <typename T>
+std::enable_if_t<std::is_integral_v<T>, String> ToString(T value) {
std::array<char, 50> buffer;
auto result =
std::to_chars(buffer.data(), buffer.data() + buffer.size(), value);
@@ -219,6 +234,12 @@ String ToString(T value) {
}
}
+template <typename T>
+std::enable_if_t<std::is_floating_point_v<T>, String> ToString(T value) {
+ auto str = std::to_string(value);
+ return String(str.cbegin(), str.cend());
+}
+
inline String ToString(String value) { return std::move(value); }
namespace details {
@@ -285,18 +306,19 @@ class CRU_BASE_API StringView {
StringView() = default;
- StringView(const std::uint16_t* ptr);
- StringView(const std::uint16_t* ptr, Index size) : ptr_(ptr), size_(size) {}
+ constexpr StringView(const std::uint16_t* ptr, Index size)
+ : ptr_(ptr), size_(size) {}
- StringView(const char16_t* ptr)
- : StringView(reinterpret_cast<const std::uint16_t*>(ptr)) {}
+ template <Index size>
+ StringView(const char16_t (&array)[size]) : StringView(array, size) {}
StringView(const char16_t* ptr, Index size)
- : StringView(reinterpret_cast<const std::uint16_t*>(ptr), size) {}
+ : ptr_(reinterpret_cast<const std::uint16_t*>(ptr)), size_(size) {}
StringView(const String& str) : StringView(str.data(), str.size()) {}
CRU_DEFAULT_COPY(StringView)
- CRU_DEFAULT_MOVE(StringView) ~StringView() = default;
+ CRU_DEFAULT_MOVE(StringView)
+ ~StringView() = default;
Index size() const { return size_; }
const std::uint16_t* data() const { return ptr_; }
diff --git a/include/cru/platform/GraphBase.hpp b/include/cru/platform/GraphicsBase.hpp
index e7201ec0..e7201ec0 100644
--- a/include/cru/platform/GraphBase.hpp
+++ b/include/cru/platform/GraphicsBase.hpp
diff --git a/include/cru/platform/Matrix.hpp b/include/cru/platform/Matrix.hpp
index 8ec5faaa..7985af0e 100644
--- a/include/cru/platform/Matrix.hpp
+++ b/include/cru/platform/Matrix.hpp
@@ -1,5 +1,5 @@
#pragma once
-#include "GraphBase.hpp"
+#include "GraphicsBase.hpp"
#include <cmath>
diff --git a/include/cru/platform/graphics/Base.hpp b/include/cru/platform/graphics/Base.hpp
index 25881772..60af40ab 100644
--- a/include/cru/platform/graphics/Base.hpp
+++ b/include/cru/platform/graphics/Base.hpp
@@ -1,5 +1,6 @@
#pragma once
-#include "../GraphBase.hpp"
+#include "../Color.hpp"
+#include "../GraphicsBase.hpp"
#include "../Matrix.hpp"
#include "../Resource.hpp"
diff --git a/include/cru/platform/gui/InputMethod.hpp b/include/cru/platform/gui/InputMethod.hpp
index c259e63c..923a2088 100644
--- a/include/cru/platform/gui/InputMethod.hpp
+++ b/include/cru/platform/gui/InputMethod.hpp
@@ -16,7 +16,7 @@ struct CompositionClause {
using CompositionClauses = std::vector<CompositionClause>;
struct CompositionText {
- std::u16string text;
+ String text;
CompositionClauses clauses;
TextRange selection;
};
@@ -49,7 +49,7 @@ struct IInputMethodContext : virtual IPlatformResource {
// Triggered every time composition text changes.
virtual IEvent<std::nullptr_t>* CompositionEvent() = 0;
- virtual IEvent<std::u16string_view>* TextEvent() = 0;
+ virtual IEvent<StringView>* TextEvent() = 0;
};
} // namespace cru::platform::gui
diff --git a/include/cru/platform/gui/Keyboard.hpp b/include/cru/platform/gui/Keyboard.hpp
index b93b44fa..b025d86e 100644
--- a/include/cru/platform/gui/Keyboard.hpp
+++ b/include/cru/platform/gui/Keyboard.hpp
@@ -123,7 +123,7 @@ struct KeyModifiers {
static constexpr KeyModifier alt{0b100};
};
-CRU_PLATFORM_GUI_API std::u16string_view ToString(KeyCode key_code);
-CRU_PLATFORM_GUI_API std::u16string ToString(
- KeyModifier key_modifier, std::u16string_view separator = u"+");
+CRU_PLATFORM_GUI_API String ToString(KeyCode key_code);
+CRU_PLATFORM_GUI_API String ToString(KeyModifier key_modifier,
+ StringView separator = u"+");
} // namespace cru::platform::gui
diff --git a/include/cru/ui/render/MeasureRequirement.hpp b/include/cru/ui/render/MeasureRequirement.hpp
index ff9dd6e3..90d02a02 100644
--- a/include/cru/ui/render/MeasureRequirement.hpp
+++ b/include/cru/ui/render/MeasureRequirement.hpp
@@ -115,9 +115,8 @@ class MeasureLength final {
}
}
- std::u16string ToDebugString() const {
- return IsSpecified() ? ToUtf16String(GetLengthOrUndefined())
- : u"UNSPECIFIED";
+ String ToDebugString() const {
+ return IsSpecified() ? ToString(GetLengthOrUndefined()) : u"UNSPECIFIED";
}
private:
diff --git a/include/cru/ui/render/RenderObject.hpp b/include/cru/ui/render/RenderObject.hpp
index 622c27f2..0dd95c71 100644
--- a/include/cru/ui/render/RenderObject.hpp
+++ b/include/cru/ui/render/RenderObject.hpp
@@ -4,7 +4,6 @@
#include "MeasureRequirement.hpp"
#include "cru/common/Base.hpp"
#include "cru/common/Event.hpp"
-#include "cru/platform/GraphBase.hpp"
#include "cru/ui/Base.hpp"
#include <cstddef>