annotate win32-mingw/include/capnp/serialize-text.h @ 50:37d53a7e8262

Headers for KJ/Capnp Win32
author Chris Cannam
date Wed, 26 Oct 2016 13:18:45 +0100
parents
children eccd51b72864
rev   line source
Chris@50 1 // Copyright (c) 2015 Philip Quinn.
Chris@50 2 // Licensed under the MIT License:
Chris@50 3 //
Chris@50 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
Chris@50 5 // of this software and associated documentation files (the "Software"), to deal
Chris@50 6 // in the Software without restriction, including without limitation the rights
Chris@50 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Chris@50 8 // copies of the Software, and to permit persons to whom the Software is
Chris@50 9 // furnished to do so, subject to the following conditions:
Chris@50 10 //
Chris@50 11 // The above copyright notice and this permission notice shall be included in
Chris@50 12 // all copies or substantial portions of the Software.
Chris@50 13 //
Chris@50 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chris@50 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Chris@50 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Chris@50 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Chris@50 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Chris@50 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Chris@50 20 // THE SOFTWARE.
Chris@50 21
Chris@50 22 #ifndef CAPNP_SERIALIZE_TEXT_H_
Chris@50 23 #define CAPNP_SERIALIZE_TEXT_H_
Chris@50 24
Chris@50 25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
Chris@50 26 #pragma GCC system_header
Chris@50 27 #endif
Chris@50 28
Chris@50 29 #include <kj/string.h>
Chris@50 30 #include "dynamic.h"
Chris@50 31 #include "orphan.h"
Chris@50 32 #include "schema.h"
Chris@50 33
Chris@50 34 namespace capnp {
Chris@50 35
Chris@50 36 class TextCodec {
Chris@50 37 // Reads and writes Cap'n Proto objects in a plain text format (as used in the schema
Chris@50 38 // language for constants, and read/written by the 'decode' and 'encode' commands of
Chris@50 39 // the capnp tool).
Chris@50 40 //
Chris@50 41 // This format is useful for debugging or human input, but it is not a robust alternative
Chris@50 42 // to the binary format. Changes to a schema's types or names that are permitted in a
Chris@50 43 // schema's binary evolution will likely break messages stored in this format.
Chris@50 44 //
Chris@50 45 // Note that definitions or references (to constants, other fields, or files) are not
Chris@50 46 // permitted in this format. To evaluate declarations with the full expressiveness of the
Chris@50 47 // schema language, see `capnp::SchemaParser`.
Chris@50 48 //
Chris@50 49 // Requires linking with the capnpc library.
Chris@50 50
Chris@50 51 public:
Chris@50 52 TextCodec();
Chris@50 53 ~TextCodec() noexcept(true);
Chris@50 54
Chris@50 55 void setPrettyPrint(bool enabled);
Chris@50 56 // If enabled, pads the output of `encode()` with spaces and newlines to make it more
Chris@50 57 // human-readable.
Chris@50 58
Chris@50 59 template <typename T>
Chris@50 60 kj::String encode(T&& value) const;
Chris@50 61 kj::String encode(DynamicValue::Reader value) const;
Chris@50 62 // Encode any Cap'n Proto value.
Chris@50 63
Chris@50 64 template <typename T>
Chris@50 65 Orphan<T> decode(kj::StringPtr input, Orphanage orphanage) const;
Chris@50 66 // Decode a text message into a Cap'n Proto object of type T, allocated in the given
Chris@50 67 // orphanage. Any errors parsing the input or assigning the fields of T are thrown as
Chris@50 68 // exceptions.
Chris@50 69
Chris@50 70 void decode(kj::StringPtr input, DynamicStruct::Builder output) const;
Chris@50 71 // Decode a text message for a struct into the given builder. Any errors parsing the
Chris@50 72 // input or assigning the fields of the output are thrown as exceptions.
Chris@50 73
Chris@50 74 // TODO(someday): expose some control over the error handling?
Chris@50 75 private:
Chris@50 76 Orphan<DynamicValue> decode(kj::StringPtr input, Type type, Orphanage orphanage) const;
Chris@50 77
Chris@50 78 bool prettyPrint;
Chris@50 79 };
Chris@50 80
Chris@50 81 // =======================================================================================
Chris@50 82 // inline stuff
Chris@50 83
Chris@50 84 template <typename T>
Chris@50 85 inline kj::String TextCodec::encode(T&& value) const {
Chris@50 86 return encode(DynamicValue::Reader(ReaderFor<FromAny<T>>(kj::fwd<T>(value))));
Chris@50 87 }
Chris@50 88
Chris@50 89 template <typename T>
Chris@50 90 inline Orphan<T> TextCodec::decode(kj::StringPtr input, Orphanage orphanage) const {
Chris@50 91 return decode(input, Type::from<T>(), orphanage).template releaseAs<T>();
Chris@50 92 }
Chris@50 93
Chris@50 94 } // namespace capnp
Chris@50 95
Chris@50 96 #endif // CAPNP_SERIALIZE_TEXT_H_