| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 // | 34 // |
| 35 // This file contains miscellaneous helper code used by generated code -- | 35 // This file contains miscellaneous helper code used by generated code -- |
| 36 // including lite types -- but which should not be used directly by users. | 36 // including lite types -- but which should not be used directly by users. |
| 37 | 37 |
| 38 #ifndef GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__ | 38 #ifndef GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__ |
| 39 #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__ | 39 #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__ |
| 40 | 40 |
| 41 #include <assert.h> | 41 #include <assert.h> |
| 42 #include <string> | 42 #include <string> |
| 43 | 43 |
| 44 #include <google/protobuf/stubs/common.h> |
| 44 #include <google/protobuf/stubs/once.h> | 45 #include <google/protobuf/stubs/once.h> |
| 45 #include <google/protobuf/stubs/common.h> | 46 #include <google/protobuf/has_bits.h> |
| 47 |
| 48 #ifndef PROTOBUF_FINAL |
| 49 #if LANG_CXX11 |
| 50 #define PROTOBUF_FINAL final |
| 51 #else |
| 52 #define PROTOBUF_FINAL |
| 53 #endif |
| 54 #endif // !PROTOBUF_FINAL |
| 46 | 55 |
| 47 namespace google { | 56 namespace google { |
| 48 | 57 |
| 49 namespace protobuf { | 58 namespace protobuf { |
| 50 | 59 |
| 51 class Arena; | 60 class Arena; |
| 52 namespace io { class CodedInputStream; } | 61 namespace io { class CodedInputStream; } |
| 53 | 62 |
| 54 namespace internal { | 63 namespace internal { |
| 55 | 64 |
| 56 | 65 |
| 57 // Annotation for the compiler to emit a deprecation message if a field marked | 66 // Annotation for the compiler to emit a deprecation message if a field marked |
| 58 // with option 'deprecated=true' is used in the code, or for other things in | 67 // with option 'deprecated=true' is used in the code, or for other things in |
| 59 // generated code which are deprecated. | 68 // generated code which are deprecated. |
| 60 // | 69 // |
| 61 // For internal use in the pb.cc files, deprecation warnings are suppressed | 70 // For internal use in the pb.cc files, deprecation warnings are suppressed |
| 62 // there. | 71 // there. |
| 63 #undef DEPRECATED_PROTOBUF_FIELD | 72 #undef DEPRECATED_PROTOBUF_FIELD |
| 64 #define PROTOBUF_DEPRECATED | 73 #define PROTOBUF_DEPRECATED |
| 65 | 74 |
| 66 #define PROTOBUF_DEPRECATED_ATTR | 75 #define GOOGLE_PROTOBUF_DEPRECATED_ATTR |
| 67 | 76 |
| 68 | 77 |
| 69 // Constants for special floating point values. | 78 // Constants for special floating point values. |
| 70 LIBPROTOBUF_EXPORT double Infinity(); | 79 LIBPROTOBUF_EXPORT double Infinity(); |
| 71 LIBPROTOBUF_EXPORT double NaN(); | 80 LIBPROTOBUF_EXPORT double NaN(); |
| 72 | 81 |
| 82 // This type is used to define a global variable, without it's constructor |
| 83 // and destructor run on start and end of the program lifetime. This circumvents |
| 84 // the initial construction order fiasco, while keeping the address of the |
| 85 // empty string a compile time constant. |
| 86 template <typename T> |
| 87 class ExplicitlyConstructed { |
| 88 public: |
| 89 void DefaultConstruct() { |
| 90 new (&union_) T(); |
| 91 init_ = true; |
| 92 } |
| 93 |
| 94 bool IsInitialized() { return init_; } |
| 95 void Shutdown() { |
| 96 if (init_) { |
| 97 init_ = false; |
| 98 get_mutable()->~T(); |
| 99 } |
| 100 } |
| 101 |
| 102 #if LANG_CXX11 |
| 103 constexpr |
| 104 #endif |
| 105 const T& |
| 106 get() const { |
| 107 return reinterpret_cast<const T&>(union_); |
| 108 } |
| 109 T* get_mutable() { return reinterpret_cast<T*>(&union_); } |
| 110 |
| 111 private: |
| 112 // Prefer c++14 aligned_storage, but for compatibility this will do. |
| 113 union AlignedUnion { |
| 114 char space[sizeof(T)]; |
| 115 int64 align_to_int64; |
| 116 void* align_to_ptr; |
| 117 } union_; |
| 118 bool init_; // false by linker |
| 119 }; |
| 120 |
| 73 // TODO(jieluo): Change to template. We have tried to use template, | 121 // TODO(jieluo): Change to template. We have tried to use template, |
| 74 // but it causes net/rpc/python:rpcutil_test fail (the empty string will | 122 // but it causes net/rpc/python:rpcutil_test fail (the empty string will |
| 75 // init twice). It may related to swig. Change to template after we | 123 // init twice). It may related to swig. Change to template after we |
| 76 // found the solution. | 124 // found the solution. |
| 77 | 125 |
| 78 // Default empty string object. Don't use the pointer directly. Instead, call | 126 // Default empty string object. Don't use this directly. Instead, call |
| 79 // GetEmptyString() to get the reference. | 127 // GetEmptyString() to get the reference. |
| 80 LIBPROTOBUF_EXPORT extern const ::std::string* empty_string_; | 128 LIBPROTOBUF_EXPORT extern ExplicitlyConstructed< ::std::string> fixed_address_em
pty_string; |
| 81 LIBPROTOBUF_EXPORT extern ProtobufOnceType empty_string_once_init_; | 129 LIBPROTOBUF_EXPORT extern ProtobufOnceType empty_string_once_init_; |
| 82 LIBPROTOBUF_EXPORT void InitEmptyString(); | 130 LIBPROTOBUF_EXPORT void InitEmptyString(); |
| 83 | 131 |
| 84 | 132 |
| 85 LIBPROTOBUF_EXPORT inline const ::std::string& GetEmptyStringAlreadyInited() { | 133 LIBPROTOBUF_EXPORT inline const ::std::string& GetEmptyStringAlreadyInited() { |
| 86 assert(empty_string_ != NULL); | 134 return fixed_address_empty_string.get(); |
| 87 return *empty_string_; | |
| 88 } | 135 } |
| 89 | 136 |
| 137 |
| 90 LIBPROTOBUF_EXPORT const ::std::string& GetEmptyString(); | 138 LIBPROTOBUF_EXPORT const ::std::string& GetEmptyString(); |
| 91 | 139 |
| 92 LIBPROTOBUF_EXPORT int StringSpaceUsedExcludingSelf(const string& str); | 140 LIBPROTOBUF_EXPORT int StringSpaceUsedExcludingSelf(const string& str); |
| 93 | 141 |
| 94 | 142 |
| 95 // True if IsInitialized() is true for all elements of t. Type is expected | 143 // True if IsInitialized() is true for all elements of t. Type is expected |
| 96 // to be a RepeatedPtrField<some message type>. It's useful to have this | 144 // to be a RepeatedPtrField<some message type>. It's useful to have this |
| 97 // helper here to keep the protobuf compiler from ever having to emit loops in | 145 // helper here to keep the protobuf compiler from ever having to emit loops in |
| 98 // IsInitialized() methods. We want the C++ compiler to inline this or not | 146 // IsInitialized() methods. We want the C++ compiler to inline this or not |
| 99 // as it sees fit. | 147 // as it sees fit. |
| 100 template <class Type> bool AllAreInitialized(const Type& t) { | 148 template <class Type> bool AllAreInitialized(const Type& t) { |
| 101 for (int i = t.size(); --i >= 0; ) { | 149 for (int i = t.size(); --i >= 0; ) { |
| 102 if (!t.Get(i).IsInitialized()) return false; | 150 if (!t.Get(i).IsInitialized()) return false; |
| 103 } | 151 } |
| 104 return true; | 152 return true; |
| 105 } | 153 } |
| 106 | 154 |
| 107 class ArenaString; | 155 LIBPROTOBUF_EXPORT void InitProtobufDefaults(); |
| 108 | 156 |
| 109 // Read a length (varint32), followed by a string, from *input. Return a | 157 // We compute sizes as size_t but cache them as int. This function converts a |
| 110 // pointer to a copy of the string that resides in *arena. Requires both | 158 // computed size to a cached size. Since we don't proceed with serialization if |
| 111 // args to be non-NULL. If something goes wrong while reading the data | 159 // the total size was > INT_MAX, it is not important what this function returns |
| 112 // then NULL is returned (e.g., input does not start with a valid varint). | 160 // for inputs > INT_MAX. |
| 113 ArenaString* ReadArenaString(::google::protobuf::io::CodedInputStream* input, | 161 inline int ToCachedSize(size_t size) { |
| 114 ::google::protobuf::Arena* arena); | 162 return static_cast<int>(size); |
| 163 } |
| 164 |
| 165 // We mainly calculate sizes in terms of size_t, but some functions that compute |
| 166 // sizes return "int". These int sizes are expected to always be positive. |
| 167 // This function is more efficient than casting an int to size_t directly on |
| 168 // 64-bit platforms because it avoids making the compiler emit a sign extending |
| 169 // instruction, which we don't want and don't want to pay for. |
| 170 inline size_t FromIntSize(int size) { |
| 171 // Convert to unsigned before widening so sign extension is not necessary. |
| 172 return static_cast<unsigned int>(size); |
| 173 } |
| 115 | 174 |
| 116 } // namespace internal | 175 } // namespace internal |
| 117 } // namespace protobuf | 176 } // namespace protobuf |
| 118 | 177 |
| 119 } // namespace google | 178 } // namespace google |
| 120 #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__ | 179 #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__ |
| OLD | NEW |