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