reflection.h 22 KB

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