libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_quark.hpp
Go to the documentation of this file.
1/*! \file
2 * \brief gbl::Quark C++ bindings for GblQuark
3 * \ingroup strings
4 *
5 * This file contains the gbl::Quark wrapper around
6 * the GblQuark C API. It provides a high-level object-
7 * oriented string wrapper around interned strings.
8 *
9 * \sa gimbal_quark.h
10 *
11 * \author 2023 Falco Girgis
12 * \copyright MIT License
13 */
14
15#ifndef GIMBAL_QUARK_HPP
16#define GIMBAL_QUARK_HPP
17
18#include "gimbal_quark.h"
19#include <string>
20#include <string_view>
21#include <iostream>
22
23namespace gbl {
24
25/*! OO C++ wrapper object around GblQuark
26 * \ingroup strings
27 *
28 * gbl::Quark is the high-level C++ wrapper around
29 * GblQuark, which represents an interned string.
30 *
31 * \sa GblQuark
32 */
33class Quark {
34private:
36
37public:
38
39 Quark(void) = default;
40
41 Quark(GblQuark cQuark):
42 value_(cQuark) {}
43
44 Quark(const char* pString, std::size_t length=0):
45 Quark(GblQuark_tryString(pString, length)) {}
46
47 Quark(const std::string& stdStr):
48 Quark(stdStr.c_str()) {}
49
50 Quark(std::string_view strv):
51 Quark(strv.data(), strv.length()) {}
52
53 bool isValid(void) const {
54 return value_ != GBL_QUARK_INVALID;
55 }
56
57 const char* toString(void) const {
58 return GblQuark_toString(value_);
59 }
60
61 std::size_t length(void) const {
62 return isValid()?
63 strlen(GblQuark_toString(value_)) :
64 0;
65 }
66
67 operator GblQuark(void) const {
68 return value_;
69 }
70
71 char operator[](std::size_t index) const {
72 if(index >= length())
73 throw std::out_of_range("Out-of-range GblQuark access!");
74
75 return toString()[index];
76 }
77
78 Quark& operator=(const Quark& rhs) = default;
79
80 Quark& operator=(GblQuark cQuark) {
81 value_ = cQuark;
82 return *this;
83 }
84
85 Quark& operator=(const char* pString) {
86 value_ = GblQuark_tryString(pString);
87 return *this;
88 }
89
90 Quark& operator=(const std::string& stdStr) {
91 return *this = stdStr.c_str();
92 }
93
94 Quark& operator=(std::string_view strv) {
95 value_ = GblQuark_tryString(strv.data(), strv.length());
96 return *this;
97 }
98
99 static inline GblQuark Invalid;
100
101 static Quark fromString(const char* pString, std::size_t length=0) {
102 return GblQuark_fromString(pString, length);
103 }
104
105 static Quark fromString(const std::string& stdStr) {
106 return fromString(stdStr.c_str());
107 }
108
109 static Quark fromString(std::string_view strv) {
110 return fromString(strv.data(), strv.length());
111 }
112
113 static Quark fromStatic(const char* pString) {
114 return GblQuark_fromStatic(pString);
115 }
116
117 static const char* internString(const char* pString, std::size_t length=0) {
118 return GblQuark_internString(pString, length);
119 }
120
121 static const char* internStatic(const char* pString) {
122 return GblQuark_internStatic(pString);
123 }
124
125 friend std::ostream& operator<<(std::ostream &os, Quark quark) {
126 os << quark.toString();
127 return os;
128 }
129};
130
131inline Quark operator""_qrk(const char* pString, std::size_t size) noexcept {
132 return GblQuark_fromStatic(pString);
133}
134
135}
136
137#endif // GIMBAL_QUARK_HPP
OO C++ wrapper object around GblQuark.
const char * GblQuark_internStatic(const char *pString)
Creates a GblQuark from the given STATIC string (if necessary, saving on allocating),...
#define GBL_QUARK_INVALID
Value of an invalid or NULL GblQuark.
GblQuark GblQuark_fromStatic(const char *pSstring)
Returns the GblQuark associated with the given STATIC string, which can save an allocation when initi...
const char * GblQuark_toString(GblQuark quark)
Returns the NULL-terminated interned C string associated with a given GblQuark.
uintptr_t GblQuark
Uniquely identifiable interned string type.