]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/opentracing-cpp/include/opentracing/util.h
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / opentracing-cpp / include / opentracing / util.h
CommitLineData
f67539c2
TL
1#ifndef OPENTRACING_UTIL_H
2#define OPENTRACING_UTIL_H
3
4#include <opentracing/string_view.h>
5#include <opentracing/version.h>
6#include <chrono>
7#include <system_error>
8
9// expected uses a C++11 implementation that follows the std::expected standard
10// library proposal.
11//
12// See https://github.com/martinmoene/expected-lite
13// https://github.com/viboes/std-make/blob/master/doc/proposal/expected/d0323r2.md
14#include <opentracing/expected/expected.hpp>
15
16namespace opentracing {
17BEGIN_OPENTRACING_ABI_NAMESPACE
18using SystemClock = std::chrono::system_clock;
19using SteadyClock = std::chrono::steady_clock;
20using SystemTime = SystemClock::time_point;
21using SteadyTime = SteadyClock::time_point;
22
23// This is unsafe to do.
24//
25// This is like an unsafe std::reference_wrapper<> that allows taking
26// references to temporaries. It must only be used for temporary
27// SpanStartOption and SpanFinishOption objects.
28template <typename T>
29class option_wrapper {
30 public:
31 option_wrapper(const T &opt) : ptr_(&opt) {}
32
33 // This will dangle unless it is only used for short-lived initializer lists.
34 const T &get() const { return *ptr_; }
35
36 private:
37 const T *ptr_;
38};
39
40// Support conversion between time_points from different clocks. There's no
41// standard way to get the difference in epochs between clocks, so this uses
42// an approximation suggested by Howard Hinnant.
43//
44// See https://stackoverflow.com/a/35282833/4447365
45template <class ToClock, class FromClock, class Duration,
46 typename std::enable_if<
47 !std::is_same<FromClock, ToClock>::value>::type * = nullptr>
48typename ToClock::time_point convert_time_point(
49 std::chrono::time_point<FromClock, Duration> from_time_point) {
50 auto from_now = FromClock::now();
51 auto to_now = ToClock::now();
52 return to_now + std::chrono::duration_cast<typename ToClock::duration>(
53 from_time_point - from_now);
54}
55
56template <class ToClock, class FromClock, class Duration,
57 typename std::enable_if<std::is_same<FromClock, ToClock>::value>::type
58 * = nullptr>
59typename ToClock::time_point convert_time_point(
60 std::chrono::time_point<FromClock, Duration> from_time_point) {
61 return std::chrono::time_point_cast<typename ToClock::time_point::duration>(
62 from_time_point);
63}
64
65// std::error_code's have default comparison operators; however, they make use
66// of singleton addresses which can cause comparisons to fail when multiple
67// versions of the opentracing library are linked in. Since this is a common
68// deployment scenario when making OpenTracing plugins, we add this utility
69// function to make comparing std::error_code across libraries easier.
70//
71// Note: There's a proposed change to the C++ standard that addresses this
72// issue. See
73// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1196r0.html
74inline bool are_errors_equal(std::error_code lhs,
75 std::error_code rhs) noexcept {
76 return opentracing::string_view{lhs.category().name()} ==
77 opentracing::string_view{rhs.category().name()} &&
78 lhs.value() == rhs.value();
79}
80
81END_OPENTRACING_ABI_NAMESPACE
82} // namespace opentracing
83
84#endif // OPENTRACING_UTIL_H