Chris@47: // Copyright (c) 2015 Philip Quinn. Chris@47: // Licensed under the MIT License: Chris@47: // Chris@47: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@47: // of this software and associated documentation files (the "Software"), to deal Chris@47: // in the Software without restriction, including without limitation the rights Chris@47: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@47: // copies of the Software, and to permit persons to whom the Software is Chris@47: // furnished to do so, subject to the following conditions: Chris@47: // Chris@47: // The above copyright notice and this permission notice shall be included in Chris@47: // all copies or substantial portions of the Software. Chris@47: // Chris@47: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@47: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@47: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@47: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@47: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@47: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@47: // THE SOFTWARE. Chris@47: Chris@47: #ifndef CAPNP_SERIALIZE_TEXT_H_ Chris@47: #define CAPNP_SERIALIZE_TEXT_H_ Chris@47: Chris@47: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) Chris@47: #pragma GCC system_header Chris@47: #endif Chris@47: Chris@47: #include Chris@47: #include "dynamic.h" Chris@47: #include "orphan.h" Chris@47: #include "schema.h" Chris@47: Chris@47: namespace capnp { Chris@47: Chris@47: class TextCodec { Chris@47: // Reads and writes Cap'n Proto objects in a plain text format (as used in the schema Chris@47: // language for constants, and read/written by the 'decode' and 'encode' commands of Chris@47: // the capnp tool). Chris@47: // Chris@47: // This format is useful for debugging or human input, but it is not a robust alternative Chris@47: // to the binary format. Changes to a schema's types or names that are permitted in a Chris@47: // schema's binary evolution will likely break messages stored in this format. Chris@47: // Chris@47: // Note that definitions or references (to constants, other fields, or files) are not Chris@47: // permitted in this format. To evaluate declarations with the full expressiveness of the Chris@47: // schema language, see `capnp::SchemaParser`. Chris@47: // Chris@47: // Requires linking with the capnpc library. Chris@47: Chris@47: public: Chris@47: TextCodec(); Chris@47: ~TextCodec() noexcept(true); Chris@47: Chris@47: void setPrettyPrint(bool enabled); Chris@47: // If enabled, pads the output of `encode()` with spaces and newlines to make it more Chris@47: // human-readable. Chris@47: Chris@47: template Chris@47: kj::String encode(T&& value) const; Chris@47: kj::String encode(DynamicValue::Reader value) const; Chris@47: // Encode any Cap'n Proto value. Chris@47: Chris@47: template Chris@47: Orphan decode(kj::StringPtr input, Orphanage orphanage) const; Chris@47: // Decode a text message into a Cap'n Proto object of type T, allocated in the given Chris@47: // orphanage. Any errors parsing the input or assigning the fields of T are thrown as Chris@47: // exceptions. Chris@47: Chris@47: void decode(kj::StringPtr input, DynamicStruct::Builder output) const; Chris@47: // Decode a text message for a struct into the given builder. Any errors parsing the Chris@47: // input or assigning the fields of the output are thrown as exceptions. Chris@47: Chris@47: // TODO(someday): expose some control over the error handling? Chris@47: private: Chris@47: Orphan decode(kj::StringPtr input, Type type, Orphanage orphanage) const; Chris@47: Chris@47: bool prettyPrint; Chris@47: }; Chris@47: Chris@47: // ======================================================================================= Chris@47: // inline stuff Chris@47: Chris@47: template Chris@47: inline kj::String TextCodec::encode(T&& value) const { Chris@47: return encode(DynamicValue::Reader(ReaderFor>(kj::fwd(value)))); Chris@47: } Chris@47: Chris@47: template Chris@47: inline Orphan TextCodec::decode(kj::StringPtr input, Orphanage orphanage) const { Chris@47: return decode(input, Type::from(), orphanage).template releaseAs(); Chris@47: } Chris@47: Chris@47: } // namespace capnp Chris@47: Chris@47: #endif // CAPNP_SERIALIZE_TEXT_H_