reflection_internal.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. #ifndef GOOGLE_PROTOBUF_REFLECTION_INTERNAL_H__
  31. #define GOOGLE_PROTOBUF_REFLECTION_INTERNAL_H__
  32. #include <google/protobuf/map_field.h>
  33. #include <google/protobuf/reflection.h>
  34. #include <google/protobuf/repeated_field.h>
  35. namespace google {
  36. namespace protobuf {
  37. namespace internal {
  38. // A base class for RepeatedFieldAccessor implementations that can support
  39. // random-access efficiently. All iterator methods delegates the work to
  40. // corresponding random-access methods.
  41. class RandomAccessRepeatedFieldAccessor : public RepeatedFieldAccessor {
  42. public:
  43. virtual ~RandomAccessRepeatedFieldAccessor() {}
  44. virtual Iterator* BeginIterator(const Field* data) const {
  45. return PositionToIterator(0);
  46. }
  47. virtual Iterator* EndIterator(const Field* data) const {
  48. return PositionToIterator(this->Size(data));
  49. }
  50. virtual Iterator* CopyIterator(const Field* data,
  51. const Iterator* iterator) const {
  52. return const_cast<Iterator*>(iterator);
  53. }
  54. virtual Iterator* AdvanceIterator(const Field* data,
  55. Iterator* iterator) const {
  56. return PositionToIterator(IteratorToPosition(iterator) + 1);
  57. }
  58. virtual bool EqualsIterator(const Field* data,
  59. const Iterator* a,
  60. const Iterator* b) const {
  61. return a == b;
  62. }
  63. virtual void DeleteIterator(const Field* data, Iterator* iterator) const {
  64. }
  65. virtual const Value* GetIteratorValue(const Field* data,
  66. const Iterator* iterator,
  67. Value* scratch_space) const {
  68. return Get(data, static_cast<int>(IteratorToPosition(iterator)),
  69. scratch_space);
  70. }
  71. private:
  72. static intptr_t IteratorToPosition(const Iterator* iterator) {
  73. return reinterpret_cast<intptr_t>(iterator);
  74. }
  75. static Iterator* PositionToIterator(intptr_t position) {
  76. return reinterpret_cast<Iterator*>(position);
  77. }
  78. };
  79. // Base class for RepeatedFieldAccessor implementations that manipulates
  80. // RepeatedField<T>.
  81. template<typename T>
  82. class RepeatedFieldWrapper : public RandomAccessRepeatedFieldAccessor {
  83. public:
  84. RepeatedFieldWrapper() {}
  85. virtual ~RepeatedFieldWrapper() {}
  86. virtual bool IsEmpty(const Field* data) const {
  87. return GetRepeatedField(data)->empty();
  88. }
  89. virtual int Size(const Field* data) const {
  90. return GetRepeatedField(data)->size();
  91. }
  92. virtual const Value* Get(const Field* data, int index,
  93. Value* scratch_space) const {
  94. return ConvertFromT(GetRepeatedField(data)->Get(index), scratch_space);
  95. }
  96. virtual void Clear(Field* data) const {
  97. MutableRepeatedField(data)->Clear();
  98. }
  99. virtual void Set(Field* data, int index, const Value* value) const {
  100. MutableRepeatedField(data)->Set(index, ConvertToT(value));
  101. }
  102. virtual void Add(Field* data, const Value* value) const {
  103. MutableRepeatedField(data)->Add(ConvertToT(value));
  104. }
  105. virtual void RemoveLast(Field* data) const {
  106. MutableRepeatedField(data)->RemoveLast();
  107. }
  108. virtual void SwapElements(Field* data, int index1, int index2) const {
  109. MutableRepeatedField(data)->SwapElements(index1, index2);
  110. }
  111. protected:
  112. typedef RepeatedField<T> RepeatedFieldType;
  113. static const RepeatedFieldType* GetRepeatedField(const Field* data) {
  114. return reinterpret_cast<const RepeatedFieldType*>(data);
  115. }
  116. static RepeatedFieldType* MutableRepeatedField(Field* data) {
  117. return reinterpret_cast<RepeatedFieldType*>(data);
  118. }
  119. // Convert an object recevied by this accessor to an object to be stored in
  120. // the underlying RepeatedField.
  121. virtual T ConvertToT(const Value* value) const = 0;
  122. // Convert an object stored in RepeatedPtrField to an object that will be
  123. // returned by this accessor. If the two objects have the same type (true
  124. // for string fields with ctype=STRING), a pointer to the source object can
  125. // be returned directly. Otherwise, data should be copied from value to
  126. // scratch_space and scratch_space should be returned.
  127. virtual const Value* ConvertFromT(const T& value,
  128. Value* scratch_space) const = 0;
  129. };
  130. // Base class for RepeatedFieldAccessor implementations that manipulates
  131. // RepeatedPtrField<T>.
  132. template<typename T>
  133. class RepeatedPtrFieldWrapper : public RandomAccessRepeatedFieldAccessor {
  134. public:
  135. RepeatedPtrFieldWrapper() {}
  136. virtual ~RepeatedPtrFieldWrapper() {}
  137. virtual bool IsEmpty(const Field* data) const {
  138. return GetRepeatedField(data)->empty();
  139. }
  140. virtual int Size(const Field* data) const {
  141. return GetRepeatedField(data)->size();
  142. }
  143. virtual const Value* Get(const Field* data, int index,
  144. Value* scratch_space) const {
  145. return ConvertFromT(GetRepeatedField(data)->Get(index), scratch_space);
  146. }
  147. virtual void Clear(Field* data) const {
  148. MutableRepeatedField(data)->Clear();
  149. }
  150. virtual void Set(Field* data, int index, const Value* value) const {
  151. ConvertToT(value, MutableRepeatedField(data)->Mutable(index));
  152. }
  153. virtual void Add(Field* data, const Value* value) const {
  154. T* allocated = New(value);
  155. ConvertToT(value, allocated);
  156. MutableRepeatedField(data)->AddAllocated(allocated);
  157. }
  158. virtual void RemoveLast(Field* data) const {
  159. MutableRepeatedField(data)->RemoveLast();
  160. }
  161. virtual void SwapElements(Field* data, int index1, int index2) const {
  162. MutableRepeatedField(data)->SwapElements(index1, index2);
  163. }
  164. protected:
  165. typedef RepeatedPtrField<T> RepeatedFieldType;
  166. static const RepeatedFieldType* GetRepeatedField(const Field* data) {
  167. return reinterpret_cast<const RepeatedFieldType*>(data);
  168. }
  169. static RepeatedFieldType* MutableRepeatedField(Field* data) {
  170. return reinterpret_cast<RepeatedFieldType*>(data);
  171. }
  172. // Create a new T instance. For repeated message fields, T can be specified
  173. // as google::protobuf::Message so we can't use "new T()" directly. In that case, value
  174. // should be a message of the same type (it's ensured by the caller) and a
  175. // new message object will be created using it.
  176. virtual T* New(const Value* value) const = 0;
  177. // Convert an object received by this accessor to an object that will be
  178. // stored in the underlying RepeatedPtrField.
  179. virtual void ConvertToT(const Value* value, T* result) const = 0;
  180. // Convert an object stored in RepeatedPtrField to an object that will be
  181. // returned by this accessor. If the two objects have the same type (true
  182. // for string fields with ctype=STRING), a pointer to the source object can
  183. // be returned directly. Otherwise, data should be copied from value to
  184. // scratch_space and scratch_space should be returned.
  185. virtual const Value* ConvertFromT(const T& value,
  186. Value* scratch_space) const = 0;
  187. };
  188. // An implementation of RandomAccessRepeatedFieldAccessor that manipulates
  189. // MapFieldBase.
  190. class MapFieldAccessor : public RandomAccessRepeatedFieldAccessor {
  191. public:
  192. MapFieldAccessor() {}
  193. virtual ~MapFieldAccessor() {}
  194. virtual bool IsEmpty(const Field* data) const {
  195. return GetRepeatedField(data)->empty();
  196. }
  197. virtual int Size(const Field* data) const {
  198. return GetRepeatedField(data)->size();
  199. }
  200. virtual const Value* Get(const Field* data, int index,
  201. Value* scratch_space) const {
  202. return ConvertFromEntry(GetRepeatedField(data)->Get(index), scratch_space);
  203. }
  204. virtual void Clear(Field* data) const {
  205. MutableRepeatedField(data)->Clear();
  206. }
  207. virtual void Set(Field* data, int index, const Value* value) const {
  208. ConvertToEntry(value, MutableRepeatedField(data)->Mutable(index));
  209. }
  210. virtual void Add(Field* data, const Value* value) const {
  211. Message* allocated = New(value);
  212. ConvertToEntry(value, allocated);
  213. MutableRepeatedField(data)->AddAllocated(allocated);
  214. }
  215. virtual void RemoveLast(Field* data) const {
  216. MutableRepeatedField(data)->RemoveLast();
  217. }
  218. virtual void SwapElements(Field* data, int index1, int index2) const {
  219. MutableRepeatedField(data)->SwapElements(index1, index2);
  220. }
  221. virtual void Swap(
  222. Field* data,
  223. const internal::RepeatedFieldAccessor* other_mutator,
  224. Field* other_data) const {
  225. GOOGLE_CHECK(this == other_mutator);
  226. MutableRepeatedField(data)->Swap(MutableRepeatedField(other_data));
  227. }
  228. protected:
  229. typedef RepeatedPtrField<Message> RepeatedFieldType;
  230. static const RepeatedFieldType* GetRepeatedField(const Field* data) {
  231. return reinterpret_cast<const RepeatedFieldType*>(
  232. (&reinterpret_cast<const MapFieldBase*>(data)->GetRepeatedField()));
  233. }
  234. static RepeatedFieldType* MutableRepeatedField(Field* data) {
  235. return reinterpret_cast<RepeatedFieldType*>(
  236. reinterpret_cast<MapFieldBase*>(data)->MutableRepeatedField());
  237. }
  238. virtual Message* New(const Value* value) const {
  239. return static_cast<const Message*>(value)->New();
  240. }
  241. // Convert an object received by this accessor to an MapEntry message to be
  242. // stored in the underlying MapFieldBase.
  243. virtual void ConvertToEntry(const Value* value, Message* result) const {
  244. result->CopyFrom(*static_cast<const Message*>(value));
  245. }
  246. // Convert a MapEntry message stored in the underlying MapFieldBase to an
  247. // object that will be returned by this accessor.
  248. virtual const Value* ConvertFromEntry(const Message& value,
  249. Value* scratch_space) const {
  250. return static_cast<const Value*>(&value);
  251. }
  252. };
  253. // Default implementations of RepeatedFieldAccessor for primitive types.
  254. template<typename T>
  255. class RepeatedFieldPrimitiveAccessor : public RepeatedFieldWrapper<T> {
  256. typedef void Field;
  257. typedef void Value;
  258. using RepeatedFieldWrapper<T>::MutableRepeatedField;
  259. public:
  260. RepeatedFieldPrimitiveAccessor() {}
  261. virtual ~RepeatedFieldPrimitiveAccessor() {}
  262. virtual void Swap(
  263. Field* data,
  264. const internal::RepeatedFieldAccessor* other_mutator,
  265. Field* other_data) const {
  266. // Currently RepeatedFieldPrimitiveAccessor is the only implementation of
  267. // RepeatedFieldAccessor for primitive types. As we are using singletons
  268. // for these accessors, here "other_mutator" must be "this".
  269. GOOGLE_CHECK(this == other_mutator);
  270. MutableRepeatedField(data)->Swap(MutableRepeatedField(other_data));
  271. }
  272. protected:
  273. virtual T ConvertToT(const Value* value) const {
  274. return *static_cast<const T*>(value);
  275. }
  276. virtual const Value* ConvertFromT(const T& value,
  277. Value* scratch_space) const {
  278. return static_cast<const Value*>(&value);
  279. }
  280. };
  281. // Default implementation of RepeatedFieldAccessor for string fields with
  282. // ctype=STRING.
  283. class RepeatedPtrFieldStringAccessor : public RepeatedPtrFieldWrapper<string> {
  284. typedef void Field;
  285. typedef void Value;
  286. using RepeatedFieldAccessor::Add;
  287. public:
  288. RepeatedPtrFieldStringAccessor() {}
  289. virtual ~RepeatedPtrFieldStringAccessor() {}
  290. virtual void Swap(
  291. Field* data,
  292. const internal::RepeatedFieldAccessor* other_mutator,
  293. Field* other_data) const {
  294. if (this == other_mutator) {
  295. MutableRepeatedField(data)->Swap(MutableRepeatedField(other_data));
  296. } else {
  297. RepeatedPtrField<string> tmp;
  298. tmp.Swap(MutableRepeatedField(data));
  299. int other_size = other_mutator->Size(other_data);
  300. for (int i = 0; i < other_size; ++i) {
  301. Add<string>(data, other_mutator->Get<string>(other_data, i));
  302. }
  303. int size = Size(data);
  304. other_mutator->Clear(other_data);
  305. for (int i = 0; i < size; ++i) {
  306. other_mutator->Add<string>(other_data, tmp.Get(i));
  307. }
  308. }
  309. }
  310. protected:
  311. virtual string* New(const Value*) const {
  312. return new string();
  313. }
  314. virtual void ConvertToT(const Value* value, string* result) const {
  315. *result = *static_cast<const string*>(value);
  316. }
  317. virtual const Value* ConvertFromT(const string& value,
  318. Value* scratch_space) const {
  319. return static_cast<const Value*>(&value);
  320. }
  321. };
  322. class RepeatedPtrFieldMessageAccessor
  323. : public RepeatedPtrFieldWrapper<Message> {
  324. typedef void Field;
  325. typedef void Value;
  326. public:
  327. RepeatedPtrFieldMessageAccessor() {}
  328. virtual ~RepeatedPtrFieldMessageAccessor() {}
  329. virtual void Swap(
  330. Field* data,
  331. const internal::RepeatedFieldAccessor* other_mutator,
  332. Field* other_data) const {
  333. GOOGLE_CHECK(this == other_mutator);
  334. MutableRepeatedField(data)->Swap(MutableRepeatedField(other_data));
  335. }
  336. protected:
  337. virtual Message* New(const Value* value) const {
  338. return static_cast<const Message*>(value)->New();
  339. }
  340. virtual void ConvertToT(const Value* value, Message* result) const {
  341. result->CopyFrom(*static_cast<const Message*>(value));
  342. }
  343. virtual const Value* ConvertFromT(const Message& value,
  344. Value* scratch_space) const {
  345. return static_cast<const Value*>(&value);
  346. }
  347. };
  348. } // namespace internal
  349. } // namespace protobuf
  350. } // namespace google
  351. #endif // GOOGLE_PROTOBUF_REFLECTION_INTERNAL_H__