reflection.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 int32
  251. // CPPTYPE_UINT32 uint32 uint32
  252. // CPPTYPE_INT64 int64 int64
  253. // CPPTYPE_UINT64 uint64 uint64
  254. // CPPTYPE_DOUBLE double double
  255. // CPPTYPE_FLOAT float float
  256. // CPPTYPE_BOOL bool bool
  257. // CPPTYPE_ENUM generated enum type int32
  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 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). 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). 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. : public std::iterator<std::forward_iterator_tag, T> {
  354. typedef typename RefTypeTraits<T>::AccessorValueType AccessorValueType;
  355. typedef typename RefTypeTraits<T>::IteratorValueType IteratorValueType;
  356. typedef typename RefTypeTraits<T>::IteratorPointerType IteratorPointerType;
  357. public:
  358. // Constructor for non-message fields.
  359. RepeatedFieldRefIterator(const void* data,
  360. const RepeatedFieldAccessor* accessor, bool begin)
  361. : data_(data),
  362. accessor_(accessor),
  363. iterator_(begin ? accessor->BeginIterator(data)
  364. : accessor->EndIterator(data)),
  365. // The end iterator must not be dereferenced, no need for scratch space.
  366. scratch_space_(begin ? new AccessorValueType : nullptr) {}
  367. // Constructor for message fields.
  368. RepeatedFieldRefIterator(const void* data,
  369. const RepeatedFieldAccessor* accessor, bool begin,
  370. AccessorValueType* scratch_space)
  371. : data_(data),
  372. accessor_(accessor),
  373. iterator_(begin ? accessor->BeginIterator(data)
  374. : accessor->EndIterator(data)),
  375. scratch_space_(scratch_space) {}
  376. ~RepeatedFieldRefIterator() { accessor_->DeleteIterator(data_, iterator_); }
  377. RepeatedFieldRefIterator operator++(int) {
  378. RepeatedFieldRefIterator tmp(*this);
  379. iterator_ = accessor_->AdvanceIterator(data_, iterator_);
  380. return tmp;
  381. }
  382. RepeatedFieldRefIterator& operator++() {
  383. iterator_ = accessor_->AdvanceIterator(data_, iterator_);
  384. return *this;
  385. }
  386. IteratorValueType operator*() const {
  387. return static_cast<IteratorValueType>(
  388. *static_cast<const AccessorValueType*>(accessor_->GetIteratorValue(
  389. data_, iterator_, scratch_space_.get())));
  390. }
  391. IteratorPointerType operator->() const {
  392. return static_cast<IteratorPointerType>(
  393. accessor_->GetIteratorValue(data_, iterator_, scratch_space_.get()));
  394. }
  395. bool operator!=(const RepeatedFieldRefIterator& other) const {
  396. assert(data_ == other.data_);
  397. assert(accessor_ == other.accessor_);
  398. return !accessor_->EqualsIterator(data_, iterator_, other.iterator_);
  399. }
  400. bool operator==(const RepeatedFieldRefIterator& other) const {
  401. return !this->operator!=(other);
  402. }
  403. RepeatedFieldRefIterator(const RepeatedFieldRefIterator& other)
  404. : data_(other.data_),
  405. accessor_(other.accessor_),
  406. iterator_(accessor_->CopyIterator(data_, other.iterator_)) {}
  407. RepeatedFieldRefIterator& operator=(const RepeatedFieldRefIterator& other) {
  408. if (this != &other) {
  409. accessor_->DeleteIterator(data_, iterator_);
  410. data_ = other.data_;
  411. accessor_ = other.accessor_;
  412. iterator_ = accessor_->CopyIterator(data_, other.iterator_);
  413. }
  414. return *this;
  415. }
  416. protected:
  417. const void* data_;
  418. const RepeatedFieldAccessor* accessor_;
  419. void* iterator_;
  420. std::unique_ptr<AccessorValueType> scratch_space_;
  421. };
  422. // TypeTraits that maps the type parameter T of RepeatedFieldRef or
  423. // MutableRepeatedFieldRef to corresponding iterator type,
  424. // RepeatedFieldAccessor type, etc.
  425. template <typename T>
  426. struct PrimitiveTraits {
  427. static constexpr bool is_primitive = false;
  428. };
  429. #define DEFINE_PRIMITIVE(TYPE, type) \
  430. template <> \
  431. struct PrimitiveTraits<type> { \
  432. static const bool is_primitive = true; \
  433. static const FieldDescriptor::CppType cpp_type = \
  434. FieldDescriptor::CPPTYPE_##TYPE; \
  435. };
  436. DEFINE_PRIMITIVE(INT32, int32)
  437. DEFINE_PRIMITIVE(UINT32, uint32)
  438. DEFINE_PRIMITIVE(INT64, int64)
  439. DEFINE_PRIMITIVE(UINT64, uint64)
  440. DEFINE_PRIMITIVE(FLOAT, float)
  441. DEFINE_PRIMITIVE(DOUBLE, double)
  442. DEFINE_PRIMITIVE(BOOL, bool)
  443. #undef DEFINE_PRIMITIVE
  444. template <typename T>
  445. struct RefTypeTraits<
  446. T, typename std::enable_if<PrimitiveTraits<T>::is_primitive>::type> {
  447. typedef RepeatedFieldRefIterator<T> iterator;
  448. typedef RepeatedFieldAccessor AccessorType;
  449. typedef T AccessorValueType;
  450. typedef T IteratorValueType;
  451. typedef T* IteratorPointerType;
  452. static constexpr FieldDescriptor::CppType cpp_type =
  453. PrimitiveTraits<T>::cpp_type;
  454. static const Descriptor* GetMessageFieldDescriptor() { return NULL; }
  455. };
  456. template <typename T>
  457. struct RefTypeTraits<
  458. T, typename std::enable_if<is_proto_enum<T>::value>::type> {
  459. typedef RepeatedFieldRefIterator<T> iterator;
  460. typedef RepeatedFieldAccessor AccessorType;
  461. // We use int32 for repeated enums in RepeatedFieldAccessor.
  462. typedef int32 AccessorValueType;
  463. typedef T IteratorValueType;
  464. typedef int32* IteratorPointerType;
  465. static constexpr FieldDescriptor::CppType cpp_type =
  466. FieldDescriptor::CPPTYPE_ENUM;
  467. static const Descriptor* GetMessageFieldDescriptor() { return NULL; }
  468. };
  469. template <typename T>
  470. struct RefTypeTraits<
  471. T, typename std::enable_if<std::is_same<std::string, T>::value>::type> {
  472. typedef RepeatedFieldRefIterator<T> iterator;
  473. typedef RepeatedFieldAccessor AccessorType;
  474. typedef std::string AccessorValueType;
  475. typedef const std::string IteratorValueType;
  476. typedef const std::string* IteratorPointerType;
  477. static constexpr FieldDescriptor::CppType cpp_type =
  478. FieldDescriptor::CPPTYPE_STRING;
  479. static const Descriptor* GetMessageFieldDescriptor() { return NULL; }
  480. };
  481. template <typename T>
  482. struct MessageDescriptorGetter {
  483. static const Descriptor* get() {
  484. return T::default_instance().GetDescriptor();
  485. }
  486. };
  487. template <>
  488. struct MessageDescriptorGetter<Message> {
  489. static const Descriptor* get() { return NULL; }
  490. };
  491. template <typename T>
  492. struct RefTypeTraits<
  493. T, typename std::enable_if<std::is_base_of<Message, T>::value>::type> {
  494. typedef RepeatedFieldRefIterator<T> iterator;
  495. typedef RepeatedFieldAccessor AccessorType;
  496. typedef Message AccessorValueType;
  497. typedef const T& IteratorValueType;
  498. typedef const T* IteratorPointerType;
  499. static constexpr FieldDescriptor::CppType cpp_type =
  500. FieldDescriptor::CPPTYPE_MESSAGE;
  501. static const Descriptor* GetMessageFieldDescriptor() {
  502. return MessageDescriptorGetter<T>::get();
  503. }
  504. };
  505. } // namespace internal
  506. } // namespace protobuf
  507. } // namespace google
  508. #include <google/protobuf/port_undef.inc>
  509. #endif // GOOGLE_PROTOBUF_REFLECTION_H__