Skip to content

Issue #73 - Replace TAutoPtr to std::unique_ptr #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6669fb7
TAutoPtr -> std::unique_ptr in http.cpp
stanislav-shchetinin Jul 11, 2024
b71750b
TAutoPtr -> std::unique_ptr in utmain.cpp
stanislav-shchetinin Jul 11, 2024
b9f44b0
TAutoPtr -> std::unique_ptr in gtest.cpp
stanislav-shchetinin Jul 11, 2024
ed8dddb
TAutoPtr -> std::unique_ptr in lz.h
stanislav-shchetinin Jul 11, 2024
1161ed6
TAutoPtr -> std::unique_ptr in last_getopt_opt.h
stanislav-shchetinin Jul 11, 2024
aeb4153
TAutoPtr -> std::unique_ptr in equeue_ut.cpp
stanislav-shchetinin Jul 11, 2024
5eb6b2a
TAutoPtr -> std::unique_ptr in queue.h
stanislav-shchetinin Jul 11, 2024
bffd9d9
TAutoPtr -> std::unique_ptr in thread_helper.h
stanislav-shchetinin Jul 11, 2024
b4ca3a1
TAutoPtr -> std::unique_ptr in init.cpp
stanislav-shchetinin Jul 11, 2024
2798964
TAutoPtr -> std::unique_ptr in dumpers.h
stanislav-shchetinin Jul 11, 2024
e9abb35
TAutoPtr -> std::unique_ptr in ptr_ut.cpp
stanislav-shchetinin Jul 11, 2024
7dae8b6
The original comment in the code has been restored
stanislav-shchetinin Jul 12, 2024
252f8fb
Rollback ptr_ut.cpp to the previous state
stanislav-shchetinin Jul 12, 2024
05ff450
rvalue arg in CreatRequest
stanislav-shchetinin Jul 15, 2024
8662bd7
replaced new with make_unique
stanislav-shchetinin Jul 15, 2024
04d5b8d
replaced the copy with an rvalue ref
stanislav-shchetinin Jul 15, 2024
a00a93f
replaced the copy with an rvalue ref
stanislav-shchetinin Jul 15, 2024
d32b7ec
replaced reset with std::make_unique
stanislav-shchetinin Jul 15, 2024
cae748c
removed the change in ptr_ut.cpp
stanislav-shchetinin Jul 15, 2024
8f2228f
rvalue refs in function arguments
stanislav-shchetinin Jul 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/ydb-cpp-sdk/util/thread/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <ydb-cpp-sdk/util/generic/noncopyable.h>

#include <string>
#include <memory>

class TDuration;

Expand Down Expand Up @@ -386,4 +387,4 @@ inline void Delete(THolder<IThreadPool> q) {
* Creates and starts TThreadPool if threadsCount > 1, or TFakeThreadPool otherwise
* You could specify blocking and catching modes for TThreadPool only
*/
THolder<IThreadPool> CreateThreadPool(size_t threadCount, size_t queueSizeLimit = 0, const IThreadPool::TParams& params = {});
std::unique_ptr<IThreadPool> CreateThreadPool(size_t threadCount, size_t queueSizeLimit = 0, const IThreadPool::TParams& params = {});
7 changes: 4 additions & 3 deletions src/library/dbg_output/dumpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <list>
#include <map>
#include <memory>
#include <set>
#include <span>
#include <string>
Expand All @@ -15,10 +16,10 @@

//smart pointers
template <class T, class D>
struct TDumper<TAutoPtr<T, D>> {
struct TDumper<std::unique_ptr<T, D>> {
template <class S>
static inline void Dump(S& s, const TAutoPtr<T, D>& v) {
s << DumpRaw("TAutoPtr(") << v.Get() << DumpRaw(")");
static inline void Dump(S& s, const std::unique_ptr<T, D>& v) {
s << DumpRaw("std::unique_ptr(") << v.get() << DumpRaw(")");
}
};

Expand Down
9 changes: 5 additions & 4 deletions src/library/getopt/small/last_getopt_opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <utility>
#include <algorithm>
#include <functional>
#include <memory>

namespace NLastGetopt {
enum EHasArg {
Expand Down Expand Up @@ -607,13 +608,13 @@ namespace NLastGetopt {
return Handler1(f);
}

TOpt& Handler(TAutoPtr<IOptHandler> handler) {
return HandlerImpl(handler.Release());
TOpt& Handler(std::unique_ptr<IOptHandler>&& handler) {
return HandlerImpl(handler.release());
}

template <typename T> // T extends IOptHandler
TOpt& Handler(TAutoPtr<T> handler) {
return HandlerImpl(handler.Release());
TOpt& Handler(std::unique_ptr<T>&& handler) {
return HandlerImpl(handler.release());
}

// Stores FromString<T>(arg) in *target
Expand Down
27 changes: 14 additions & 13 deletions src/library/http/server/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <cerrno>
#include <cstring>
#include <memory>
#include <mutex>

using namespace NAddr;
Expand Down Expand Up @@ -169,10 +170,10 @@ class THttpServer::TImpl {
const THttpServerOptions& Options;
};

TAutoPtr<TClientRequest> CreateRequest(TAutoPtr<TClientConnection> c) {
THolder<TClientRequest> obj(Cb_->CreateClient());
std::unique_ptr<TClientRequest> CreateRequest(std::unique_ptr<TClientConnection>&& c) {
std::unique_ptr<TClientRequest> obj(Cb_->CreateClient());

obj->Conn_.Reset(c.Release());
obj->Conn_.Reset(c.release());

return obj;
}
Expand Down Expand Up @@ -287,9 +288,9 @@ class THttpServer::TImpl {
// ignore result
}

void AddRequest(TAutoPtr<TClientRequest> req, bool fail) {
void AddRequest(std::unique_ptr<TClientRequest>&& req, bool fail) {
struct TFailRequest: public THttpClientRequestEx {
inline TFailRequest(TAutoPtr<TClientRequest> parent) {
inline TFailRequest(std::unique_ptr<TClientRequest>&& parent) {
Conn_.Reset(parent->Conn_.Release());
HttpConn_.Reset(parent->HttpConn_.Release());
}
Expand All @@ -304,13 +305,13 @@ class THttpServer::TImpl {
}
};

if (!fail && Requests->Add(req.Get())) {
Y_UNUSED(req.Release());
if (!fail && Requests->Add(req.get())) {
Y_UNUSED(req.release());
} else {
req = new TFailRequest(req);
req = std::make_unique<TFailRequest>(std::move(req));

if (FailRequests->Add(req.Get())) {
Y_UNUSED(req.Release());
if (FailRequests->Add(req.get())) {
Y_UNUSED(req.release());
} else {
Cb_->OnFailRequest(-1);
}
Expand Down Expand Up @@ -620,7 +621,7 @@ void TClientConnection::ScheduleDelete() {
}

void TClientConnection::OnPollEvent(TInstant now) {
THolder<TClientConnection> this_(this);
std::unique_ptr<TClientConnection> this_(this);
Activate(now);

{
Expand All @@ -637,10 +638,10 @@ void TClientConnection::OnPollEvent(TInstant now) {
}
}

THolder<TClientRequest> obj(HttpServ_->CreateRequest(this_));
std::unique_ptr<TClientRequest> obj = HttpServ_->CreateRequest(std::move(this_));
AcceptMoment = now;

HttpServ_->AddRequest(obj, Reject_);
HttpServ_->AddRequest(std::move(obj), Reject_);
}

void TClientConnection::Activate(TInstant now) noexcept {
Expand Down
5 changes: 3 additions & 2 deletions src/library/openssl/init/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <openssl/conf.h>
#include <openssl/crypto.h>
#include <mutex>
#include <memory>
#include <vector>

namespace {
Expand All @@ -27,7 +28,7 @@ namespace {
: Mutexes(CRYPTO_num_locks())
{
for (auto& mpref : Mutexes) {
mpref.Reset(new std::mutex());
mpref = std::make_unique<std::mutex>();
}
}

Expand All @@ -41,7 +42,7 @@ namespace {
}
}

std::vector<TAutoPtr<std::mutex>> Mutexes;
std::vector<std::unique_ptr<std::mutex>> Mutexes;
};

inline TInitSsl() {
Expand Down
14 changes: 8 additions & 6 deletions src/library/streams/lz/lz.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <src/library/streams/lz/lz4/lz4.h>
#include <src/library/streams/lz/snappy/snappy.h>

#include <memory>

/**
* @file
*
Expand Down Expand Up @@ -159,10 +161,10 @@ class TLzqDecompress: public IInputStream {
* @param input Stream to decompress.
* @return Decompressing proxy input stream.
*/
TAutoPtr<IInputStream> OpenLzDecompressor(IInputStream* input);
TAutoPtr<IInputStream> TryOpenLzDecompressor(IInputStream* input);
TAutoPtr<IInputStream> TryOpenLzDecompressor(const std::string_view& signature, IInputStream* input);
std::unique_ptr<IInputStream> OpenLzDecompressor(IInputStream* input);
std::unique_ptr<IInputStream> TryOpenLzDecompressor(IInputStream* input);
std::unique_ptr<IInputStream> TryOpenLzDecompressor(const std::string_view& signature, IInputStream* input);

TAutoPtr<IInputStream> OpenOwnedLzDecompressor(TAutoPtr<IInputStream> input);
TAutoPtr<IInputStream> TryOpenOwnedLzDecompressor(TAutoPtr<IInputStream> input);
TAutoPtr<IInputStream> TryOpenOwnedLzDecompressor(const std::string_view& signature, TAutoPtr<IInputStream> input);
std::unique_ptr<IInputStream> OpenOwnedLzDecompressor(std::unique_ptr<IInputStream>&& input);
std::unique_ptr<IInputStream> TryOpenOwnedLzDecompressor(std::unique_ptr<IInputStream>&& input);
std::unique_ptr<IInputStream> TryOpenOwnedLzDecompressor(const std::string_view& signature, std::unique_ptr<IInputStream>&& input);
8 changes: 5 additions & 3 deletions src/library/testing/unittest/gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <ydb-cpp-sdk/util/system/type_name.h>

#include <memory>

using namespace NUnitTest;
using namespace NUnitTest::NPrivate;

Expand Down Expand Up @@ -53,13 +55,13 @@ namespace {
}

IGTestFactory* NUnitTest::NPrivate::ByName(const char* name) {
static std::map<std::string_view, TAutoPtr<TGTestFactory>> tests;
static std::map<std::string_view, std::unique_ptr<TGTestFactory>> tests;

auto& ret = tests[name];

if (!ret) {
ret = new TGTestFactory(name);
ret = std::make_unique<TGTestFactory>(name);
}

return ret.Get();
return ret.get();
}
6 changes: 3 additions & 3 deletions src/library/testing/unittest/utmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ class TTraceWriterProcessor: public ITestSuiteProcessor {
inline TTraceWriterProcessor(const char* traceFilePath, EOpenMode mode)
: PrevTime(TInstant::Now())
{
TraceFile = new TUnbufferedFileOutput(TFile(traceFilePath, mode | WrOnly | Seq));
TraceFile = std::make_unique<TUnbufferedFileOutput>(TFile(traceFilePath, mode | WrOnly | Seq));
}

private:
TAutoPtr<TUnbufferedFileOutput> TraceFile;
std::unique_ptr<TUnbufferedFileOutput> TraceFile;
std::string TraceFilePath;
TInstant PrevTime;
std::vector<std::string> ErrorMessages;
Expand All @@ -148,7 +148,7 @@ class TTraceWriterProcessor: public ITestSuiteProcessor {

json.EndObject();

json.FlushTo(TraceFile.Get());
json.FlushTo(TraceFile.get());
*TraceFile << "\n";
}

Expand Down
9 changes: 5 additions & 4 deletions src/library/threading/chunk_queue/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <ydb-cpp-sdk/util/system/yassert.h>

#include <mutex>
#include <memory>
#include <type_traits>
#include <utility>

Expand Down Expand Up @@ -522,21 +523,21 @@ namespace NThreading {
TImpl Impl;

public:
using TItem = TAutoPtr<T>;
using TItem = std::unique_ptr<T>;

~TAutoQueueBase() {
TAutoPtr<T> value;
std::unique_ptr<T> value;
while (Dequeue(value)) {
// do nothing
}
}

void Enqueue(TAutoPtr<T> value) {
void Enqueue(std::unique_ptr<T>&& value) {
Impl.Enqueue(value.Get());
Y_UNUSED(value.Release());
}

bool Dequeue(TAutoPtr<T>& value) {
bool Dequeue(std::unique_ptr<T>&& value) {
T* ptr = nullptr;
if (Impl.Dequeue(ptr)) {
value.Reset(ptr);
Expand Down
3 changes: 2 additions & 1 deletion src/library/threading/equeue/equeue_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <ydb-cpp-sdk/util/system/event.h>
#include <ydb-cpp-sdk/util/datetime/base.h>

#include <memory>

Y_UNIT_TEST_SUITE(TElasticQueueTest) {
const size_t MaxQueueSize = 20;
Expand Down Expand Up @@ -139,7 +140,7 @@ Y_UNIT_TEST_SUITE(TElasticQueueTest) {
{
typename TEnv<T>::TQueueSetup setup;

std::vector< TAutoPtr<IThreadFactory::IThread> > senders;
std::vector< std::unique_ptr<IThreadFactory::IThread> > senders;
for (size_t i = 0; i < ThreadCount; ++i) {
senders.push_back(::SystemThreadFactory()->Run(&sender));
}
Expand Down
4 changes: 2 additions & 2 deletions src/library/threading/poor_man_openmp/thread_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TMtpQueueHelper {
SetThreadCount(NSystemInfo::CachedNumberOfCpus());
}
IThreadPool* Get() {
return q.Get();
return q.get();
}
size_t GetThreadCount() {
return ThreadCount;
Expand All @@ -32,7 +32,7 @@ class TMtpQueueHelper {

private:
size_t ThreadCount;
TAutoPtr<IThreadPool> q;
std::unique_ptr<IThreadPool> q;
};

namespace NYmp {
Expand Down
8 changes: 4 additions & 4 deletions src/util/thread/pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -767,12 +767,12 @@ IThread* IThreadPool::DoCreate() {
return new TPoolThread(this);
}

THolder<IThreadPool> CreateThreadPool(size_t threadsCount, size_t queueSizeLimit, const TThreadPoolParams& params) {
THolder<IThreadPool> queue;
std::unique_ptr<IThreadPool> CreateThreadPool(size_t threadsCount, size_t queueSizeLimit, const TThreadPoolParams& params) {
std::unique_ptr<IThreadPool> queue;
if (threadsCount > 1) {
queue.Reset(new TThreadPool(params));
queue = std::make_unique<TThreadPool>(params);
} else {
queue.Reset(new TFakeThreadPool());
queue = std::make_unique<TFakeThreadPool>();
}
queue->Start(threadsCount, queueSizeLimit);
return queue;
Expand Down
Loading