• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_CORE_PLATFORM_PROTOBUF_H_
17 #define TENSORFLOW_CORE_PLATFORM_PROTOBUF_H_
18 
19 #include "tensorflow/core/platform/platform.h"
20 #include "tensorflow/core/platform/types.h"
21 
22 // Import whatever namespace protobuf comes from into the
23 // ::tensorflow::protobuf namespace.
24 //
25 // TensorFlow code should use the ::tensorflow::protobuf namespace to
26 // refer to all protobuf APIs.
27 
28 #ifndef TENSORFLOW_LITE_PROTOS
29 #include "google/protobuf/io/tokenizer.h"
30 #include "google/protobuf/descriptor.pb.h"
31 #include "google/protobuf/descriptor.h"
32 #include "google/protobuf/dynamic_message.h"
33 #include "google/protobuf/text_format.h"
34 #include "google/protobuf/util/json_util.h"
35 #include "google/protobuf/util/type_resolver_util.h"
36 #endif
37 
38 #include "google/protobuf/io/coded_stream.h"
39 #include "google/protobuf/io/zero_copy_stream.h"
40 #include "google/protobuf/io/zero_copy_stream_impl_lite.h"
41 #include "google/protobuf/arena.h"
42 #include "google/protobuf/map.h"
43 #include "google/protobuf/repeated_field.h"
44 
45 namespace tensorflow {
46 
47 namespace protobuf = ::google::protobuf;
48 using protobuf_int64 = ::google::protobuf::int64;
49 using protobuf_uint64 = ::google::protobuf::uint64;
50 extern const char* kProtobufInt64Typename;
51 extern const char* kProtobufUint64Typename;
52 
53 // Parses a protocol buffer contained in a string in the binary wire format.
54 // Returns true on success. Note: Unlike protobuf's builtin ParseFromString,
55 // this function has no size restrictions on the total size of the encoded
56 // protocol buffer.
57 bool ParseProtoUnlimited(protobuf::MessageLite* proto,
58                          const string& serialized);
59 bool ParseProtoUnlimited(protobuf::MessageLite* proto, const void* serialized,
60                          size_t size);
ParseProtoUnlimited(protobuf::MessageLite * proto,const tstring & serialized)61 inline bool ParseProtoUnlimited(protobuf::MessageLite* proto,
62                                 const tstring& serialized) {
63   return ParseProtoUnlimited(proto, serialized.data(), serialized.size());
64 }
65 
66 // Returns the string value for the value of a string or bytes protobuf field.
ProtobufStringToString(const string & s)67 inline const string& ProtobufStringToString(const string& s) { return s; }
68 
69 // Set <dest> to <src>. Swapping is allowed, as <src> does not need to be
70 // preserved.
SetProtobufStringSwapAllowed(string * src,string * dest)71 inline void SetProtobufStringSwapAllowed(string* src, string* dest) {
72   *dest = std::move(*src);
73 }
74 
75 #if defined(TENSORFLOW_PROTOBUF_USES_CORD)
76 // These versions of ProtobufStringToString and SetProtobufString get used by
77 // tools/proto_text's generated code.  They have the same name as the versions
78 // in core/platform/protobuf.h, so the generation code doesn't need to determine
79 // if the type is Cord or string at generation time.
ProtobufStringToString(const Cord & s)80 inline string ProtobufStringToString(const Cord& s) { return s.ToString(); }
SetProtobufStringSwapAllowed(string * src,Cord * dest)81 inline void SetProtobufStringSwapAllowed(string* src, Cord* dest) {
82   dest->CopyFrom(*src);
83 }
84 #endif  // defined(TENSORFLOW_PROTOBUF_USES_CORD)
85 
SerializeToTString(const protobuf::MessageLite & proto,tstring * output)86 inline bool SerializeToTString(const protobuf::MessageLite& proto,
87                                tstring* output) {
88   size_t size = proto.ByteSizeLong();
89   output->resize_uninitialized(size);
90   return proto.SerializeToArray(output->data(), static_cast<int>(size));
91 }
92 
ParseFromTString(const tstring & input,protobuf::MessageLite * proto)93 inline bool ParseFromTString(const tstring& input,
94                              protobuf::MessageLite* proto) {
95   return proto->ParseFromArray(input.data(), static_cast<int>(input.size()));
96 }
97 
98 // Analogue to StringOutputStream for tstring.
99 class TStringOutputStream : public protobuf::io::ZeroCopyOutputStream {
100  public:
101   explicit TStringOutputStream(tstring* target);
102   ~TStringOutputStream() override = default;
103 
104   TStringOutputStream(const TStringOutputStream&) = delete;
105   void operator=(const TStringOutputStream&) = delete;
106 
107   bool Next(void** data, int* size) override;
108   void BackUp(int count) override;
109   int64_t ByteCount() const override;
110 
111  private:
112   static const int kMinimumSize = 16;
113 
114   tstring* target_;
115 };
116 
117 }  // namespace tensorflow
118 
119 #endif  // TENSORFLOW_CORE_PLATFORM_PROTOBUF_H_
120