param_traits.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #pragma once
  2. #include "signature.h"
  3. #include <util/datetime/base.h>
  4. #include <util/string/builder.h>
  5. #include <limits>
  6. #ifndef LWTRACE_DISABLE
  7. namespace NLWTrace {
  8. template <>
  9. struct TParamTraits<TInstant> {
  10. using TStoreType = double;
  11. using TFuncParam = TInstant;
  12. inline static void ToString(TStoreType value, TString* out) {
  13. *out = TParamConv<TStoreType>::ToString(value);
  14. }
  15. inline static TStoreType ToStoreType(TInstant value) {
  16. if (value == TInstant::Max()) {
  17. return std::numeric_limits<TStoreType>::infinity();
  18. } else {
  19. return static_cast<TStoreType>(value.MicroSeconds()) / 1000000.0; // seconds count
  20. }
  21. }
  22. };
  23. template <>
  24. struct TParamTraits<TDuration> {
  25. using TStoreType = double;
  26. using TFuncParam = TDuration;
  27. inline static void ToString(TStoreType value, TString* out) {
  28. *out = TParamConv<TStoreType>::ToString(value);
  29. }
  30. inline static TStoreType ToStoreType(TDuration value) {
  31. if (value == TDuration::Max()) {
  32. return std::numeric_limits<TStoreType>::infinity();
  33. } else {
  34. return static_cast<TStoreType>(value.MicroSeconds()) / 1000.0; // milliseconds count
  35. }
  36. }
  37. };
  38. // Param for enum with GENERATE_ENUM_SERIALIZATION enabled or operator<< implemented
  39. template <class TEnum>
  40. struct TEnumParamWithSerialization {
  41. using TStoreType = typename TParamTraits<std::underlying_type_t<TEnum>>::TStoreType;
  42. using TFuncParam = TEnum;
  43. inline static void ToString(TStoreType stored, TString* out) {
  44. *out = TStringBuilder() << static_cast<TEnum>(stored) << " (" << stored << ")";
  45. }
  46. inline static TStoreType ToStoreType(TFuncParam v) {
  47. return static_cast<TStoreType>(v);
  48. }
  49. };
  50. } // namespace NLWTrace
  51. #endif // LWTRACE_DISABLE