Home | History | Annotate | Download | only in io
      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_LIB_IO_PROTO_ENCODE_HELPER_H_
     17 #define TENSORFLOW_LIB_IO_PROTO_ENCODE_HELPER_H_
     18 
     19 #include "tensorflow/core/lib/core/coding.h"
     20 #include "tensorflow/core/lib/core/stringpiece.h"
     21 #include "tensorflow/core/platform/protobuf.h"
     22 
     23 // A helper class for appending various kinds of values in protocol
     24 // buffer encoding format to a buffer.  The client gives a pointer to
     25 // a buffer and a maximum size guarantee for the number of bytes they
     26 // will add to this buffer.
     27 namespace tensorflow {
     28 namespace io {
     29 
     30 class ProtoEncodeHelper {
     31  public:
     32   ProtoEncodeHelper(char* buf, int max_size)
     33       : base_(buf), p_(buf), limit_(base_ + max_size) {}
     34 
     35   ~ProtoEncodeHelper() {
     36     // Make sure callers didn't do operations that went over max_size promised
     37     DCHECK_LE(p_, limit_);
     38   }
     39 
     40   const char* data() const { return base_; }
     41   size_t size() const { return p_ - base_; }
     42 
     43   void WriteUint64(int tag, uint64 v) {
     44     Encode32(combine(tag, WIRETYPE_VARINT));
     45     Encode64(v);
     46   }
     47   void WriteBool(int tag, bool v) {
     48     Encode32(combine(tag, WIRETYPE_VARINT));
     49     EncodeBool(v);
     50   }
     51   void WriteString(int tag, StringPiece v) {
     52     Encode32(combine(tag, WIRETYPE_LENGTH_DELIMITED));
     53     Encode32(v.size());
     54     EncodeBytes(v.data(), v.size());
     55   }
     56   void WriteVarlengthBeginning(int tag, uint32 len) {
     57     Encode32(combine(tag, WIRETYPE_LENGTH_DELIMITED));
     58     Encode32(len);
     59   }
     60   void WriteRawBytes(StringPiece v) { EncodeBytes(v.data(), v.size()); }
     61 
     62  private:
     63   // Note: this module's behavior must match the protocol buffer wire encoding
     64   // format.
     65   enum {
     66     WIRETYPE_VARINT = 0,
     67     WIRETYPE_LENGTH_DELIMITED = 2,
     68   };
     69   static uint32 combine(uint32 tag, uint32 type) { return ((tag << 3) | type); }
     70   inline void Encode32(uint32 v) {
     71     if (v < 128) {
     72       // Fast path for single-byte values.  Many of the calls will use a
     73       // constant value for v, so the comparison will get optimized away
     74       // when Encode32 is inlined into the caller.
     75       *p_ = v;
     76       p_++;
     77     } else {
     78       p_ = core::EncodeVarint32(p_, v);
     79     }
     80   }
     81   void Encode64(uint64 v) { p_ = core::EncodeVarint64(p_, v); }
     82   void EncodeBool(bool v) {
     83     *p_ = (v ? 1 : 0);  // Equal to varint32 encoding of 0 or 1
     84     p_++;
     85   }
     86   void EncodeBytes(const char* bytes, int N) {
     87     memcpy(p_, bytes, N);
     88     p_ += N;
     89   }
     90 
     91   char* base_;
     92   char* p_;
     93   char* limit_;  // Just for CHECKs
     94 };
     95 }  // namespace io
     96 }  // namespace tensorflow
     97 
     98 #endif  // TENSORFLOW_LIB_IO_PROTO_ENCODE_HELPER_H_
     99