Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ Checks: >
-modernize-make-unique,
-modernize-make-shared,

CheckOptions:
- key: bugprone-exception-escape.TreatFunctionsWithoutSpecificationAsThrowing
value: OnlyUndefined

12 changes: 6 additions & 6 deletions sdk/include/opentelemetry/sdk/common/atomic_shared_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ template <class T>
class AtomicSharedPtr
{
public:
explicit AtomicSharedPtr(std::shared_ptr<T> ptr) noexcept : ptr_{std::move(ptr)} {}
explicit AtomicSharedPtr(std::shared_ptr<T> ptr) : ptr_{std::move(ptr)} {}

void store(const std::shared_ptr<T> &other) noexcept
void store(const std::shared_ptr<T> &other)
{
std::lock_guard<std::mutex> lock_guard{mu_};
ptr_ = other;
}

std::shared_ptr<T> load() const noexcept
std::shared_ptr<T> load() const
{
std::lock_guard<std::mutex> lock_guard{mu_};
return ptr_;
Expand All @@ -48,11 +48,11 @@ template <class T>
class AtomicSharedPtr
{
public:
explicit AtomicSharedPtr(std::shared_ptr<T> ptr) noexcept : ptr_{std::move(ptr)} {}
explicit AtomicSharedPtr(std::shared_ptr<T> ptr) : ptr_{std::move(ptr)} {}

void store(const std::shared_ptr<T> &other) noexcept { std::atomic_store(&ptr_, other); }
void store(const std::shared_ptr<T> &other) { std::atomic_store(&ptr_, other); }

std::shared_ptr<T> load() const noexcept { return std::atomic_load(&ptr_); }
std::shared_ptr<T> load() const { return std::atomic_load(&ptr_); }

private:
std::shared_ptr<T> ptr_;
Expand Down
21 changes: 10 additions & 11 deletions sdk/include/opentelemetry/sdk/common/atomic_unique_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,36 @@ template <class T>
class AtomicUniquePtr
{
public:
AtomicUniquePtr() noexcept {}
AtomicUniquePtr() {}
AtomicUniquePtr(const AtomicUniquePtr &) = delete;
AtomicUniquePtr(AtomicUniquePtr &&) = delete;
AtomicUniquePtr &operator=(const AtomicUniquePtr &) = delete;
AtomicUniquePtr &operator=(AtomicUniquePtr &&) = delete;

explicit AtomicUniquePtr(std::unique_ptr<T> &&other) noexcept : ptr_(std::move(other).release())
{}
explicit AtomicUniquePtr(std::unique_ptr<T> &&other) : ptr_(std::move(other).release()) {}

~AtomicUniquePtr() noexcept { Reset(); }
~AtomicUniquePtr() { Reset(); }

T &operator*() const noexcept { return *Get(); }
T &operator*() const { return *Get(); }

T *operator->() const noexcept { return Get(); }
T *operator->() const { return Get(); }

/**
* @return the underly pointer managed.
*/
T *Get() const noexcept { return ptr_; }
T *Get() const { return ptr_; }

/**
* @return true if the pointer is null
*/
bool IsNull() const noexcept { return ptr_.load() == nullptr; }
bool IsNull() const { return ptr_.load() == nullptr; }

/**
* Atomically swap the pointer only if it's null.
* @param owner the pointer to swap with
* @return true if the swap was successful
*/
bool SwapIfNull(std::unique_ptr<T> &owner) noexcept
bool SwapIfNull(std::unique_ptr<T> &owner)
{
auto ptr = owner.get();
T *expected = nullptr;
Expand All @@ -69,13 +68,13 @@ class AtomicUniquePtr
* Atomically swap the pointer with another.
* @param other The pointer to swap with
*/
void Swap(std::unique_ptr<T> &other) noexcept { other.reset(ptr_.exchange(other.release())); }
void Swap(std::unique_ptr<T> &other) { other.reset(ptr_.exchange(other.release())); }

/**
* Set the pointer to a new value and delete the current value if non-null.
* @param ptr the new pointer value to set
*/
void Reset(T *ptr = nullptr) noexcept
void Reset(T *ptr = nullptr)
{
ptr = ptr_.exchange(ptr);
if (ptr != nullptr)
Expand Down
43 changes: 18 additions & 25 deletions sdk/include/opentelemetry/sdk/common/attribute_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ enum OwnedAttributeType : std::uint8_t // NOLINT(cppcoreguidelines-use-enum-cla
// 0xE0-0xEF 1110xxxx lead of a 3-byte sequence
// 0xF0-0xF7 11110xxx lead of a 4-byte sequence
// 0xF8-0xFF not a valid lead byte
inline std::size_t Utf8SafePrefixLength(const char *data,
std::size_t size,
std::size_t max_bytes) noexcept
inline std::size_t Utf8SafePrefixLength(const char *data, std::size_t size, std::size_t max_bytes)
{
// When the whole value fits within the budget (including the common
// unbounded case where max_bytes is SIZE_MAX), no truncation is possible,
Expand Down Expand Up @@ -162,7 +160,7 @@ struct AttributeConverter
/// Constructs a converter that truncates string and bytes alternatives to
/// at most `max_length` bytes during conversion. Other alternatives are
/// unaffected. Used by recordables enforcing attribute_value_length_limit.
explicit AttributeConverter(std::size_t max_length) noexcept : max_length_(max_length) {}
explicit AttributeConverter(std::size_t max_length) : max_length_(max_length) {}

OwnedAttributeValue operator()(bool v) { return OwnedAttributeValue(v); }
OwnedAttributeValue operator()(int32_t v) { return OwnedAttributeValue(v); }
Expand Down Expand Up @@ -236,33 +234,33 @@ struct AttributeEqualToVisitor
{
// Different types are not equal including containers of different element types
template <typename T, typename U>
bool operator()(const T &, const U &) const noexcept
bool operator()(const T &, const U &) const
{
return false;
}

// Compare the same arithmetic types
template <typename T>
bool operator()(const T &owned_value, const T &value) const noexcept
bool operator()(const T &owned_value, const T &value) const
{
return owned_value == value;
}

// Compare std::string and const char*
bool operator()(const std::string &owned_value, const char *value) const noexcept
bool operator()(const std::string &owned_value, const char *value) const
{
return owned_value == value;
}

// Compare std::string and nostd::string_view
bool operator()(const std::string &owned_value, nostd::string_view value) const noexcept
bool operator()(const std::string &owned_value, nostd::string_view value) const
{
return owned_value == value;
}

// Compare std::vector<std::string> and nostd::span<const nostd::string_view>
bool operator()(const std::vector<std::string> &owned_value,
const nostd::span<const nostd::string_view> &value) const noexcept
const nostd::span<const nostd::string_view> &value) const
{
return owned_value.size() == value.size() &&
std::equal(owned_value.begin(), owned_value.end(), value.begin(),
Expand All @@ -273,8 +271,7 @@ struct AttributeEqualToVisitor

// Compare nostd::span<const T> and std::vector<T> for arithmetic types
template <typename T>
bool operator()(const std::vector<T> &owned_value,
const nostd::span<const T> &value) const noexcept
bool operator()(const std::vector<T> &owned_value, const nostd::span<const T> &value) const
{
return owned_value.size() == value.size() &&
std::equal(owned_value.begin(), owned_value.end(), value.begin());
Expand All @@ -291,7 +288,7 @@ struct AttributeEqualToVisitor
*/
template <typename Map, typename Value>
inline std::pair<typename Map::iterator, bool>
AttributeInsertOrAssign(Map &map, opentelemetry::nostd::string_view key, Value &&value) noexcept
AttributeInsertOrAssign(Map &map, opentelemetry::nostd::string_view key, Value &&value)
{
#if __cplusplus >= 201703L
// Use insert_or_assign if C++17 is available
Expand Down Expand Up @@ -352,7 +349,7 @@ inline std::pair<ValueType, bool> VisitVariantImpl(Visitor &&visitor,
*
*/
template <typename Visitor, typename Variant>
inline auto VisitVariant(Visitor &&visitor, const Variant &value) noexcept
inline auto VisitVariant(Visitor &&visitor, const Variant &value)
{
using VisitorReturnType =
decltype(opentelemetry::nostd::visit(std::forward<Visitor>(visitor), value));
Expand Down Expand Up @@ -421,22 +418,22 @@ class AttributeMap : public std::unordered_map<std::string, OwnedAttributeValue>
void ConstructFrom(const opentelemetry::common::KeyValueIterable &attributes)
{
attributes.ForEachKeyValue(
[&](nostd::string_view key, const opentelemetry::common::AttributeValue &value) noexcept {
[&](nostd::string_view key, const opentelemetry::common::AttributeValue &value) {
SetAttribute(key, value);
return true;
});
}

// Returns a reference to this map
const std::unordered_map<std::string, OwnedAttributeValue> &GetAttributes() const noexcept
const std::unordered_map<std::string, OwnedAttributeValue> &GetAttributes() const
{
return (*this);
}

// Convert non-owning key-value to owning std::string(key) and OwnedAttributeValue(value)
bool SetAttribute(nostd::string_view key,
const opentelemetry::common::AttributeValue &value,
std::size_t max_length = (std::numeric_limits<std::size_t>::max)()) noexcept
std::size_t max_length = (std::numeric_limits<std::size_t>::max)())
{
std::pair<OwnedAttributeValue, bool> result =
VisitVariant(AttributeConverter(max_length), value);
Expand All @@ -449,16 +446,15 @@ class AttributeMap : public std::unordered_map<std::string, OwnedAttributeValue>
}

// Compare the attributes of this map with another KeyValueIterable
bool EqualTo(const opentelemetry::common::KeyValueIterable &attributes) const noexcept
bool EqualTo(const opentelemetry::common::KeyValueIterable &attributes) const
{
if (attributes.size() != this->size())
{
return false;
}

const bool is_equal = attributes.ForEachKeyValue(
[this](nostd::string_view key,
const opentelemetry::common::AttributeValue &value) noexcept {
[this](nostd::string_view key, const opentelemetry::common::AttributeValue &value) {
// Perform a linear search to find the key assuming the map is small
// This avoids temporary string creation from this->find(std::string(key))
for (const auto &kv : *this)
Expand Down Expand Up @@ -491,7 +487,7 @@ class OrderedAttributeMap : public std::map<std::string, OwnedAttributeValue>
: OrderedAttributeMap()
{
attributes.ForEachKeyValue(
[&](nostd::string_view key, opentelemetry::common::AttributeValue value) noexcept {
[&](nostd::string_view key, opentelemetry::common::AttributeValue value) {
SetAttribute(key, value);
return true;
});
Expand All @@ -510,15 +506,12 @@ class OrderedAttributeMap : public std::map<std::string, OwnedAttributeValue>
}

// Returns a reference to this map
const std::map<std::string, OwnedAttributeValue> &GetAttributes() const noexcept
{
return (*this);
}
const std::map<std::string, OwnedAttributeValue> &GetAttributes() const { return (*this); }

// Convert non-owning key-value to owning std::string(key) and OwnedAttributeValue(value)
void SetAttribute(nostd::string_view key,
const opentelemetry::common::AttributeValue &value,
std::size_t max_length = (std::numeric_limits<std::size_t>::max)()) noexcept
std::size_t max_length = (std::numeric_limits<std::size_t>::max)())
{
std::pair<OwnedAttributeValue, bool> result =
VisitVariant(AttributeConverter(max_length), value);
Expand Down
24 changes: 12 additions & 12 deletions sdk/include/opentelemetry/sdk/common/circular_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CircularBuffer
*
* Note: This method must only be called from the consumer thread.
*/
CircularBufferRange<const AtomicUniquePtr<T>> Peek() const noexcept { return PeekImpl(); }
CircularBufferRange<const AtomicUniquePtr<T>> Peek() const { return PeekImpl(); }

/**
* Consume elements from the circular buffer's tail.
Expand All @@ -51,7 +51,7 @@ class CircularBuffer
* Note: This method must only be called from the consumer thread.
*/
template <class Callback>
void Consume(size_t n, Callback callback) noexcept
void Consume(size_t n, Callback callback)
{
assert(n <= static_cast<size_t>(head_ - tail_));
auto range = PeekImpl().Take(n);
Expand All @@ -66,7 +66,7 @@ class CircularBuffer
*
* Note: This method must only be called from the consumer thread.
*/
void Consume(size_t n) noexcept
void Consume(size_t n)
{
Consume(n, [](CircularBufferRange<AtomicUniquePtr<T>> &range) noexcept {
range.ForEach([](AtomicUniquePtr<T> &ptr) noexcept {
Expand All @@ -81,7 +81,7 @@ class CircularBuffer
* @param ptr a pointer to the element to add
* @return true if the element was successfully added; false, otherwise.
*/
bool Add(std::unique_ptr<T> &ptr) noexcept
bool Add(std::unique_ptr<T> &ptr)
{
while (true)
{
Expand Down Expand Up @@ -116,7 +116,7 @@ class CircularBuffer
}
}

bool Add(std::unique_ptr<T> &&ptr) noexcept
bool Add(std::unique_ptr<T> &&ptr)
{
std::unique_ptr<T> owner = std::move(ptr);
return Add(owner);
Expand All @@ -127,25 +127,25 @@ class CircularBuffer
*
* Note: This method must only be called from the consumer thread.
*/
void Clear() noexcept { Consume(size()); }
void Clear() { Consume(size()); }

/**
* @return the maximum number of bytes that can be stored in the buffer.
*/
size_t max_size() const noexcept { return capacity_ - 1; }
size_t max_size() const { return capacity_ - 1; }

/**
* @return true if the buffer is empty.
*/
bool empty() const noexcept { return head_ == tail_; }
bool empty() const { return head_ == tail_; }

/**
* @return the number of bytes stored in the circular buffer.
*
* Note: this method will only return a correct snapshot of the size if called
* from the consumer thread.
*/
size_t size() const noexcept
size_t size() const
{
uint64_t tail = tail_;
uint64_t head = head_;
Expand All @@ -156,20 +156,20 @@ class CircularBuffer
/**
* @return the number of elements consumed from the circular buffer.
*/
uint64_t consumption_count() const noexcept { return tail_; }
uint64_t consumption_count() const { return tail_; }

/**
* @return the number of elements added to the circular buffer.
*/
uint64_t production_count() const noexcept { return head_; }
uint64_t production_count() const { return head_; }

private:
std::unique_ptr<AtomicUniquePtr<T>[]> data_;
size_t capacity_;
std::atomic<uint64_t> head_{0};
std::atomic<uint64_t> tail_{0};

CircularBufferRange<AtomicUniquePtr<T>> PeekImpl() const noexcept
CircularBufferRange<AtomicUniquePtr<T>> PeekImpl() const
{
uint64_t tail_index = tail_ % capacity_;
uint64_t head_index = head_ % capacity_;
Expand Down
Loading
Loading