You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

reflection.h 23 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. // This header defines the RepeatedFieldRef class template used to access
  31. // repeated fields with protobuf reflection API.
  32. #ifndef GOOGLE_PROTOBUF_REFLECTION_H__
  33. #define GOOGLE_PROTOBUF_REFLECTION_H__
  34. #include <memory>
  35. #include <google/protobuf/message.h>
  36. #include <google/protobuf/generated_enum_util.h>
  37. #ifdef SWIG
  38. #error "You cannot SWIG proto headers"
  39. #endif
  40. // Must be included last.
  41. #include <google/protobuf/port_def.inc>
  42. namespace google {
  43. namespace protobuf {
  44. namespace internal {
  45. template <typename T, typename Enable = void>
  46. struct RefTypeTraits;
  47. } // namespace internal
  48. template <typename T>
  49. RepeatedFieldRef<T> Reflection::GetRepeatedFieldRef(
  50. const Message& message, const FieldDescriptor* field) const {
  51. return RepeatedFieldRef<T>(message, field);
  52. }
  53. template <typename T>
  54. MutableRepeatedFieldRef<T> Reflection::GetMutableRepeatedFieldRef(
  55. Message* message, const FieldDescriptor* field) const {
  56. return MutableRepeatedFieldRef<T>(message, field);
  57. }
  58. // RepeatedFieldRef definition for non-message types.
  59. template <typename T>
  60. class RepeatedFieldRef<
  61. T, typename std::enable_if<!std::is_base_of<Message, T>::value>::type> {
  62. typedef typename internal::RefTypeTraits<T>::iterator IteratorType;
  63. typedef typename internal::RefTypeTraits<T>::AccessorType AccessorType;
  64. public:
  65. bool empty() const { return accessor_->IsEmpty(data_); }
  66. int size() const { return accessor_->Size(data_); }
  67. T Get(int index) const { return accessor_->template Get<T>(data_, index); }
  68. typedef IteratorType iterator;
  69. typedef IteratorType const_iterator;
  70. typedef T value_type;
  71. typedef T& reference;
  72. typedef const T& const_reference;
  73. typedef int size_type;
  74. typedef ptrdiff_t difference_type;
  75. iterator begin() const { return iterator(data_, accessor_, true); }
  76. iterator end() const { return iterator(data_, accessor_, false); }
  77. private:
  78. friend class Reflection;
  79. RepeatedFieldRef(const Message& message, const FieldDescriptor* field) {
  80. const Reflection* reflection = message.GetReflection();
  81. data_ = reflection->RepeatedFieldData(const_cast<Message*>(&message), field,
  82. internal::RefTypeTraits<T>::cpp_type,
  83. nullptr);
  84. accessor_ = reflection->RepeatedFieldAccessor(field);
  85. }
  86. const void* data_;
  87. const AccessorType* accessor_;
  88. };
  89. // MutableRepeatedFieldRef definition for non-message types.
  90. template <typename T>
  91. class MutableRepeatedFieldRef<
  92. T, typename std::enable_if<!std::is_base_of<Message, T>::value>::type> {
  93. typedef typename internal::RefTypeTraits<T>::AccessorType AccessorType;
  94. public:
  95. bool empty() const { return accessor_->IsEmpty(data_); }
  96. int size() const { return accessor_->Size(data_); }
  97. T Get(int index) const { return accessor_->template Get<T>(data_, index); }
  98. void Set(int index, const T& value) const {
  99. accessor_->template Set<T>(data_, index, value);
  100. }
  101. void Add(const T& value) const { accessor_->template Add<T>(data_, value); }
  102. void RemoveLast() const { accessor_->RemoveLast(data_); }
  103. void SwapElements(int index1, int index2) const {
  104. accessor_->SwapElements(data_, index1, index2);
  105. }
  106. void Clear() const { accessor_->Clear(data_); }
  107. void Swap(const MutableRepeatedFieldRef& other) const {
  108. accessor_->Swap(data_, other.accessor_, other.data_);
  109. }
  110. template <typename Container>
  111. void MergeFrom(const Container& container) const {
  112. typedef typename Container::const_iterator Iterator;
  113. for (Iterator it = container.begin(); it != container.end(); ++it) {
  114. Add(*it);
  115. }
  116. }
  117. template <typename Container>
  118. void CopyFrom(const Container& container) const {
  119. Clear();
  120. MergeFrom(container);
  121. }
  122. private:
  123. friend class Reflection;
  124. MutableRepeatedFieldRef(Message* message, const FieldDescriptor* field) {
  125. const Reflection* reflection = message->GetReflection();
  126. data_ = reflection->RepeatedFieldData(
  127. message, field, internal::RefTypeTraits<T>::cpp_type, nullptr);
  128. accessor_ = reflection->RepeatedFieldAccessor(field);
  129. }
  130. void* data_;
  131. const AccessorType* accessor_;
  132. };
  133. // RepeatedFieldRef definition for message types.
  134. template <typename T>
  135. class RepeatedFieldRef<
  136. T, typename std::enable_if<std::is_base_of<Message, T>::value>::type> {
  137. typedef typename internal::RefTypeTraits<T>::iterator IteratorType;
  138. typedef typename internal::RefTypeTraits<T>::AccessorType AccessorType;
  139. public:
  140. bool empty() const { return accessor_->IsEmpty(data_); }
  141. int size() const { return accessor_->Size(data_); }
  142. // This method returns a reference to the underlying message object if it
  143. // exists. If a message object doesn't exist (e.g., data stored in serialized
  144. // form), scratch_space will be filled with the data and a reference to it
  145. // will be returned.
  146. //
  147. // Example:
  148. // RepeatedFieldRef<Message> h = ...
  149. // unique_ptr<Message> scratch_space(h.NewMessage());
  150. // const Message& item = h.Get(index, scratch_space.get());
  151. const T& Get(int index, T* scratch_space) const {
  152. return *static_cast<const T*>(accessor_->Get(data_, index, scratch_space));
  153. }
  154. // Create a new message of the same type as the messages stored in this
  155. // repeated field. Caller takes ownership of the returned object.
  156. T* NewMessage() const { return static_cast<T*>(default_instance_->New()); }
  157. typedef IteratorType iterator;
  158. typedef IteratorType const_iterator;
  159. typedef T value_type;
  160. typedef T& reference;
  161. typedef const T& const_reference;
  162. typedef int size_type;
  163. typedef ptrdiff_t difference_type;
  164. iterator begin() const {
  165. return iterator(data_, accessor_, true, NewMessage());
  166. }
  167. iterator end() const {
  168. // The end iterator must not be dereferenced, no need for scratch space.
  169. return iterator(data_, accessor_, false, nullptr);
  170. }
  171. private:
  172. friend class Reflection;
  173. RepeatedFieldRef(const Message& message, const FieldDescriptor* field) {
  174. const Reflection* reflection = message.GetReflection();
  175. data_ = reflection->RepeatedFieldData(
  176. const_cast<Message*>(&message), field,
  177. internal::RefTypeTraits<T>::cpp_type,
  178. internal::RefTypeTraits<T>::GetMessageFieldDescriptor());
  179. accessor_ = reflection->RepeatedFieldAccessor(field);
  180. default_instance_ =
  181. reflection->GetMessageFactory()->GetPrototype(field->message_type());
  182. }
  183. const void* data_;
  184. const AccessorType* accessor_;
  185. const Message* default_instance_;
  186. };
  187. // MutableRepeatedFieldRef definition for message types.
  188. template <typename T>
  189. class MutableRepeatedFieldRef<
  190. T, typename std::enable_if<std::is_base_of<Message, T>::value>::type> {
  191. typedef typename internal::RefTypeTraits<T>::AccessorType AccessorType;
  192. public:
  193. bool empty() const { return accessor_->IsEmpty(data_); }
  194. int size() const { return accessor_->Size(data_); }
  195. // See comments for RepeatedFieldRef<Message>::Get()
  196. const T& Get(int index, T* scratch_space) const {
  197. return *static_cast<const T*>(accessor_->Get(data_, index, scratch_space));
  198. }
  199. // Create a new message of the same type as the messages stored in this
  200. // repeated field. Caller takes ownership of the returned object.
  201. T* NewMessage() const { return static_cast<T*>(default_instance_->New()); }
  202. void Set(int index, const T& value) const {
  203. accessor_->Set(data_, index, &value);
  204. }
  205. void Add(const T& value) const { accessor_->Add(data_, &value); }
  206. void RemoveLast() const { accessor_->RemoveLast(data_); }
  207. void SwapElements(int index1, int index2) const {
  208. accessor_->SwapElements(data_, index1, index2);
  209. }
  210. void Clear() const { accessor_->Clear(data_); }
  211. void Swap(const MutableRepeatedFieldRef& other) const {
  212. accessor_->Swap(data_, other.accessor_, other.data_);
  213. }
  214. template <typename Container>
  215. void MergeFrom(const Container& container) const {
  216. typedef typename Container::const_iterator Iterator;
  217. for (Iterator it = container.begin(); it != container.end(); ++it) {
  218. Add(*it);
  219. }
  220. }
  221. template <typename Container>
  222. void CopyFrom(const Container& container) const {
  223. Clear();
  224. MergeFrom(container);
  225. }
  226. private:
  227. friend class Reflection;
  228. MutableRepeatedFieldRef(Message* message, const FieldDescriptor* field) {
  229. const Reflection* reflection = message->GetReflection();
  230. data_ = reflection->RepeatedFieldData(
  231. message, field, internal::RefTypeTraits<T>::cpp_type,
  232. internal::RefTypeTraits<T>::GetMessageFieldDescriptor());
  233. accessor_ = reflection->RepeatedFieldAccessor(field);
  234. default_instance_ =
  235. reflection->GetMessageFactory()->GetPrototype(field->message_type());
  236. }
  237. void* data_;
  238. const AccessorType* accessor_;
  239. const Message* default_instance_;
  240. };
  241. namespace internal {
  242. // Interfaces used to implement reflection RepeatedFieldRef API.
  243. // Reflection::GetRepeatedAccessor() should return a pointer to an singleton
  244. // object that implements the below interface.
  245. //
  246. // This interface passes/returns values using void pointers. The actual type
  247. // of the value depends on the field's cpp_type. Following is a mapping from
  248. // cpp_type to the type that should be used in this interface:
  249. //
  250. // field->cpp_type() T Actual type of void*
  251. // CPPTYPE_INT32 int32_t int32_t
  252. // CPPTYPE_UINT32 uint32_t uint32_t
  253. // CPPTYPE_INT64 int64_t int64_t
  254. // CPPTYPE_UINT64 uint64_t uint64_t
  255. // CPPTYPE_DOUBLE double double
  256. // CPPTYPE_FLOAT float float
  257. // CPPTYPE_BOOL bool bool
  258. // CPPTYPE_ENUM generated enum type int32_t
  259. // CPPTYPE_STRING string std::string
  260. // CPPTYPE_MESSAGE generated message type google::protobuf::Message
  261. // or google::protobuf::Message
  262. //
  263. // Note that for enums we use int32_t in the interface.
  264. //
  265. // You can map from T to the actual type using RefTypeTraits:
  266. // typedef RefTypeTraits<T>::AccessorValueType ActualType;
  267. class PROTOBUF_EXPORT RepeatedFieldAccessor {
  268. public:
  269. // Typedefs for clarity.
  270. typedef void Field;
  271. typedef void Value;
  272. typedef void Iterator;
  273. virtual bool IsEmpty(const Field* data) const = 0;
  274. virtual int Size(const Field* data) const = 0;
  275. // Depends on the underlying representation of the repeated field, this
  276. // method can return a pointer to the underlying object if such an object
  277. // exists, or fill the data into scratch_space and return scratch_space.
  278. // Callers of this method must ensure scratch_space is a valid pointer
  279. // to a mutable object of the correct type.
  280. virtual const Value* Get(const Field* data, int index,
  281. Value* scratch_space) const = 0;
  282. virtual void Clear(Field* data) const = 0;
  283. virtual void Set(Field* data, int index, const Value* value) const = 0;
  284. virtual void Add(Field* data, const Value* value) const = 0;
  285. virtual void RemoveLast(Field* data) const = 0;
  286. virtual void SwapElements(Field* data, int index1, int index2) const = 0;
  287. virtual void Swap(Field* data, const RepeatedFieldAccessor* other_mutator,
  288. Field* other_data) const = 0;
  289. // Create an iterator that points at the beginning of the repeated field.
  290. virtual Iterator* BeginIterator(const Field* data) const = 0;
  291. // Create an iterator that points at the end of the repeated field.
  292. virtual Iterator* EndIterator(const Field* data) const = 0;
  293. // Make a copy of an iterator and return the new copy.
  294. virtual Iterator* CopyIterator(const Field* data,
  295. const Iterator* iterator) const = 0;
  296. // Move an iterator to point to the next element.
  297. virtual Iterator* AdvanceIterator(const Field* data,
  298. Iterator* iterator) const = 0;
  299. // Compare whether two iterators point to the same element.
  300. virtual bool EqualsIterator(const Field* data, const Iterator* a,
  301. const Iterator* b) const = 0;
  302. // Delete an iterator created by BeginIterator(), EndIterator() and
  303. // CopyIterator().
  304. virtual void DeleteIterator(const Field* data, Iterator* iterator) const = 0;
  305. // Like Get() but for iterators.
  306. virtual const Value* GetIteratorValue(const Field* data,
  307. const Iterator* iterator,
  308. Value* scratch_space) const = 0;
  309. // Templated methods that make using this interface easier for non-message
  310. // types.
  311. template <typename T>
  312. T Get(const Field* data, int index) const {
  313. typedef typename RefTypeTraits<T>::AccessorValueType ActualType;
  314. ActualType scratch_space;
  315. return static_cast<T>(*reinterpret_cast<const ActualType*>(
  316. Get(data, index, static_cast<Value*>(&scratch_space))));
  317. }
  318. template <typename T, typename ValueType>
  319. void Set(Field* data, int index, const ValueType& value) const {
  320. typedef typename RefTypeTraits<T>::AccessorValueType ActualType;
  321. // In this RepeatedFieldAccessor interface we pass/return data using
  322. // raw pointers. Type of the data these raw pointers point to should
  323. // be ActualType. Here we have a ValueType object and want a ActualType
  324. // pointer. We can't cast a ValueType pointer to an ActualType pointer
  325. // directly because their type might be different (for enums ValueType
  326. // may be a generated enum type while ActualType is int32_t). To be safe
  327. // we make a copy to get a temporary ActualType object and use it.
  328. ActualType tmp = static_cast<ActualType>(value);
  329. Set(data, index, static_cast<const Value*>(&tmp));
  330. }
  331. template <typename T, typename ValueType>
  332. void Add(Field* data, const ValueType& value) const {
  333. typedef typename RefTypeTraits<T>::AccessorValueType ActualType;
  334. // In this RepeatedFieldAccessor interface we pass/return data using
  335. // raw pointers. Type of the data these raw pointers point to should
  336. // be ActualType. Here we have a ValueType object and want a ActualType
  337. // pointer. We can't cast a ValueType pointer to an ActualType pointer
  338. // directly because their type might be different (for enums ValueType
  339. // may be a generated enum type while ActualType is int32_t). To be safe
  340. // we make a copy to get a temporary ActualType object and use it.
  341. ActualType tmp = static_cast<ActualType>(value);
  342. Add(data, static_cast<const Value*>(&tmp));
  343. }
  344. protected:
  345. // We want the destructor to be completely trivial as to allow it to be
  346. // a function local static. Hence we make it non-virtual and protected,
  347. // this class only live as part of a global singleton and should not be
  348. // deleted.
  349. ~RepeatedFieldAccessor() = default;
  350. };
  351. // Implement (Mutable)RepeatedFieldRef::iterator
  352. template <typename T>
  353. class RepeatedFieldRefIterator {
  354. typedef typename RefTypeTraits<T>::AccessorValueType AccessorValueType;
  355. typedef typename RefTypeTraits<T>::IteratorValueType IteratorValueType;
  356. typedef typename RefTypeTraits<T>::IteratorPointerType IteratorPointerType;
  357. public:
  358. using iterator_category = std::forward_iterator_tag;
  359. using value_type = T;
  360. using pointer = T*;
  361. using reference = T&;
  362. using difference_type = std::ptrdiff_t;
  363. // Constructor for non-message fields.
  364. RepeatedFieldRefIterator(const void* data,
  365. const RepeatedFieldAccessor* accessor, bool begin)
  366. : data_(data),
  367. accessor_(accessor),
  368. iterator_(begin ? accessor->BeginIterator(data)
  369. : accessor->EndIterator(data)),
  370. // The end iterator must not be dereferenced, no need for scratch space.
  371. scratch_space_(begin ? new AccessorValueType : nullptr) {}
  372. // Constructor for message fields.
  373. RepeatedFieldRefIterator(const void* data,
  374. const RepeatedFieldAccessor* accessor, bool begin,
  375. AccessorValueType* scratch_space)
  376. : data_(data),
  377. accessor_(accessor),
  378. iterator_(begin ? accessor->BeginIterator(data)
  379. : accessor->EndIterator(data)),
  380. scratch_space_(scratch_space) {}
  381. ~RepeatedFieldRefIterator() { accessor_->DeleteIterator(data_, iterator_); }
  382. RepeatedFieldRefIterator operator++(int) {
  383. RepeatedFieldRefIterator tmp(*this);
  384. iterator_ = accessor_->AdvanceIterator(data_, iterator_);
  385. return tmp;
  386. }
  387. RepeatedFieldRefIterator& operator++() {
  388. iterator_ = accessor_->AdvanceIterator(data_, iterator_);
  389. return *this;
  390. }
  391. IteratorValueType operator*() const {
  392. return static_cast<IteratorValueType>(
  393. *static_cast<const AccessorValueType*>(accessor_->GetIteratorValue(
  394. data_, iterator_, scratch_space_.get())));
  395. }
  396. IteratorPointerType operator->() const {
  397. return static_cast<IteratorPointerType>(
  398. accessor_->GetIteratorValue(data_, iterator_, scratch_space_.get()));
  399. }
  400. bool operator!=(const RepeatedFieldRefIterator& other) const {
  401. assert(data_ == other.data_);
  402. assert(accessor_ == other.accessor_);
  403. return !accessor_->EqualsIterator(data_, iterator_, other.iterator_);
  404. }
  405. bool operator==(const RepeatedFieldRefIterator& other) const {
  406. return !this->operator!=(other);
  407. }
  408. RepeatedFieldRefIterator(const RepeatedFieldRefIterator& other)
  409. : data_(other.data_),
  410. accessor_(other.accessor_),
  411. iterator_(accessor_->CopyIterator(data_, other.iterator_)) {}
  412. RepeatedFieldRefIterator& operator=(const RepeatedFieldRefIterator& other) {
  413. if (this != &other) {
  414. accessor_->DeleteIterator(data_, iterator_);
  415. data_ = other.data_;
  416. accessor_ = other.accessor_;
  417. iterator_ = accessor_->CopyIterator(data_, other.iterator_);
  418. }
  419. return *this;
  420. }
  421. protected:
  422. const void* data_;
  423. const RepeatedFieldAccessor* accessor_;
  424. void* iterator_;
  425. std::unique_ptr<AccessorValueType> scratch_space_;
  426. };
  427. // TypeTraits that maps the type parameter T of RepeatedFieldRef or
  428. // MutableRepeatedFieldRef to corresponding iterator type,
  429. // RepeatedFieldAccessor type, etc.
  430. template <typename T>
  431. struct PrimitiveTraits {
  432. static constexpr bool is_primitive = false;
  433. };
  434. #define DEFINE_PRIMITIVE(TYPE, type) \
  435. template <> \
  436. struct PrimitiveTraits<type> { \
  437. static const bool is_primitive = true; \
  438. static const FieldDescriptor::CppType cpp_type = \
  439. FieldDescriptor::CPPTYPE_##TYPE; \
  440. };
  441. DEFINE_PRIMITIVE(INT32, int32_t)
  442. DEFINE_PRIMITIVE(UINT32, uint32_t)
  443. DEFINE_PRIMITIVE(INT64, int64_t)
  444. DEFINE_PRIMITIVE(UINT64, uint64_t)
  445. DEFINE_PRIMITIVE(FLOAT, float)
  446. DEFINE_PRIMITIVE(DOUBLE, double)
  447. DEFINE_PRIMITIVE(BOOL, bool)
  448. #undef DEFINE_PRIMITIVE
  449. template <typename T>
  450. struct RefTypeTraits<
  451. T, typename std::enable_if<PrimitiveTraits<T>::is_primitive>::type> {
  452. typedef RepeatedFieldRefIterator<T> iterator;
  453. typedef RepeatedFieldAccessor AccessorType;
  454. typedef T AccessorValueType;
  455. typedef T IteratorValueType;
  456. typedef T* IteratorPointerType;
  457. static constexpr FieldDescriptor::CppType cpp_type =
  458. PrimitiveTraits<T>::cpp_type;
  459. static const Descriptor* GetMessageFieldDescriptor() { return nullptr; }
  460. };
  461. template <typename T>
  462. struct RefTypeTraits<
  463. T, typename std::enable_if<is_proto_enum<T>::value>::type> {
  464. typedef RepeatedFieldRefIterator<T> iterator;
  465. typedef RepeatedFieldAccessor AccessorType;
  466. // We use int32_t for repeated enums in RepeatedFieldAccessor.
  467. typedef int32_t AccessorValueType;
  468. typedef T IteratorValueType;
  469. typedef int32_t* IteratorPointerType;
  470. static constexpr FieldDescriptor::CppType cpp_type =
  471. FieldDescriptor::CPPTYPE_ENUM;
  472. static const Descriptor* GetMessageFieldDescriptor() { return nullptr; }
  473. };
  474. template <typename T>
  475. struct RefTypeTraits<
  476. T, typename std::enable_if<std::is_same<std::string, T>::value>::type> {
  477. typedef RepeatedFieldRefIterator<T> iterator;
  478. typedef RepeatedFieldAccessor AccessorType;
  479. typedef std::string AccessorValueType;
  480. typedef const std::string IteratorValueType;
  481. typedef const std::string* IteratorPointerType;
  482. static constexpr FieldDescriptor::CppType cpp_type =
  483. FieldDescriptor::CPPTYPE_STRING;
  484. static const Descriptor* GetMessageFieldDescriptor() { return nullptr; }
  485. };
  486. template <typename T>
  487. struct MessageDescriptorGetter {
  488. static const Descriptor* get() {
  489. return T::default_instance().GetDescriptor();
  490. }
  491. };
  492. template <>
  493. struct MessageDescriptorGetter<Message> {
  494. static const Descriptor* get() { return nullptr; }
  495. };
  496. template <typename T>
  497. struct RefTypeTraits<
  498. T, typename std::enable_if<std::is_base_of<Message, T>::value>::type> {
  499. typedef RepeatedFieldRefIterator<T> iterator;
  500. typedef RepeatedFieldAccessor AccessorType;
  501. typedef Message AccessorValueType;
  502. typedef const T& IteratorValueType;
  503. typedef const T* IteratorPointerType;
  504. static constexpr FieldDescriptor::CppType cpp_type =
  505. FieldDescriptor::CPPTYPE_MESSAGE;
  506. static const Descriptor* GetMessageFieldDescriptor() {
  507. return MessageDescriptorGetter<T>::get();
  508. }
  509. };
  510. } // namespace internal
  511. } // namespace protobuf
  512. } // namespace google
  513. #include <google/protobuf/port_undef.inc>
  514. #endif // GOOGLE_PROTOBUF_REFLECTION_H__