1 /*
2 * Copyright 2021 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef FCP_TRACING_TRACING_SPAN_ID_H_
18 #define FCP_TRACING_TRACING_SPAN_ID_H_
19
20 #include <atomic>
21 #include <cstdint>
22 #include <ostream>
23
24 namespace fcp {
25
26 // Uniquely identifies tracing span within a process.
27 struct TracingSpanId {
28 std::int64_t value;
29
30 // Generates next unique id
31 static TracingSpanId NextUniqueId();
TracingSpanIdTracingSpanId32 explicit constexpr TracingSpanId(int id) : value(id) {}
33
34 private:
35 static std::atomic<std::int64_t> id_source;
36 };
37
38 inline bool operator==(const TracingSpanId& a, const TracingSpanId& b) {
39 return a.value == b.value;
40 }
41
42 inline bool operator!=(const TracingSpanId& a, const TracingSpanId& b) {
43 return a.value != b.value;
44 }
45
46 // Overload comparison operators to make it possible to sort by order of ID
47 // generation.
48 inline bool operator<(const TracingSpanId& a, const TracingSpanId& b) {
49 return a.value < b.value;
50 }
51
52 inline std::ostream& operator<<(std::ostream& s, const TracingSpanId& id) {
53 return s << id.value;
54 }
55
56 template <typename H>
AbslHashValue(H h,const TracingSpanId & id)57 H AbslHashValue(H h, const TracingSpanId& id) {
58 return H::combine(std::move(h), id.value);
59 }
60
61 } // namespace fcp
62
63 #endif // FCP_TRACING_TRACING_SPAN_ID_H_
64