simple_reflection.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #pragma once
  2. #include "cast.h"
  3. #include "path.h"
  4. #include "traits.h"
  5. #include <google/protobuf/descriptor.h>
  6. #include <google/protobuf/message.h>
  7. #include <util/generic/maybe.h>
  8. #include <util/generic/typetraits.h>
  9. #include <util/generic/vector.h>
  10. #include <util/system/defaults.h>
  11. namespace NProtoBuf {
  12. class TConstField {
  13. public:
  14. TConstField(const Message& msg, const FieldDescriptor* fd)
  15. : Msg(msg)
  16. , Fd(fd)
  17. {
  18. Y_ASSERT(Fd && Fd->containing_type() == Msg.GetDescriptor());
  19. }
  20. static TMaybe<TConstField> ByPath(const Message& msg, const TStringBuf& path);
  21. static TMaybe<TConstField> ByPath(const Message& msg, const TVector<const FieldDescriptor*>& fieldsPath);
  22. static TMaybe<TConstField> ByPath(const Message& msg, const TFieldPath& fieldsPath);
  23. const Message& Parent() const {
  24. return Msg;
  25. }
  26. const FieldDescriptor* Field() const {
  27. return Fd;
  28. }
  29. bool HasValue() const {
  30. return IsRepeated() ? Refl().FieldSize(Msg, Fd) > 0
  31. : Refl().HasField(Msg, Fd);
  32. }
  33. // deprecated, use HasValue() instead
  34. bool Has() const {
  35. return HasValue();
  36. }
  37. size_t Size() const {
  38. return IsRepeated() ? Refl().FieldSize(Msg, Fd)
  39. : (Refl().HasField(Msg, Fd) ? 1 : 0);
  40. }
  41. template <typename T>
  42. inline typename TSelectCppType<T>::T Get(size_t index = 0) const;
  43. template <typename TMsg>
  44. inline const TMsg* GetAs(size_t index = 0) const {
  45. // casting version of Get
  46. return IsMessageInstance<TMsg>() ? CheckedCast<const TMsg*>(Get<const Message*>(index)) : nullptr;
  47. }
  48. template <typename T>
  49. bool IsInstance() const {
  50. return CppType() == TSelectCppType<T>::Result;
  51. }
  52. template <typename TMsg>
  53. bool IsMessageInstance() const {
  54. return IsMessage() && Fd->message_type() == TMsg::descriptor();
  55. }
  56. template <typename TMsg>
  57. bool IsInstance(std::enable_if_t<std::is_base_of<Message, TMsg>::value && !std::is_same<Message, TMsg>::value, void>* = NULL) const { // template will be selected when specifying Message children types
  58. return IsMessage() && Fd->message_type() == TMsg::descriptor();
  59. }
  60. bool IsString() const {
  61. return CppType() == FieldDescriptor::CPPTYPE_STRING;
  62. }
  63. bool IsMessage() const {
  64. return CppType() == FieldDescriptor::CPPTYPE_MESSAGE;
  65. }
  66. bool HasSameType(const TConstField& other) const {
  67. if (CppType() != other.CppType())
  68. return false;
  69. if (IsMessage() && Field()->message_type() != other.Field()->message_type())
  70. return false;
  71. if (CppType() == FieldDescriptor::CPPTYPE_ENUM && Field()->enum_type() != other.Field()->enum_type())
  72. return false;
  73. return true;
  74. }
  75. protected:
  76. bool IsRepeated() const {
  77. return Fd->is_repeated();
  78. }
  79. FieldDescriptor::CppType CppType() const {
  80. return Fd->cpp_type();
  81. }
  82. const Reflection& Refl() const {
  83. return *Msg.GetReflection();
  84. }
  85. [[noreturn]] void RaiseUnknown() const {
  86. ythrow yexception() << "Unknown field cpp-type: " << (size_t)CppType();
  87. }
  88. bool IsSameField(const TConstField& other) const {
  89. return &Parent() == &other.Parent() && Field() == other.Field();
  90. }
  91. protected:
  92. const Message& Msg;
  93. const FieldDescriptor* Fd;
  94. };
  95. class TMutableField: public TConstField {
  96. public:
  97. TMutableField(Message& msg, const FieldDescriptor* fd)
  98. : TConstField(msg, fd)
  99. {
  100. }
  101. static TMaybe<TMutableField> ByPath(Message& msg, const TStringBuf& path, bool createPath = false);
  102. static TMaybe<TMutableField> ByPath(Message& msg, const TVector<const FieldDescriptor*>& fieldsPath, bool createPath = false);
  103. static TMaybe<TMutableField> ByPath(Message& msg, const TFieldPath& fieldsPath, bool createPath = false);
  104. Message* MutableParent() {
  105. return Mut();
  106. }
  107. template <typename T>
  108. inline void Set(T value, size_t index = 0);
  109. template <typename T>
  110. inline void Add(T value);
  111. inline void MergeFrom(const TConstField& src);
  112. inline void Clear() {
  113. Refl().ClearField(Mut(), Fd);
  114. }
  115. /*
  116. void Swap(TMutableField& f) {
  117. Y_ASSERT(Field() == f.Field());
  118. // not implemented yet, TODO: implement when Reflection::Mutable(Ptr)RepeatedField
  119. // is ported into arcadia protobuf library from up-stream.
  120. }
  121. */
  122. inline void RemoveLast() {
  123. Y_ASSERT(HasValue());
  124. if (IsRepeated())
  125. Refl().RemoveLast(Mut(), Fd);
  126. else
  127. Clear();
  128. }
  129. inline void SwapElements(size_t index1, size_t index2) {
  130. Y_ASSERT(IsRepeated());
  131. Y_ASSERT(index1 < Size());
  132. Y_ASSERT(index2 < Size());
  133. if (index1 == index2)
  134. return;
  135. Refl().SwapElements(Mut(), Fd, index1, index2);
  136. }
  137. inline void Remove(size_t index) {
  138. if (index >= Size())
  139. return;
  140. // Move to the end
  141. for (size_t i = index, size = Size(); i < size - 1; ++i)
  142. SwapElements(i, i + 1);
  143. RemoveLast();
  144. }
  145. Message* MutableMessage(size_t index = 0) {
  146. Y_ASSERT(IsMessage());
  147. if (IsRepeated()) {
  148. Y_ASSERT(index < Size());
  149. return Refl().MutableRepeatedMessage(Mut(), Fd, index);
  150. } else {
  151. Y_ASSERT(index == 0);
  152. return Refl().MutableMessage(Mut(), Fd);
  153. }
  154. }
  155. template <typename TMsg>
  156. inline TMsg* AddMessage() {
  157. return CheckedCast<TMsg*>(AddMessage());
  158. }
  159. inline Message* AddMessage() {
  160. Y_ASSERT(IsMessage() && IsRepeated());
  161. return Refl().AddMessage(Mut(), Fd);
  162. }
  163. private:
  164. Message* Mut() {
  165. return const_cast<Message*>(&Msg);
  166. }
  167. template <typename T>
  168. inline void MergeValue(T srcValue);
  169. };
  170. // template implementations
  171. template <typename T>
  172. inline typename TSelectCppType<T>::T TConstField::Get(size_t index) const {
  173. Y_ASSERT(index < Size() || !Fd->is_repeated() && index == 0); // Get for single fields is always allowed because of default values
  174. #define TMP_MACRO_FOR_CPPTYPE(CPPTYPE) \
  175. case CPPTYPE: \
  176. return CompatCast<CPPTYPE, TSelectCppType<T>::Result>(TSimpleFieldTraits<CPPTYPE>::Get(Msg, Fd, index));
  177. switch (CppType()) {
  178. APPLY_TMP_MACRO_FOR_ALL_CPPTYPES()
  179. default:
  180. RaiseUnknown();
  181. }
  182. #undef TMP_MACRO_FOR_CPPTYPE
  183. }
  184. template <typename T>
  185. inline void TMutableField::Set(T value, size_t index) {
  186. Y_ASSERT(!IsRepeated() && index == 0 || index < Size());
  187. #define TMP_MACRO_FOR_CPPTYPE(CPPTYPE) \
  188. case CPPTYPE: \
  189. TSimpleFieldTraits<CPPTYPE>::Set(*Mut(), Fd, CompatCast<TSelectCppType<T>::Result, CPPTYPE>(value), index); \
  190. break;
  191. switch (CppType()) {
  192. APPLY_TMP_MACRO_FOR_ALL_CPPTYPES()
  193. default:
  194. RaiseUnknown();
  195. }
  196. #undef TMP_MACRO_FOR_CPPTYPE
  197. }
  198. template <typename T>
  199. inline void TMutableField::Add(T value) {
  200. #define TMP_MACRO_FOR_CPPTYPE(CPPTYPE) \
  201. case CPPTYPE: \
  202. TSimpleFieldTraits<CPPTYPE>::Add(*Mut(), Fd, CompatCast<TSelectCppType<T>::Result, CPPTYPE>(value)); \
  203. break;
  204. switch (CppType()) {
  205. APPLY_TMP_MACRO_FOR_ALL_CPPTYPES()
  206. default:
  207. RaiseUnknown();
  208. }
  209. #undef TMP_MACRO_FOR_CPPTYPE
  210. }
  211. template <typename T>
  212. inline void TMutableField::MergeValue(T srcValue) {
  213. Add(srcValue);
  214. }
  215. template <>
  216. inline void TMutableField::MergeValue<const Message*>(const Message* srcValue) {
  217. if (IsRepeated()) {
  218. Add(srcValue);
  219. } else {
  220. MutableMessage()->MergeFrom(*srcValue);
  221. }
  222. }
  223. inline void TMutableField::MergeFrom(const TConstField& src) {
  224. Y_ASSERT(HasSameType(src));
  225. if (IsSameField(src))
  226. return;
  227. #define TMP_MACRO_FOR_CPPTYPE(CPPTYPE) \
  228. case CPPTYPE: { \
  229. for (size_t itemIdx = 0; itemIdx < src.Size(); ++itemIdx) { \
  230. MergeValue(TSimpleFieldTraits<CPPTYPE>::Get(src.Parent(), src.Field(), itemIdx)); \
  231. } \
  232. break; \
  233. }
  234. switch (CppType()) {
  235. APPLY_TMP_MACRO_FOR_ALL_CPPTYPES()
  236. default:
  237. RaiseUnknown();
  238. }
  239. #undef TMP_MACRO_FOR_CPPTYPE
  240. }
  241. }