Chris@63: // Copyright (c) 2015 Philip Quinn. 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_SERIALIZE_TEXT_H_ Chris@63: #define CAPNP_SERIALIZE_TEXT_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 "dynamic.h" Chris@63: #include "orphan.h" Chris@63: #include "schema.h" Chris@63: Chris@63: namespace capnp { Chris@63: Chris@63: class TextCodec { Chris@63: // Reads and writes Cap'n Proto objects in a plain text format (as used in the schema Chris@63: // language for constants, and read/written by the 'decode' and 'encode' commands of Chris@63: // the capnp tool). Chris@63: // Chris@63: // This format is useful for debugging or human input, but it is not a robust alternative Chris@63: // to the binary format. Changes to a schema's types or names that are permitted in a Chris@63: // schema's binary evolution will likely break messages stored in this format. Chris@63: // Chris@63: // Note that definitions or references (to constants, other fields, or files) are not Chris@63: // permitted in this format. To evaluate declarations with the full expressiveness of the Chris@63: // schema language, see `capnp::SchemaParser`. Chris@63: // Chris@63: // Requires linking with the capnpc library. Chris@63: Chris@63: public: Chris@63: TextCodec(); Chris@63: ~TextCodec() noexcept(true); Chris@63: Chris@63: void setPrettyPrint(bool enabled); Chris@63: // If enabled, pads the output of `encode()` with spaces and newlines to make it more Chris@63: // human-readable. Chris@63: Chris@63: template Chris@63: kj::String encode(T&& value) const; Chris@63: kj::String encode(DynamicValue::Reader value) const; Chris@63: // Encode any Cap'n Proto value. Chris@63: Chris@63: template Chris@63: Orphan decode(kj::StringPtr input, Orphanage orphanage) const; Chris@63: // Decode a text message into a Cap'n Proto object of type T, allocated in the given Chris@63: // orphanage. Any errors parsing the input or assigning the fields of T are thrown as Chris@63: // exceptions. Chris@63: Chris@63: void decode(kj::StringPtr input, DynamicStruct::Builder output) const; Chris@63: // Decode a text message for a struct into the given builder. Any errors parsing the Chris@63: // input or assigning the fields of the output are thrown as exceptions. Chris@63: Chris@63: // TODO(someday): expose some control over the error handling? Chris@63: private: Chris@63: Orphan decode(kj::StringPtr input, Type type, Orphanage orphanage) const; Chris@63: Chris@63: bool prettyPrint; Chris@63: }; Chris@63: Chris@63: // ======================================================================================= Chris@63: // inline stuff Chris@63: Chris@63: template Chris@63: inline kj::String TextCodec::encode(T&& value) const { Chris@63: return encode(DynamicValue::Reader(ReaderFor>(kj::fwd(value)))); Chris@63: } Chris@63: Chris@63: template Chris@63: inline Orphan TextCodec::decode(kj::StringPtr input, Orphanage orphanage) const { Chris@63: return decode(input, Type::from(), orphanage).template releaseAs(); Chris@63: } Chris@63: Chris@63: } // namespace capnp Chris@63: Chris@63: #endif // CAPNP_SERIALIZE_TEXT_H_