userver: userver/storages/clickhouse/io/floating_point_types.hpp Source File
Loading...
Searching...
No Matches
floating_point_types.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/clickhouse/io/floating_point_types.hpp
4/// @brief @copybrief storages::clickhouse::io::FloatingWithPrecision
5
6#include <concepts>
7
8#include <fmt/format.h>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace storages::clickhouse::io {
13
14/// @brief Class that can be escaped for clickhouse queries instead of [double] and [float].
15/// [double] and [float] are escaped as FloatingWithPrecision<double, 12> and
16/// FloatingWithPrecision<float, 6> respectively. These constants were derived from the accuracity the numbers from
17/// ranges [0.0-100000.0] (doubles) and [0.0-100.0] (floats) can be represented by these type. Clickhouse also supports
18/// inf, -inf and nan, see clickhouse docs.
19template <std::floating_point FloatingPointT, std::uint32_t Precision = std::is_same_v<FloatingPointT, float> ? 6 : 12>
21public:
22 template <typename U>
23 FloatingWithPrecision(U value)
24 : value_(value)
25 {}
26
27 template <typename U, std::uint32_t AnotherPrecision>
28 FloatingWithPrecision(const FloatingWithPrecision<U, AnotherPrecision>& other)
29 : value_(other.value_)
30 {}
31
32 template <typename U, std::uint32_t AnotherPrecision>
33 FloatingWithPrecision(FloatingWithPrecision<U, AnotherPrecision>&& other)
34 : value_(other.value_)
35 {}
36
37 template <typename U, std::uint32_t AnotherPrecision>
38 FloatingWithPrecision& operator=(const FloatingWithPrecision<U, AnotherPrecision>& other) {
39 value_ = other.value_;
40 return *this;
41 }
42
43 template <typename U, std::uint32_t AnotherPrecision>
44 FloatingWithPrecision& operator=(FloatingWithPrecision<U, AnotherPrecision>&& other) {
45 if (this == &other) {
46 return *this;
47 }
48 value_ = other.value_;
49 return *this;
50 }
51
52 std::string ToString() { return fmt::format("{:.{}f}", value_, Precision); }
53
54private:
55 FloatingPointT value_;
56
57 template <std::floating_point U, std::uint32_t AnotherPrecision>
58 friend class FloatingWithPrecision;
59};
60
61} // namespace storages::clickhouse::io
62
63USERVER_NAMESPACE_END