comparison osx/include/capnp/blob.h @ 49:3ab5a40c4e3b

Add Capnp and KJ builds for OSX
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 25 Oct 2016 14:48:23 +0100
parents
children 0994c39f1e94
comparison
equal deleted inserted replaced
48:9530b331f8c1 49:3ab5a40c4e3b
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21
22 #ifndef CAPNP_BLOB_H_
23 #define CAPNP_BLOB_H_
24
25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
26 #pragma GCC system_header
27 #endif
28
29 #include <kj/common.h>
30 #include <kj/string.h>
31 #include "common.h"
32 #include <string.h>
33
34 namespace capnp {
35
36 struct Data {
37 Data() = delete;
38 class Reader;
39 class Builder;
40 class Pipeline {};
41 };
42
43 struct Text {
44 Text() = delete;
45 class Reader;
46 class Builder;
47 class Pipeline {};
48 };
49
50 class Data::Reader: public kj::ArrayPtr<const byte> {
51 // Points to a blob of bytes. The usual Reader rules apply -- Data::Reader behaves like a simple
52 // pointer which does not own its target, can be passed by value, etc.
53
54 public:
55 typedef Data Reads;
56
57 Reader() = default;
58 inline Reader(decltype(nullptr)): ArrayPtr<const byte>(nullptr) {}
59 inline Reader(const byte* value, size_t size): ArrayPtr<const byte>(value, size) {}
60 inline Reader(const kj::Array<const byte>& value): ArrayPtr<const byte>(value) {}
61 inline Reader(const ArrayPtr<const byte>& value): ArrayPtr<const byte>(value) {}
62 inline Reader(const kj::Array<byte>& value): ArrayPtr<const byte>(value) {}
63 inline Reader(const ArrayPtr<byte>& value): ArrayPtr<const byte>(value) {}
64 };
65
66 class Text::Reader: public kj::StringPtr {
67 // Like Data::Reader, but points at NUL-terminated UTF-8 text. The NUL terminator is not counted
68 // in the size but must be present immediately after the last byte.
69 //
70 // Text::Reader's interface contract is that its data MUST be NUL-terminated. The producer of
71 // the Text::Reader must guarantee this, so that the consumer need not check. The data SHOULD
72 // also be valid UTF-8, but this is NOT guaranteed -- the consumer must verify if it cares.
73
74 public:
75 typedef Text Reads;
76
77 Reader() = default;
78 inline Reader(decltype(nullptr)): StringPtr(nullptr) {}
79 inline Reader(const char* value): StringPtr(value) {}
80 inline Reader(const char* value, size_t size): StringPtr(value, size) {}
81 inline Reader(const kj::String& value): StringPtr(value) {}
82 inline Reader(const StringPtr& value): StringPtr(value) {}
83
84 #if KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP
85 template <typename T, typename = decltype(kj::instance<T>().c_str())>
86 inline Reader(const T& t): StringPtr(t) {}
87 // Allow implicit conversion from any class that has a c_str() method (namely, std::string).
88 // We use a template trick to detect std::string in order to avoid including the header for
89 // those who don't want it.
90 #endif
91 };
92
93 class Data::Builder: public kj::ArrayPtr<byte> {
94 // Like Data::Reader except the pointers aren't const.
95
96 public:
97 typedef Data Builds;
98
99 Builder() = default;
100 inline Builder(decltype(nullptr)): ArrayPtr<byte>(nullptr) {}
101 inline Builder(byte* value, size_t size): ArrayPtr<byte>(value, size) {}
102 inline Builder(kj::Array<byte>& value): ArrayPtr<byte>(value) {}
103 inline Builder(ArrayPtr<byte> value): ArrayPtr<byte>(value) {}
104
105 inline Data::Reader asReader() const { return Data::Reader(*this); }
106 inline operator Reader() const { return asReader(); }
107 };
108
109 class Text::Builder: public kj::DisallowConstCopy {
110 // Basically identical to kj::StringPtr, except that the contents are non-const.
111
112 public:
113 inline Builder(): content(nulstr, 1) {}
114 inline Builder(decltype(nullptr)): content(nulstr, 1) {}
115 inline Builder(char* value): content(value, strlen(value) + 1) {}
116 inline Builder(char* value, size_t size): content(value, size + 1) {
117 KJ_IREQUIRE(value[size] == '\0', "StringPtr must be NUL-terminated.");
118 }
119
120 inline Reader asReader() const { return Reader(content.begin(), content.size() - 1); }
121 inline operator Reader() const { return asReader(); }
122
123 inline operator kj::ArrayPtr<char>();
124 inline kj::ArrayPtr<char> asArray();
125 inline operator kj::ArrayPtr<const char>() const;
126 inline kj::ArrayPtr<const char> asArray() const;
127 inline kj::ArrayPtr<byte> asBytes() { return asArray().asBytes(); }
128 inline kj::ArrayPtr<const byte> asBytes() const { return asArray().asBytes(); }
129 // Result does not include NUL terminator.
130
131 inline operator kj::StringPtr() const;
132 inline kj::StringPtr asString() const;
133
134 inline const char* cStr() const { return content.begin(); }
135 // Returns NUL-terminated string.
136
137 inline size_t size() const { return content.size() - 1; }
138 // Result does not include NUL terminator.
139
140 inline char operator[](size_t index) const { return content[index]; }
141 inline char& operator[](size_t index) { return content[index]; }
142
143 inline char* begin() { return content.begin(); }
144 inline char* end() { return content.end() - 1; }
145 inline const char* begin() const { return content.begin(); }
146 inline const char* end() const { return content.end() - 1; }
147
148 inline bool operator==(decltype(nullptr)) const { return content.size() <= 1; }
149 inline bool operator!=(decltype(nullptr)) const { return content.size() > 1; }
150
151 inline bool operator==(Builder other) const { return asString() == other.asString(); }
152 inline bool operator!=(Builder other) const { return asString() != other.asString(); }
153 inline bool operator< (Builder other) const { return asString() < other.asString(); }
154 inline bool operator> (Builder other) const { return asString() > other.asString(); }
155 inline bool operator<=(Builder other) const { return asString() <= other.asString(); }
156 inline bool operator>=(Builder other) const { return asString() >= other.asString(); }
157
158 inline kj::StringPtr slice(size_t start) const;
159 inline kj::ArrayPtr<const char> slice(size_t start, size_t end) const;
160 inline Builder slice(size_t start);
161 inline kj::ArrayPtr<char> slice(size_t start, size_t end);
162 // A string slice is only NUL-terminated if it is a suffix, so slice() has a one-parameter
163 // version that assumes end = size().
164
165 private:
166 inline explicit Builder(kj::ArrayPtr<char> content): content(content) {}
167
168 kj::ArrayPtr<char> content;
169
170 static char nulstr[1];
171 };
172
173 inline kj::StringPtr KJ_STRINGIFY(Text::Builder builder) {
174 return builder.asString();
175 }
176
177 inline bool operator==(const char* a, const Text::Builder& b) { return a == b.asString(); }
178 inline bool operator!=(const char* a, const Text::Builder& b) { return a != b.asString(); }
179
180 inline Text::Builder::operator kj::StringPtr() const {
181 return kj::StringPtr(content.begin(), content.size() - 1);
182 }
183
184 inline kj::StringPtr Text::Builder::asString() const {
185 return kj::StringPtr(content.begin(), content.size() - 1);
186 }
187
188 inline Text::Builder::operator kj::ArrayPtr<char>() {
189 return content.slice(0, content.size() - 1);
190 }
191
192 inline kj::ArrayPtr<char> Text::Builder::asArray() {
193 return content.slice(0, content.size() - 1);
194 }
195
196 inline Text::Builder::operator kj::ArrayPtr<const char>() const {
197 return content.slice(0, content.size() - 1);
198 }
199
200 inline kj::ArrayPtr<const char> Text::Builder::asArray() const {
201 return content.slice(0, content.size() - 1);
202 }
203
204 inline kj::StringPtr Text::Builder::slice(size_t start) const {
205 return asReader().slice(start);
206 }
207 inline kj::ArrayPtr<const char> Text::Builder::slice(size_t start, size_t end) const {
208 return content.slice(start, end);
209 }
210
211 inline Text::Builder Text::Builder::slice(size_t start) {
212 return Text::Builder(content.slice(start, content.size()));
213 }
214 inline kj::ArrayPtr<char> Text::Builder::slice(size_t start, size_t end) {
215 return content.slice(start, end);
216 }
217
218 } // namespace capnp
219
220 #endif // CAPNP_BLOB_H_