reflection.h 22 KB

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