webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
arenastring.h
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef GOOGLE_PROTOBUF_ARENASTRING_H__
32 #define GOOGLE_PROTOBUF_ARENASTRING_H__
33 
34 #include <string>
35 
39 #include <google/protobuf/arena.h>
41 
42 
43 
44 // This is the implementation of arena string fields written for the open-source
45 // release. The ArenaStringPtr struct below is an internal implementation class
46 // and *should not be used* by user code. It is used to collect string
47 // operations together into one place and abstract away the underlying
48 // string-field pointer representation, so that (for example) an alternate
49 // implementation that knew more about ::std::string's internals could integrate more
50 // closely with the arena allocator.
51 
52 namespace google {
53 namespace protobuf {
54 namespace internal {
55 
57  inline void Set(const ::std::string* default_value,
59  if (ptr_ == default_value) {
60  CreateInstance(arena, &value);
61  } else {
62  *ptr_ = value;
63  }
64  }
65 
66  // Basic accessors.
67  inline const ::std::string& Get(const ::std::string* /* default_value */) const {
68  return *ptr_;
69  }
70 
72  ::google::protobuf::Arena* arena) {
73  if (ptr_ == default_value) {
74  CreateInstance(arena, default_value);
75  }
76  return ptr_;
77  }
78 
79  // Release returns a ::std::string* instance that is heap-allocated and is not
80  // Own()'d by any arena. If the field was not set, it returns NULL. The caller
81  // retains ownership. Clears this field back to NULL state. Used to implement
82  // release_<field>() methods on generated classes.
84  ::google::protobuf::Arena* arena) {
85  if (ptr_ == default_value) {
86  return NULL;
87  }
89  if (arena != NULL) {
90  // ptr_ is owned by the arena -- we need to return a copy.
91  released = new ::std::string(*ptr_);
92  } else {
93  released = ptr_;
94  }
95  ptr_ = const_cast< ::std::string* >(default_value);
96  return released;
97  }
98 
99  // UnsafeArenaRelease returns a ::std::string*, but it may be arena-owned (i.e.
100  // have its destructor already registered) if arena != NULL. If the field was
101  // not set, this returns NULL. This method clears this field back to NULL
102  // state. Used to implement unsafe_arena_release_<field>() methods on
103  // generated classes.
105  ::google::protobuf::Arena* /* arena */) {
106  if (ptr_ == default_value) {
107  return NULL;
108  }
109  ::std::string* released = ptr_;
110  ptr_ = const_cast< ::std::string* >(default_value);
111  return released;
112  }
113 
114  // Takes a string that is heap-allocated, and takes ownership. The string's
115  // destructor is registered with the arena. Used to implement
116  // set_allocated_<field> in generated classes.
117  inline void SetAllocated(const ::std::string* default_value,
119  if (arena == NULL && ptr_ != default_value) {
120  Destroy(default_value, arena);
121  }
122  if (value != NULL) {
123  ptr_ = value;
124  if (arena != NULL) {
125  arena->Own(value);
126  }
127  } else {
128  ptr_ = const_cast< ::std::string* >(default_value);
129  }
130  }
131 
132  // Takes a string that has lifetime equal to the arena's lifetime. The arena
133  // must be non-null. It is safe only to pass this method a value returned by
134  // UnsafeArenaRelease() on another field of a message in the same arena. Used
135  // to implement unsafe_arena_set_allocated_<field> in generated classes.
136  inline void UnsafeArenaSetAllocated(const ::std::string* default_value,
137  ::std::string* value,
138  ::google::protobuf::Arena* /* arena */) {
139  if (value != NULL) {
140  ptr_ = value;
141  } else {
142  ptr_ = const_cast< ::std::string* >(default_value);
143  }
144  }
145 
146  // Swaps internal pointers. Arena-safety semantics: this is guarded by the
147  // logic in Swap()/UnsafeArenaSwap() at the message level, so this method is
148  // 'unsafe' if called directly.
150  std::swap(ptr_, other->ptr_);
151  }
152 
153  // Frees storage (if not on an arena) and sets field to default value.
154  inline void Destroy(const ::std::string* default_value,
155  ::google::protobuf::Arena* arena) {
156  if (arena == NULL && ptr_ != default_value) {
157  delete ptr_;
158  }
159  ptr_ = const_cast< ::std::string* >(default_value);
160  }
161 
162  // Clears content, but keeps allocated string if arena != NULL, to avoid the
163  // overhead of heap operations. After this returns, the content (as seen by
164  // the user) will always be the empty string. Assumes that |default_value|
165  // is an empty string.
166  inline void ClearToEmpty(const ::std::string* default_value,
167  ::google::protobuf::Arena* /* arena */) {
168  if (ptr_ == default_value) {
169  // Already set to default (which is empty) -- do nothing.
170  } else {
171  ptr_->clear();
172  }
173  }
174 
175  // Clears content, but keeps allocated string if arena != NULL, to avoid the
176  // overhead of heap operations. After this returns, the content (as seen by
177  // the user) will always be equal to |default_value|.
178  inline void ClearToDefault(const ::std::string* default_value,
179  ::google::protobuf::Arena* /* arena */) {
180  if (ptr_ == default_value) {
181  // Already set to default -- do nothing.
182  } else {
183  // Have another allocated string -- rather than throwing this away and
184  // resetting ptr_ to the canonical default string instance, we just reuse
185  // this instance.
186  *ptr_ = *default_value;
187  }
188  }
189 
190  // Called from generated code / reflection runtime only. Resets value to point
191  // to a default string pointer, with the semantics that this ArenaStringPtr
192  // does not own the pointed-to memory. Disregards initial value of ptr_ (so
193  // this is the *ONLY* safe method to call after construction or when
194  // reinitializing after becoming the active field in a oneof union).
195  inline void UnsafeSetDefault(const ::std::string* default_value) {
196  // Casting away 'const' is safe here: accessors ensure that ptr_ is only
197  // returned as a const if it is equal to default_value.
198  ptr_ = const_cast< ::std::string* >(default_value);
199  }
200 
201  // The 'NoArena' variants of methods below assume arena == NULL and are
202  // optimized to provide very little overhead relative to a raw string pointer
203  // (while still being in-memory compatible with other code that assumes
204  // ArenaStringPtr). Note the invariant that a class instance that has only
205  // ever been mutated by NoArena methods must *only* be in the String state
206  // (i.e., tag bits are not used), *NEVER* ArenaString. This allows all
207  // tagged-pointer manipulations to be avoided.
208  inline void SetNoArena(const ::std::string* default_value,
210  if (ptr_ == default_value) {
211  CreateInstanceNoArena(&value);
212  } else {
213  *ptr_ = value;
214  }
215  }
216 
217  void AssignWithDefault(const ::std::string* default_value, ArenaStringPtr value);
218 
219  inline const ::std::string& GetNoArena(const ::std::string* /* default_value */) const {
220  return *ptr_;
221  }
222 
223  ::std::string* MutableNoArena(const ::std::string* default_value);
224 
226  if (ptr_ == default_value) {
227  return NULL;
228  } else {
229  ::std::string* released = ptr_;
230  ptr_ = const_cast< ::std::string* >(default_value);
231  return released;
232  }
233  }
234 
235  inline void SetAllocatedNoArena(const ::std::string* default_value,
236  ::std::string* value) {
237  if (ptr_ != default_value) {
238  delete ptr_;
239  }
240  if (value != NULL) {
241  ptr_ = value;
242  } else {
243  ptr_ = const_cast< ::std::string* >(default_value);
244  }
245  }
246 
247  void DestroyNoArena(const ::std::string* default_value);
248 
249  inline void ClearToEmptyNoArena(const ::std::string* default_value) {
250  if (ptr_ == default_value) {
251  // Nothing: already equal to default (which is the empty string).
252  } else {
253  ptr_->clear();
254  }
255  }
256 
257  inline void ClearToDefaultNoArena(const ::std::string* default_value) {
258  if (ptr_ == default_value) {
259  // Nothing: already set to default.
260  } else {
261  // Reuse existing allocated instance.
262  *ptr_ = *default_value;
263  }
264  }
265 
266  // Internal accessor used only at parse time to provide direct access to the
267  // raw pointer from the shared parse routine (in the non-arenas case). The
268  // parse routine does the string allocation in order to save code size in the
269  // generated parsing code.
271  return &ptr_;
272  }
273 
274  private:
275  ::std::string* ptr_;
276 
277  GOOGLE_ATTRIBUTE_NOINLINE void CreateInstance(::google::protobuf::Arena* arena,
278  const ::std::string* initial_value) {
279  // Assumes ptr_ is not NULL.
280  if (initial_value != NULL) {
281  ptr_ = new ::std::string(*initial_value);
282  } else {
283  ptr_ = new ::std::string();
284  }
285  if (arena != NULL) {
286  arena->Own(ptr_);
287  }
288  }
289  GOOGLE_ATTRIBUTE_NOINLINE void CreateInstanceNoArena(const ::std::string* initial_value) {
290  if (initial_value != NULL) {
291  ptr_ = new ::std::string(*initial_value);
292  } else {
293  ptr_ = new ::std::string();
294  }
295  }
296 };
297 
298 } // namespace internal
299 } // namespace protobuf
300 
301 
302 
303 } // namespace google
304 #endif // GOOGLE_PROTOBUF_ARENASTRING_H__
GOOGLE_ATTRIBUTE_ALWAYS_INLINE void Swap(ArenaStringPtr *other)
Definition: arenastring.h:149
void ClearToDefault(const ::std::string *default_value, ::google::protobuf::Arena *)
Definition: arenastring.h:178
inline ::std::string ** UnsafeRawStringPointer()
Definition: arenastring.h:270
inline ::std::string * Mutable(const ::std::string *default_value, ::google::protobuf::Arena *arena)
Definition: arenastring.h:71
void UnsafeSetDefault(const ::std::string *default_value)
Definition: arenastring.h:195
void ClearToEmpty(const ::std::string *default_value, ::google::protobuf::Arena *)
Definition: arenastring.h:166
void SetAllocated(const ::std::string *default_value, ::std::string *value, ::google::protobuf::Arena *arena)
Definition: arenastring.h:117
#define GOOGLE_ATTRIBUTE_NOINLINE
Definition: port.h:189
void ClearToEmptyNoArena(const ::std::string *default_value)
Definition: arenastring.h:249
Definition: arenastring.h:56
#define GOOGLE_ATTRIBUTE_ALWAYS_INLINE
Definition: port.h:175
const ::std::string & GetNoArena(const ::std::string *) const
Definition: arenastring.h:219
const FieldDescriptor const OneofDescriptor value
Definition: descriptor.h:1717
inline ::std::string * UnsafeArenaRelease(const ::std::string *default_value, ::google::protobuf::Arena *)
Definition: arenastring.h:104
void Destroy(const ::std::string *default_value, ::google::protobuf::Arena *arena)
Definition: arenastring.h:154
inline ::std::string * ReleaseNoArena(const ::std::string *default_value)
Definition: arenastring.h:225
EGLAttrib * value
Definition: eglext.h:120
inline ::std::string * Release(const ::std::string *default_value, ::google::protobuf::Arena *arena)
Definition: arenastring.h:83
const ::std::string & Get(const ::std::string *) const
Definition: arenastring.h:67
void UnsafeArenaSetAllocated(const ::std::string *default_value, ::std::string *value, ::google::protobuf::Arena *)
Definition: arenastring.h:136
void Set(const ::std::string *default_value, const ::std::string &value, ::google::protobuf::Arena *arena)
Definition: arenastring.h:57
Definition: __init__.py:1
Definition: arena.h:218
GOOGLE_ATTRIBUTE_NOINLINE void Own(T *object)
Definition: arena.h:469
GLsizei const GLchar *const * string
Definition: gl2.h:479
Definition: document.h:393
void SetAllocatedNoArena(const ::std::string *default_value, ::std::string *value)
Definition: arenastring.h:235
#define NULL
Definition: common_types.h:41
#define LIBPROTOBUF_EXPORT
Definition: port.h:97
Definition: gflags_completions.h:115
void SetNoArena(const ::std::string *default_value, const ::std::string &value)
Definition: arenastring.h:208
void ClearToDefaultNoArena(const ::std::string *default_value)
Definition: arenastring.h:257
Definition: EncryptedMediaExtensions.idl:76
void swap(optional< T > &x, optional< T > &y) __NOEXCEPT_(__NOEXCEPT_(x.swap(y)))
Definition: Optional.h:1047