Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/upb/mini_descriptor/internal/encode.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2023 Google LLC.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 #ifndef UPB_MINI_TABLE_ENCODE_INTERNAL_HPP_
0009 #define UPB_MINI_TABLE_ENCODE_INTERNAL_HPP_
0010 
0011 #include <string>
0012 
0013 #include "upb/base/internal/log2.h"
0014 #include "upb/mini_descriptor/internal/encode.h"
0015 
0016 namespace upb {
0017 
0018 class MtDataEncoder {
0019  public:
0020   MtDataEncoder() : appender_(&encoder_) {}
0021 
0022   bool StartMessage(uint64_t msg_mod) {
0023     return appender_([this, msg_mod](char* buf) {
0024       return upb_MtDataEncoder_StartMessage(&encoder_, buf, msg_mod);
0025     });
0026   }
0027 
0028   bool PutField(upb_FieldType type, uint32_t field_num, uint64_t field_mod) {
0029     return appender_([this, type, field_num, field_mod](char* buf) {
0030       return upb_MtDataEncoder_PutField(&encoder_, buf, type, field_num,
0031                                         field_mod);
0032     });
0033   }
0034 
0035   bool StartOneof() {
0036     return appender_([this](char* buf) {
0037       return upb_MtDataEncoder_StartOneof(&encoder_, buf);
0038     });
0039   }
0040 
0041   bool PutOneofField(uint32_t field_num) {
0042     return appender_([this, field_num](char* buf) {
0043       return upb_MtDataEncoder_PutOneofField(&encoder_, buf, field_num);
0044     });
0045   }
0046 
0047   bool StartEnum() {
0048     return appender_([this](char* buf) {
0049       return upb_MtDataEncoder_StartEnum(&encoder_, buf);
0050     });
0051   }
0052 
0053   bool PutEnumValue(uint32_t enum_value) {
0054     return appender_([this, enum_value](char* buf) {
0055       return upb_MtDataEncoder_PutEnumValue(&encoder_, buf, enum_value);
0056     });
0057   }
0058 
0059   bool EndEnum() {
0060     return appender_([this](char* buf) {
0061       return upb_MtDataEncoder_EndEnum(&encoder_, buf);
0062     });
0063   }
0064 
0065   bool EncodeExtension(upb_FieldType type, uint32_t field_num,
0066                        uint64_t field_mod) {
0067     return appender_([this, type, field_num, field_mod](char* buf) {
0068       return upb_MtDataEncoder_EncodeExtension(&encoder_, buf, type, field_num,
0069                                                field_mod);
0070     });
0071   }
0072 
0073   bool EncodeMap(upb_FieldType key_type, upb_FieldType val_type,
0074                  uint64_t key_mod, uint64_t val_mod) {
0075     return appender_([this, key_type, val_type, key_mod, val_mod](char* buf) {
0076       return upb_MtDataEncoder_EncodeMap(&encoder_, buf, key_type, val_type,
0077                                          key_mod, val_mod);
0078     });
0079   }
0080 
0081   bool EncodeMessageSet() {
0082     return appender_([this](char* buf) {
0083       return upb_MtDataEncoder_EncodeMessageSet(&encoder_, buf);
0084     });
0085   }
0086 
0087   const std::string& data() const { return appender_.data(); }
0088 
0089  private:
0090   class StringAppender {
0091    public:
0092     StringAppender(upb_MtDataEncoder* e) { e->end = buf_ + sizeof(buf_); }
0093 
0094     template <class T>
0095     bool operator()(T&& func) {
0096       char* end = func(buf_);
0097       if (!end) return false;
0098       // C++ does not guarantee that string has doubling growth behavior, but
0099       // we need it to avoid O(n^2).
0100       str_.reserve(upb_Log2CeilingSize(str_.size() + (end - buf_)));
0101       str_.append(buf_, end - buf_);
0102       return true;
0103     }
0104 
0105     const std::string& data() const { return str_; }
0106 
0107    private:
0108     char buf_[kUpb_MtDataEncoder_MinSize];
0109     std::string str_;
0110   };
0111 
0112   upb_MtDataEncoder encoder_;
0113   StringAppender appender_;
0114 };
0115 
0116 }  // namespace upb
0117 
0118 #endif /* UPB_MINI_TABLE_ENCODE_INTERNAL_HPP_ */