Program Listing for File circular_buffer.h
↰ Return to documentation for file (/home/docs/checkouts/readthedocs.org/user_builds/opentelemetry-cpp/checkouts/latest/sdk/include/opentelemetry/sdk/metrics/data/circular_buffer.h
)
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "opentelemetry/nostd/variant.h"
#include <cstdint>
#include <limits>
#include <vector>
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
class AdaptingIntegerArray
{
public:
// Construct an adapting integer array of a given size.
explicit AdaptingIntegerArray(size_t size) : backing_(std::vector<uint8_t>(size, 0)) {}
AdaptingIntegerArray(const AdaptingIntegerArray &other) = default;
AdaptingIntegerArray(AdaptingIntegerArray &&other) = default;
AdaptingIntegerArray &operator=(const AdaptingIntegerArray &other) = default;
AdaptingIntegerArray &operator=(AdaptingIntegerArray &&other) = default;
void Increment(size_t index, uint64_t count);
uint64_t Get(size_t index) const;
size_t Size() const;
void Clear();
private:
void EnlargeToFit(uint64_t value);
nostd::variant<std::vector<uint8_t>,
std::vector<uint16_t>,
std::vector<uint32_t>,
std::vector<uint64_t>>
backing_;
};
class AdaptingCircularBufferCounter
{
public:
explicit AdaptingCircularBufferCounter(size_t max_size) : backing_(max_size) {}
AdaptingCircularBufferCounter(const AdaptingCircularBufferCounter &other) = default;
AdaptingCircularBufferCounter(AdaptingCircularBufferCounter &&other) = default;
AdaptingCircularBufferCounter &operator=(const AdaptingCircularBufferCounter &other) = default;
AdaptingCircularBufferCounter &operator=(AdaptingCircularBufferCounter &&other) = default;
int32_t StartIndex() const { return start_index_; }
int32_t EndIndex() const { return end_index_; }
bool Empty() const { return base_index_ == kNullIndex; }
size_t MaxSize() const { return backing_.Size(); }
void Clear();
bool Increment(int32_t index, uint64_t delta);
uint64_t Get(int32_t index);
private:
size_t ToBufferIndex(int32_t index) const;
static constexpr int32_t kNullIndex = std::numeric_limits<int32_t>::min();
// Index of the first populated element, may be kNullIndex if container is empty.
int32_t start_index_ = kNullIndex;
// Index of the last populated element, may be kNullIndex if container is empty.
int32_t end_index_ = kNullIndex;
// Index corresponding to the element located at the start of the backing array, may be kNullIndex
// if container is empty.
int32_t base_index_ = kNullIndex;
AdaptingIntegerArray backing_;
};
} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE