diff --git a/core/include/prometheus/detail/future_std.h b/core/include/prometheus/detail/future_std.h index 55956aa0..0cb7eb5e 100644 --- a/core/include/prometheus/detail/future_std.h +++ b/core/include/prometheus/detail/future_std.h @@ -6,11 +6,14 @@ namespace prometheus { namespace detail { -// Remove as soon C++14 can be used. +#if __cplusplus >= 201402L +using std::make_unique; +#else template std::unique_ptr make_unique(Args&&... args) { return std::unique_ptr(new T(std::forward(args)...)); } +#endif } // namespace detail } // namespace prometheus diff --git a/pull/include/prometheus/exposer.h b/pull/include/prometheus/exposer.h index 93e0b2b5..7e143c58 100644 --- a/pull/include/prometheus/exposer.h +++ b/pull/include/prometheus/exposer.h @@ -21,6 +21,7 @@ class Endpoint; class PROMETHEUS_CPP_PULL_EXPORT Exposer { public: + explicit Exposer(std::shared_ptr server); explicit Exposer(const std::string& bind_address, std::size_t num_threads = 2, const CivetCallbacks* callbacks = nullptr); explicit Exposer(std::vector options, @@ -48,7 +49,7 @@ class PROMETHEUS_CPP_PULL_EXPORT Exposer { private: detail::Endpoint& GetEndpointForUri(const std::string& uri); - std::unique_ptr server_; + std::shared_ptr server_; std::vector> endpoints_; std::mutex mutex_; }; diff --git a/pull/src/exposer.cc b/pull/src/exposer.cc index 5ee3adea..96c928cf 100644 --- a/pull/src/exposer.cc +++ b/pull/src/exposer.cc @@ -11,6 +11,13 @@ namespace prometheus { +Exposer::Exposer(std::shared_ptr server) + : server_(std::move(server)) { + if (!server_) { + throw std::invalid_argument("Invalid CivetServer: cannot be null"); + } +} + Exposer::Exposer(const std::string& bind_address, const std::size_t num_threads, const CivetCallbacks* callbacks) : Exposer( @@ -20,7 +27,7 @@ Exposer::Exposer(const std::string& bind_address, const std::size_t num_threads, Exposer::Exposer(std::vector options, const CivetCallbacks* callbacks) - : server_(detail::make_unique(std::move(options), callbacks)) { + : Exposer(std::make_shared(std::move(options), callbacks)) { } Exposer::~Exposer() = default; diff --git a/pull/tests/unit/exposer_test.cc b/pull/tests/unit/exposer_test.cc index 94c2f32d..a304a963 100644 --- a/pull/tests/unit/exposer_test.cc +++ b/pull/tests/unit/exposer_test.cc @@ -2,6 +2,8 @@ #include +#include "CivetServer.h" + namespace prometheus { namespace { @@ -21,5 +23,9 @@ TEST(ExposerTest, listenOnDistinctPorts) { EXPECT_NE(firstExposerPorts, secondExposerPorts); } +TEST(ExposerTest, invalidExternalServer) { + EXPECT_THROW(Exposer(std::shared_ptr(nullptr)), std::invalid_argument); +} + } // namespace } // namespace prometheus