Program Listing for File instrumentation_scope.h

Return to documentation for file (/home/docs/checkouts/readthedocs.org/user_builds/opentelemetry-cpp/checkouts/v1.8.1/sdk/include/opentelemetry/sdk/instrumentationscope/instrumentation_scope.h)

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/nostd/unique_ptr.h"
#include "opentelemetry/version.h"

#include <functional>
#include <string>

OPENTELEMETRY_BEGIN_NAMESPACE

namespace sdk
{
namespace instrumentationscope
{

class InstrumentationScope
{
public:
  InstrumentationScope(const InstrumentationScope &) = default;

  static nostd::unique_ptr<InstrumentationScope> Create(nostd::string_view name,
                                                        nostd::string_view version    = "",
                                                        nostd::string_view schema_url = "")
  {
    return nostd::unique_ptr<InstrumentationScope>(
        new InstrumentationScope{name, version, schema_url});
  }

  std::size_t HashCode() const noexcept { return hash_code_; }

  bool operator==(const InstrumentationScope &other) const
  {
    return equal(other.name_, other.version_, other.schema_url_);
  }

  bool equal(const nostd::string_view name,
             const nostd::string_view version,
             const nostd::string_view schema_url = "") const
  {
    return this->name_ == name && this->version_ == version && this->schema_url_ == schema_url;
  }

  const std::string &GetName() const { return name_; }
  const std::string &GetVersion() const { return version_; }
  const std::string &GetSchemaURL() const { return schema_url_; }

private:
  InstrumentationScope(nostd::string_view name,
                       nostd::string_view version,
                       nostd::string_view schema_url = "")
      : name_(name), version_(version), schema_url_(schema_url)
  {
    std::string hash_data;
    hash_data.reserve(name_.size() + version_.size() + schema_url_.size());
    hash_data += name_;
    hash_data += version_;
    hash_data += schema_url_;
    hash_code_ = std::hash<std::string>{}(hash_data);
  }

private:
  std::string name_;
  std::string version_;
  std::string schema_url_;
  std::size_t hash_code_;
};

}  // namespace instrumentationscope
}  // namespace sdk

OPENTELEMETRY_END_NAMESPACE