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