aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/lwtrace/signature.h
blob: f710d67ec76a07abe420e8611a1c6aa6f908cb82 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
#pragma once

#include "preprocessor.h"

#include <library/cpp/lwtrace/protos/lwtrace.pb.h> 

#include <util/generic/cast.h>
#include <util/generic/string.h>
#include <util/generic/typetraits.h>
#include <util/string/builder.h>
#include <util/string/cast.h>
#include <util/string/printf.h>

#include <google/protobuf/descriptor.h>
#include <google/protobuf/generated_enum_reflection.h>

#include <library/cpp/lwtrace/protos/lwtrace.pb.h>

#include <type_traits>

namespace NLWTrace {
    // Class to hold parameter values parsed from trace query predicate operators
    template <class T>
    struct TParamConv {
        static T FromString(const TString& text) {
            return ::FromString<T>(text);
        }
        static TString ToString(const T& param) {
            return ::ToString(param);
        }
    };

    template <>
    struct TParamConv<TString const*> {
        static TString FromString(const TString& text) {
            return text;
        }
        static TString ToString(TString const* param) {
            return TString(*param);
        }
    };

    template <>
    struct TParamConv<ui8> {
        static ui8 FromString(const TString& text) {
            return (ui8)::FromString<ui16>(text);
        }
        static TString ToString(ui8 param) {
            return ::ToString((ui16)param);
        }
    };

    template <>
    struct TParamConv<i8> {
        static i8 FromString(const TString& text) {
            return (i8)::FromString<i16>(text);
        }
        static TString ToString(i8 param) {
            return ::ToString((i16)param);
        }
    };

    template <>
    struct TParamConv<double> {
        static double FromString(const TString& text) {
            return ::FromString<double>(text);
        }
        static TString ToString(double param) {
            return Sprintf("%.6lf", param);
        }
    };

    // Fake parameter type used as a placeholder for not used parameters (above the number of defined params for a specific probe)
    class TNil {
    };

    // Struct that holds and handles a value of parameter of any supported type.
    struct TParam {
        char Data[LWTRACE_MAX_PARAM_SIZE];

        template <class T>
        const T& Get() const {
            return *reinterpret_cast<const T*>(Data);
        }

        template <class T>
        T& Get() {
            return *reinterpret_cast<T*>(Data);
        }

        template <class T>
        void DefaultConstruct() {
            new (&Data) T();
        }

        template <class T>
        void CopyConstruct(const T& other) {
            new (&Data) T(other);
        }

        template <class T>
        void Destruct() {
            Get<T>().~T();
        }
    };

    template <>
    inline void TParam::DefaultConstruct<TNil>() {
    }

    template <>
    inline void TParam::CopyConstruct<TNil>(const TNil&) {
    }

    template <>
    inline void TParam::Destruct<TNil>() {
    }

    class TTypedParam {
    private:
        EParamTypePb Type;
        TParam Param; // Contains garbage if PT_UNKNOWN
    public:
        TTypedParam()
            : Type(PT_UNKNOWN)
        {
        }

        explicit TTypedParam(EParamTypePb type)
            : Type(type)
        {
            switch (Type) {
#define FOREACH_PARAMTYPE_MACRO(n, t, v) \
    case PT_##v:                         \
        Param.DefaultConstruct<t>();     \
        return;
                FOREACH_PARAMTYPE(FOREACH_PARAMTYPE_MACRO)
#undef FOREACH_PARAMTYPE_MACRO
                case PT_UNKNOWN:
                    return;
                default:
                    Y_FAIL("unknown param type");
            }
        }

        template <class T>
        explicit TTypedParam(const T& x, EParamTypePb type = PT_UNKNOWN)
            : Type(type)
        {
            Param.CopyConstruct<T>(x);
        }

#define FOREACH_PARAMTYPE_MACRO(n, t, v) \
    explicit TTypedParam(const t& x)     \
        : Type(PT_##v) {                 \
        Param.CopyConstruct<t>(x);       \
    }
        FOREACH_PARAMTYPE(FOREACH_PARAMTYPE_MACRO)
#undef FOREACH_PARAMTYPE_MACRO

        TTypedParam(const TTypedParam& o) {
            Assign(o);
        }

        TTypedParam& operator=(const TTypedParam& o) {
            Reset();
            Assign(o);
            return *this;
        }

        void Assign(const TTypedParam& o) {
            Type = o.Type;
            switch (Type) {
#define FOREACH_PARAMTYPE_MACRO(n, t, v)          \
    case PT_##v:                                  \
        Param.CopyConstruct<t>(o.Param.Get<t>()); \
        return;
                FOREACH_PARAMTYPE(FOREACH_PARAMTYPE_MACRO)
#undef FOREACH_PARAMTYPE_MACRO
                case PT_UNKNOWN:
                    return;
                default:
                    Y_FAIL("unknown param type");
            }
        }

        TTypedParam(TTypedParam&& o)
            : Type(o.Type)
            , Param(o.Param)
        {
            o.Type = PT_UNKNOWN; // To avoid Param destroy by source object dtor
        }

        TTypedParam(EParamTypePb type, const TParam& param)
            : Type(type)
        {
            Y_UNUSED(param); // for disabled lwtrace
            switch (Type) {
#define FOREACH_PARAMTYPE_MACRO(n, t, v)        \
    case PT_##v:                                \
        Param.CopyConstruct<t>(param.Get<t>()); \
        return;
                FOREACH_PARAMTYPE(FOREACH_PARAMTYPE_MACRO)
#undef FOREACH_PARAMTYPE_MACRO
                case PT_UNKNOWN:
                    return;
                default:
                    Y_FAIL("unknown param type");
            }
        }

        ~TTypedParam() {
            Reset();
        }

        void Reset() {
            switch (Type) {
#define FOREACH_PARAMTYPE_MACRO(n, t, v) \
    case PT_##v:                         \
        Param.Destruct<t>();             \
        return;
                FOREACH_PARAMTYPE(FOREACH_PARAMTYPE_MACRO)
#undef FOREACH_PARAMTYPE_MACRO
                case PT_UNKNOWN:
                    return;
                default:
                    Y_FAIL("unknown param type");
            }
            Type = PT_UNKNOWN;
        }

        bool operator==(const TTypedParam& rhs) const {
            if (Y_LIKELY(Type == rhs.Type)) {
                switch (Type) {
#define FOREACH_PARAMTYPE_MACRO(n, t, v) \
    case PT_##v:                         \
        return Param.Get<t>() == rhs.Param.Get<t>();
                    FOREACH_PARAMTYPE(FOREACH_PARAMTYPE_MACRO)
#undef FOREACH_PARAMTYPE_MACRO
                    case PT_UNKNOWN:
                        return false; // All unknowns are equal
                    default:
                        Y_FAIL("unknown param type");
                }
            } else {
                return false;
            }
        }
        bool operator!=(const TTypedParam& rhs) const {
            return !operator==(rhs);
        }

        bool operator<(const TTypedParam& rhs) const {
            if (Y_LIKELY(Type == rhs.Type)) {
                switch (Type) {
#define FOREACH_PARAMTYPE_MACRO(n, t, v) \
    case PT_##v:                         \
        return Param.Get<t>() < rhs.Param.Get<t>();
                    FOREACH_PARAMTYPE(FOREACH_PARAMTYPE_MACRO)
#undef FOREACH_PARAMTYPE_MACRO
                    case PT_UNKNOWN:
                        return false; // All unknowns are equal
                    default:
                        Y_FAIL("unknown param type");
                }
            } else {
                return Type < rhs.Type;
            }
        }

        bool operator<=(const TTypedParam& rhs) const {
            if (Y_LIKELY(Type == rhs.Type)) {
                switch (Type) {
#define FOREACH_PARAMTYPE_MACRO(n, t, v) \
    case PT_##v:                         \
        return Param.Get<t>() <= rhs.Param.Get<t>();
                    FOREACH_PARAMTYPE(FOREACH_PARAMTYPE_MACRO)
#undef FOREACH_PARAMTYPE_MACRO
                    case PT_UNKNOWN:
                        return true; // All unknowns are equal
                    default:
                        Y_FAIL("unknown param type");
                }
            } else {
                return Type < rhs.Type;
            }
        }

        bool operator>(const TTypedParam& rhs) const {
            return !operator<=(rhs);
        }
        bool operator>=(const TTypedParam& rhs) const {
            return !operator<(rhs);
        }

        EParamTypePb GetType() const {
            return Type;
        }
        const TParam& GetParam() const {
            return Param;
        }
    };

    class TLiteral {
    private:
        TTypedParam Values[EParamTypePb_ARRAYSIZE];

    public:
        explicit TLiteral(const TString& text) {
            Y_UNUSED(text); /* That's for windows, where we have lwtrace disabled. */

#define FOREACH_PARAMTYPE_MACRO(n, t, v)                               \
    try {                                                              \
        Values[PT_##v] = TTypedParam(TParamConv<t>::FromString(text)); \
    } catch (...) {                                                    \
        Values[PT_##v] = TTypedParam();                                \
    }                                                                  \
    /**/
            FOREACH_PARAMTYPE(FOREACH_PARAMTYPE_MACRO)
#undef FOREACH_PARAMTYPE_MACRO
        }

        TLiteral() {
        }

        TLiteral(const TLiteral& o) {
            for (size_t i = 0; i < EParamTypePb_ARRAYSIZE; i++) {
                Values[i] = o.Values[i];
            }
        }

        TLiteral& operator=(const TLiteral& o) {
            for (size_t i = 0; i < EParamTypePb_ARRAYSIZE; i++) {
                Values[i] = o.Values[i];
            }
            return *this;
        }

        const TTypedParam& GetValue(EParamTypePb type) const {
            return Values[type];
        }

        bool operator==(const TTypedParam& rhs) const {
            return Values[rhs.GetType()] == rhs;
        }
        bool operator!=(const TTypedParam& rhs) const {
            return !operator==(rhs);
        }

        bool operator<(const TTypedParam& rhs) const {
            return Values[rhs.GetType()] < rhs;
        }

        bool operator<=(const TTypedParam& rhs) const {
            return Values[rhs.GetType()] <= rhs;
        }

        bool operator>(const TTypedParam& rhs) const {
            return !operator<=(rhs);
        }
        bool operator>=(const TTypedParam& rhs) const {
            return !operator<(rhs);
        }
    };

    inline bool operator==(const TTypedParam& lhs, const TLiteral& rhs) {
        return lhs == rhs.GetValue(lhs.GetType());
    }
    inline bool operator!=(const TTypedParam& lhs, const TLiteral& rhs) {
        return !operator==(lhs, rhs);
    }

    inline bool operator<(const TTypedParam& lhs, const TLiteral& rhs) {
        return lhs < rhs.GetValue(lhs.GetType());
    }

    inline bool operator<=(const TTypedParam& lhs, const TLiteral& rhs) {
        return lhs <= rhs.GetValue(lhs.GetType());
    }

    inline bool operator>(const TTypedParam& lhs, const TLiteral& rhs) {
        return !operator<=(lhs, rhs);
    }
    inline bool operator>=(const TTypedParam& lhs, const TLiteral& rhs) {
        return !operator<(lhs, rhs);
    }

    // Struct that holds and handles all parameter values of different supported types
    struct TParams {
        TParam Param[LWTRACE_MAX_PARAMS];
    };

    using TSerializedParams = google::protobuf::RepeatedPtrField<NLWTrace::TTraceParam>;

    // Represents a common class for all function "signatures" (parameter types and names).
    // Provides non-virtual interface to handle the signature and (emulated) virtual interface to handle TParams corresponding to the signature
    struct TSignature {
        const char** ParamTypes;
        const char* ParamNames[LWTRACE_MAX_PARAMS + 1];
        size_t ParamCount;

        // Virtual table
        void (*SerializeParamsFunc)(const TParams& params, TString* values);
        void (*CloneParamsFunc)(TParams& newParams, const TParams& oldParams);
        void (*DestroyParamsFunc)(TParams& params);
        void (*SerializeToPbFunc)(const TParams& params, TSerializedParams& arr);
        bool (*DeserializeFromPbFunc)(TParams& params, const TSerializedParams& arr);

        // Virtual calls emulation
        void SerializeParams(const TParams& params, TString* values) const {
            (*SerializeParamsFunc)(params, values);
        }

        void CloneParams(TParams& newParams, const TParams& oldParams) const {
            (*CloneParamsFunc)(newParams, oldParams);
        }

        void DestroyParams(TParams& params) const {
            (*DestroyParamsFunc)(params);
        }

        void SerializeToPb(const TParams& params, TSerializedParams& arr) const
        {
            (*SerializeToPbFunc)(params, arr);
        }

        bool DeserializeFromPb(TParams& params, const TSerializedParams& arr) const
        {
            return (*DeserializeFromPbFunc)(params, arr);
        }

        void ToProtobuf(TEventPb& pb) const;

        size_t FindParamIndex(const TString& param) const {
            for (size_t i = 0; i < ParamCount; i++) {
                if (ParamNames[i] == param) {
                    return i;
                }
            }
            return size_t(-1);
        }
    };

#ifndef LWTRACE_DISABLE

    // Implementation. Used for compilation error if not all expected parameters passed to a function call
    struct ERROR_not_enough_parameters : TNil {};

    // Struct that holds static string with a name of parameter type
    template <class T>
    struct TParamType {
        enum { Supported = 0 };
        static const char* NameString;
    };

#define FOREACH_PARAMTYPE_MACRO(n, t, v) \
    template <>                          \
    struct TParamType<t> {               \
        enum { Supported = 1 };          \
        static const char* NameString;   \
    };                                   \
    /**/
    FOREACH_PARAMTYPE(FOREACH_PARAMTYPE_MACRO)
    FOR_NIL_PARAMTYPE(FOREACH_PARAMTYPE_MACRO)
#undef FOREACH_PARAMTYPE_MACRO

    template <class T>
    struct TParamTraits;

    // Enum types traits impl.
    template <class TEnum, class = std::enable_if_t<std::is_enum_v<TEnum>>>
    struct TEnumParamTraitsImpl {
        using TStoreType = typename TParamTraits<std::underlying_type_t<TEnum>>::TStoreType;
        using TFuncParam = TEnum;

        inline static void ToString(typename TTypeTraits<TStoreType>::TFuncParam stored, TString* out) {
            if constexpr (google::protobuf::is_proto_enum<TEnum>::value) {
                const google::protobuf::EnumValueDescriptor* valueDescriptor = google::protobuf::GetEnumDescriptor<TEnum>()->FindValueByNumber(stored);
                if (valueDescriptor) {
                    *out = TStringBuilder() << valueDescriptor->name() << " (" << stored << ")";
                } else {
                    *out = TParamConv<TStoreType>::ToString(stored);
                }
            } else {
                *out = TParamConv<TStoreType>::ToString(stored);
            }
        }

        inline static TStoreType ToStoreType(TFuncParam v) {
            return static_cast<TStoreType>(v);
        }
    };

    template <class TCustomType>
    struct TCustomTraitsImpl {
        using TStoreType = typename TParamTraits<typename TCustomType::TStoreType>::TStoreType; //see STORE_TYPE_AS
        using TFuncParam = typename TCustomType::TFuncParam;

        inline static void ToString(typename TTypeTraits<TStoreType>::TFuncParam stored, TString* out) {
            TCustomType::ToString(stored, out);
        }

        inline static TStoreType ToStoreType(TFuncParam v) {
            return TCustomType::ToStoreType(v);
        }
    };

    template <class T, bool isEnum>
    struct TParamTraitsImpl;

    template <class TEnum>
    struct TParamTraitsImpl<TEnum, true> : TEnumParamTraitsImpl<TEnum> {
    };

    template <class TCustomType>
    struct TParamTraitsImpl<TCustomType, false> : TCustomTraitsImpl<TCustomType> {
    };

    template <class T>
    struct TParamTraits : TParamTraitsImpl<T, std::is_enum_v<T>> {
    };

    // Standard stored types traits.

#define STORE_TYPE_AS(input_t, store_as_t)                                                      \
    template <>                                                                                 \
    struct TParamTraits<input_t> {                                                              \
        using TStoreType = store_as_t;                                                          \
        using TFuncParam = typename TTypeTraits<input_t>::TFuncParam;                           \
                                                                                                \
        inline static void ToString(typename TTypeTraits<TStoreType>::TFuncParam stored, TString* out) { \
            *out = TParamConv<TStoreType>::ToString(stored);                                    \
        }                                                                                       \
                                                                                                \
        inline static TStoreType ToStoreType(TFuncParam v) {                                    \
            return v;                                                                           \
        }                                                                                       \
    };                                                                                          \
    /**/
    STORE_TYPE_AS(ui8, ui64);
    STORE_TYPE_AS(i8, i64);
    STORE_TYPE_AS(ui16, ui64);
    STORE_TYPE_AS(i16, i64);
    STORE_TYPE_AS(ui32, ui64);
    STORE_TYPE_AS(i32, i64);
    STORE_TYPE_AS(bool, ui64);
    STORE_TYPE_AS(float, double);
#define FOREACH_PARAMTYPE_MACRO(n, t, v) STORE_TYPE_AS(t, t)
    FOREACH_PARAMTYPE(FOREACH_PARAMTYPE_MACRO)
#undef STORE_TYPE_AS
#undef FOREACH_PARAMTYPE_MACRO

    // Nil type staits.
    template <>
    struct TParamTraits<TNil> {
        using TStoreType = TNil;
        using TFuncParam = TTypeTraits<TNil>::TFuncParam;

        inline static void ToString(typename TTypeTraits<TNil>::TFuncParam, TString*) {
        }

        inline static TNil ToStoreType(TFuncParam v) {
            return v;
        }
    };

    inline EParamTypePb ParamTypeToProtobuf(const char* paramType) {
#define FOREACH_PARAMTYPE_MACRO(n, t, v) \
    if (strcmp(paramType, n) == 0) {     \
        return PT_##v;                   \
    }                                    \
    /**/
        FOREACH_PARAMTYPE(FOREACH_PARAMTYPE_MACRO)
#undef FOREACH_PARAMTYPE_MACRO
        return PT_UNKNOWN;
    }

    template <typename T>
    inline void SaveParamToPb(TSerializedParams& msg, const TParam& param);

    template <>
    inline void SaveParamToPb<TNil>(TSerializedParams& msg, const TParam& param)
    {
        Y_UNUSED(msg);
        Y_UNUSED(param);
    }

    template <>
    inline void SaveParamToPb<i64>(TSerializedParams& msg, const TParam& param)
    {
        msg.Add()->SetIntValue(param.Get<typename TParamTraits<i64>::TStoreType>());
    }

    template <>
    inline void SaveParamToPb<ui64>(TSerializedParams& msg, const TParam& param)
    {
        msg.Add()->SetUintValue(param.Get<typename TParamTraits<ui64>::TStoreType>());
    }

    template <>
    inline void SaveParamToPb<double>(TSerializedParams& msg, const TParam& param)
    {
        msg.Add()->SetDoubleValue(param.Get<typename TParamTraits<double>::TStoreType>());
    }

    template <>
    inline void SaveParamToPb<TString>(TSerializedParams& msg, const TParam& param)
    {
        msg.Add()->SetStrValue(param.Get<typename TParamTraits<TString>::TStoreType>());
    }

    template <>
    inline void SaveParamToPb<TSymbol>(TSerializedParams& msg, const TParam& param)
    {
        msg.Add()->SetStrValue(*param.Get<typename TParamTraits<TSymbol>::TStoreType>().Str);
    }

    template <>
    inline void SaveParamToPb<TCheck>(TSerializedParams& msg, const TParam& param)
    {
        msg.Add()->SetIntValue(param.Get<typename TParamTraits<TCheck>::TStoreType>().Value);
    }

    template <typename T>
    inline void LoadParamFromPb(const TTraceParam& msg, TParam& param);

    template <>
    inline void LoadParamFromPb<i64>(const TTraceParam& msg, TParam& param)
    {
        param.DefaultConstruct<i64>();
        param.Get<i64>() = msg.GetIntValue();
    }

    template <>
    inline void LoadParamFromPb<ui64>(const TTraceParam& msg, TParam& param)
    {
        param.DefaultConstruct<ui64>();
        param.Get<ui64>() = msg.GetUintValue();
    }

    template <>
    inline void LoadParamFromPb<double>(const TTraceParam& msg, TParam& param)
    {
        param.DefaultConstruct<double>();
        param.Get<double>() = msg.GetDoubleValue();
    }

    template <>
    inline void LoadParamFromPb<TCheck>(const TTraceParam& msg, TParam& param)
    {
        param.CopyConstruct<TCheck>(TCheck(msg.GetIntValue()));
    }

    template <>
    inline void LoadParamFromPb<TSymbol>(const TTraceParam& msg, TParam& param)
    {
        Y_UNUSED(msg);
        Y_UNUSED(param);
        static TString unsupported("unsupported");
        // so far TSymbol deserialization is not supported
        // since it is not used for probes, it is ok
        param.CopyConstruct<TSymbol>(TSymbol(&unsupported));
    }

    template <>
    inline void LoadParamFromPb<TString>(const TTraceParam& msg, TParam& param)
    {
        param.DefaultConstruct<TString>();
        param.Get<TString>() = msg.GetStrValue();
    }

    template <>
    inline void LoadParamFromPb<TNil>(const TTraceParam& msg, TParam& param)
    {
        Y_UNUSED(msg);
        Y_UNUSED(param);
    }

    // Class representing a specific signature
    template <LWTRACE_TEMPLATE_PARAMS>
    struct TUserSignature {
#define FOREACH_PARAMNUM_MACRO(i) static_assert(TParamType<typename TParamTraits<TP##i>::TStoreType>::Supported == 1, "expect TParamType< typename TParamTraits<TP ## i>::TStoreType >::Supported == 1");
        FOREACH_PARAMNUM(FOREACH_PARAMNUM_MACRO) // ERROR: unsupported type used as probe/event parameter type
#undef FOREACH_PARAMNUM_MACRO
        static const char* ParamTypes[];
        static const int ParamCount = LWTRACE_COUNT_PARAMS;

        // Implementation of virtual function (TSignature derived classes vtable emulation)
        inline static void SerializeParams(const TParams& params, TString* values) {
#define FOREACH_PARAMNUM_MACRO(i) TParamTraits<TP##i>::ToString(params.Param[i].Get<typename TParamTraits<TP##i>::TStoreType>(), values + i);
            FOREACH_PARAMNUM(FOREACH_PARAMNUM_MACRO);
#undef FOREACH_PARAMNUM_MACRO
        }

        // Implementation of virtual function (TSignature derived classes vtable emulation)
        inline static void CloneParams(TParams& newParams, const TParams& oldParams) {
#define FOREACH_PARAMNUM_MACRO(i) newParams.Param[i].CopyConstruct<typename TParamTraits<TP##i>::TStoreType>(oldParams.Param[i].Get<typename TParamTraits<TP##i>::TStoreType>());
            FOREACH_PARAMNUM(FOREACH_PARAMNUM_MACRO);
#undef FOREACH_PARAMNUM_MACRO
        }

        // Implementation of virtual function (TSignature derived classes vtable emulation)
        inline static void DestroyParams(TParams& params) {
#define FOREACH_PARAMNUM_MACRO(i) params.Param[i].Destruct<typename TParamTraits<TP##i>::TStoreType>();
            FOREACH_PARAMNUM(FOREACH_PARAMNUM_MACRO);
#undef FOREACH_PARAMNUM_MACRO
        }

        // Implementation of virtual function (TSignature derived classes vtable emulation)
        inline static void SerializeToPb(const TParams& params, TSerializedParams& arr)
        {
#define FOREACH_PARAMNUM_MACRO(i)                                              \
    SaveParamToPb<typename TParamTraits<TP##i>::TStoreType>(                   \
        arr,                                                                   \
        params.Param[i]);                                                      \
// FOREACH_PARAMNUM_MACRO
            FOREACH_PARAMNUM(FOREACH_PARAMNUM_MACRO);
#undef FOREACH_PARAMNUM_MACRO
        }

        // Implementation of virtual function (TSignature derived classes vtable emulation)
        inline static bool DeserializeFromPb(TParams& params, const TSerializedParams& arr) {
            if (arr.size() != ParamCount) {
                return false;
            }
            if (!ParamCount) {
                return true;
            }

            int paramIdx = 0;
#define FOREACH_PARAMNUM_MACRO(i)                                              \
    if (paramIdx >= arr.size()) {                                              \
        return true;                                                           \
    };                                                                         \
    LoadParamFromPb<typename TParamTraits<TP##i>::TStoreType>(                 \
        arr.Get(paramIdx),                                                     \
        params.Param[paramIdx]);                                               \
    ++paramIdx;                                                                \
//  FOREACH_PARAMNUM_MACRO
            FOREACH_PARAMNUM(FOREACH_PARAMNUM_MACRO);
#undef FOREACH_PARAMNUM_MACRO
            return true;
        }
    };

    // Array of static strings pointers for names of parameter types in a specific signature
    template <LWTRACE_TEMPLATE_PARAMS_NODEF>
    const char* TUserSignature<LWTRACE_TEMPLATE_ARGS>::ParamTypes[] = {
#define FOREACH_PARAMNUM_MACRO(i) TParamType<typename TParamTraits<TP##i>::TStoreType>::NameString,
        FOREACH_PARAMNUM(FOREACH_PARAMNUM_MACRO) nullptr
#undef FOREACH_PARAMNUM_MACRO
    };

    inline void TSignature::ToProtobuf(TEventPb& pb) const {
        for (size_t i = 0; i < ParamCount; i++) {
            pb.AddParamTypes(ParamTypeToProtobuf(ParamTypes[i]));
            pb.AddParamNames(ParamNames[i]);
        }
    }

#else

    inline void TSignature::ToProtobuf(TEventPb&) const {
    }

    inline EParamTypePb ParamTypeToProtobuf(const char*) {
        return PT_UNKNOWN;
    }

#endif

}