Skip to content

Commit a772525

Browse files
committed
REF: Use some C++17 features
1 parent 9b18344 commit a772525

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+198
-299
lines changed

examples/historical/metadata.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ int main() {
4949

5050
const auto all_unit_prices = client.MetadataListUnitPrices(kGlbxMdp3);
5151
std::cout << "Unit prices:\n";
52-
for (const auto& mode_and_prices : all_unit_prices) {
53-
const auto* mode_str = ToString(mode_and_prices.mode);
54-
for (const auto& schema_and_price : mode_and_prices.unit_prices) {
55-
std::cout << "- (" << mode_str << ", " << schema_and_price.first
56-
<< "): " << schema_and_price.second << '\n';
52+
for (const auto& [mode, unit_prices] : all_unit_prices) {
53+
const auto* mode_str = ToString(mode);
54+
for (const auto [schema, price] : unit_prices) {
55+
std::cout << "- (" << mode_str << ", " << schema << "): " << price
56+
<< '\n';
5757
}
5858
}
5959
std::cout << '\n';

examples/live/live_smoke_test.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <sstream>
1212
#include <stdexcept>
1313
#include <string>
14-
#include <unordered_map>
1514

1615
using namespace databento;
1716

@@ -28,7 +27,6 @@ std::vector<std::string> SplitSymbols(const std::string& symbols) {
2827
}
2928

3029
std::pair<bool, UnixNanos> TryConvertToUnixNanos(const char* start) {
31-
std::stringstream ss(start);
3230
std::size_t pos;
3331
const uint64_t result = std::stoul(start, &pos, 10);
3432
if (pos != std::strlen(start)) {
@@ -187,10 +185,10 @@ int main(int argc, char* argv[]) {
187185
if (use_snapshot) {
188186
client.SubscribeWithSnapshot(symbols, schema, stype);
189187
} else if (start) {
190-
const auto converted = TryConvertToUnixNanos(start);
191-
if (converted.first) {
192-
start_from_epoch = converted.second.time_since_epoch().count() == 0;
193-
client.Subscribe(symbols, schema, stype, converted.second);
188+
const auto [success, start_nanos] = TryConvertToUnixNanos(start);
189+
if (success) {
190+
start_from_epoch = start_nanos.time_since_epoch().count() == 0;
191+
client.Subscribe(symbols, schema, stype, start_nanos);
194192
} else {
195193
client.Subscribe(symbols, schema, stype, start);
196194
}

examples/live/simple.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ static std::sig_atomic_t volatile gSignal;
1616

1717
int main() {
1818
databento::PitSymbolMap symbol_mappings;
19-
std::unique_ptr<databento::ILogReceiver> log_receiver{
20-
new databento::ConsoleLogReceiver{databento::LogLevel::Debug}};
19+
auto log_receiver = std::make_unique<databento::ConsoleLogReceiver>(
20+
databento::LogLevel::Debug);
2121

2222
auto client = databento::LiveBuilder{}
2323
.SetLogReceiver(log_receiver.get())

include/databento/detail/json_helpers.hpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ namespace httplib {
1515
using Params = std::multimap<std::string, std::string>;
1616
}
1717

18-
namespace databento {
19-
namespace detail {
18+
namespace databento::detail {
2019
void SetIfNotEmpty(httplib::Params* params, const std::string& key,
2120
const std::string& value);
2221
void SetIfNotEmpty(httplib::Params* params, const std::string& key,
@@ -95,5 +94,4 @@ date::year_month_day ParseAt(const std::string& endpoint,
9594
const nlohmann::json& json,
9695
const std::string& key);
9796

98-
} // namespace detail
99-
} // namespace databento
97+
} // namespace databento::detail

include/databento/detail/scoped_fd.hpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
#include <winsock2.h> // SOCKET
55
#endif
66

7-
namespace databento {
8-
namespace detail {
7+
namespace databento::detail {
98
#ifdef _WIN32
109
using Socket = SOCKET;
1110
#else
@@ -35,5 +34,4 @@ class ScopedFd {
3534
private:
3635
Socket fd_{kUnset};
3736
};
38-
} // namespace detail
39-
} // namespace databento
37+
} // namespace databento::detail

include/databento/detail/scoped_thread.hpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
#include <thread>
44
#include <utility> // forward, move
55

6-
namespace databento {
7-
namespace detail {
6+
namespace databento::detail {
87
// An RAII thread that joins if necessary on destruction, like std::jthread in
98
// C++20.
109
class ScopedThread {
@@ -37,5 +36,4 @@ class ScopedThread {
3736
private:
3837
std::thread thread_;
3938
};
40-
} // namespace detail
41-
} // namespace databento
39+
} // namespace databento::detail

include/databento/detail/shared_channel.hpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
#include "databento/ireadable.hpp"
88

9-
namespace databento {
10-
namespace detail {
9+
namespace databento::detail {
1110
// Copyable, thread-safe, unidirectional channel.
1211
class SharedChannel : public IReadable {
1312
public:
@@ -28,5 +27,4 @@ class SharedChannel : public IReadable {
2827

2928
std::shared_ptr<Channel> channel_;
3029
};
31-
} // namespace detail
32-
} // namespace databento
30+
} // namespace databento::detail

include/databento/detail/tcp_client.hpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
#include "databento/detail/scoped_fd.hpp" // ScopedFd
88

9-
namespace databento {
10-
namespace detail {
9+
namespace databento::detail {
1110
class TcpClient {
1211
public:
1312
enum class Status : std::uint8_t {
@@ -47,5 +46,4 @@ class TcpClient {
4746

4847
ScopedFd socket_;
4948
};
50-
} // namespace detail
51-
} // namespace databento
49+
} // namespace databento::detail

include/databento/detail/zstd_stream.hpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
#include "databento/iwritable.hpp"
1212
#include "databento/log.hpp"
1313

14-
namespace databento {
15-
namespace detail {
14+
namespace databento::detail {
1615
class ZstdDecodeStream : public IReadable {
1716
public:
1817
explicit ZstdDecodeStream(std::unique_ptr<IReadable> input);
@@ -54,5 +53,4 @@ class ZstdCompressStream : public IWritable {
5453
std::size_t in_size_;
5554
std::vector<std::uint8_t> out_buffer_;
5655
};
57-
} // namespace detail
58-
} // namespace databento
56+
} // namespace databento::detail

include/databento/record.hpp

+2-7
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,10 @@ struct RecordHeader {
4646
};
4747

4848
// Type trait helper for templated functions accepting DBN records.
49-
namespace detail {
50-
// std::void_t added in C++17
51-
template <typename... Ts>
52-
using void_t = void;
53-
} // namespace detail
54-
template <typename, typename = detail::void_t<>>
49+
template <typename, typename = std::void_t<>>
5550
struct has_header : std::false_type {};
5651
template <typename T>
57-
struct has_header<T, detail::void_t<decltype(std::declval<T>().hd)>>
52+
struct has_header<T, std::void_t<decltype(std::declval<T>().hd)>>
5853
: std::is_same<decltype(std::declval<T>().hd), RecordHeader> {};
5954

6055
class Record {

include/databento/v2.hpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
#include "databento/constants.hpp" // kSymbolCstrLen
44
#include "databento/record.hpp"
55

6-
namespace databento {
7-
namespace v2 {
6+
namespace databento::v2 {
87
static constexpr std::size_t kSymbolCstrLen = databento::kSymbolCstrLen;
98

109
using MboMsg = databento::MboMsg;
@@ -28,5 +27,4 @@ using StatMsg = databento::StatMsg;
2827
using ErrorMsg = databento::ErrorMsg;
2928
using SymbolMappingMsg = databento::SymbolMappingMsg;
3029
using SystemMsg = databento::SystemMsg;
31-
} // namespace v2
32-
} // namespace databento
30+
} // namespace databento::v2

include/databento/v3.hpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
#include "databento/enums.hpp" // InstrumentClass, MatchingAlgorithm, RType, SecurityUpdateAction, Side, UserDefinedInstrument
99
#include "databento/record.hpp" // RecordHeader
1010

11-
namespace databento {
12-
namespace v3 {
11+
namespace databento::v3 {
1312
static constexpr std::size_t kSymbolCstrLen = databento::kSymbolCstrLen;
1413

1514
using MboMsg = databento::MboMsg;
@@ -142,5 +141,4 @@ inline bool operator!=(const InstrumentDefMsg& lhs,
142141
std::string ToString(const InstrumentDefMsg& instr_def_msg);
143142
std::ostream& operator<<(std::ostream& stream,
144143
const InstrumentDefMsg& instr_def_msg);
145-
} // namespace v3
146-
} // namespace databento
144+
} // namespace databento::v3

src/dbn_decoder.cpp

+8-10
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,11 @@ date::year_month_day DecodeIso8601Date(std::uint32_t yyyymmdd_int) {
6565
DbnDecoder::DbnDecoder(ILogReceiver* log_receiver,
6666
detail::SharedChannel channel)
6767
: DbnDecoder(log_receiver,
68-
std::unique_ptr<IReadable>{
69-
new detail::SharedChannel{std::move(channel)}}) {}
68+
std::make_unique<detail::SharedChannel>(std::move(channel))) {}
7069

7170
DbnDecoder::DbnDecoder(ILogReceiver* log_receiver, InFileStream file_stream)
72-
: DbnDecoder(log_receiver, std::unique_ptr<IReadable>{
73-
new InFileStream{std::move(file_stream)}}) {}
71+
: DbnDecoder(log_receiver,
72+
std::make_unique<InFileStream>(std::move(file_stream))) {}
7473

7574
DbnDecoder::DbnDecoder(ILogReceiver* log_receiver,
7675
std::unique_ptr<IReadable> input)
@@ -85,9 +84,8 @@ DbnDecoder::DbnDecoder(ILogReceiver* log_receiver,
8584
input_{std::move(input)} {
8685
read_buffer_.reserve(kBufferCapacity);
8786
if (DetectCompression()) {
88-
input_ =
89-
std::unique_ptr<detail::ZstdDecodeStream>(new detail::ZstdDecodeStream(
90-
std::move(input_), std::move(read_buffer_)));
87+
input_ = std::make_unique<detail::ZstdDecodeStream>(
88+
std::move(input_), std::move(read_buffer_));
9189
// Reinitialize buffer and get it into the same state as uncompressed input
9290
read_buffer_ = std::vector<std::uint8_t>();
9391
read_buffer_.reserve(kBufferCapacity);
@@ -192,10 +190,10 @@ databento::Metadata DbnDecoder::DecodeMetadata() {
192190
// already read first 4 bytes detecting compression
193191
read_buffer_.resize(kMetadataPreludeSize);
194192
input_->ReadExact(&read_buffer_[4], 4);
195-
const auto version_and_size = DbnDecoder::DecodeMetadataVersionAndSize(
193+
const auto [version, size] = DbnDecoder::DecodeMetadataVersionAndSize(
196194
read_buffer_.data(), kMetadataPreludeSize);
197-
version_ = version_and_size.first;
198-
read_buffer_.resize(version_and_size.second);
195+
version_ = version;
196+
read_buffer_.resize(size);
199197
input_->ReadExact(read_buffer_.data(), read_buffer_.size());
200198
buffer_idx_ = read_buffer_.size();
201199
auto metadata = DbnDecoder::DecodeMetadataFields(version_, read_buffer_);

src/dbn_file_store.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <utility> // move
55

66
#include "databento/file_stream.hpp"
7-
#include "databento/ireadable.hpp"
87
#include "databento/record.hpp"
98

109
using databento::DbnFileStore;
@@ -15,8 +14,7 @@ DbnFileStore::DbnFileStore(const std::string& file_path)
1514
DbnFileStore::DbnFileStore(ILogReceiver* log_receiver,
1615
const std::string& file_path,
1716
VersionUpgradePolicy upgrade_policy)
18-
: decoder_{log_receiver,
19-
std::unique_ptr<IReadable>{new InFileStream{file_path}},
17+
: decoder_{log_receiver, std::make_unique<InFileStream>(file_path),
2018
upgrade_policy} {}
2119

2220
void DbnFileStore::Replay(const MetadataCallback& metadata_callback,

src/detail/json_helpers.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
#include <numeric> // accumulate
44
#include <sstream> // istringstream
55

6-
namespace databento {
7-
namespace detail {
6+
namespace databento::detail {
87
void SetIfNotEmpty(httplib::Params* params, const std::string& key,
98
const std::string& value) {
109
if (!value.empty()) {
@@ -128,5 +127,4 @@ date::year_month_day ParseAt(const std::string& endpoint,
128127
}
129128
return start;
130129
}
131-
} // namespace detail
132-
} // namespace databento
130+
} // namespace databento::detail

src/detail/shared_channel.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
#include "databento/exceptions.hpp" // DbnResponseError
99

10-
namespace databento {
11-
namespace detail {
10+
namespace databento::detail {
1211
class SharedChannel::Channel {
1312
public:
1413
Channel() = default;
@@ -36,8 +35,7 @@ class SharedChannel::Channel {
3635
std::condition_variable cv_;
3736
std::stringstream stream_;
3837
};
39-
} // namespace detail
40-
} // namespace databento
38+
} // namespace databento::detail
4139

4240
using databento::detail::SharedChannel;
4341

src/flag_set.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ std::ostream& operator<<(std::ostream& stream, FlagSet flag_set) {
1717
}};
1818

1919
bool has_written_flag = false;
20-
for (const auto& pair : kFlagsAndNames) {
21-
if ((flag_set.*pair.first)()) {
20+
for (const auto& [flag, name] : kFlagsAndNames) {
21+
if ((flag_set.*flag)()) {
2222
if (has_written_flag) {
23-
stream << " | " << pair.second;
23+
stream << " | " << name;
2424
} else {
25-
stream << pair.second;
25+
stream << name;
2626
has_written_flag = true;
2727
}
2828
}

src/live_blocking.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,12 @@ void LiveBlocking::Subscribe(const std::string& sub_msg,
121121
databento::Metadata LiveBlocking::Start() {
122122
client_.WriteAll("start_session\n");
123123
client_.ReadExact(read_buffer_.data(), kMetadataPreludeSize);
124-
const auto version_and_size = DbnDecoder::DecodeMetadataVersionAndSize(
124+
const auto [version, size] = DbnDecoder::DecodeMetadataVersionAndSize(
125125
reinterpret_cast<std::uint8_t*>(read_buffer_.data()),
126126
kMetadataPreludeSize);
127-
std::vector<std::uint8_t> meta_buffer(version_and_size.second);
128-
client_.ReadExact(reinterpret_cast<char*>(meta_buffer.data()),
129-
version_and_size.second);
130-
auto metadata =
131-
DbnDecoder::DecodeMetadataFields(version_and_size.first, meta_buffer);
127+
std::vector<std::uint8_t> meta_buffer(size);
128+
client_.ReadExact(reinterpret_cast<char*>(meta_buffer.data()), size);
129+
auto metadata = DbnDecoder::DecodeMetadataFields(version, meta_buffer);
132130
version_ = metadata.version;
133131
metadata.Upgrade(upgrade_policy_);
134132
return metadata;

src/live_threaded.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,18 @@ LiveThreaded::LiveThreaded(ILogReceiver* log_receiver, std::string key,
5959
std::string dataset, bool send_ts_out,
6060
VersionUpgradePolicy upgrade_policy,
6161
std::chrono::seconds heartbeat_interval)
62-
: impl_{new Impl{log_receiver, std::move(key), std::move(dataset),
63-
send_ts_out, upgrade_policy, heartbeat_interval}} {}
62+
: impl_{std::make_unique<Impl>(log_receiver, std::move(key),
63+
std::move(dataset), send_ts_out,
64+
upgrade_policy, heartbeat_interval)} {}
6465

6566
LiveThreaded::LiveThreaded(ILogReceiver* log_receiver, std::string key,
6667
std::string dataset, std::string gateway,
6768
std::uint16_t port, bool send_ts_out,
6869
VersionUpgradePolicy upgrade_policy,
6970
std::chrono::seconds heartbeat_interval)
70-
: impl_{new Impl{log_receiver, std::move(key), std::move(dataset),
71-
std::move(gateway), port, send_ts_out, upgrade_policy,
72-
heartbeat_interval}} {}
71+
: impl_{std::make_unique<Impl>(
72+
log_receiver, std::move(key), std::move(dataset), std::move(gateway),
73+
port, send_ts_out, upgrade_policy, heartbeat_interval)} {}
7374

7475
const std::string& LiveThreaded::Key() const { return impl_->blocking.Key(); }
7576

src/log.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
databento::ILogReceiver* databento::ILogReceiver::Default() {
77
static const std::unique_ptr<ILogReceiver> gDefaultLogger{
8-
new ConsoleLogReceiver{}};
8+
std::make_unique<ConsoleLogReceiver>()};
99
return gDefaultLogger.get();
1010
}
1111

src/metadata.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "databento/metadata.hpp"
22

3-
#include <sstream>
3+
#include <ostream>
44

55
#include "stream_op_helper.hpp"
66

0 commit comments

Comments
 (0)