reflection.h 22 KB

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