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/once.h> |
44 #include <google/protobuf/stubs/common.h> | 45 #include <google/protobuf/stubs/common.h> |
45 #include <google/protobuf/stubs/once.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 | |
55 | 46 |
56 namespace google { | 47 namespace google { |
57 | 48 |
58 namespace protobuf { | 49 namespace protobuf { |
59 | 50 |
60 class Arena; | 51 class Arena; |
61 namespace io { class CodedInputStream; } | 52 namespace io { class CodedInputStream; } |
62 | 53 |
63 namespace internal { | 54 namespace internal { |
64 | 55 |
65 | 56 |
66 // Annotation for the compiler to emit a deprecation message if a field marked | 57 // Annotation for the compiler to emit a deprecation message if a field marked |
67 // with option 'deprecated=true' is used in the code, or for other things in | 58 // with option 'deprecated=true' is used in the code, or for other things in |
68 // generated code which are deprecated. | 59 // generated code which are deprecated. |
69 // | 60 // |
70 // For internal use in the pb.cc files, deprecation warnings are suppressed | 61 // For internal use in the pb.cc files, deprecation warnings are suppressed |
71 // there. | 62 // there. |
72 #undef DEPRECATED_PROTOBUF_FIELD | 63 #undef DEPRECATED_PROTOBUF_FIELD |
73 #define PROTOBUF_DEPRECATED | 64 #define PROTOBUF_DEPRECATED |
74 | 65 |
75 #define GOOGLE_PROTOBUF_DEPRECATED_ATTR | 66 #define PROTOBUF_DEPRECATED_ATTR |
76 | 67 |
77 | 68 |
78 // Constants for special floating point values. | 69 // Constants for special floating point values. |
79 LIBPROTOBUF_EXPORT double Infinity(); | 70 LIBPROTOBUF_EXPORT double Infinity(); |
80 LIBPROTOBUF_EXPORT double NaN(); | 71 LIBPROTOBUF_EXPORT double NaN(); |
81 | 72 |
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 | |
121 // TODO(jieluo): Change to template. We have tried to use template, | 73 // TODO(jieluo): Change to template. We have tried to use template, |
122 // but it causes net/rpc/python:rpcutil_test fail (the empty string will | 74 // but it causes net/rpc/python:rpcutil_test fail (the empty string will |
123 // init twice). It may related to swig. Change to template after we | 75 // init twice). It may related to swig. Change to template after we |
124 // found the solution. | 76 // found the solution. |
125 | 77 |
126 // Default empty string object. Don't use this directly. Instead, call | 78 // Default empty string object. Don't use the pointer directly. Instead, call |
127 // GetEmptyString() to get the reference. | 79 // GetEmptyString() to get the reference. |
128 LIBPROTOBUF_EXPORT extern ExplicitlyConstructed< ::std::string> fixed_address_em
pty_string; | 80 LIBPROTOBUF_EXPORT extern const ::std::string* empty_string_; |
129 LIBPROTOBUF_EXPORT extern ProtobufOnceType empty_string_once_init_; | 81 LIBPROTOBUF_EXPORT extern ProtobufOnceType empty_string_once_init_; |
130 LIBPROTOBUF_EXPORT void InitEmptyString(); | 82 LIBPROTOBUF_EXPORT void InitEmptyString(); |
131 | 83 |
132 | 84 |
133 LIBPROTOBUF_EXPORT inline const ::std::string& GetEmptyStringAlreadyInited() { | 85 LIBPROTOBUF_EXPORT inline const ::std::string& GetEmptyStringAlreadyInited() { |
134 return fixed_address_empty_string.get(); | 86 assert(empty_string_ != NULL); |
| 87 return *empty_string_; |
135 } | 88 } |
136 | 89 |
137 | |
138 LIBPROTOBUF_EXPORT const ::std::string& GetEmptyString(); | 90 LIBPROTOBUF_EXPORT const ::std::string& GetEmptyString(); |
139 | 91 |
140 LIBPROTOBUF_EXPORT int StringSpaceUsedExcludingSelf(const string& str); | 92 LIBPROTOBUF_EXPORT int StringSpaceUsedExcludingSelf(const string& str); |
141 | 93 |
142 | 94 |
143 // True if IsInitialized() is true for all elements of t. Type is expected | 95 // True if IsInitialized() is true for all elements of t. Type is expected |
144 // to be a RepeatedPtrField<some message type>. It's useful to have this | 96 // to be a RepeatedPtrField<some message type>. It's useful to have this |
145 // helper here to keep the protobuf compiler from ever having to emit loops in | 97 // helper here to keep the protobuf compiler from ever having to emit loops in |
146 // IsInitialized() methods. We want the C++ compiler to inline this or not | 98 // IsInitialized() methods. We want the C++ compiler to inline this or not |
147 // as it sees fit. | 99 // as it sees fit. |
148 template <class Type> bool AllAreInitialized(const Type& t) { | 100 template <class Type> bool AllAreInitialized(const Type& t) { |
149 for (int i = t.size(); --i >= 0; ) { | 101 for (int i = t.size(); --i >= 0; ) { |
150 if (!t.Get(i).IsInitialized()) return false; | 102 if (!t.Get(i).IsInitialized()) return false; |
151 } | 103 } |
152 return true; | 104 return true; |
153 } | 105 } |
154 | 106 |
155 LIBPROTOBUF_EXPORT void InitProtobufDefaults(); | 107 class ArenaString; |
156 | 108 |
157 // We compute sizes as size_t but cache them as int. This function converts a | 109 // Read a length (varint32), followed by a string, from *input. Return a |
158 // computed size to a cached size. Since we don't proceed with serialization if | 110 // pointer to a copy of the string that resides in *arena. Requires both |
159 // the total size was > INT_MAX, it is not important what this function returns | 111 // args to be non-NULL. If something goes wrong while reading the data |
160 // for inputs > INT_MAX. | 112 // then NULL is returned (e.g., input does not start with a valid varint). |
161 inline int ToCachedSize(size_t size) { | 113 ArenaString* ReadArenaString(::google::protobuf::io::CodedInputStream* input, |
162 return static_cast<int>(size); | 114 ::google::protobuf::Arena* arena); |
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 } | |
174 | 115 |
175 } // namespace internal | 116 } // namespace internal |
176 } // namespace protobuf | 117 } // namespace protobuf |
177 | 118 |
178 } // namespace google | 119 } // namespace google |
179 #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__ | 120 #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_UTIL_H__ |
OLD | NEW |