• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef FCP_TRACING_TRACING_TAG_H_
16 #define FCP_TRACING_TRACING_TAG_H_
17 
18 #include <stdint.h>
19 
20 #include <iosfwd>
21 #include <ostream>
22 #include <string>
23 
24 #include "absl/base/attributes.h"
25 #include "flatbuffers/flatbuffers.h"
26 
27 namespace fcp {
28 
29 // 4-character tag uniquely identifying FlatBuffers tables used in tracing.
30 union TracingTag {
31   char data[4];
32   uint32_t value;
33   // Constructing from 4-char string literals. Supplied char array
34   // has a length of 5, to accommodate terminating 0, which is NOT stored in the
35   // tag.
TracingTag(char const (& fourCharsLiteral)[5])36   explicit constexpr TracingTag(char const (&fourCharsLiteral)[5])
37       : data{fourCharsLiteral[0], fourCharsLiteral[1], fourCharsLiteral[2],
38              fourCharsLiteral[3]} {}
39 
str()40   ABSL_MUST_USE_RESULT std::string str() const {
41     return std::string{data[0], data[1], data[2], data[3]};
42   }
43   TracingTag() = delete;
44 
FromFlatbuf(const flatbuffers::DetachedBuffer & buf)45   static inline const TracingTag* FromFlatbuf(
46       const flatbuffers::DetachedBuffer& buf) {
47     return reinterpret_cast<const TracingTag*>(&buf.data()[4]);
48   }
49 };
50 
51 inline std::ostream& operator<<(std::ostream& out, const TracingTag& tag) {
52   out << tag.str();
53   return out;
54 }
55 
56 inline bool operator==(TracingTag const& a, TracingTag const& b) {
57   return a.value == b.value;
58 }
59 
60 inline bool operator!=(TracingTag const& a, TracingTag const& b) {
61   return a.value != b.value;
62 }
63 
64 template <typename H>
AbslHashValue(H h,const fcp::TracingTag & t)65 H AbslHashValue(H h, const fcp::TracingTag& t) {
66   return H::combine(std::move(h), t.value);
67 }
68 }  // namespace fcp
69 
70 #endif  // FCP_TRACING_TRACING_TAG_H_
71