libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_string_ref.hpp
1#ifndef GIMBAL_STRING_REF_HPP
2#define GIMBAL_STRING_REF_HPP
3
6
7namespace gbl {
8
9class StringRef {
10private:
11 GblStringRef* pRef_ = nullptr;
12
13public:
14
15 StringRef() noexcept = default;
16
17 StringRef(const char* pStr, std::size_t len=0) noexcept:
18 pRef_(GblStringRef_create(pStr, len)) { }
19
20 StringRef(const StringRef& rhs) noexcept:
21 pRef_(GblStringRef_ref(rhs.pRef_)) { }
22
23 StringRef(StringRef&& rhs) noexcept:
24 pRef_(rhs.pRef_)
25 {
26 rhs.pRef_ = nullptr;
27 }
28
29 StringRef fromGblRef(GblStringRef* pRef) noexcept {
30 StringRef ref;
31 ref.pRef_ = pRef;
32 return ref;
33 }
34
35 GblStringRef* toGblRef() noexcept {
36 GblStringRef* pTemp = pRef_;
37 pRef_ = nullptr;
38 return pTemp;
39 }
40
41 ~StringRef() noexcept {
43 }
44
45 StringRef& operator=(const char* pCStr) noexcept {
47 GblStringRef_create(pCStr);
48 return *this;
49 }
50
51 StringRef& operator=(const StringRef& rhs) noexcept {
53 GblStringRef_ref(rhs.pRef_);
54 return *this;
55 }
56
57 StringRef& operator=(StringRef&& rhs) noexcept {
59 pRef_ = rhs.pRef_;
60 rhs.pRef_ = nullptr;
61 return *this;
62 }
63
64 operator const char*() const noexcept {
65 return pRef_;
66 }
67
68 char operator[](std::size_t index) const {
69 if(index >= length())
70 throw std::out_of_range {
71 "Attempt to index StringRef out-of-range!"
72 };
73
74 return pRef_[index];
75 }
76
77 StringView view(std::size_t offset=0, std::size_t len=0) const {
78 if(!len)
79 len = length() - offset;
80
81 if(offset + len > length())
82 throw std::out_of_range {
83 "Attempt to create out-of-range StringView from StringRef"
84 };
85
86
87 GblStringView view;
88 view.pData = pRef_ + offset;
89 view.length = len;
90 view.nullTerminated = (offset + len == length());
91
92 return view;
93 }
94
95 std::size_t refCount() const noexcept {
96 return GblStringRef_refCount(pRef_);
97 }
98
99 std::size_t length() const noexcept {
100 return GblStringRef_length(pRef_);
101 }
102
103 bool valid() const noexcept {
104 return GblStringRef_valid(pRef_);
105 }
106
107 bool empty() const noexcept {
108 return GblStringRef_empty(pRef_);
109 }
110
111 bool blank() const noexcept {
112 return GblStringRef_blank(pRef_);
113 }
114
115 // comparisons
116 // ostream
117 // hash
118 // swap
119 // better creation? snprintf()-y stuff?
120};
121
122}
123
124#endif // GIMBAL_STRING_REF_HPP
size_t GblStringRef_length(const GblStringRef *pSelf)
Returns the cached length of the given GblStringRef.
GblStringRef * GblStringRef_ref(GblStringRef *pRef)
Returns a new reference to pRef, incrementing its internal reference count rather than actually copyi...
GblBool GblStringRef_blank(const GblStringRef *pSelf)
Returns whether the given GblStringRef is blank, containing only NULL or spacing characters.
GblBool GblStringRef_valid(const GblStringRef *pSelf)
Returns whether the given GblStringRef is valid (not NULL)
GblRefCount GblStringRef_unref(GblStringRef *pRef)
Releases a reference to pRef, freeing the allocation if it was the last, returning the new refCount.
GblBool GblStringRef_empty(const GblStringRef *pSelf)
Returns whether the given GblStringRef is empty, with nothing but a NULL terminator.
GblRefCount GblStringRef_refCount(const GblStringRef *pSelf)
Returns the number of active references remaining to the given GblStringRef.
const char GblStringRef
Reference-counted, const char*-compatible string type.
const char * pData
Start address of the string being viewed.
OO C++ binding object around GblStringView.