.. _program_listing_file_include_opentelemetry_trace_span_id.h: Program Listing for File span_id.h ================================== |exhale_lsh| :ref:`Return to documentation for file ` (``include/opentelemetry/trace/span_id.h``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp // Copyright 2019, OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #pragma once #include #include #include "opentelemetry/nostd/span.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace trace { class SpanId final { public: // The size in bytes of the SpanId. static constexpr int kSize = 8; // An invalid SpanId (all zeros). SpanId() noexcept : rep_{0} {} // Creates a SpanId with the given ID. explicit SpanId(nostd::span id) noexcept { memcpy(rep_, id.data(), kSize); } // Populates the buffer with the lowercase base16 representation of the ID. void ToLowerBase16(nostd::span buffer) const noexcept { constexpr char kHex[] = "0123456789abcdef"; for (int i = 0; i < kSize; ++i) { buffer[i * 2 + 0] = kHex[(rep_[i] >> 4) & 0xF]; buffer[i * 2 + 1] = kHex[(rep_[i] >> 0) & 0xF]; } } // Returns a nostd::span of the ID. nostd::span Id() const noexcept { return nostd::span(rep_); } bool operator==(const SpanId &that) const noexcept { return memcmp(rep_, that.rep_, kSize) == 0; } bool operator!=(const SpanId &that) const noexcept { return !(*this == that); } // Returns false if the SpanId is all zeros. bool IsValid() const noexcept { static_assert(kSize == 8, "update is needed if kSize is not 8"); return *reinterpret_cast(&rep_) != 0ull; } // Copies the opaque SpanId data to dest. void CopyBytesTo(nostd::span dest) const noexcept { memcpy(dest.data(), rep_, kSize); } private: uint8_t rep_[kSize]; }; } // namespace trace OPENTELEMETRY_END_NAMESPACE