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