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