Chris@64: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@64: // Licensed under the MIT License: Chris@64: // Chris@64: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@64: // of this software and associated documentation files (the "Software"), to deal Chris@64: // in the Software without restriction, including without limitation the rights Chris@64: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@64: // copies of the Software, and to permit persons to whom the Software is Chris@64: // furnished to do so, subject to the following conditions: Chris@64: // Chris@64: // The above copyright notice and this permission notice shall be included in Chris@64: // all copies or substantial portions of the Software. Chris@64: // Chris@64: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@64: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@64: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@64: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@64: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@64: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@64: // THE SOFTWARE. Chris@64: Chris@64: #ifndef CAPNP_BLOB_H_ Chris@64: #define CAPNP_BLOB_H_ Chris@64: Chris@64: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) Chris@64: #pragma GCC system_header Chris@64: #endif Chris@64: Chris@64: #include Chris@64: #include Chris@64: #include "common.h" Chris@64: #include Chris@64: Chris@64: namespace capnp { Chris@64: Chris@64: struct Data { Chris@64: Data() = delete; Chris@64: class Reader; Chris@64: class Builder; Chris@64: class Pipeline {}; Chris@64: }; Chris@64: Chris@64: struct Text { Chris@64: Text() = delete; Chris@64: class Reader; Chris@64: class Builder; Chris@64: class Pipeline {}; Chris@64: }; Chris@64: Chris@64: class Data::Reader: public kj::ArrayPtr { Chris@64: // Points to a blob of bytes. The usual Reader rules apply -- Data::Reader behaves like a simple Chris@64: // pointer which does not own its target, can be passed by value, etc. Chris@64: Chris@64: public: Chris@64: typedef Data Reads; Chris@64: Chris@64: Reader() = default; Chris@64: inline Reader(decltype(nullptr)): ArrayPtr(nullptr) {} Chris@64: inline Reader(const byte* value, size_t size): ArrayPtr(value, size) {} Chris@64: inline Reader(const kj::Array& value): ArrayPtr(value) {} Chris@64: inline Reader(const ArrayPtr& value): ArrayPtr(value) {} Chris@64: inline Reader(const kj::Array& value): ArrayPtr(value) {} Chris@64: inline Reader(const ArrayPtr& value): ArrayPtr(value) {} Chris@64: }; Chris@64: Chris@64: class Text::Reader: public kj::StringPtr { Chris@64: // Like Data::Reader, but points at NUL-terminated UTF-8 text. The NUL terminator is not counted Chris@64: // in the size but must be present immediately after the last byte. Chris@64: // Chris@64: // Text::Reader's interface contract is that its data MUST be NUL-terminated. The producer of Chris@64: // the Text::Reader must guarantee this, so that the consumer need not check. The data SHOULD Chris@64: // also be valid UTF-8, but this is NOT guaranteed -- the consumer must verify if it cares. Chris@64: Chris@64: public: Chris@64: typedef Text Reads; Chris@64: Chris@64: Reader() = default; Chris@64: inline Reader(decltype(nullptr)): StringPtr(nullptr) {} Chris@64: inline Reader(const char* value): StringPtr(value) {} Chris@64: inline Reader(const char* value, size_t size): StringPtr(value, size) {} Chris@64: inline Reader(const kj::String& value): StringPtr(value) {} Chris@64: inline Reader(const StringPtr& value): StringPtr(value) {} Chris@64: Chris@64: #if KJ_COMPILER_SUPPORTS_STL_STRING_INTEROP Chris@64: template ().c_str())> Chris@64: inline Reader(const T& t): StringPtr(t) {} Chris@64: // Allow implicit conversion from any class that has a c_str() method (namely, std::string). Chris@64: // We use a template trick to detect std::string in order to avoid including the header for Chris@64: // those who don't want it. Chris@64: #endif Chris@64: }; Chris@64: Chris@64: class Data::Builder: public kj::ArrayPtr { Chris@64: // Like Data::Reader except the pointers aren't const. Chris@64: Chris@64: public: Chris@64: typedef Data Builds; Chris@64: Chris@64: Builder() = default; Chris@64: inline Builder(decltype(nullptr)): ArrayPtr(nullptr) {} Chris@64: inline Builder(byte* value, size_t size): ArrayPtr(value, size) {} Chris@64: inline Builder(kj::Array& value): ArrayPtr(value) {} Chris@64: inline Builder(ArrayPtr value): ArrayPtr(value) {} Chris@64: Chris@64: inline Data::Reader asReader() const { return Data::Reader(*this); } Chris@64: inline operator Reader() const { return asReader(); } Chris@64: }; Chris@64: Chris@64: class Text::Builder: public kj::DisallowConstCopy { Chris@64: // Basically identical to kj::StringPtr, except that the contents are non-const. Chris@64: Chris@64: public: Chris@64: inline Builder(): content(nulstr, 1) {} Chris@64: inline Builder(decltype(nullptr)): content(nulstr, 1) {} Chris@64: inline Builder(char* value): content(value, strlen(value) + 1) {} Chris@64: inline Builder(char* value, size_t size): content(value, size + 1) { Chris@64: KJ_IREQUIRE(value[size] == '\0', "StringPtr must be NUL-terminated."); Chris@64: } Chris@64: Chris@64: inline Reader asReader() const { return Reader(content.begin(), content.size() - 1); } Chris@64: inline operator Reader() const { return asReader(); } Chris@64: Chris@64: inline operator kj::ArrayPtr(); Chris@64: inline kj::ArrayPtr asArray(); Chris@64: inline operator kj::ArrayPtr() const; Chris@64: inline kj::ArrayPtr asArray() const; Chris@64: inline kj::ArrayPtr asBytes() { return asArray().asBytes(); } Chris@64: inline kj::ArrayPtr asBytes() const { return asArray().asBytes(); } Chris@64: // Result does not include NUL terminator. Chris@64: Chris@64: inline operator kj::StringPtr() const; Chris@64: inline kj::StringPtr asString() const; Chris@64: Chris@64: inline const char* cStr() const { return content.begin(); } Chris@64: // Returns NUL-terminated string. Chris@64: Chris@64: inline size_t size() const { return content.size() - 1; } Chris@64: // Result does not include NUL terminator. Chris@64: Chris@64: inline char operator[](size_t index) const { return content[index]; } Chris@64: inline char& operator[](size_t index) { return content[index]; } Chris@64: Chris@64: inline char* begin() { return content.begin(); } Chris@64: inline char* end() { return content.end() - 1; } Chris@64: inline const char* begin() const { return content.begin(); } Chris@64: inline const char* end() const { return content.end() - 1; } Chris@64: Chris@64: inline bool operator==(decltype(nullptr)) const { return content.size() <= 1; } Chris@64: inline bool operator!=(decltype(nullptr)) const { return content.size() > 1; } Chris@64: Chris@64: inline bool operator==(Builder other) const { return asString() == other.asString(); } Chris@64: inline bool operator!=(Builder other) const { return asString() != other.asString(); } Chris@64: inline bool operator< (Builder other) const { return asString() < other.asString(); } Chris@64: inline bool operator> (Builder other) const { return asString() > other.asString(); } Chris@64: inline bool operator<=(Builder other) const { return asString() <= other.asString(); } Chris@64: inline bool operator>=(Builder other) const { return asString() >= other.asString(); } Chris@64: Chris@64: inline kj::StringPtr slice(size_t start) const; Chris@64: inline kj::ArrayPtr slice(size_t start, size_t end) const; Chris@64: inline Builder slice(size_t start); Chris@64: inline kj::ArrayPtr slice(size_t start, size_t end); Chris@64: // A string slice is only NUL-terminated if it is a suffix, so slice() has a one-parameter Chris@64: // version that assumes end = size(). Chris@64: Chris@64: private: Chris@64: inline explicit Builder(kj::ArrayPtr content): content(content) {} Chris@64: Chris@64: kj::ArrayPtr content; Chris@64: Chris@64: static char nulstr[1]; Chris@64: }; Chris@64: Chris@64: inline kj::StringPtr KJ_STRINGIFY(Text::Builder builder) { Chris@64: return builder.asString(); Chris@64: } Chris@64: Chris@64: inline bool operator==(const char* a, const Text::Builder& b) { return a == b.asString(); } Chris@64: inline bool operator!=(const char* a, const Text::Builder& b) { return a != b.asString(); } Chris@64: Chris@64: inline Text::Builder::operator kj::StringPtr() const { Chris@64: return kj::StringPtr(content.begin(), content.size() - 1); Chris@64: } Chris@64: Chris@64: inline kj::StringPtr Text::Builder::asString() const { Chris@64: return kj::StringPtr(content.begin(), content.size() - 1); Chris@64: } Chris@64: Chris@64: inline Text::Builder::operator kj::ArrayPtr() { Chris@64: return content.slice(0, content.size() - 1); Chris@64: } Chris@64: Chris@64: inline kj::ArrayPtr Text::Builder::asArray() { Chris@64: return content.slice(0, content.size() - 1); Chris@64: } Chris@64: Chris@64: inline Text::Builder::operator kj::ArrayPtr() const { Chris@64: return content.slice(0, content.size() - 1); Chris@64: } Chris@64: Chris@64: inline kj::ArrayPtr Text::Builder::asArray() const { Chris@64: return content.slice(0, content.size() - 1); Chris@64: } Chris@64: Chris@64: inline kj::StringPtr Text::Builder::slice(size_t start) const { Chris@64: return asReader().slice(start); Chris@64: } Chris@64: inline kj::ArrayPtr Text::Builder::slice(size_t start, size_t end) const { Chris@64: return content.slice(start, end); Chris@64: } Chris@64: Chris@64: inline Text::Builder Text::Builder::slice(size_t start) { Chris@64: return Text::Builder(content.slice(start, content.size())); Chris@64: } Chris@64: inline kj::ArrayPtr Text::Builder::slice(size_t start, size_t end) { Chris@64: return content.slice(start, end); Chris@64: } Chris@64: Chris@64: } // namespace capnp Chris@64: Chris@64: #endif // CAPNP_BLOB_H_