Skip to content

Commit

Permalink
Add nostd::make_unique
Browse files Browse the repository at this point in the history
  • Loading branch information
owent committed Oct 14, 2024
1 parent f69963f commit 9218c4a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
6 changes: 6 additions & 0 deletions api/include/opentelemetry/nostd/unique_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ bool operator!=(std::nullptr_t, const unique_ptr<T> &rhs) noexcept
{
return nullptr != rhs.get();
}

template <class T, class... Args>
unique_ptr<T> make_unique(Args &&...args)
{
return unique_ptr<T>(new T(std::forward<Args>(args)...));
}
} // namespace nostd
OPENTELEMETRY_END_NAMESPACE
#endif /* OPENTELEMETRY_HAVE_STD_UNIQUE_PTR */
11 changes: 11 additions & 0 deletions api/include/opentelemetry/std/unique_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,16 @@ namespace nostd
template <class... _Types>
using unique_ptr = std::unique_ptr<_Types...>;

#if (defined(__cplusplus) && __cplusplus >= 201402L) || \
(defined(_MSVC_LANG) && _MSVC_LANG >= 201402L)
using std::make_unique;
#else
template <class T, class... Args>
unique_ptr<T> make_unique(Args &&...args)
{
return unique_ptr<T>(new T(std::forward<Args>(args)...));
}
#endif

} // namespace nostd
OPENTELEMETRY_END_NAMESPACE
5 changes: 3 additions & 2 deletions exporters/memory/src/in_memory_metric_exporter_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "opentelemetry/exporters/memory/in_memory_metric_exporter_factory.h"
#include "opentelemetry/exporters/memory/in_memory_metric_data.h"
#include "opentelemetry/nostd/unique_ptr.h"
#include "opentelemetry/sdk/common/global_log_handler.h"
#include "opentelemetry/sdk/metrics/export/metric_producer.h"
#include "opentelemetry/sdk/metrics/push_metric_exporter.h"
Expand Down Expand Up @@ -49,7 +50,7 @@ class InMemoryMetricExporter final : public sdk::metrics::PushMetricExporter
OTEL_INTERNAL_LOG_ERROR("[In Memory Metric Exporter] Exporting failed, exporter is shutdown");
return ExportResult::kFailure;
}
data_->Add(std::make_unique<ResourceMetrics>(data));
data_->Add(opentelemetry::nostd::make_unique<ResourceMetrics>(data));
return ExportResult::kSuccess;
}

Expand Down Expand Up @@ -85,7 +86,7 @@ std::unique_ptr<PushMetricExporter> InMemoryMetricExporterFactory::Create(
const std::shared_ptr<InMemoryMetricData> &data,
const AggregationTemporalitySelector &temporality)
{
return std::make_unique<InMemoryMetricExporter>(data, temporality);
return opentelemetry::nostd::make_unique<InMemoryMetricExporter>(data, temporality);
}

} // namespace memory
Expand Down
5 changes: 3 additions & 2 deletions exporters/memory/test/in_memory_metric_data_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/exporters/memory/in_memory_metric_data.h"
#include "opentelemetry/nostd/unique_ptr.h"
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
#include "opentelemetry/sdk/metrics/export/metric_producer.h"
#include "opentelemetry/sdk/resource/resource.h"
Expand All @@ -23,7 +24,7 @@ TEST(InMemoryMetricDataTest, CircularBuffer)
{
CircularBufferInMemoryMetricData buf(10);
Resource resource = Resource::GetEmpty();
buf.Add(std::make_unique<ResourceMetrics>(
buf.Add(opentelemetry::nostd::make_unique<ResourceMetrics>(
&resource, std::vector<ScopeMetrics>{{nullptr, std::vector<MetricData>{}}}));
EXPECT_EQ((*buf.Get().begin())->resource_, &resource);
}
Expand All @@ -45,7 +46,7 @@ TEST(InMemoryMetricDataTest, SimpleAggregate)
md.instrument_descriptor.name_ = "my-metric";
md.point_data_attr_.push_back(pda);

agg.Add(std::make_unique<ResourceMetrics>(
agg.Add(opentelemetry::nostd::make_unique<ResourceMetrics>(
&resource, std::vector<ScopeMetrics>{{scope.get(), std::vector<MetricData>{md}}}));
auto it = agg.Get("my-scope", "my-metric").begin();

Expand Down

0 comments on commit 9218c4a

Please sign in to comment.