skiff_schema.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #pragma once
  2. #include "public.h"
  3. #include <util/generic/string.h>
  4. #include <util/string/cast.h>
  5. #include <vector>
  6. namespace NSkiff {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. template <EWireType WireType>
  9. class TComplexSchema;
  10. using TTupleSchema = TComplexSchema<EWireType::Tuple>;
  11. using TVariant8Schema = TComplexSchema<EWireType::Variant8>;
  12. using TVariant16Schema = TComplexSchema<EWireType::Variant16>;
  13. using TRepeatedVariant8Schema = TComplexSchema<EWireType::RepeatedVariant8>;
  14. using TRepeatedVariant16Schema = TComplexSchema<EWireType::RepeatedVariant16>;
  15. using TTupleSchemaPtr = std::shared_ptr<TTupleSchema>;
  16. using TVariant8SchemaPtr = std::shared_ptr<TVariant8Schema>;
  17. using TVariant16SchemaPtr = std::shared_ptr<TVariant16Schema>;
  18. using TRepeatedVariant8SchemaPtr = std::shared_ptr<TRepeatedVariant8Schema>;
  19. using TRepeatedVariant16SchemaPtr = std::shared_ptr<TRepeatedVariant16Schema>;
  20. ////////////////////////////////////////////////////////////////////////////////
  21. class TSkiffSchema
  22. : public std::enable_shared_from_this<TSkiffSchema>
  23. {
  24. public:
  25. virtual ~TSkiffSchema() = default;
  26. EWireType GetWireType() const;
  27. std::shared_ptr<TSkiffSchema> SetName(TString name);
  28. const TString& GetName() const;
  29. virtual const TSkiffSchemaList& GetChildren() const;
  30. protected:
  31. explicit TSkiffSchema(EWireType type);
  32. private:
  33. const EWireType Type_;
  34. TString Name_;
  35. };
  36. bool operator==(const TSkiffSchema& lhs, const TSkiffSchema& rhs);
  37. bool operator!=(const TSkiffSchema& lhs, const TSkiffSchema& rhs);
  38. ////////////////////////////////////////////////////////////////////////////////
  39. class TSimpleTypeSchema
  40. : public TSkiffSchema
  41. {
  42. public:
  43. explicit TSimpleTypeSchema(EWireType type);
  44. };
  45. ////////////////////////////////////////////////////////////////////////////////
  46. template <EWireType WireType>
  47. class TComplexSchema
  48. : public TSkiffSchema
  49. {
  50. public:
  51. explicit TComplexSchema(TSkiffSchemaList elements);
  52. virtual const TSkiffSchemaList& GetChildren() const override;
  53. private:
  54. const TSkiffSchemaList Elements_;
  55. };
  56. ////////////////////////////////////////////////////////////////////////////////
  57. bool IsSimpleType(EWireType type);
  58. TString GetShortDebugString(const std::shared_ptr<const TSkiffSchema>& schema);
  59. void PrintShortDebugString(const std::shared_ptr<const TSkiffSchema>& schema, IOutputStream* out);
  60. std::shared_ptr<TSimpleTypeSchema> CreateSimpleTypeSchema(EWireType type);
  61. std::shared_ptr<TTupleSchema> CreateTupleSchema(TSkiffSchemaList children);
  62. std::shared_ptr<TVariant8Schema> CreateVariant8Schema(TSkiffSchemaList children);
  63. std::shared_ptr<TVariant16Schema> CreateVariant16Schema(TSkiffSchemaList children);
  64. std::shared_ptr<TRepeatedVariant8Schema> CreateRepeatedVariant8Schema(TSkiffSchemaList children);
  65. std::shared_ptr<TRepeatedVariant16Schema> CreateRepeatedVariant16Schema(TSkiffSchemaList children);
  66. ////////////////////////////////////////////////////////////////////////////////
  67. struct TSkiffSchemaPtrHasher
  68. {
  69. size_t operator()(const std::shared_ptr<TSkiffSchema>& schema) const;
  70. };
  71. struct TSkiffSchemaPtrEqual
  72. {
  73. size_t operator()(
  74. const std::shared_ptr<TSkiffSchema>& lhs,
  75. const std::shared_ptr<TSkiffSchema>& rhs) const;
  76. };
  77. } // namespace NSkiff
  78. ////////////////////////////////////////////////////////////////////////////////
  79. template <>
  80. struct THash<NSkiff::TSkiffSchema>
  81. {
  82. size_t operator()(const NSkiff::TSkiffSchema& schema) const;
  83. };
  84. ////////////////////////////////////////////////////////////////////////////////
  85. #define SKIFF_SCHEMA_H
  86. #include "skiff_schema-inl.h"
  87. #undef SKIFF_SCHEMA_H