aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/lwtrace/param_traits.h
blob: a0faeb50dbb261e98fbf2924ce21adbc9b987ca4 (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
#pragma once
#include "signature.h"

#include <util/datetime/base.h>
#include <util/string/builder.h>

#include <limits>

#ifndef LWTRACE_DISABLE

namespace NLWTrace {

    template <>
    struct TParamTraits<TInstant> {
        using TStoreType = double;
        using TFuncParam = TInstant;

        inline static void ToString(TStoreType value, TString* out) {
            *out = TParamConv<TStoreType>::ToString(value);
        }

        inline static TStoreType ToStoreType(TInstant value) {
            if (value == TInstant::Max()) {
                return std::numeric_limits<TStoreType>::infinity();
            } else {
                return static_cast<TStoreType>(value.MicroSeconds()) / 1000000.0; // seconds count
            }
        }
    };

    template <>
    struct TParamTraits<TDuration> {
        using TStoreType = double;
        using TFuncParam = TDuration;

        inline static void ToString(TStoreType value, TString* out) {
            *out = TParamConv<TStoreType>::ToString(value);
        }

        inline static TStoreType ToStoreType(TDuration value) {
            if (value == TDuration::Max()) {
                return std::numeric_limits<TStoreType>::infinity();
            } else {
                return static_cast<TStoreType>(value.MicroSeconds()) / 1000.0; // milliseconds count
            }
        }
    };

    // Param for enum with GENERATE_ENUM_SERIALIZATION enabled or operator<< implemented
    template <class TEnum>
    struct TEnumParamWithSerialization {
        using TStoreType = typename TParamTraits<std::underlying_type_t<TEnum>>::TStoreType;
        using TFuncParam = TEnum;

        inline static void ToString(TStoreType stored, TString* out) {
            *out = TStringBuilder() << static_cast<TEnum>(stored) << " (" << stored << ")";
        }

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

} // namespace NLWTrace

#endif // LWTRACE_DISABLE