diff options
author | serxa <serxa@yandex-team.ru> | 2022-02-10 16:49:08 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:08 +0300 |
commit | e5d4696304c6689379ac7ce334512404d4b7836c (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/lwtrace/signature.h | |
parent | d6d7db348c2cc64e71243cab9940ee6778f4317d (diff) | |
download | ydb-e5d4696304c6689379ac7ce334512404d4b7836c.tar.gz |
Restoring authorship annotation for <serxa@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/lwtrace/signature.h')
-rw-r--r-- | library/cpp/lwtrace/signature.h | 1044 |
1 files changed, 522 insertions, 522 deletions
diff --git a/library/cpp/lwtrace/signature.h b/library/cpp/lwtrace/signature.h index 87ce835228..868bd9bcf2 100644 --- a/library/cpp/lwtrace/signature.h +++ b/library/cpp/lwtrace/signature.h @@ -1,16 +1,16 @@ -#pragma once - +#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/generic/typetraits.h> #include <util/string/builder.h> -#include <util/string/cast.h> -#include <util/string/printf.h> - +#include <util/string/cast.h> +#include <util/string/printf.h> + #include <google/protobuf/descriptor.h> #include <google/protobuf/generated_enum_reflection.h> @@ -18,407 +18,407 @@ #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 { +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() { + } + + 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]; - }; - + } + + 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); + // 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); - } - + + // 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); @@ -429,42 +429,42 @@ namespace NLWTrace { 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> + 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. @@ -473,7 +473,7 @@ namespace NLWTrace { using TStoreType = typename TParamTraits<std::underlying_type_t<TEnum>>::TStoreType; using TFuncParam = TEnum; - inline static void ToString(typename TTypeTraits<TStoreType>::TFuncParam stored, TString* out) { + 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) { @@ -484,13 +484,13 @@ namespace NLWTrace { } 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 @@ -522,48 +522,48 @@ namespace NLWTrace { // Standard stored types traits. -#define STORE_TYPE_AS(input_t, store_as_t) \ - template <> \ - struct TParamTraits<input_t> { \ +#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); \ - } \ + *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); + }; \ + /**/ + 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) + FOREACH_PARAMTYPE(FOREACH_PARAMTYPE_MACRO) #undef STORE_TYPE_AS -#undef FOREACH_PARAMTYPE_MACRO - +#undef FOREACH_PARAMTYPE_MACRO + // Nil type staits. - template <> - struct TParamTraits<TNil> { + 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) { \ @@ -676,35 +676,35 @@ namespace NLWTrace { 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 - } + // 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) @@ -741,32 +741,32 @@ namespace NLWTrace { #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 - -} + }; + + // 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 + +} |