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