Mercurial > hg > sv-dependency-builds
comparison win64-msvc/include/capnp/serialize-text.h @ 47:d93140aac40b
Current Capnp libs and headers from git
author | Chris Cannam |
---|---|
date | Thu, 20 Oct 2016 18:15:38 +0100 |
parents | |
children | 0f2d93caa50c |
comparison
equal
deleted
inserted
replaced
46:efe5b9f38b13 | 47:d93140aac40b |
---|---|
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 #ifndef CAPNP_SERIALIZE_TEXT_H_ | |
23 #define CAPNP_SERIALIZE_TEXT_H_ | |
24 | |
25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) | |
26 #pragma GCC system_header | |
27 #endif | |
28 | |
29 #include <kj/string.h> | |
30 #include "dynamic.h" | |
31 #include "orphan.h" | |
32 #include "schema.h" | |
33 | |
34 namespace capnp { | |
35 | |
36 class TextCodec { | |
37 // Reads and writes Cap'n Proto objects in a plain text format (as used in the schema | |
38 // language for constants, and read/written by the 'decode' and 'encode' commands of | |
39 // the capnp tool). | |
40 // | |
41 // This format is useful for debugging or human input, but it is not a robust alternative | |
42 // to the binary format. Changes to a schema's types or names that are permitted in a | |
43 // schema's binary evolution will likely break messages stored in this format. | |
44 // | |
45 // Note that definitions or references (to constants, other fields, or files) are not | |
46 // permitted in this format. To evaluate declarations with the full expressiveness of the | |
47 // schema language, see `capnp::SchemaParser`. | |
48 // | |
49 // Requires linking with the capnpc library. | |
50 | |
51 public: | |
52 TextCodec(); | |
53 ~TextCodec() noexcept(true); | |
54 | |
55 void setPrettyPrint(bool enabled); | |
56 // If enabled, pads the output of `encode()` with spaces and newlines to make it more | |
57 // human-readable. | |
58 | |
59 template <typename T> | |
60 kj::String encode(T&& value) const; | |
61 kj::String encode(DynamicValue::Reader value) const; | |
62 // Encode any Cap'n Proto value. | |
63 | |
64 template <typename T> | |
65 Orphan<T> decode(kj::StringPtr input, Orphanage orphanage) const; | |
66 // Decode a text message into a Cap'n Proto object of type T, allocated in the given | |
67 // orphanage. Any errors parsing the input or assigning the fields of T are thrown as | |
68 // exceptions. | |
69 | |
70 void decode(kj::StringPtr input, DynamicStruct::Builder output) const; | |
71 // Decode a text message for a struct into the given builder. Any errors parsing the | |
72 // input or assigning the fields of the output are thrown as exceptions. | |
73 | |
74 // TODO(someday): expose some control over the error handling? | |
75 private: | |
76 Orphan<DynamicValue> decode(kj::StringPtr input, Type type, Orphanage orphanage) const; | |
77 | |
78 bool prettyPrint; | |
79 }; | |
80 | |
81 // ======================================================================================= | |
82 // inline stuff | |
83 | |
84 template <typename T> | |
85 inline kj::String TextCodec::encode(T&& value) const { | |
86 return encode(DynamicValue::Reader(ReaderFor<FromAny<T>>(kj::fwd<T>(value)))); | |
87 } | |
88 | |
89 template <typename T> | |
90 inline Orphan<T> TextCodec::decode(kj::StringPtr input, Orphanage orphanage) const { | |
91 return decode(input, Type::from<T>(), orphanage).template releaseAs<T>(); | |
92 } | |
93 | |
94 } // namespace capnp | |
95 | |
96 #endif // CAPNP_SERIALIZE_TEXT_H_ |