Ultralight  1.0.0
A fast, lightweight, HTML UI engine for native apps.
String16.h
Go to the documentation of this file.
1 #pragma once
15 #include <Ultralight/Defines.h>
16 #include <Ultralight/RefPtr.h>
17 #include <stddef.h>
18 
19 namespace ultralight {
20 
21 namespace detail {
22  template<int> struct selector;
23  template<> struct selector<4> { typedef char16_t Char16; };
24  template<> struct selector<2> { typedef wchar_t Char16; };
25 }
26 
27 #ifdef DISABLE_NATIVE_WCHAR_T
28 // Force Char16 type to use char16_t, used on Windows when native wchar_t support is disabled.
29 typedef char16_t Char16;
30 #else
31 // We use wchar_t if size == 2, otherwise use char16_t
32 typedef detail::selector<sizeof(wchar_t)>::Char16 Char16;
33 #endif
34 
38 class UExport String16 {
39 public:
40  // Make an empty String16
41  String16();
42 
43  // Make a String16 from null-terminated ASCII C-string
44  String16(const char* c_str);
45 
46  // Make a String16 from ASCII C-string with certain length
47  String16(const char* c_str, size_t len);
48 
49  // Make a String16 from raw UTF-16 buffer with certain length
50  String16(const Char16* str, size_t len);
51 
52  // Make a String16 from raw unsigned short UTF-16 buffer with certain length. Useful on Windows
53  // when native support for wchar_t is disabled (eg, /Zc:wchar_t-).
54  String16(const unsigned short* str, size_t len);
55 
56  // Make a deep copy of String16
57  String16(const String16& other);
58 
59  ~String16();
60 
61  // Assign a String16 to this one, deep copy is made
62  String16& operator=(const String16& other);
63 
64  // Append a String16 to this one.
65  String16& operator+=(const String16& other);
66 
67  // Concatenation operator
68  inline friend String16 operator+(String16 lhs, const String16& rhs) { lhs += rhs; return lhs; }
69 
70  // Get raw UTF-16 data
71  Char16* data() { return data_; }
72 
73  // Get raw UTF-16 data (const)
74  const Char16* data() const { return data_; }
75 
76  // Get raw UTF-16 data as unsigned short. This is useful on Windows if you compile without native
77  // support for wchar_t (eg, /Zc:wchar_t-)
78  unsigned short* udata() { return reinterpret_cast<unsigned short*>(data_); }
79 
80  // Get raw UTF-16 data as unsigned short (const).
81  const unsigned short* udata() const { return reinterpret_cast<const unsigned short*>(data_); }
82 
83  // Get length in characters.
84  size_t length() const { return length_; }
85 
86  // Get size in characters (synonym for length)
87  size_t size() const { return length_; }
88 
89  // Check if string is empty.
90  bool empty() const { return !data_ || length_ == 0; }
91 
92  // Get character at specific position
93  Char16& operator[](size_t pos) { return data_[pos]; }
94 
95  // Get character at specific position (const)
96  const Char16& operator[](size_t pos) const { return data_[pos]; }
97 
98 private:
99  Char16* data_;
100  size_t length_;
101 };
102 
106 class UExport String16Vector : public RefCounted {
107 public:
108  // Create an empty string vector
109  static Ref<String16Vector> Create();
110 
111  // Create a string vector from an existing array (a deep copy is made)
112  static Ref<String16Vector> Create(const String16* stringArray, size_t len);
113 
114  // Add an element to the back of the string vector
115  virtual void push_back(const String16& val) = 0;
116 
117  // Get raw String16 vector array
118  virtual String16* data() = 0;
119 
120  // Get the number of elements in vector
121  virtual size_t size() const = 0;
122 
123 protected:
124  String16Vector();
125  virtual ~String16Vector();
127  void operator=(const String16Vector&);
128 };
129 
130 } // namespace ultralight
A UTF-16 string vector.
Definition: String16.h:106
A non-nullable smart pointer.
Definition: RefPtr.h:64
This is a set of common JavaScriptCore Helper functions to simplify sample code.
Definition: App.h:19
The header for all ref-counting utilities.
Interface for all ref-counted objects that will be managed using the Ref<> and RefPtr<> smart pointer...
Definition: RefPtr.h:53
A UTF-16 string container.
Definition: String16.h:38