Skip to content

Commit c26d435

Browse files
committed
Use C++17 standards and move from std::experimental::optional to std::optional
2 parents 780e80d + 280d2a5 commit c26d435

File tree

3 files changed

+39
-39
lines changed

3 files changed

+39
-39
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
project(StatsdClient)
12
cmake_minimum_required(VERSION 3.2)
23

3-
project(StatsdClient)
4+
set(CMAKE_CXX_STANDARD 17)
5+
set(CMAKE_CXX_EXTENSIONS OFF)
46

57
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/cpp-statsd-client)
68

include/cpp-statsd-client/StatsdClient.hpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define STATSD_CLIENT_HPP
33

44
#include <cstdlib>
5-
#include <experimental/optional>
5+
#include <optional>
66
#include <string>
77
#include "UDPSender.hpp"
88

@@ -25,39 +25,39 @@ class StatsdClient {
2525
//!@{
2626

2727
//! Constructor
28-
StatsdClient(const std::string &host,
28+
StatsdClient(const std::string& host,
2929
const uint16_t port,
30-
const std::string &prefix,
31-
const std::experimental::optional<uint64_t> batchsize = std::experimental::nullopt) noexcept;
30+
const std::string& prefix,
31+
const std::optional<uint64_t> batchsize = std::nullopt) noexcept;
3232

3333
//!@}
3434

3535
//!@name Methods
3636
//!@{
3737

3838
//! Sets a configuration { host, port, prefix }
39-
inline void setConfig(const std::string &host, const uint16_t port, const std::string &prefix) noexcept;
39+
inline void setConfig(const std::string& host, const uint16_t port, const std::string& prefix) noexcept;
4040

4141
//! Returns the error message as an optional std::string
42-
inline std::experimental::optional<std::string> errorMessage() const noexcept;
42+
inline std::optional<std::string> errorMessage() const noexcept;
4343

4444
//! Increments the key, at a given frequency rate
45-
inline void increment(const std::string &key, const float frequency = 1.0f) const noexcept;
45+
inline void increment(const std::string& key, const float frequency = 1.0f) const noexcept;
4646

4747
//! Increments the key, at a given frequency rate
48-
inline void decrement(const std::string &key, const float frequency = 1.0f) const noexcept;
48+
inline void decrement(const std::string& key, const float frequency = 1.0f) const noexcept;
4949

5050
//! Adjusts the specified key by a given delta, at a given frequency rate
51-
inline void count(const std::string &key, const int delta, const float frequency = 1.0f) const noexcept;
51+
inline void count(const std::string& key, const int delta, const float frequency = 1.0f) const noexcept;
5252

5353
//! Records a gauge for the key, with a given value, at a given frequency rate
54-
inline void gauge(const std::string &key, const unsigned int value, const float frequency = 1.0f) const noexcept;
54+
inline void gauge(const std::string& key, const unsigned int value, const float frequency = 1.0f) const noexcept;
5555

5656
//! Records a timing for a key, at a given frequency
57-
inline void timing(const std::string &key, const unsigned int ms, const float frequency = 1.0f) const noexcept;
57+
inline void timing(const std::string& key, const unsigned int ms, const float frequency = 1.0f) const noexcept;
5858

5959
//! Send a value for a key, according to its type, at a given frequency
60-
void send(const std::string &key, const int value, const std::string &type, const float frequency = 1.0f) const
60+
void send(const std::string& key, const int value, const std::string& type, const float frequency = 1.0f) const
6161
noexcept;
6262

6363
//!@}
@@ -70,45 +70,45 @@ class StatsdClient {
7070
mutable UDPSender m_sender;
7171
};
7272

73-
StatsdClient::StatsdClient(const std::string &host,
73+
StatsdClient::StatsdClient(const std::string& host,
7474
const uint16_t port,
75-
const std::string &prefix,
76-
const std::experimental::optional<uint64_t> batchsize) noexcept
75+
const std::string& prefix,
76+
const std::optional<uint64_t> batchsize) noexcept
7777
: m_prefix(prefix), m_sender(host, port, batchsize) {
7878
// Initialize the randorm generator to be used for sampling
7979
std::srand(time(nullptr));
8080
}
8181

82-
void StatsdClient::setConfig(const std::string &host, const uint16_t port, const std::string &prefix) noexcept {
82+
void StatsdClient::setConfig(const std::string& host, const uint16_t port, const std::string& prefix) noexcept {
8383
m_prefix = prefix;
8484
m_sender.setConfig(host, port);
8585
}
8686

87-
std::experimental::optional<std::string> StatsdClient::errorMessage() const noexcept {
87+
std::optional<std::string> StatsdClient::errorMessage() const noexcept {
8888
return m_sender.errorMessage();
8989
}
9090

91-
void StatsdClient::decrement(const std::string &key, const float frequency) const noexcept {
91+
void StatsdClient::decrement(const std::string& key, const float frequency) const noexcept {
9292
return count(key, -1, frequency);
9393
}
9494

95-
void StatsdClient::increment(const std::string &key, const float frequency) const noexcept {
95+
void StatsdClient::increment(const std::string& key, const float frequency) const noexcept {
9696
return count(key, 1, frequency);
9797
}
9898

99-
void StatsdClient::count(const std::string &key, const int delta, const float frequency) const noexcept {
99+
void StatsdClient::count(const std::string& key, const int delta, const float frequency) const noexcept {
100100
return send(key, delta, "c", frequency);
101101
}
102102

103-
void StatsdClient::gauge(const std::string &key, const unsigned int value, const float frequency) const noexcept {
103+
void StatsdClient::gauge(const std::string& key, const unsigned int value, const float frequency) const noexcept {
104104
return send(key, value, "g", frequency);
105105
}
106106

107-
void StatsdClient::timing(const std::string &key, const unsigned int ms, const float frequency) const noexcept {
107+
void StatsdClient::timing(const std::string& key, const unsigned int ms, const float frequency) const noexcept {
108108
return send(key, ms, "ms", frequency);
109109
}
110110

111-
void StatsdClient::send(const std::string &key, const int value, const std::string &type, const float frequency) const
111+
void StatsdClient::send(const std::string& key, const int value, const std::string& type, const float frequency) const
112112
noexcept {
113113
const auto isFrequencyOne = [](const float frequency) noexcept {
114114
constexpr float epsilon{0.0001f};

include/cpp-statsd-client/UDPSender.hpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include <cmath>
1010
#include <cstring>
1111
#include <deque>
12-
#include <experimental/optional>
1312
#include <mutex>
13+
#include <optional>
1414
#include <string>
1515
#include <thread>
1616

@@ -30,9 +30,9 @@ class UDPSender final {
3030
//!@{
3131

3232
//! Constructor
33-
UDPSender(const std::string &host,
33+
UDPSender(const std::string& host,
3434
const uint16_t port,
35-
const std::experimental::optional<uint64_t> batchsize = std::experimental::nullopt) noexcept;
35+
const std::optional<uint64_t> batchsize = std::nullopt) noexcept;
3636

3737
//! Destructor
3838
~UDPSender();
@@ -43,13 +43,13 @@ class UDPSender final {
4343
//!@{
4444

4545
//! Sets a configuration { host, port }
46-
inline void setConfig(const std::string &host, const uint16_t port) noexcept;
46+
inline void setConfig(const std::string& host, const uint16_t port) noexcept;
4747

4848
//! Send a message
49-
void send(const std::string &message) noexcept;
49+
void send(const std::string& message) noexcept;
5050

5151
//! Returns the error message as an optional string
52-
inline std::experimental::optional<std::string> errorMessage() const noexcept;
52+
inline std::optional<std::string> errorMessage() const noexcept;
5353

5454
//!@}
5555

@@ -61,7 +61,7 @@ class UDPSender final {
6161
bool initialize() noexcept;
6262

6363
//! Send a message to the daemon
64-
void sendToDaemon(const std::string &message) noexcept;
64+
void sendToDaemon(const std::string& message) noexcept;
6565

6666
//!@}
6767

@@ -115,12 +115,10 @@ class UDPSender final {
115115
//!@}
116116

117117
//! Error message (optional string)
118-
std::experimental::optional<std::string> m_errorMessage;
118+
std::optional<std::string> m_errorMessage;
119119
};
120120

121-
UDPSender::UDPSender(const std::string &host,
122-
const uint16_t port,
123-
const std::experimental::optional<uint64_t> batchsize) noexcept
121+
UDPSender::UDPSender(const std::string& host, const uint16_t port, const std::optional<uint64_t> batchsize) noexcept
124122
: m_host(host), m_port(port) {
125123
// If batching is on, use a dedicated thread to send every now and then
126124
if (batchsize) {
@@ -165,7 +163,7 @@ UDPSender::~UDPSender() {
165163
}
166164
}
167165

168-
void UDPSender::setConfig(const std::string &host, const uint16_t port) noexcept {
166+
void UDPSender::setConfig(const std::string& host, const uint16_t port) noexcept {
169167
m_host = host;
170168
m_port = port;
171169

@@ -177,7 +175,7 @@ void UDPSender::setConfig(const std::string &host, const uint16_t port) noexcept
177175
m_socket = -1;
178176
}
179177

180-
void UDPSender::send(const std::string &message) noexcept {
178+
void UDPSender::send(const std::string& message) noexcept {
181179
// If batching is on, accumulate messages in the queue
182180
if (m_batching) {
183181
std::unique_lock<std::mutex> batchingLock(m_batchingMutex);
@@ -191,7 +189,7 @@ void UDPSender::send(const std::string &message) noexcept {
191189
}
192190
}
193191

194-
std::experimental::optional<std::string> UDPSender::errorMessage() const noexcept {
192+
std::optional<std::string> UDPSender::errorMessage() const noexcept {
195193
return m_errorMessage;
196194
}
197195

@@ -245,7 +243,7 @@ bool UDPSender::initialize() noexcept {
245243
return true;
246244
}
247245

248-
void UDPSender::sendToDaemon(const std::string &message) noexcept {
246+
void UDPSender::sendToDaemon(const std::string& message) noexcept {
249247
// Can't send until the sender is initialized
250248
if (!initialize()) {
251249
return;

0 commit comments

Comments
 (0)