Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 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_INLINED_STRING_FIELD_H__ |
| 32 | #define GOOGLE_PROTOBUF_INLINED_STRING_FIELD_H__ |
| 33 | |
| 34 | #include <string> |
| 35 | |
| 36 | #include <google/protobuf/stubs/port.h> |
| 37 | #include <google/protobuf/stubs/stringpiece.h> |
| 38 | |
| 39 | namespace google { |
| 40 | namespace protobuf { |
| 41 | |
| 42 | class Arena; |
| 43 | |
| 44 | namespace internal { |
| 45 | |
| 46 | // InlinedStringField wraps a ::std::string instance and exposes an API similar to |
| 47 | // ArenaStringPtr's wrapping of a ::std::string* instance. As ::std::string is never |
| 48 | // allocated on the Arena, we expose only the *NoArena methods of |
| 49 | // ArenaStringPtr. |
| 50 | // |
| 51 | // default_value parameters are taken for consistency with ArenaStringPtr, but |
| 52 | // are not used for most methods. With inlining, these should be removed from |
| 53 | // the generated binary. |
| 54 | class LIBPROTOBUF_EXPORT InlinedStringField { |
| 55 | public: |
| 56 | InlinedStringField() |
| 57 | GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; |
| 58 | explicit InlinedStringField(const ::std::string& default_value); |
| 59 | |
| 60 | void AssignWithDefault(const ::std::string* default_value, |
| 61 | const InlinedStringField& from) |
| 62 | GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; |
| 63 | |
| 64 | void ClearToEmpty(const ::std::string* default_value, Arena* arena) |
| 65 | GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { |
| 66 | ClearToEmptyNoArena(default_value); |
| 67 | } |
| 68 | void ClearNonDefaultToEmpty() GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { |
| 69 | ClearNonDefaultToEmptyNoArena(); |
| 70 | } |
| 71 | void ClearToEmptyNoArena(const ::std::string* default_value) |
| 72 | GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { |
| 73 | ClearNonDefaultToEmptyNoArena(); |
| 74 | } |
| 75 | void ClearNonDefaultToEmptyNoArena() |
| 76 | GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; |
| 77 | |
| 78 | void ClearToDefault(const ::std::string* default_value, Arena* arena) |
| 79 | GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { |
| 80 | ClearToDefaultNoArena(default_value); |
| 81 | } |
| 82 | void ClearToDefaultNoArena(const ::std::string* default_value) |
| 83 | GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; |
| 84 | |
| 85 | void Destroy(const ::std::string* default_value, Arena* arena) |
| 86 | GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { |
| 87 | DestroyNoArena(default_value); |
| 88 | } |
| 89 | void DestroyNoArena(const ::std::string* default_value) |
| 90 | GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; |
| 91 | |
| 92 | const ::std::string& Get() const GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { |
| 93 | return GetNoArena(); |
| 94 | } |
| 95 | const ::std::string& GetNoArena() const GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; |
| 96 | |
| 97 | ::std::string* Mutable(const ::std::string* default_value, Arena* arena) |
| 98 | GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { |
| 99 | return MutableNoArena(default_value); |
| 100 | } |
| 101 | ::std::string* MutableNoArena(const ::std::string* default_value) |
| 102 | GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; |
| 103 | |
| 104 | ::std::string* Release(const ::std::string* default_value, Arena* arena) { |
| 105 | return ReleaseNoArena(default_value); |
| 106 | } |
| 107 | ::std::string* ReleaseNonDefault(const ::std::string* default_value, Arena* arena) { |
| 108 | return ReleaseNonDefaultNoArena(default_value); |
| 109 | } |
| 110 | ::std::string* ReleaseNoArena(const ::std::string* default_value) { |
| 111 | return ReleaseNonDefaultNoArena(default_value); |
| 112 | } |
| 113 | ::std::string* ReleaseNonDefaultNoArena(const ::std::string* default_value); |
| 114 | |
| 115 | void Set(const ::std::string* default_value, |
| 116 | StringPiece value, |
| 117 | Arena* arena) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { |
| 118 | SetNoArena(default_value, value); |
| 119 | } |
| 120 | void SetLite(const ::std::string* default_value, |
| 121 | StringPiece value, |
| 122 | Arena* arena) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { |
| 123 | SetNoArena(default_value, value); |
| 124 | } |
| 125 | void SetNoArena(const ::std::string* default_value, |
| 126 | StringPiece value) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; |
| 127 | |
| 128 | void Set(const ::std::string* default_value, |
| 129 | const ::std::string& value, |
| 130 | Arena* arena) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { |
| 131 | SetNoArena(default_value, value); |
| 132 | } |
| 133 | void SetLite(const ::std::string* default_value, |
| 134 | const ::std::string& value, |
| 135 | Arena* arena) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE { |
| 136 | SetNoArena(default_value, value); |
| 137 | } |
| 138 | void SetNoArena(const ::std::string* default_value, |
| 139 | const ::std::string& value) |
| 140 | GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; |
| 141 | |
| 142 | #if LANG_CXX11 |
| 143 | void SetNoArena(const ::std::string* default_value, |
| 144 | ::std::string&& value) |
| 145 | GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; |
| 146 | #endif |
| 147 | void SetAllocated(const ::std::string* default_value, |
| 148 | ::std::string* value, |
| 149 | Arena* arena) { |
| 150 | SetAllocatedNoArena(default_value, value); |
| 151 | } |
| 152 | void SetAllocatedNoArena(const ::std::string* default_value, |
| 153 | ::std::string* value); |
| 154 | void Swap(InlinedStringField* from) |
| 155 | GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE; |
| 156 | ::std::string* UnsafeMutablePointer(); |
| 157 | void UnsafeSetDefault(const ::std::string* default_value); |
| 158 | ::std::string* UnsafeArenaRelease(const ::std::string* default_value, Arena* arena); |
| 159 | void UnsafeArenaSetAllocated( |
| 160 | const ::std::string* default_value, ::std::string* value, Arena* arena); |
| 161 | |
| 162 | bool IsDefault(const ::std::string* default_value) { |
| 163 | return false; |
| 164 | } |
| 165 | private: |
| 166 | ::std::string value_; |
| 167 | }; |
| 168 | |
| 169 | inline InlinedStringField::InlinedStringField() {} |
| 170 | |
| 171 | inline InlinedStringField::InlinedStringField(const ::std::string& default_value) : |
| 172 | value_(default_value) {} |
| 173 | |
| 174 | inline void InlinedStringField::AssignWithDefault( |
| 175 | const ::std::string* default_value, const InlinedStringField& from) { |
| 176 | value_ = from.value_; |
| 177 | } |
| 178 | |
| 179 | inline const ::std::string& InlinedStringField::GetNoArena() const { |
| 180 | return value_; |
| 181 | } |
| 182 | |
| 183 | inline ::std::string* InlinedStringField::MutableNoArena(const ::std::string*) { |
| 184 | return &value_; |
| 185 | } |
| 186 | |
| 187 | inline void InlinedStringField::SetAllocatedNoArena( |
| 188 | const ::std::string* default_value, ::std::string* value) { |
| 189 | if (value == NULL) { |
| 190 | value_.assign(*default_value); |
| 191 | } else { |
| 192 | #if LANG_CXX11 |
| 193 | value_.assign(std::move(*value)); |
| 194 | #else |
| 195 | value_.swap(*value); |
| 196 | #endif |
| 197 | delete value; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | inline void InlinedStringField::DestroyNoArena(const ::std::string*) { |
| 202 | // This is invoked from the generated message's ArenaDtor, which is used to |
| 203 | // clean up objects not allocated on the Arena. |
| 204 | this->~InlinedStringField(); |
| 205 | } |
| 206 | |
| 207 | inline void InlinedStringField::ClearNonDefaultToEmptyNoArena() { |
| 208 | value_.clear(); |
| 209 | } |
| 210 | |
| 211 | inline void InlinedStringField::ClearToDefaultNoArena( |
| 212 | const ::std::string* default_value) { |
| 213 | value_.assign(*default_value); |
| 214 | } |
| 215 | |
| 216 | inline ::std::string* InlinedStringField::ReleaseNonDefaultNoArena( |
| 217 | const ::std::string* default_value) { |
| 218 | ::std::string* released = new ::std::string(*default_value); |
| 219 | value_.swap(*released); |
| 220 | return released; |
| 221 | } |
| 222 | |
| 223 | inline void InlinedStringField::SetNoArena( |
| 224 | const ::std::string* default_value, StringPiece value) { |
| 225 | value_.assign(value.data(), value.length()); |
| 226 | } |
| 227 | |
| 228 | inline void InlinedStringField::SetNoArena( |
| 229 | const ::std::string* default_value, const ::std::string& value) { |
| 230 | value_.assign(value); |
| 231 | } |
| 232 | |
| 233 | #if LANG_CXX11 |
| 234 | inline void InlinedStringField::SetNoArena( |
| 235 | const ::std::string* default_value, ::std::string&& value) { |
| 236 | value_.assign(std::move(value)); |
| 237 | } |
| 238 | #endif |
| 239 | |
| 240 | inline void InlinedStringField::Swap(InlinedStringField* from) { |
| 241 | value_.swap(from->value_); |
| 242 | } |
| 243 | |
| 244 | inline ::std::string* InlinedStringField::UnsafeMutablePointer() { |
| 245 | return &value_; |
| 246 | } |
| 247 | |
| 248 | inline void InlinedStringField::UnsafeSetDefault( |
| 249 | const ::std::string* default_value) { |
| 250 | value_.assign(*default_value); |
| 251 | } |
| 252 | |
| 253 | inline ::std::string* InlinedStringField::UnsafeArenaRelease( |
| 254 | const ::std::string* default_value, Arena* arena) { |
| 255 | return ReleaseNoArena(default_value); |
| 256 | } |
| 257 | |
| 258 | inline void InlinedStringField::UnsafeArenaSetAllocated( |
| 259 | const ::std::string* default_value, ::std::string* value, Arena* arena) { |
| 260 | if (value == NULL) { |
| 261 | value_.assign(*default_value); |
| 262 | } else { |
| 263 | value_.assign(*value); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | } // namespace internal |
| 268 | } // namespace protobuf |
| 269 | |
| 270 | } // namespace google |
| 271 | #endif // GOOGLE_PROTOBUF_INLINED_STRING_FIELD_H__ |