aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/skiff/skiff_schema.h
blob: 8952a84bacd0549dc51e8d48e700178a864bd942 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#pragma once

#include "public.h"

#include <util/generic/string.h>
#include <util/string/cast.h>

#include <vector>

namespace NSkiff {

////////////////////////////////////////////////////////////////////////////////

template <EWireType WireType>
class TComplexSchema;

using TTupleSchema = TComplexSchema<EWireType::Tuple>;
using TVariant8Schema = TComplexSchema<EWireType::Variant8>;
using TVariant16Schema = TComplexSchema<EWireType::Variant16>;
using TRepeatedVariant8Schema = TComplexSchema<EWireType::RepeatedVariant8>;
using TRepeatedVariant16Schema = TComplexSchema<EWireType::RepeatedVariant16>;

using TTupleSchemaPtr = std::shared_ptr<TTupleSchema>;
using TVariant8SchemaPtr = std::shared_ptr<TVariant8Schema>;
using TVariant16SchemaPtr = std::shared_ptr<TVariant16Schema>;
using TRepeatedVariant8SchemaPtr = std::shared_ptr<TRepeatedVariant8Schema>;
using TRepeatedVariant16SchemaPtr = std::shared_ptr<TRepeatedVariant16Schema>;



////////////////////////////////////////////////////////////////////////////////

class TSkiffSchema
    : public std::enable_shared_from_this<TSkiffSchema>
{
public:
    virtual ~TSkiffSchema() = default;

    EWireType GetWireType() const;
    std::shared_ptr<TSkiffSchema> SetName(TString name);
    const TString& GetName() const;

    virtual const TSkiffSchemaList& GetChildren() const;

protected:
    explicit TSkiffSchema(EWireType type);

private:
    const EWireType Type_;
    TString Name_;
};

bool operator==(const TSkiffSchema& lhs, const TSkiffSchema& rhs);
bool operator!=(const TSkiffSchema& lhs, const TSkiffSchema& rhs);

////////////////////////////////////////////////////////////////////////////////

class TSimpleTypeSchema
    : public TSkiffSchema
{
public:
    explicit TSimpleTypeSchema(EWireType type);
};

////////////////////////////////////////////////////////////////////////////////

template <EWireType WireType>
class TComplexSchema
    : public TSkiffSchema
{
public:
    explicit TComplexSchema(TSkiffSchemaList elements);

    virtual const TSkiffSchemaList& GetChildren() const override;

private:
    const TSkiffSchemaList Elements_;
};

////////////////////////////////////////////////////////////////////////////////

bool IsSimpleType(EWireType type);
TString GetShortDebugString(const std::shared_ptr<const TSkiffSchema>& schema);
void PrintShortDebugString(const std::shared_ptr<const TSkiffSchema>& schema, IOutputStream* out);

std::shared_ptr<TSimpleTypeSchema> CreateSimpleTypeSchema(EWireType type);
std::shared_ptr<TTupleSchema> CreateTupleSchema(TSkiffSchemaList children);
std::shared_ptr<TVariant8Schema> CreateVariant8Schema(TSkiffSchemaList children);
std::shared_ptr<TVariant16Schema> CreateVariant16Schema(TSkiffSchemaList children);
std::shared_ptr<TRepeatedVariant8Schema> CreateRepeatedVariant8Schema(TSkiffSchemaList children);
std::shared_ptr<TRepeatedVariant16Schema> CreateRepeatedVariant16Schema(TSkiffSchemaList children);

////////////////////////////////////////////////////////////////////////////////

struct TSkiffSchemaPtrHasher
{
    size_t operator()(const std::shared_ptr<TSkiffSchema>& schema) const;
};

struct TSkiffSchemaPtrEqual
{
    size_t operator()(
        const std::shared_ptr<TSkiffSchema>& lhs,
        const std::shared_ptr<TSkiffSchema>& rhs) const;
};

} // namespace NSkiff

////////////////////////////////////////////////////////////////////////////////

template <>
struct THash<NSkiff::TSkiffSchema>
{
    size_t operator()(const NSkiff::TSkiffSchema& schema) const;
};

////////////////////////////////////////////////////////////////////////////////

#define SKIFF_SCHEMA_H
#include "skiff_schema-inl.h"
#undef SKIFF_SCHEMA_H