Skip to content

Commit e1c2393

Browse files
xDimonturuslan
andauthored
Replace Microsoft.GSL by STL analogs (#221)
* refactor: remove GSL Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]> * fix: review issues Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]> * lexicographical_compare_three_way Signed-off-by: turuslan <[email protected]> * remove concept Signed-off-by: turuslan <[email protected]> * fix: review issues Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]> * fix: review issues Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]> * fix: review issue Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]> * fix: review issue Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]> * fix: review issue Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]> * fixup: incomplete rename Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]> * feature: deduction guideline for FinalAction Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]> --------- Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]> Signed-off-by: turuslan <[email protected]> Co-authored-by: turuslan <[email protected]>
1 parent ac2eec9 commit e1c2393

File tree

260 files changed

+1494
-1328
lines changed

Some content is hidden

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

260 files changed

+1494
-1328
lines changed

Diff for: .clang-tidy

-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ CheckOptions:
5858
value: '::free;::realloc;::freopen;::fclose'
5959
- key: cppcoreguidelines-owning-memory.LegacyResourceProducers
6060
value: '::malloc;::aligned_alloc;::realloc;::calloc;::fopen;::freopen;::tmpfile'
61-
- key: cppcoreguidelines-pro-bounds-constant-array-index.GslHeader
62-
value: ''
6361
- key: cppcoreguidelines-pro-bounds-constant-array-index.IncludeStyle
6462
value: google
6563
- key: cppcoreguidelines-pro-type-member-init.IgnoreArrays

Diff for: cmake/Hunter/init.cmake

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ set(
3131
include(${CMAKE_CURRENT_LIST_DIR}/HunterGate.cmake)
3232

3333
HunterGate(
34-
URL https://github.com/qdrvm/hunter/archive/refs/tags/v0.23.257-qdrvm6.tar.gz
35-
SHA1 4fb3b9a0257bc92e89f103d3e2c6768a38bef127
34+
URL https://github.com/qdrvm/hunter/archive/refs/tags/v0.23.257-qdrvm10.tar.gz
35+
SHA1 72b446a4424ba28ea90f9a68a9134b0f8e44b5b2
3636
LOCAL
3737
)

Diff for: cmake/dependencies.cmake

-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ endif()
1313
hunter_add_package(Boost COMPONENTS random filesystem program_options)
1414
find_package(Boost CONFIG REQUIRED random filesystem program_options)
1515

16-
# added from hunter_config
17-
hunter_add_package(Microsoft.GSL)
18-
1916
# https://www.openssl.org/
2017
hunter_add_package(OpenSSL)
2118
find_package(OpenSSL REQUIRED)

Diff for: example/00-install/CMakeLists.txt

-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ set(CMAKE_CXX_EXTENSIONS OFF)
1818
hunter_add_package(Boost)
1919
find_package(Boost CONFIG REQUIRED)
2020

21-
hunter_add_package(Microsoft.GSL)
22-
find_package(Microsoft.GSL CONFIG REQUIRED)
23-
2421
# look for libp2p in OS
2522
find_package(libp2p CONFIG REQUIRED)
2623

Diff for: example/01-echo/libp2p_echo_server.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <memory>
88
#include <string>
99

10-
#include <gsl/multi_span>
1110
#include <libp2p/common/literals.hpp>
1211
#include <libp2p/host/basic_host.hpp>
1312
#include <libp2p/injector/host_injector.hpp>
@@ -64,7 +63,7 @@ int main(int argc, char **argv) {
6463
using libp2p::common::operator""_unhex;
6564

6665
auto has_arg = [&](std::string_view arg) {
67-
auto args = gsl::make_span(argv, argc).subspan(1);
66+
auto args = std::span(argv, argc).subspan(1);
6867
return std::find(args.begin(), args.end(), arg) != args.end();
6968
};
7069

Diff for: example/02-kademlia/rendezvous_chat.cpp

+4-8
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ class Session : public std::enable_shared_from_this<Session> {
4242
}
4343

4444
stream_->readSome(
45-
gsl::span(incoming_->data(), static_cast<ssize_t>(incoming_->size())),
46-
incoming_->size(),
45+
*incoming_, incoming_->size(),
4746
[self = shared_from_this()](libp2p::outcome::result<size_t> result) {
4847
if (not result) {
4948
self->close();
@@ -53,8 +52,7 @@ class Session : public std::enable_shared_from_this<Session> {
5352
}
5453
std::cout << self->stream_->remotePeerId().value().toBase58() << " > "
5554
<< std::string(self->incoming_->begin(),
56-
self->incoming_->begin()
57-
+ static_cast<ssize_t>(result.value()));
55+
self->incoming_->begin() + result.value());
5856
std::cout.flush();
5957
self->read();
6058
});
@@ -68,8 +66,7 @@ class Session : public std::enable_shared_from_this<Session> {
6866
}
6967

7068
stream_->write(
71-
gsl::span(buffer->data(), static_cast<ssize_t>(buffer->size())),
72-
buffer->size(),
69+
*buffer, buffer->size(),
7370
[self = shared_from_this(),
7471
buffer](libp2p::outcome::result<size_t> result) {
7572
if (not result) {
@@ -80,8 +77,7 @@ class Session : public std::enable_shared_from_this<Session> {
8077
}
8178
std::cout << self->stream_->remotePeerId().value().toBase58() << " < "
8279
<< std::string(buffer->begin(),
83-
buffer->begin()
84-
+ static_cast<ssize_t>(result.value()));
80+
buffer->begin() + result.value());
8581
std::cout.flush();
8682
});
8783
return true;

Diff for: include/libp2p/basic/message_read_writer.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <memory>
1010
#include <vector>
1111

12-
#include <gsl/span>
12+
#include <span>
1313
#include <libp2p/basic/readwriter.hpp>
1414
#include <libp2p/outcome/outcome.hpp>
1515

@@ -39,7 +39,7 @@ namespace libp2p::basic {
3939
* @param cb is called when the message is written or an error happened.
4040
* Quantity of bytes written is passed as an argument in case of success
4141
*/
42-
virtual void write(gsl::span<const uint8_t> buffer,
42+
virtual void write(BytesIn buffer,
4343
Writer::WriteCallbackFunc cb) = 0;
4444
};
4545
} // namespace libp2p::basic

Diff for: include/libp2p/basic/message_read_writer_bigendian.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <memory>
1010
#include <vector>
1111

12-
#include <gsl/span>
12+
#include <span>
1313
#include <libp2p/basic/message_read_writer.hpp>
1414
#include <libp2p/basic/readwriter.hpp>
1515
#include <libp2p/outcome/outcome.hpp>
@@ -44,7 +44,7 @@ namespace libp2p::basic {
4444
* @param buffer - the message to be written
4545
* @param cb, which is called, when the message is read or error happens
4646
*/
47-
void write(gsl::span<const uint8_t> buffer,
47+
void write(BytesIn buffer,
4848
Writer::WriteCallbackFunc cb) override;
4949

5050
private:

Diff for: include/libp2p/basic/message_read_writer_uvarint.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <memory>
1010
#include <vector>
1111

12-
#include <gsl/span>
12+
#include <span>
1313
#include <libp2p/basic/message_read_writer.hpp>
1414
#include <libp2p/basic/readwriter.hpp>
1515
#include <libp2p/outcome/outcome.hpp>
@@ -40,7 +40,7 @@ namespace libp2p::basic {
4040
* @param buffer - the message to be written
4141
* @param cb, which is called, when the message is read or error happens
4242
*/
43-
void write(gsl::span<const uint8_t> buffer,
43+
void write(BytesIn buffer,
4444
Writer::WriteCallbackFunc cb) override;
4545

4646
private:

Diff for: include/libp2p/basic/read.hpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
#define LIBP2P_BASIC_READ_HPP
88

99
#include <libp2p/basic/reader.hpp>
10-
#include <libp2p/common/span_size.hpp>
1110
#include <memory>
1211

1312
namespace libp2p {
1413
/// Read exactly `out.size()` bytes
15-
inline void read(const std::shared_ptr<basic::Reader> &reader,
16-
gsl::span<uint8_t> out,
14+
inline void read(const std::shared_ptr<basic::Reader> &reader, BytesOut out,
1715
std::function<void(outcome::result<void>)> cb) {
1816
auto post_cb = [](decltype(reader) reader, decltype(cb) &&cb,
1917
outcome::result<size_t> r) {
@@ -31,7 +29,7 @@ namespace libp2p {
3129
}
3230
// read some bytes
3331
reader->readSome(
34-
out, spanSize(out),
32+
out, out.size(),
3533
[weak{std::weak_ptr{reader}}, out, cb{std::move(cb)},
3634
post_cb](outcome::result<size_t> n_res) mutable {
3735
auto reader = weak.lock();
@@ -45,10 +43,10 @@ namespace libp2p {
4543
if (n == 0) {
4644
throw std::logic_error{"libp2p::read zero bytes read"};
4745
}
48-
if (n > spanSize(out)) {
46+
if (n > out.size()) {
4947
throw std::logic_error{"libp2p::read too much bytes read"};
5048
}
51-
if (n == spanSize(out)) {
49+
if (n == out.size()) {
5250
// successfully read last bytes
5351
return post_cb(reader, std::move(cb), outcome::success());
5452
}

Diff for: include/libp2p/basic/read_buffer.hpp

+8-11
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010
#include <vector>
1111

1212
#include <boost/optional.hpp>
13-
#include <gsl/span>
13+
14+
#include <libp2p/common/types.hpp>
1415

1516
namespace libp2p::basic {
1617

1718
class ReadBuffer {
1819
public:
19-
using BytesRef = gsl::span<uint8_t>;
20-
2120
static constexpr size_t kDefaultAllocGranularity = 65536;
2221

2322
ReadBuffer(const ReadBuffer &) = delete;
@@ -38,13 +37,13 @@ namespace libp2p::basic {
3837
}
3938

4039
/// Adds new data to the buffer
41-
void add(BytesRef bytes);
40+
void add(BytesIn bytes);
4241

4342
/// Returns # of bytes actually copied into out
44-
size_t consume(BytesRef &out);
43+
size_t consume(BytesOut out);
4544

4645
/// Returns # of bytes actually copied into out
47-
size_t addAndConsume(BytesRef in, BytesRef &out);
46+
size_t addAndConsume(BytesIn in, BytesOut out);
4847

4948
/// Clears and deallocates
5049
void clear();
@@ -53,7 +52,7 @@ namespace libp2p::basic {
5352
using Fragment = std::vector<uint8_t>;
5453

5554
/// Consumes all data into out
56-
size_t consumeAll(BytesRef &out);
55+
size_t consumeAll(BytesOut out);
5756

5857
/// Consumes the 1st fragment or part of it
5958
size_t consumePart(uint8_t *out, size_t n);
@@ -78,8 +77,6 @@ namespace libp2p::basic {
7877
/// data up to expected size
7978
class FixedBufferCollector {
8079
public:
81-
using CBytesRef = gsl::span<const uint8_t>;
82-
using BytesRef = gsl::span<uint8_t>;
8380
using Buffer = std::vector<uint8_t>;
8481

8582
static constexpr size_t kDefaultMemoryThreshold = 65536;
@@ -96,8 +93,8 @@ namespace libp2p::basic {
9693
/// returns data if filled up to expected size or empty option if not,
9794
/// modifies data (cuts head)
9895
/// Data returned is valid until next expect() call && data is live
99-
boost::optional<CBytesRef> add(CBytesRef &data);
100-
boost::optional<BytesRef> add(BytesRef &data);
96+
boost::optional<BytesIn> add(BytesIn &data);
97+
boost::optional<BytesOut> add(BytesOut &data);
10198

10299
/// Resets to initial state
103100
void reset();

Diff for: include/libp2p/basic/read_return_size.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
namespace libp2p {
1212
/// Read exactly `out.size()` bytes
1313
inline void readReturnSize(const std::shared_ptr<basic::Reader> &reader,
14-
gsl::span<uint8_t> out,
14+
BytesOut out,
1515
basic::Reader::ReadCallbackFunc cb) {
1616
read(reader, out,
17-
[n{spanSize(out)}, cb{std::move(cb)}](outcome::result<void> r) {
17+
[n{out.size()}, cb{std::move(cb)}](outcome::result<void> r) {
1818
if (r.has_error()) {
1919
cb(r.error());
2020
} else {

Diff for: include/libp2p/basic/reader.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <functional>
1010
#include <vector>
1111

12-
#include <gsl/span>
12+
#include <libp2p/common/types.hpp>
1313
#include <libp2p/outcome/outcome.hpp>
1414

1515
namespace libp2p::basic {
@@ -32,7 +32,7 @@ namespace libp2p::basic {
3232
* pointer, or having buffer as part of some class/struct, and using
3333
* enable_shared_from_this()
3434
*/
35-
virtual void read(gsl::span<uint8_t> out, size_t bytes,
35+
virtual void read(BytesOut out, size_t bytes,
3636
ReadCallbackFunc cb) = 0;
3737

3838
/**
@@ -47,7 +47,7 @@ namespace libp2p::basic {
4747
* pointer, or having buffer as part of some class/struct, and using
4848
* enable_shared_from_this()
4949
*/
50-
virtual void readSome(gsl::span<uint8_t> out, size_t bytes,
50+
virtual void readSome(BytesOut out, size_t bytes,
5151
ReadCallbackFunc cb) = 0;
5252

5353
/**

Diff for: include/libp2p/basic/scheduler/scheduler_impl.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <vector>
1212

1313
#include <boost/optional.hpp>
14-
#include <gsl/span>
14+
#include <span>
1515

1616
#include <libp2p/basic/scheduler.hpp>
1717

@@ -137,11 +137,11 @@ namespace libp2p::basic {
137137
/// Processes non-cancellable current items, in absense of cancellable
138138
/// ones. Called from onTimer()
139139
static void processNonCancellableItems(
140-
gsl::span<Item> items, const std::shared_ptr<Scheduler> &owner);
140+
std::span<Item> items, const std::shared_ptr<Scheduler> &owner);
141141

142142
/// Processes cancellable current items, in absense of non-cancellable
143143
/// ones. Called from onTimer()
144-
void processCancellableItems(gsl::span<uint64_t> items,
144+
void processCancellableItems(std::span<uint64_t> items,
145145
const std::shared_ptr<Scheduler> &owner);
146146

147147
/// Processes both cancellable and non-cancellable items in proper order

Diff for: include/libp2p/basic/varint_prefix_reader.hpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
#ifndef LIBP2P_VARINT_PREFIX_READER_HPP
77
#define LIBP2P_VARINT_PREFIX_READER_HPP
88

9-
#include <gsl/span>
9+
#include <cstdint>
10+
#include <span>
11+
12+
#include <libp2p/common/types.hpp>
1013

1114
namespace libp2p::basic {
1215

@@ -50,7 +53,7 @@ namespace libp2p::basic {
5053
/// On success, modifies buffer (cuts off first bytes which were consumed),
5154
/// returns reader's state
5255
/// (or kError if called when state() == kReady)
53-
State consume(gsl::span<const uint8_t> &buffer);
56+
State consume(BytesIn &buffer);
5457

5558
private:
5659
/// Current value accumulated

Diff for: include/libp2p/basic/write_queue.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace libp2p::basic {
1515

1616
class WriteQueue {
1717
public:
18-
using DataRef = gsl::span<const uint8_t>;
18+
using DataRef = BytesIn;
1919

2020
static constexpr size_t kDefaultSizeLimit = 64 * 1024 * 1024;
2121

@@ -59,7 +59,7 @@ namespace libp2p::basic {
5959
/// Data item w/callback
6060
struct Data {
6161
// data reference
62-
gsl::span<const uint8_t> data;
62+
BytesIn data;
6363

6464
// allow to send large messages partially
6565
size_t acknowledged;

Diff for: include/libp2p/basic/writer.hpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
#include <functional>
1010

11-
#include <gsl/span>
12-
11+
#include <libp2p/common/types.hpp>
1312
#include <libp2p/outcome/outcome.hpp>
1413

1514
namespace libp2p::basic {
@@ -33,7 +32,7 @@ namespace libp2p::basic {
3332
* pointer, or having buffer as part of some class/struct, and using
3433
* enable_shared_from_this()
3534
*/
36-
virtual void write(gsl::span<const uint8_t> in, size_t bytes,
35+
virtual void write(BytesIn in, size_t bytes,
3736
WriteCallbackFunc cb) = 0;
3837

3938
/**
@@ -49,7 +48,7 @@ namespace libp2p::basic {
4948
* pointer, or having buffer as part of some class/struct, and using
5049
* enable_shared_from_this()
5150
*/
52-
virtual void writeSome(gsl::span<const uint8_t> in, size_t bytes,
51+
virtual void writeSome(BytesIn in, size_t bytes,
5352
WriteCallbackFunc cb) = 0;
5453

5554
/**

Diff for: include/libp2p/common/ambigous_size.hpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
#define LIBP2P_COMMON_AMBIGOUS_SIZE_HPP
88

99
#include <cstdint>
10-
#include <libp2p/common/span_size.hpp>
1110

1211
namespace libp2p {
1312
// TODO(turuslan): https://github.com/libp2p/cpp-libp2p/issues/203
1413
template <typename T>
15-
void ambigousSize(gsl::span<T> &s, size_t n) {
16-
if (n > spanSize(s)) {
14+
void ambigousSize(std::span<T> &s, size_t n) {
15+
if (n > s.size()) {
1716
throw std::logic_error{"libp2p::ambigousSize"};
1817
}
1918
s = s.first(n);

0 commit comments

Comments
 (0)