diff options
author | ishfb <ishfb@yandex-team.ru> | 2022-02-10 16:48:08 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:48:08 +0300 |
commit | 069c69f58fd78de3cf2e6eb2304047281e21fb65 (patch) | |
tree | b222e5ac2e2e98872661c51ccceee5da0d291e13 | |
parent | 0170772a2dbf133f32e17ca137ff64790d43831f (diff) | |
download | ydb-069c69f58fd78de3cf2e6eb2304047281e21fb65.tar.gz |
Restoring authorship annotation for <ishfb@yandex-team.ru>. Commit 2 of 2.
35 files changed, 721 insertions, 721 deletions
diff --git a/contrib/libs/pire/pire/encoding.cpp b/contrib/libs/pire/pire/encoding.cpp index 3274dc0747..842e2b534d 100644 --- a/contrib/libs/pire/pire/encoding.cpp +++ b/contrib/libs/pire/pire/encoding.cpp @@ -118,17 +118,17 @@ namespace { namespace Encodings { const Encoding& Utf8() - { - static const Pire::Utf8 utf8; + { + static const Pire::Utf8 utf8; return utf8; - } + } const Encoding& Latin1() - { + { static const Pire::Latin1 latin1; return latin1; - } - + } + } } diff --git a/contrib/libs/pire/pire/platform.h b/contrib/libs/pire/pire/platform.h index 1296a3d1e9..54ded6b387 100644 --- a/contrib/libs/pire/pire/platform.h +++ b/contrib/libs/pire/pire/platform.h @@ -42,22 +42,22 @@ #define PIRE_HOT_FUNCTION #endif -#ifndef PIRE_LIKELY -#ifdef __GNUC__ -#define PIRE_LIKELY(x) (__builtin_expect((x), 1)) -#else -#define PIRE_LIKELY(x) (x) -#endif -#endif - -#ifndef PIRE_UNLIKELY -#ifdef __GNUC__ -#define PIRE_UNLIKELY(x) (__builtin_expect((x), 0)) -#else -#define PIRE_UNLIKELY(x) (x) -#endif -#endif - +#ifndef PIRE_LIKELY +#ifdef __GNUC__ +#define PIRE_LIKELY(x) (__builtin_expect((x), 1)) +#else +#define PIRE_LIKELY(x) (x) +#endif +#endif + +#ifndef PIRE_UNLIKELY +#ifdef __GNUC__ +#define PIRE_UNLIKELY(x) (__builtin_expect((x), 0)) +#else +#define PIRE_UNLIKELY(x) (x) +#endif +#endif + #ifdef _MSC_VER #include <stdio.h> #include <stdarg.h> diff --git a/contrib/libs/pire/pire/run.h b/contrib/libs/pire/pire/run.h index 264cf1320f..f6e1ff734d 100644 --- a/contrib/libs/pire/pire/run.h +++ b/contrib/libs/pire/pire/run.h @@ -139,7 +139,7 @@ namespace Impl { if (PIRE_UNLIKELY(size == 0)) return Continue; - + size_t chunk = Impl::ToLittleEndian(*p) >> 8*pos; const char* ptr = (const char*) p + pos + size + 1; diff --git a/library/cpp/cgiparam/cgiparam.h b/library/cpp/cgiparam/cgiparam.h index e25eb6e42c..87d1ab0ad4 100644 --- a/library/cpp/cgiparam/cgiparam.h +++ b/library/cpp/cgiparam/cgiparam.h @@ -46,11 +46,11 @@ public: void ScanAddAllUnescaped(const TStringBuf cgiParStr); void ScanAddAll(const TStringBuf cgiParStr); - /// Returns the string representation of all the stored parameters - /** - * @note The returned string has format <name1>=<value1>&<name2>=<value2>&... - * @note Names and values in the returned string are CGI-escaped. - */ + /// Returns the string representation of all the stored parameters + /** + * @note The returned string has format <name1>=<value1>&<name2>=<value2>&... + * @note Names and values in the returned string are CGI-escaped. + */ TString Print() const; char* Print(char* res) const; @@ -78,10 +78,10 @@ public: const auto pair = equal_range(name); return pair.first != pair.second; } - /// Returns value by name - /** - * @note The returned value is CGI-unescaped. - */ + /// Returns value by name + /** + * @note The returned value is CGI-unescaped. + */ Y_PURE_FUNCTION const TString& Get(const TStringBuf name, size_t numOfValue = 0) const noexcept; diff --git a/library/cpp/http/server/http.cpp b/library/cpp/http/server/http.cpp index f1d0fcc3a1..128583bdd7 100644 --- a/library/cpp/http/server/http.cpp +++ b/library/cpp/http/server/http.cpp @@ -771,7 +771,7 @@ THttpServer* TClientRequest::HttpServ() const noexcept { const TSocket& TClientRequest::Socket() const noexcept { return Conn_->Socket_; } - + NAddr::IRemoteAddrRef TClientRequest::GetListenerSockAddrRef() const noexcept { return Conn_->ListenerSockAddrRef_; } @@ -780,21 +780,21 @@ TInstant TClientRequest::AcceptMoment() const noexcept { return Conn_->AcceptMoment; } -/* - * TRequestReplier - */ -TRequestReplier::TRequestReplier() { -} - -TRequestReplier::~TRequestReplier() { -} - -bool TRequestReplier::Reply(void* threadSpecificResource) { - const TReplyParams params = { +/* + * TRequestReplier + */ +TRequestReplier::TRequestReplier() { +} + +TRequestReplier::~TRequestReplier() { +} + +bool TRequestReplier::Reply(void* threadSpecificResource) { + const TReplyParams params = { threadSpecificResource, Input(), Output()}; - return DoReply(params); -} + return DoReply(params); +} bool TryToBindAddresses(const THttpServerOptions& options, const std::function<void(TSocket)>* callbackOnBoundAddress) { THttpServerOptions::TBindAddresses addrs; diff --git a/library/cpp/http/server/http.h b/library/cpp/http/server/http.h index 2993415bfe..b292d38f27 100644 --- a/library/cpp/http/server/http.h +++ b/library/cpp/http/server/http.h @@ -99,9 +99,9 @@ private: THolder<TImpl> Impl_; }; -/** - * @deprecated Use TRequestReplier instead - */ +/** + * @deprecated Use TRequestReplier instead + */ class TClientRequest: public IObjectInQueue { friend class THttpServer::TImpl; @@ -149,28 +149,28 @@ private: }; class TRequestReplier: public TClientRequest { -public: - TRequestReplier(); +public: + TRequestReplier(); ~TRequestReplier() override; - - struct TReplyParams { - void* ThreadSpecificResource; - THttpInput& Input; - THttpOutput& Output; - }; - - /* - * Processes the request after 'connection' been created and 'Headers' been read - * Returns 'false' if the processing must be continued by the next handler, - * 'true' otherwise ('this' will be deleted) - */ - virtual bool DoReply(const TReplyParams& params) = 0; - -private: + + struct TReplyParams { + void* ThreadSpecificResource; + THttpInput& Input; + THttpOutput& Output; + }; + + /* + * Processes the request after 'connection' been created and 'Headers' been read + * Returns 'false' if the processing must be continued by the next handler, + * 'true' otherwise ('this' will be deleted) + */ + virtual bool DoReply(const TReplyParams& params) = 0; + +private: bool Reply(void* threadSpecificResource) final; - - using TClientRequest::Input; - using TClientRequest::Output; -}; + + using TClientRequest::Input; + using TClientRequest::Output; +}; bool TryToBindAddresses(const THttpServerOptions& options, const std::function<void(TSocket)>* callbackOnBoundAddress = nullptr); diff --git a/library/cpp/http/server/response.cpp b/library/cpp/http/server/response.cpp index 52d5f28e5b..52d64c91ce 100644 --- a/library/cpp/http/server/response.cpp +++ b/library/cpp/http/server/response.cpp @@ -1,65 +1,65 @@ -#include "response.h" +#include "response.h" -#include <util/stream/output.h> +#include <util/stream/output.h> #include <util/stream/mem.h> #include <util/string/cast.h> - -THttpResponse& THttpResponse::AddMultipleHeaders(const THttpHeaders& headers) { - for (THttpHeaders::TConstIterator i = headers.Begin(); i != headers.End(); ++i) { - this->Headers.AddHeader(*i); - } - return *this; -} - + +THttpResponse& THttpResponse::AddMultipleHeaders(const THttpHeaders& headers) { + for (THttpHeaders::TConstIterator i = headers.Begin(); i != headers.End(); ++i) { + this->Headers.AddHeader(*i); + } + return *this; +} + THttpResponse& THttpResponse::SetContentType(const TStringBuf& contentType) { Headers.AddOrReplaceHeader(THttpInputHeader("Content-Type", ToString(contentType))); return *this; -} - +} + void THttpResponse::OutTo(IOutputStream& os) const { TVector<IOutputStream::TPart> parts; - const size_t FIRST_LINE_PARTS = 3; + const size_t FIRST_LINE_PARTS = 3; const size_t HEADERS_PARTS = Headers.Count() * 4; - const size_t CONTENT_PARTS = 5; - parts.reserve(FIRST_LINE_PARTS + HEADERS_PARTS + CONTENT_PARTS); - - // first line + const size_t CONTENT_PARTS = 5; + parts.reserve(FIRST_LINE_PARTS + HEADERS_PARTS + CONTENT_PARTS); + + // first line parts.push_back(IOutputStream::TPart(TStringBuf("HTTP/1.1 "))); parts.push_back(IOutputStream::TPart(HttpCodeStrEx(Code))); parts.push_back(IOutputStream::TPart::CrLf()); - - // headers + + // headers for (THttpHeaders::TConstIterator i = Headers.Begin(); i != Headers.End(); ++i) { parts.push_back(IOutputStream::TPart(i->Name())); parts.push_back(IOutputStream::TPart(TStringBuf(": "))); parts.push_back(IOutputStream::TPart(i->Value())); parts.push_back(IOutputStream::TPart::CrLf()); - } - + } + char buf[50]; if (!Content.empty()) { TMemoryOutput mo(buf, sizeof(buf)); - + mo << Content.size(); parts.push_back(IOutputStream::TPart(TStringBuf("Content-Length: "))); parts.push_back(IOutputStream::TPart(buf, mo.Buf() - buf)); parts.push_back(IOutputStream::TPart::CrLf()); - } - - // content + } + + // content parts.push_back(IOutputStream::TPart::CrLf()); if (!Content.empty()) { parts.push_back(IOutputStream::TPart(Content)); - } - + } + os.Write(parts.data(), parts.size()); } template <> void Out<THttpResponse>(IOutputStream& os, const THttpResponse& resp) { resp.OutTo(os); -} +} diff --git a/library/cpp/http/server/response.h b/library/cpp/http/server/response.h index 20751e9096..a75cb85605 100644 --- a/library/cpp/http/server/response.h +++ b/library/cpp/http/server/response.h @@ -1,69 +1,69 @@ -#pragma once +#pragma once #include <library/cpp/http/misc/httpcodes.h> #include <library/cpp/http/io/stream.h> -#include <util/generic/strbuf.h> -#include <util/string/cast.h> - -class THttpHeaders; +#include <util/generic/strbuf.h> +#include <util/string/cast.h> + +class THttpHeaders; class IOutputStream; - -class THttpResponse { -public: + +class THttpResponse { +public: THttpResponse() noexcept : Code(HTTP_OK) { } explicit THttpResponse(HttpCodes code) noexcept - : Code(code) - { - } - + : Code(code) + { + } + template <typename ValueType> THttpResponse& AddHeader(const TString& name, const ValueType& value) { - return AddHeader(THttpInputHeader(name, ToString(value))); - } - - THttpResponse& AddHeader(const THttpInputHeader& header) { - Headers.AddHeader(header); - - return *this; - } - - THttpResponse& AddMultipleHeaders(const THttpHeaders& headers); - + return AddHeader(THttpInputHeader(name, ToString(value))); + } + + THttpResponse& AddHeader(const THttpInputHeader& header) { + Headers.AddHeader(header); + + return *this; + } + + THttpResponse& AddMultipleHeaders(const THttpHeaders& headers); + const THttpHeaders& GetHeaders() const { return Headers; } THttpResponse& SetContentType(const TStringBuf& contentType); - /** - * @note If @arg content isn't empty its size is automatically added as a + /** + * @note If @arg content isn't empty its size is automatically added as a * "Content-Length" header during output to IOutputStream. * @see IOutputStream& operator << (IOutputStream&, const THttpResponse&) - */ + */ THttpResponse& SetContent(const TString& content) { - Content = content; + Content = content; + + return *this; + } - return *this; - } - TString GetContent() const { return Content; } - /** - * @note If @arg content isn't empty its size is automatically added as a + /** + * @note If @arg content isn't empty its size is automatically added as a * "Content-Length" header during output to IOutputStream. * @see IOutputStream& operator << (IOutputStream&, const THttpResponse&) - */ + */ THttpResponse& SetContent(const TString& content, const TStringBuf& contentType) { return SetContent(content).SetContentType(contentType); } - + HttpCodes HttpCode() const { return Code; } @@ -75,8 +75,8 @@ public: void OutTo(IOutputStream& out) const; -private: - HttpCodes Code; - THttpHeaders Headers; +private: + HttpCodes Code; + THttpHeaders Headers; TString Content; -}; +}; diff --git a/library/cpp/http/server/response_ut.cpp b/library/cpp/http/server/response_ut.cpp index 6f2914d612..73e2112ad3 100644 --- a/library/cpp/http/server/response_ut.cpp +++ b/library/cpp/http/server/response_ut.cpp @@ -1,51 +1,51 @@ -#include "response.h" - +#include "response.h" + #include <library/cpp/testing/unittest/registar.h> - + #include <util/string/cast.h> Y_UNIT_TEST_SUITE(TestHttpResponse) { Y_UNIT_TEST(TestCodeOnly) { UNIT_ASSERT_STRINGS_EQUAL(ToString(THttpResponse()), "HTTP/1.1 200 Ok\r\n\r\n"); - UNIT_ASSERT_STRINGS_EQUAL(ToString(THttpResponse(HTTP_NOT_FOUND)), "HTTP/1.1 404 Not found\r\n\r\n"); - } - + UNIT_ASSERT_STRINGS_EQUAL(ToString(THttpResponse(HTTP_NOT_FOUND)), "HTTP/1.1 404 Not found\r\n\r\n"); + } + Y_UNIT_TEST(TestRedirect) { - THttpResponse resp = THttpResponse(HTTP_FOUND).AddHeader("Location", "yandex.ru"); - UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), "HTTP/1.1 302 Moved temporarily\r\n" - "Location: yandex.ru\r\n" - "\r\n"); - } - + THttpResponse resp = THttpResponse(HTTP_FOUND).AddHeader("Location", "yandex.ru"); + UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), "HTTP/1.1 302 Moved temporarily\r\n" + "Location: yandex.ru\r\n" + "\r\n"); + } + Y_UNIT_TEST(TestAddHeader) { - THttpResponse resp(HTTP_FORBIDDEN); - resp.AddHeader(THttpInputHeader("X-Header-1", "ValueOne")); - resp.AddHeader("X-Header-2", 10); - resp.AddHeader("X-Header-3", true); - - const char* EXPECTED = "HTTP/1.1 403 Forbidden\r\n" - "X-Header-1: ValueOne\r\n" - "X-Header-2: 10\r\n" - "X-Header-3: 1\r\n" - "\r\n"; - UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), EXPECTED); - } - + THttpResponse resp(HTTP_FORBIDDEN); + resp.AddHeader(THttpInputHeader("X-Header-1", "ValueOne")); + resp.AddHeader("X-Header-2", 10); + resp.AddHeader("X-Header-3", true); + + const char* EXPECTED = "HTTP/1.1 403 Forbidden\r\n" + "X-Header-1: ValueOne\r\n" + "X-Header-2: 10\r\n" + "X-Header-3: 1\r\n" + "\r\n"; + UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), EXPECTED); + } + Y_UNIT_TEST(TestAddMultipleHeaders) { - THttpHeaders headers; - headers.AddHeader(THttpInputHeader("X-Header-1", "ValueOne")); - headers.AddHeader(THttpInputHeader("X-Header-2", "ValueTwo")); - headers.AddHeader(THttpInputHeader("X-Header-3", "ValueThree")); - - const char* EXPECTED = "HTTP/1.1 403 Forbidden\r\n" - "X-Header-1: ValueOne\r\n" - "X-Header-2: ValueTwo\r\n" - "X-Header-3: ValueThree\r\n" - "\r\n"; - UNIT_ASSERT_STRINGS_EQUAL(ToString(THttpResponse(HTTP_FORBIDDEN).AddMultipleHeaders(headers)), - EXPECTED); - } - + THttpHeaders headers; + headers.AddHeader(THttpInputHeader("X-Header-1", "ValueOne")); + headers.AddHeader(THttpInputHeader("X-Header-2", "ValueTwo")); + headers.AddHeader(THttpInputHeader("X-Header-3", "ValueThree")); + + const char* EXPECTED = "HTTP/1.1 403 Forbidden\r\n" + "X-Header-1: ValueOne\r\n" + "X-Header-2: ValueTwo\r\n" + "X-Header-3: ValueThree\r\n" + "\r\n"; + UNIT_ASSERT_STRINGS_EQUAL(ToString(THttpResponse(HTTP_FORBIDDEN).AddMultipleHeaders(headers)), + EXPECTED); + } + Y_UNIT_TEST(TestGetHeaders) { THttpResponse resp(HTTP_FORBIDDEN); @@ -66,66 +66,66 @@ Y_UNIT_TEST_SUITE(TestHttpResponse) { Y_UNIT_TEST(TestSetContent) { - const char* EXPECTED = "HTTP/1.1 200 Ok\r\n" - "Content-Length: 10\r\n" - "\r\n" - "0123456789"; + const char* EXPECTED = "HTTP/1.1 200 Ok\r\n" + "Content-Length: 10\r\n" + "\r\n" + "0123456789"; UNIT_ASSERT_STRINGS_EQUAL(ToString(THttpResponse().SetContent("0123456789")), - EXPECTED); - } - + EXPECTED); + } + Y_UNIT_TEST(TestSetContentWithContentType) { - const char* EXPECTED = "HTTP/1.1 200 Ok\r\n" - "Content-Type: text/xml\r\n" - "Content-Length: 28\r\n" - "\r\n" - "<xml><tag value=\"1\" /></xml>"; + const char* EXPECTED = "HTTP/1.1 200 Ok\r\n" + "Content-Type: text/xml\r\n" + "Content-Length: 28\r\n" + "\r\n" + "<xml><tag value=\"1\" /></xml>"; THttpResponse resp; resp.SetContent("<xml><tag value=\"1\" /></xml>").SetContentType("text/xml"); - UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), EXPECTED); - } - + UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), EXPECTED); + } + Y_UNIT_TEST(TestCopyConstructor) { - THttpResponse resp(HTTP_FORBIDDEN); - resp.AddHeader(THttpInputHeader("X-Header-1", "ValueOne")) - .AddHeader("X-Header-2", "ValueTwo") - .AddHeader(THttpInputHeader("X-Header-3", "ValueThree")) + THttpResponse resp(HTTP_FORBIDDEN); + resp.AddHeader(THttpInputHeader("X-Header-1", "ValueOne")) + .AddHeader("X-Header-2", "ValueTwo") + .AddHeader(THttpInputHeader("X-Header-3", "ValueThree")) .SetContent("Some stuff") .SetContentType("text/plain"); - - THttpResponse copy = resp; - UNIT_ASSERT_STRINGS_EQUAL(ToString(copy), ToString(resp)); - } - + + THttpResponse copy = resp; + UNIT_ASSERT_STRINGS_EQUAL(ToString(copy), ToString(resp)); + } + Y_UNIT_TEST(TestAssignment) { - THttpResponse resp(HTTP_FORBIDDEN); - resp.AddHeader(THttpInputHeader("X-Header-1", "ValueOne")); - resp.AddHeader(THttpInputHeader("X-Header-2", "ValueTwo")); - resp.AddHeader(THttpInputHeader("X-Header-3", "ValueThree")); + THttpResponse resp(HTTP_FORBIDDEN); + resp.AddHeader(THttpInputHeader("X-Header-1", "ValueOne")); + resp.AddHeader(THttpInputHeader("X-Header-2", "ValueTwo")); + resp.AddHeader(THttpInputHeader("X-Header-3", "ValueThree")); resp.SetContent("Some stuff").SetContentType("text/plain"); - + THttpResponse copy; - copy = resp; - UNIT_ASSERT_STRINGS_EQUAL(ToString(copy), ToString(resp)); - } - + copy = resp; + UNIT_ASSERT_STRINGS_EQUAL(ToString(copy), ToString(resp)); + } + Y_UNIT_TEST(TestEmptyContent) { UNIT_ASSERT_STRINGS_EQUAL(ToString(THttpResponse().SetContent("")), "HTTP/1.1 200 Ok\r\n\r\n"); - } - + } + Y_UNIT_TEST(TestReturnReference) { THttpResponse resp; - UNIT_ASSERT_EQUAL(&resp, &resp.AddHeader("Header1", 1)); - UNIT_ASSERT_EQUAL(&resp, &resp.AddHeader(THttpInputHeader("Header2", "2"))); - - THttpHeaders headers; - headers.AddHeader(THttpInputHeader("Header3", "3")); - headers.AddHeader(THttpInputHeader("Header4", "4")); - UNIT_ASSERT_EQUAL(&resp, &resp.AddMultipleHeaders(headers)); - - UNIT_ASSERT_EQUAL(&resp, &resp.SetContent("some stuff")); + UNIT_ASSERT_EQUAL(&resp, &resp.AddHeader("Header1", 1)); + UNIT_ASSERT_EQUAL(&resp, &resp.AddHeader(THttpInputHeader("Header2", "2"))); + + THttpHeaders headers; + headers.AddHeader(THttpInputHeader("Header3", "3")); + headers.AddHeader(THttpInputHeader("Header4", "4")); + UNIT_ASSERT_EQUAL(&resp, &resp.AddMultipleHeaders(headers)); + + UNIT_ASSERT_EQUAL(&resp, &resp.SetContent("some stuff")); UNIT_ASSERT_EQUAL(&resp, &resp.SetContent("some other stuff").SetContentType("text/plain")); - } + } Y_UNIT_TEST(TestSetContentType) { const char* EXPECTED = "HTTP/1.1 200 Ok\r\n" @@ -139,4 +139,4 @@ Y_UNIT_TEST_SUITE(TestHttpResponse) { .SetContentType("text/xml"); UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), EXPECTED); } -} +} diff --git a/library/cpp/http/server/ya.make b/library/cpp/http/server/ya.make index d1821c0878..bae6f33306 100644 --- a/library/cpp/http/server/ya.make +++ b/library/cpp/http/server/ya.make @@ -13,7 +13,7 @@ SRCS( http.cpp http_ex.cpp options.cpp - response.cpp + response.cpp ) PEERDIR( diff --git a/library/cpp/lwtrace/mon/analytics/ya.make b/library/cpp/lwtrace/mon/analytics/ya.make index 643e499923..c18e23c8e1 100644 --- a/library/cpp/lwtrace/mon/analytics/ya.make +++ b/library/cpp/lwtrace/mon/analytics/ya.make @@ -2,9 +2,9 @@ LIBRARY() OWNER(serxa g:kikimr) -PEERDIR( -) - +PEERDIR( +) + SRCS( analytics.cpp ) diff --git a/library/cpp/terminate_handler/sample/ya.make b/library/cpp/terminate_handler/sample/ya.make index cae72d1bfd..af089abc65 100644 --- a/library/cpp/terminate_handler/sample/ya.make +++ b/library/cpp/terminate_handler/sample/ya.make @@ -1,6 +1,6 @@ RECURSE( exception pure-virtual - rethrow - segv + rethrow + segv ) diff --git a/library/cpp/threading/future/async.cpp b/library/cpp/threading/future/async.cpp index 6a0564d1cb..ad9b21a2cf 100644 --- a/library/cpp/threading/future/async.cpp +++ b/library/cpp/threading/future/async.cpp @@ -1 +1 @@ -#include "async.h" +#include "async.h" diff --git a/library/cpp/threading/future/async.h b/library/cpp/threading/future/async.h index b2952a0548..8543fdd5c6 100644 --- a/library/cpp/threading/future/async.h +++ b/library/cpp/threading/future/async.h @@ -1,22 +1,22 @@ -#pragma once - -#include "future.h" - +#pragma once + +#include "future.h" + #include <util/generic/function.h> #include <util/thread/pool.h> - -namespace NThreading { + +namespace NThreading { /** - * @brief Asynchronously executes @arg func in @arg queue returning a future for the result. - * - * @arg func should be a callable object with signature T(). - * @arg queue where @arg will be executed - * @returns For @arg func with signature T() the function returns TFuture<T> unless T is TFuture<U>. - * In this case the function returns TFuture<U>. - * - * If you want to use another queue for execution just write an overload, @see ExtensionExample - * unittest. - */ + * @brief Asynchronously executes @arg func in @arg queue returning a future for the result. + * + * @arg func should be a callable object with signature T(). + * @arg queue where @arg will be executed + * @returns For @arg func with signature T() the function returns TFuture<T> unless T is TFuture<U>. + * In this case the function returns TFuture<U>. + * + * If you want to use another queue for execution just write an overload, @see ExtensionExample + * unittest. + */ template <typename Func> TFuture<TFutureType<TFunctionResult<Func>>> Async(Func&& func, IThreadPool& queue) { auto promise = NewPromise<TFutureType<TFunctionResult<Func>>>(); @@ -24,8 +24,8 @@ namespace NThreading { NImpl::SetValue(promise, func); }; queue.SafeAddFunc(std::move(lambda)); - + return promise.GetFuture(); } - -} + +} diff --git a/library/cpp/threading/future/async_ut.cpp b/library/cpp/threading/future/async_ut.cpp index c2801dbd6e..a3699744e4 100644 --- a/library/cpp/threading/future/async_ut.cpp +++ b/library/cpp/threading/future/async_ut.cpp @@ -1,32 +1,32 @@ -#include "async.h" - +#include "async.h" + #include <library/cpp/testing/unittest/registar.h> - -#include <util/generic/ptr.h> -#include <util/generic/vector.h> - -namespace { + +#include <util/generic/ptr.h> +#include <util/generic/vector.h> + +namespace { struct TMySuperTaskQueue { }; - -} - -namespace NThreading { + +} + +namespace NThreading { /* Here we provide an Async overload for TMySuperTaskQueue indide NThreading namespace - * so that we can call it in the way - * - * TMySuperTaskQueue queue; - * NThreading::Async([](){}, queue); - * - * See also ExtensionExample unittest. - */ + * so that we can call it in the way + * + * TMySuperTaskQueue queue; + * NThreading::Async([](){}, queue); + * + * See also ExtensionExample unittest. + */ template <typename Func> TFuture<TFunctionResult<Func>> Async(Func func, TMySuperTaskQueue&) { return MakeFuture(func()); } - -} - + +} + Y_UNIT_TEST_SUITE(Async) { Y_UNIT_TEST(ExtensionExample) { TMySuperTaskQueue queue; @@ -34,24 +34,24 @@ Y_UNIT_TEST_SUITE(Async) { future.Wait(); UNIT_ASSERT_VALUES_EQUAL(future.GetValue(), 5); } - + Y_UNIT_TEST(WorksWithIMtpQueue) { auto queue = MakeHolder<TThreadPool>(); queue->Start(1); - + auto future = NThreading::Async([]() { return 5; }, *queue); future.Wait(); UNIT_ASSERT_VALUES_EQUAL(future.GetValue(), 5); } - + Y_UNIT_TEST(ProperlyDeducesFutureType) { // Compileability test auto queue = CreateThreadPool(1); - + NThreading::TFuture<void> f1 = NThreading::Async([]() {}, *queue); NThreading::TFuture<int> f2 = NThreading::Async([]() { return 5; }, *queue); NThreading::TFuture<double> f3 = NThreading::Async([]() { return 5.0; }, *queue); NThreading::TFuture<TVector<int>> f4 = NThreading::Async([]() { return TVector<int>(); }, *queue); NThreading::TFuture<int> f5 = NThreading::Async([]() { return NThreading::MakeFuture(5); }, *queue); } -} +} diff --git a/library/cpp/threading/future/core/future-inl.h b/library/cpp/threading/future/core/future-inl.h index bb1aef1ed1..5fd4296a93 100644 --- a/library/cpp/threading/future/core/future-inl.h +++ b/library/cpp/threading/future/core/future-inl.h @@ -4,7 +4,7 @@ #error "you should never include future-inl.h directly" #endif // INCLUDE_FUTURE_INL_H -namespace NThreading { +namespace NThreading { namespace NImpl { //////////////////////////////////////////////////////////////////////////////// diff --git a/library/cpp/threading/future/core/future.h b/library/cpp/threading/future/core/future.h index 3d5570ba5b..2e82bb953e 100644 --- a/library/cpp/threading/future/core/future.h +++ b/library/cpp/threading/future/core/future.h @@ -11,7 +11,7 @@ #include <util/system/event.h> #include <util/system/spinlock.h> -namespace NThreading { +namespace NThreading { //////////////////////////////////////////////////////////////////////////////// struct TFutureException: public yexception {}; diff --git a/library/cpp/threading/future/future_ut.cpp b/library/cpp/threading/future/future_ut.cpp index 5cc51fa145..05950a568d 100644 --- a/library/cpp/threading/future/future_ut.cpp +++ b/library/cpp/threading/future/future_ut.cpp @@ -5,7 +5,7 @@ #include <list> #include <type_traits> -namespace NThreading { +namespace NThreading { namespace { @@ -420,7 +420,7 @@ namespace { auto promise = NewPromise<TRec>(); promise.SetValue(TRec(1)); - + auto future = MakeFuture(TRec(1)); const auto& rec = future.GetValue(); Y_UNUSED(rec); @@ -637,4 +637,4 @@ namespace { } } -} +} diff --git a/library/cpp/threading/future/legacy_future.h b/library/cpp/threading/future/legacy_future.h index 4cb446047b..6f1eabad73 100644 --- a/library/cpp/threading/future/legacy_future.h +++ b/library/cpp/threading/future/legacy_future.h @@ -1,25 +1,25 @@ -#pragma once - +#pragma once + #include "fwd.h" -#include "future.h" - +#include "future.h" + #include <util/thread/factory.h> #include <functional> -namespace NThreading { +namespace NThreading { template <typename TR, bool IgnoreException> class TLegacyFuture: public IThreadFactory::IThreadAble, TNonCopyable { public: typedef TR(TFunctionSignature)(); using TFunctionObjectType = std::function<TFunctionSignature>; using TResult = typename TFunctionObjectType::result_type; - + private: TFunctionObjectType Func_; TPromise<TResult> Result_; THolder<IThreadFactory::IThread> Thread_; - + public: inline TLegacyFuture(const TFunctionObjectType func, IThreadFactory* pool = SystemThreadFactory()) : Func_(func) @@ -27,24 +27,24 @@ namespace NThreading { , Thread_(pool->Run(this)) { } - + inline ~TLegacyFuture() override { this->Join(); } - + inline TResult Get() { this->Join(); return Result_.GetValue(); } - + private: inline void Join() { if (Thread_) { Thread_->Join(); Thread_.Destroy(); } - } - + } + template <typename Result, bool IgnoreException_> struct TExecutor { static void SetPromise(TPromise<Result>& promise, const TFunctionObjectType& func) { @@ -54,11 +54,11 @@ namespace NThreading { } catch (...) { } } else { - promise.SetValue(func()); - } - } + promise.SetValue(func()); + } + } }; - + template <bool IgnoreException_> struct TExecutor<void, IgnoreException_> { static void SetPromise(TPromise<void>& promise, const TFunctionObjectType& func) { @@ -69,15 +69,15 @@ namespace NThreading { } catch (...) { } } else { - func(); - promise.SetValue(); - } - } + func(); + promise.SetValue(); + } + } }; void DoExecute() override { TExecutor<TResult, IgnoreException>::SetPromise(Result_, Func_); - } - }; - -} + } + }; + +} diff --git a/library/cpp/threading/future/legacy_future_ut.cpp b/library/cpp/threading/future/legacy_future_ut.cpp index 67c0ea504e..ff63db1725 100644 --- a/library/cpp/threading/future/legacy_future_ut.cpp +++ b/library/cpp/threading/future/legacy_future_ut.cpp @@ -1,73 +1,73 @@ -#include "legacy_future.h" - +#include "legacy_future.h" + #include <library/cpp/testing/unittest/registar.h> - -namespace NThreading { + +namespace NThreading { Y_UNIT_TEST_SUITE(TLegacyFutureTest) { int intf() { return 17; } - + Y_UNIT_TEST(TestIntFunction) { TLegacyFuture<int> f((&intf)); UNIT_ASSERT_VALUES_EQUAL(17, f.Get()); } - + static int r; - + void voidf() { r = 18; } - + Y_UNIT_TEST(TestVoidFunction) { r = 0; TLegacyFuture<> f((&voidf)); f.Get(); UNIT_ASSERT_VALUES_EQUAL(18, r); } - + struct TSampleClass { int mValue; - + TSampleClass(int value) : mValue(value) { } - + int Calc() { return mValue + 1; } }; - + Y_UNIT_TEST(TestMethod) { TLegacyFuture<int> f11(std::bind(&TSampleClass::Calc, TSampleClass(3))); UNIT_ASSERT_VALUES_EQUAL(4, f11.Get()); - + TLegacyFuture<int> f12(std::bind(&TSampleClass::Calc, TSampleClass(3)), SystemThreadFactory()); UNIT_ASSERT_VALUES_EQUAL(4, f12.Get()); - + TSampleClass c(5); - + TLegacyFuture<int> f21(std::bind(&TSampleClass::Calc, std::ref(c))); UNIT_ASSERT_VALUES_EQUAL(6, f21.Get()); - + TLegacyFuture<int> f22(std::bind(&TSampleClass::Calc, std::ref(c)), SystemThreadFactory()); UNIT_ASSERT_VALUES_EQUAL(6, f22.Get()); } - + struct TSomeThreadPool: public IThreadFactory {}; - + Y_UNIT_TEST(TestFunction) { std::function<int()> f((&intf)); - + UNIT_ASSERT_VALUES_EQUAL(17, TLegacyFuture<int>(f).Get()); UNIT_ASSERT_VALUES_EQUAL(17, TLegacyFuture<int>(f, SystemThreadFactory()).Get()); - + if (false) { TSomeThreadPool* q = nullptr; TLegacyFuture<int>(f, q); // just check compiles, do not start } - } - } - -} + } + } + +} diff --git a/library/cpp/threading/future/ut/ya.make b/library/cpp/threading/future/ut/ya.make index b2dce89394..566b622370 100644 --- a/library/cpp/threading/future/ut/ya.make +++ b/library/cpp/threading/future/ut/ya.make @@ -1,14 +1,14 @@ UNITTEST_FOR(library/cpp/threading/future) - + OWNER( g:rtmr ishfb ) - -SRCS( + +SRCS( async_ut.cpp - future_ut.cpp - legacy_future_ut.cpp -) - -END() + future_ut.cpp + legacy_future_ut.cpp +) + +END() diff --git a/library/cpp/threading/future/wait/wait-inl.h b/library/cpp/threading/future/wait/wait-inl.h index 27f7528cd9..2753d5446c 100644 --- a/library/cpp/threading/future/wait/wait-inl.h +++ b/library/cpp/threading/future/wait/wait-inl.h @@ -4,7 +4,7 @@ #error "you should never include wait-inl.h directly" #endif // INCLUDE_FUTURE_INL_H -namespace NThreading { +namespace NThreading { namespace NImpl { template <typename TContainer> TVector<TFuture<void>> ToVoidFutures(const TContainer& futures) { diff --git a/library/cpp/threading/future/wait/wait.cpp b/library/cpp/threading/future/wait/wait.cpp index e538e2876f..a173833a7f 100644 --- a/library/cpp/threading/future/wait/wait.cpp +++ b/library/cpp/threading/future/wait/wait.cpp @@ -3,7 +3,7 @@ #include "wait_group.h" #include "wait_policy.h" -namespace NThreading { +namespace NThreading { namespace { template <class WaitPolicy> TFuture<void> WaitGeneric(const TFuture<void>& f1) { diff --git a/library/cpp/threading/future/wait/wait.h b/library/cpp/threading/future/wait/wait.h index 361af5c2b9..6ff7d57baa 100644 --- a/library/cpp/threading/future/wait/wait.h +++ b/library/cpp/threading/future/wait/wait.h @@ -7,7 +7,7 @@ #include <util/generic/array_ref.h> -namespace NThreading { +namespace NThreading { namespace NImpl { template <class TContainer> using EnableGenericWait = std::enable_if_t< diff --git a/library/cpp/threading/future/ya.make b/library/cpp/threading/future/ya.make index aea750cb3e..6591031f46 100644 --- a/library/cpp/threading/future/ya.make +++ b/library/cpp/threading/future/ya.make @@ -2,10 +2,10 @@ OWNER( g:rtmr ) -LIBRARY() - +LIBRARY() + SRCS( - async.cpp + async.cpp core/future.cpp core/fwd.cpp fwd.cpp @@ -15,7 +15,7 @@ SRCS( wait/wait_policy.cpp ) -END() +END() RECURSE_FOR_TESTS( mt_ut diff --git a/util/generic/algorithm.h b/util/generic/algorithm.h index f905557de7..badfb88993 100644 --- a/util/generic/algorithm.h +++ b/util/generic/algorithm.h @@ -4,35 +4,35 @@ #include "utility.h" #include <util/system/defaults.h> -#include <util/generic/fwd.h> +#include <util/generic/fwd.h> #include <numeric> #include <algorithm> #include <iterator> #include <utility> -namespace NPrivate { - template <class I, class F, class P> - I ExtremeElementBy(I begin, I end, F func, P pred) { - if (begin == end) { - return end; - } - - auto bestValue = func(*begin); - auto bestPos = begin; - - for (auto i = ++begin; i != end; ++i) { - auto curValue = func(*i); - if (pred(curValue, bestValue)) { - bestValue = curValue; - bestPos = i; - } - } - - return bestPos; - } -} - +namespace NPrivate { + template <class I, class F, class P> + I ExtremeElementBy(I begin, I end, F func, P pred) { + if (begin == end) { + return end; + } + + auto bestValue = func(*begin); + auto bestPos = begin; + + for (auto i = ++begin; i != end; ++i) { + auto curValue = func(*i); + if (pred(curValue, bestValue)) { + bestValue = curValue; + bestPos = i; + } + } + + return bestPos; + } +} + template <class T> static inline void Sort(T f, T l) { std::sort(f, l); @@ -145,14 +145,14 @@ static inline I FindIf(I f, I l, P p) { return std::find_if(f, l, p); } -template <class C, class P> -static inline auto FindIf(C&& c, P p) { - using std::begin; - using std::end; - - return FindIf(begin(c), end(c), p); -} - +template <class C, class P> +static inline auto FindIf(C&& c, P p) { + using std::begin; + using std::end; + + return FindIf(begin(c), end(c), p); +} + template <class I, class P> static inline bool AllOf(I f, I l, P pred) { return std::all_of(f, l, pred); @@ -517,38 +517,38 @@ static inline T MaxElement(T begin, T end, C comp) { return std::max_element(begin, end, comp); } -template <class I, class F> -I MaxElementBy(I begin, I end, F&& func) { - using TValue = decltype(func(*begin)); - return ::NPrivate::ExtremeElementBy(begin, end, std::forward<F>(func), TGreater<TValue>()); -} - -template <class C, class F> +template <class I, class F> +I MaxElementBy(I begin, I end, F&& func) { + using TValue = decltype(func(*begin)); + return ::NPrivate::ExtremeElementBy(begin, end, std::forward<F>(func), TGreater<TValue>()); +} + +template <class C, class F> auto MaxElementBy(C& c, F&& func) { return MaxElementBy(std::begin(c), std::end(c), std::forward<F>(func)); } template <class C, class F> auto MaxElementBy(const C& c, F&& func) { - return MaxElementBy(std::begin(c), std::end(c), std::forward<F>(func)); -} - -template <class I, class F> -I MinElementBy(I begin, I end, F&& func) { - using TValue = decltype(func(*begin)); - return ::NPrivate::ExtremeElementBy(begin, end, std::forward<F>(func), TLess<TValue>()); -} - -template <class C, class F> + return MaxElementBy(std::begin(c), std::end(c), std::forward<F>(func)); +} + +template <class I, class F> +I MinElementBy(I begin, I end, F&& func) { + using TValue = decltype(func(*begin)); + return ::NPrivate::ExtremeElementBy(begin, end, std::forward<F>(func), TLess<TValue>()); +} + +template <class C, class F> auto MinElementBy(C& c, F&& func) { return MinElementBy(std::begin(c), std::end(c), std::forward<F>(func)); } template <class C, class F> auto MinElementBy(const C& c, F&& func) { - return MinElementBy(std::begin(c), std::end(c), std::forward<F>(func)); -} - + return MinElementBy(std::begin(c), std::end(c), std::forward<F>(func)); +} + template <class TOp, class... TArgs> void ApplyToMany(TOp op, TArgs&&... args) { int dummy[] = {((void)op(std::forward<TArgs>(args)), 0)...}; diff --git a/util/generic/algorithm_ut.cpp b/util/generic/algorithm_ut.cpp index bdb9e594f0..8d732fcc0c 100644 --- a/util/generic/algorithm_ut.cpp +++ b/util/generic/algorithm_ut.cpp @@ -611,91 +611,91 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { UNIT_ASSERT_EQUAL(p2.first, TString("data")); UNIT_ASSERT_EQUAL(p2.second, TString("test")); } - + Y_UNIT_TEST(TestMaxElementBy) { - const int array[] = {1, 2, 5, 3, 4, 5}; - UNIT_ASSERT_VALUES_EQUAL(*MaxElementBy(array, [](int x) { - return x * x; + const int array[] = {1, 2, 5, 3, 4, 5}; + UNIT_ASSERT_VALUES_EQUAL(*MaxElementBy(array, [](int x) { + return x * x; }), 5); - + const TVector<int> vec(array, array + Y_ARRAY_SIZE(array)); - UNIT_ASSERT_VALUES_EQUAL(*MaxElementBy(vec, [](int x) { - return -1.0 * x; + UNIT_ASSERT_VALUES_EQUAL(*MaxElementBy(vec, [](int x) { + return -1.0 * x; }), 1); - + int arrayMutable[] = {1, 2, 5, 3, 4, 5}; auto maxPtr = MaxElementBy(arrayMutable, [](int x) { return x; }); *maxPtr += 100; UNIT_ASSERT_VALUES_EQUAL(*maxPtr, 105); - auto identity = [](char x) { - return x; - }; - auto singleElementSequence = {'z'}; - UNIT_ASSERT_VALUES_EQUAL(*MaxElementBy(singleElementSequence, identity), 'z'); - + auto identity = [](char x) { + return x; + }; + auto singleElementSequence = {'z'}; + UNIT_ASSERT_VALUES_EQUAL(*MaxElementBy(singleElementSequence, identity), 'z'); + const TString strings[] = {"one", "two", "three", "four"}; UNIT_ASSERT_STRINGS_EQUAL(*MaxElementBy(strings, [](TString s) { return s.size(); }), "three"); - } - + } + Y_UNIT_TEST(TestMinElementBy) { - const int array[] = {2, 3, 4, 1, 5}; - UNIT_ASSERT_VALUES_EQUAL(*MinElementBy(array, [](int x) -> char { - return 'a' + x; + const int array[] = {2, 3, 4, 1, 5}; + UNIT_ASSERT_VALUES_EQUAL(*MinElementBy(array, [](int x) -> char { + return 'a' + x; }), 1); - + const TVector<int> vec(std::begin(array), std::end(array)); - UNIT_ASSERT_VALUES_EQUAL(*MinElementBy(vec, [](int x) { - return -x; + UNIT_ASSERT_VALUES_EQUAL(*MinElementBy(vec, [](int x) { + return -x; }), 5); - + int arrayMutable[] = {1, 2, 5, 3, 4, 5}; auto minPtr = MinElementBy(arrayMutable, [](int x) { return x; }); *minPtr += 100; UNIT_ASSERT_VALUES_EQUAL(*minPtr, 101); - auto identity = [](char x) { - return x; - }; - auto singleElementSequence = {'z'}; - UNIT_ASSERT_VALUES_EQUAL(*MinElementBy(singleElementSequence, identity), 'z'); - + auto identity = [](char x) { + return x; + }; + auto singleElementSequence = {'z'}; + UNIT_ASSERT_VALUES_EQUAL(*MinElementBy(singleElementSequence, identity), 'z'); + const TVector<TStringBuf> strings = {"one", "two", "three", "four"}; - auto stringLength = [](TStringBuf s) { - return s.size(); - }; - UNIT_ASSERT_STRINGS_EQUAL(*MinElementBy(strings, stringLength), "one"); - UNIT_ASSERT_STRINGS_EQUAL(*MinElementBy(strings.rbegin(), strings.rend(), stringLength), "two"); - } - + auto stringLength = [](TStringBuf s) { + return s.size(); + }; + UNIT_ASSERT_STRINGS_EQUAL(*MinElementBy(strings, stringLength), "one"); + UNIT_ASSERT_STRINGS_EQUAL(*MinElementBy(strings.rbegin(), strings.rend(), stringLength), "two"); + } + Y_UNIT_TEST(MaxElementByReturnsEndForEmptyRange) { const TVector<int> empty; - UNIT_ASSERT_EQUAL(MaxElementBy(empty, [](int) { return 0; }), empty.end()); - } - + UNIT_ASSERT_EQUAL(MaxElementBy(empty, [](int) { return 0; }), empty.end()); + } + Y_UNIT_TEST(MaxElementByDoesntCallFunctorForEmptyRange) { const TVector<int> empty; - auto functor = [](int) { - UNIT_ASSERT(false); - return 0; - }; - MaxElementBy(empty, functor); - } - + auto functor = [](int) { + UNIT_ASSERT(false); + return 0; + }; + MaxElementBy(empty, functor); + } + Y_UNIT_TEST(MinElementByReturnsEndForEmptyRange) { const TVector<int> empty; - UNIT_ASSERT_EQUAL(MinElementBy(empty, [](int) { return 0; }), empty.end()); - } - + UNIT_ASSERT_EQUAL(MinElementBy(empty, [](int) { return 0; }), empty.end()); + } + Y_UNIT_TEST(MinElementByDoesntCallFunctorForEmptyRange) { const TVector<int> empty; - auto functor = [](int) { - UNIT_ASSERT(false); - return 0; - }; - MinElementBy(empty, functor); - } - + auto functor = [](int) { + UNIT_ASSERT(false); + return 0; + }; + MinElementBy(empty, functor); + } + Y_UNIT_TEST(TestApplyToMany) { int res = 0; ApplyToMany([&res](auto v) { res += v; }, 1, 2, 3, 4, 5); @@ -757,45 +757,45 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { } Y_UNIT_TEST(FindIfForContainer) { - using std::begin; - using std::end; - - int array[] = {1, 2, 3, 4, 5}; - UNIT_ASSERT_EQUAL(FindIf(array, [](int x) { return x == 1; }), begin(array)); - UNIT_ASSERT_EQUAL(FindIf(array, [](int x) { return x > 5; }), end(array)); - + using std::begin; + using std::end; + + int array[] = {1, 2, 3, 4, 5}; + UNIT_ASSERT_EQUAL(FindIf(array, [](int x) { return x == 1; }), begin(array)); + UNIT_ASSERT_EQUAL(FindIf(array, [](int x) { return x > 5; }), end(array)); + TVector<int> vector = {1, 2, 3, 4, 5}; - UNIT_ASSERT_EQUAL(FindIf(vector, [](int x) { return x == 1; }), begin(vector)); - UNIT_ASSERT_EQUAL(FindIf(vector, [](int x) { return x > 5; }), end(vector)); - - // Compilability test. Check if the returned iterator is non const - auto iter = FindIf(vector, [](int x) { return x == 1; }); - *iter = 5; - - // Compilability test. Check if the returned iterator is const. Should not compile + UNIT_ASSERT_EQUAL(FindIf(vector, [](int x) { return x == 1; }), begin(vector)); + UNIT_ASSERT_EQUAL(FindIf(vector, [](int x) { return x > 5; }), end(vector)); + + // Compilability test. Check if the returned iterator is non const + auto iter = FindIf(vector, [](int x) { return x == 1; }); + *iter = 5; + + // Compilability test. Check if the returned iterator is const. Should not compile const TVector<int> constVector = {1, 2, 3, 4, 5}; - auto constIter = FindIf(constVector, [](int x) { return x == 1; }); - Y_UNUSED(constIter); - // *constIter = 5; - } - - struct TRange { - }; - - const TRange* begin(const TRange& r) { - return &r; - } - - const TRange* end(const TRange& r) { - return &r + 1; - } - + auto constIter = FindIf(constVector, [](int x) { return x == 1; }); + Y_UNUSED(constIter); + // *constIter = 5; + } + + struct TRange { + }; + + const TRange* begin(const TRange& r) { + return &r; + } + + const TRange* end(const TRange& r) { + return &r + 1; + } + Y_UNIT_TEST(FindIfForUserType) { - // Compileability test. Should work for user types with begin/end overloads - TRange range; - auto i = FindIf(range, [](auto) { return false; }); - Y_UNUSED(i); - } + // Compileability test. Should work for user types with begin/end overloads + TRange range; + auto i = FindIf(range, [](auto) { return false; }); + Y_UNUSED(i); + } Y_UNIT_TEST(TestLowerBoundBy) { using TIntPairs = TVector<std::pair<i32, i32>>; @@ -847,4 +847,4 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { std::vector<int> v = {1, 2, 777}; UNIT_ASSERT_VALUES_EQUAL(TString("begin;1;2;777"), Accumulate(v, TString("begin"), [](auto&& a, auto& b) { return a + ";" + ToString(b); })); } -}; +}; diff --git a/util/generic/fwd.h b/util/generic/fwd.h index 79b6ad8515..5cc2da40e5 100644 --- a/util/generic/fwd.h +++ b/util/generic/fwd.h @@ -26,8 +26,8 @@ template <class T = void> struct TLess; template <class T = void> -struct TGreater; - +struct TGreater; + template <class T = void> struct TEqualTo; diff --git a/util/generic/iterator_range_ut.cpp b/util/generic/iterator_range_ut.cpp index eb65d65808..a7e3670ae1 100644 --- a/util/generic/iterator_range_ut.cpp +++ b/util/generic/iterator_range_ut.cpp @@ -1,15 +1,15 @@ #include "iterator_range.h" - + #include <library/cpp/testing/unittest/registar.h> -#include <util/generic/algorithm.h> +#include <util/generic/algorithm.h> #include <util/generic/vector.h> - + Y_UNIT_TEST_SUITE(IteratorRange) { Y_UNIT_TEST(DefaultConstructor) { TIteratorRange<int*> range; UNIT_ASSERT(range.empty()); } - + Y_UNIT_TEST(DefaultConstructorSentinel) { TIteratorRange<int*, void*> range; UNIT_ASSERT(range.empty()); @@ -21,7 +21,7 @@ Y_UNIT_TEST_SUITE(IteratorRange) { Y_UNUSED(i); } } - + Y_UNIT_TEST(RangeBasedForLoopSentinel) { // compileability test for (int i : TIteratorRange<int*, void*>()) { @@ -35,8 +35,8 @@ Y_UNIT_TEST_SUITE(IteratorRange) { UNIT_ASSERT_VALUES_EQUAL(range.size(), Y_ARRAY_SIZE(values)); UNIT_ASSERT(Equal(range.begin(), range.end(), values)); UNIT_ASSERT(!range.empty()); - } - + } + Y_UNIT_TEST(WorksSentinel) { struct TRangeSentinel { }; @@ -95,4 +95,4 @@ Y_UNIT_TEST_SUITE(IteratorRange) { // we should be able to use TIteratorRange as a container parameter for standard algorithms UNIT_ASSERT(AllOf(range, [](int x) { return x > 0; })); } -} +} diff --git a/util/generic/ptr.h b/util/generic/ptr.h index 2f2d9ec08f..19db0e3ec5 100644 --- a/util/generic/ptr.h +++ b/util/generic/ptr.h @@ -262,13 +262,13 @@ public: : T_(that.Release()) { } - + template <class U, class = TGuardConversion<T, U>> inline THolder(THolder<U, D>&& that) noexcept : T_(that.Release()) { } - + THolder(const THolder&) = delete; THolder& operator=(const THolder&) = delete; @@ -320,13 +320,13 @@ public: this->Reset(that.Release()); return *this; } - + template <class U> THolder& operator=(THolder<U, D>&& that) noexcept { this->Reset(that.Release()); return *this; } - + #ifdef __cpp_impl_three_way_comparison template <class Other> inline bool operator==(const Other& p) const noexcept { @@ -347,8 +347,8 @@ private: template <typename T, typename... Args> [[nodiscard]] THolder<T> MakeHolder(Args&&... args) { return THolder<T>(new T(std::forward<Args>(args)...)); -} - +} + /* * usage: * class T: public TRefCounted<T> @@ -950,18 +950,18 @@ using TSimpleSharedPtr = TSharedPtr<T, TSimpleCounter, D>; template <typename T, typename C, typename... Args> [[nodiscard]] TSharedPtr<T, C> MakeShared(Args&&... args) { return new T{std::forward<Args>(args)...}; -} - +} + template <typename T, typename... Args> [[nodiscard]] inline TAtomicSharedPtr<T> MakeAtomicShared(Args&&... args) { return MakeShared<T, TAtomicCounter>(std::forward<Args>(args)...); -} - +} + template <typename T, typename... Args> [[nodiscard]] inline TSimpleSharedPtr<T> MakeSimpleShared(Args&&... args) { return MakeShared<T, TSimpleCounter>(std::forward<Args>(args)...); -} - +} + class TCopyClone { public: template <class T> diff --git a/util/generic/ptr_ut.cpp b/util/generic/ptr_ut.cpp index c32a691093..c2dcff23f6 100644 --- a/util/generic/ptr_ut.cpp +++ b/util/generic/ptr_ut.cpp @@ -18,7 +18,7 @@ class TPointerTest: public TTestBase { UNIT_TEST(TestHolderPtrMoveConstructorInheritance); UNIT_TEST(TestHolderPtrMoveAssignment); UNIT_TEST(TestHolderPtrMoveAssignmentInheritance); - UNIT_TEST(TestMakeHolder); + UNIT_TEST(TestMakeHolder); UNIT_TEST(TestTrulePtr); UNIT_TEST(TestAutoToHolder); UNIT_TEST(TestCopyPtr); @@ -30,7 +30,7 @@ class TPointerTest: public TTestBase { UNIT_TEST(TestCopyOnWritePtr1); UNIT_TEST(TestCopyOnWritePtr2); UNIT_TEST(TestOperatorBool); - UNIT_TEST(TestMakeShared); + UNIT_TEST(TestMakeShared); UNIT_TEST(TestComparison); UNIT_TEST(TestSimpleIntrusivePtrCtorTsan); UNIT_TEST(TestRefCountedPtrsInHashSet) @@ -70,7 +70,7 @@ private: void TestHolderPtrMoveConstructorInheritance(); void TestHolderPtrMoveAssignment(); void TestHolderPtrMoveAssignmentInheritance(); - void TestMakeHolder(); + void TestMakeHolder(); void TestTrulePtr(); void TestAutoToHolder(); void TestCopyPtr(); @@ -82,7 +82,7 @@ private: void TestCopyOnWritePtr1(); void TestCopyOnWritePtr2(); void TestOperatorBool(); - void TestMakeShared(); + void TestMakeShared(); void TestComparison(); template <class T, class TRefCountedPtr> void TestRefCountedPtrsInHashSetImpl(); @@ -148,97 +148,97 @@ void TPointerTest::TestHolderPtr() { UNIT_ASSERT_VALUES_EQUAL(cnt, 0); } -THolder<int> CreateInt(int value) { - THolder<int> res(new int); - *res = value; - return res; -} - +THolder<int> CreateInt(int value) { + THolder<int> res(new int); + *res = value; + return res; +} + void TPointerTest::TestHolderPtrMoveConstructor() { - THolder<int> h = CreateInt(42); - UNIT_ASSERT_VALUES_EQUAL(*h, 42); -} - + THolder<int> h = CreateInt(42); + UNIT_ASSERT_VALUES_EQUAL(*h, 42); +} + void TPointerTest::TestHolderPtrMoveAssignment() { - THolder<int> h(new int); - h = CreateInt(42); - UNIT_ASSERT_VALUES_EQUAL(*h, 42); -} - -struct TBase { + THolder<int> h(new int); + h = CreateInt(42); + UNIT_ASSERT_VALUES_EQUAL(*h, 42); +} + +struct TBase { virtual ~TBase() = default; -}; - +}; + struct TDerived: public TBase { -}; - +}; + void TPointerTest::TestHolderPtrMoveConstructorInheritance() { - // compileability test - THolder<TBase> basePtr(THolder<TDerived>(new TDerived)); -} - + // compileability test + THolder<TBase> basePtr(THolder<TDerived>(new TDerived)); +} + void TPointerTest::TestHolderPtrMoveAssignmentInheritance() { - // compileability test - THolder<TBase> basePtr; - basePtr = THolder<TDerived>(new TDerived); -} - + // compileability test + THolder<TBase> basePtr; + basePtr = THolder<TDerived>(new TDerived); +} + void TPointerTest::TestMakeHolder() { - { - auto ptr = MakeHolder<int>(5); - UNIT_ASSERT_VALUES_EQUAL(*ptr, 5); - } - { - struct TRec { - int X, Y; + { + auto ptr = MakeHolder<int>(5); + UNIT_ASSERT_VALUES_EQUAL(*ptr, 5); + } + { + struct TRec { + int X, Y; TRec() : X(1) , Y(2) { - } - }; - THolder<TRec> ptr = MakeHolder<TRec>(); - UNIT_ASSERT_VALUES_EQUAL(ptr->X, 1); - UNIT_ASSERT_VALUES_EQUAL(ptr->Y, 2); - } - { - struct TRec { - int X, Y; + } + }; + THolder<TRec> ptr = MakeHolder<TRec>(); + UNIT_ASSERT_VALUES_EQUAL(ptr->X, 1); + UNIT_ASSERT_VALUES_EQUAL(ptr->Y, 2); + } + { + struct TRec { + int X, Y; TRec(int x, int y) : X(x) , Y(y) { } - }; - auto ptr = MakeHolder<TRec>(1, 2); - UNIT_ASSERT_VALUES_EQUAL(ptr->X, 1); - UNIT_ASSERT_VALUES_EQUAL(ptr->Y, 2); - } - { - class TRec { - private: + }; + auto ptr = MakeHolder<TRec>(1, 2); + UNIT_ASSERT_VALUES_EQUAL(ptr->X, 1); + UNIT_ASSERT_VALUES_EQUAL(ptr->Y, 2); + } + { + class TRec { + private: int X_, Y_; - public: + public: TRec(int x, int y) : X_(x) , Y_(y) { - } - + } + int GetX() const { return X_; } int GetY() const { return Y_; } - }; - auto ptr = MakeHolder<TRec>(1, 2); - UNIT_ASSERT_VALUES_EQUAL(ptr->GetX(), 1); - UNIT_ASSERT_VALUES_EQUAL(ptr->GetY(), 2); - } -} - + }; + auto ptr = MakeHolder<TRec>(1, 2); + UNIT_ASSERT_VALUES_EQUAL(ptr->GetX(), 1); + UNIT_ASSERT_VALUES_EQUAL(ptr->GetY(), 2); + } +} + void TPointerTest::TestTrulePtr() { { TAutoPtr<A> a1(MakeA()); @@ -620,94 +620,94 @@ void TPointerTest::TestOperatorBool() { // bool c = (a < b); // does not compile } } - + void TPointerTest::TestMakeShared() { - { - TSimpleSharedPtr<int> ptr = MakeSimpleShared<int>(5); - UNIT_ASSERT_VALUES_EQUAL(*ptr, 5); - } - { - struct TRec { - int X, Y; + { + TSimpleSharedPtr<int> ptr = MakeSimpleShared<int>(5); + UNIT_ASSERT_VALUES_EQUAL(*ptr, 5); + } + { + struct TRec { + int X, Y; TRec() : X(1) , Y(2) { - } - }; - auto ptr = MakeAtomicShared<TRec>(); - UNIT_ASSERT_VALUES_EQUAL(ptr->X, 1); - UNIT_ASSERT_VALUES_EQUAL(ptr->Y, 2); - } - { - struct TRec { - int X, Y; - }; - TAtomicSharedPtr<TRec> ptr = MakeAtomicShared<TRec>(1, 2); - UNIT_ASSERT_VALUES_EQUAL(ptr->X, 1); - UNIT_ASSERT_VALUES_EQUAL(ptr->Y, 2); - } - { - class TRec { - private: + } + }; + auto ptr = MakeAtomicShared<TRec>(); + UNIT_ASSERT_VALUES_EQUAL(ptr->X, 1); + UNIT_ASSERT_VALUES_EQUAL(ptr->Y, 2); + } + { + struct TRec { + int X, Y; + }; + TAtomicSharedPtr<TRec> ptr = MakeAtomicShared<TRec>(1, 2); + UNIT_ASSERT_VALUES_EQUAL(ptr->X, 1); + UNIT_ASSERT_VALUES_EQUAL(ptr->Y, 2); + } + { + class TRec { + private: int X_, Y_; - public: + public: TRec(int x, int y) : X_(x) , Y_(y) { - } - + } + int GetX() const { return X_; } int GetY() const { return Y_; } - }; - TSimpleSharedPtr<TRec> ptr = MakeSimpleShared<TRec>(1, 2); - UNIT_ASSERT_VALUES_EQUAL(ptr->GetX(), 1); - UNIT_ASSERT_VALUES_EQUAL(ptr->GetY(), 2); - } - { - enum EObjectState { - OS_NOT_CREATED, - OS_CREATED, - OS_DESTROYED, - }; - - struct TObject { - EObjectState& State; - - TObject(EObjectState& state) - : State(state) - { - State = OS_CREATED; - } - - ~TObject() { - State = OS_DESTROYED; - } - }; - - auto throwsException = []() { - throw yexception(); - return 5; - }; - - auto testFunction = [](TSimpleSharedPtr<TObject>, int) { - }; - - EObjectState state = OS_NOT_CREATED; - try { - testFunction(MakeSimpleShared<TObject>(state), throwsException()); - } catch (yexception&) { - } - - UNIT_ASSERT(state == OS_NOT_CREATED || state == OS_DESTROYED); - } -} + }; + TSimpleSharedPtr<TRec> ptr = MakeSimpleShared<TRec>(1, 2); + UNIT_ASSERT_VALUES_EQUAL(ptr->GetX(), 1); + UNIT_ASSERT_VALUES_EQUAL(ptr->GetY(), 2); + } + { + enum EObjectState { + OS_NOT_CREATED, + OS_CREATED, + OS_DESTROYED, + }; + + struct TObject { + EObjectState& State; + + TObject(EObjectState& state) + : State(state) + { + State = OS_CREATED; + } + + ~TObject() { + State = OS_DESTROYED; + } + }; + + auto throwsException = []() { + throw yexception(); + return 5; + }; + + auto testFunction = [](TSimpleSharedPtr<TObject>, int) { + }; + + EObjectState state = OS_NOT_CREATED; + try { + testFunction(MakeSimpleShared<TObject>(state), throwsException()); + } catch (yexception&) { + } + + UNIT_ASSERT(state == OS_NOT_CREATED || state == OS_DESTROYED); + } +} template <class TPtr> void TestPtrComparison(const TPtr& ptr) { diff --git a/util/generic/xrange.h b/util/generic/xrange.h index f7b8e0cebe..5fc8c82912 100644 --- a/util/generic/xrange.h +++ b/util/generic/xrange.h @@ -1,7 +1,7 @@ #pragma once #include "typetraits.h" -#include "utility.h" +#include "utility.h" #include <util/system/yassert.h> #include <iterator> diff --git a/util/generic/xrange_ut.cpp b/util/generic/xrange_ut.cpp index 86f316918e..8106da03e7 100644 --- a/util/generic/xrange_ut.cpp +++ b/util/generic/xrange_ut.cpp @@ -1,5 +1,5 @@ -#include "xrange.h" - +#include "xrange.h" + #include "algorithm.h" #include "maybe.h" #include "vector.h" @@ -112,50 +112,50 @@ Y_UNIT_TEST_SUITE(XRange) { UNIT_ASSERT(arr == data); } } - - template <class XRangeContainer> - void TestEmptyRanges(const XRangeContainer& c) { - for (const auto& emptyRange : c) { - UNIT_ASSERT_VALUES_EQUAL(emptyRange.size(), 0); - - for (auto i : emptyRange) { + + template <class XRangeContainer> + void TestEmptyRanges(const XRangeContainer& c) { + for (const auto& emptyRange : c) { + UNIT_ASSERT_VALUES_EQUAL(emptyRange.size(), 0); + + for (auto i : emptyRange) { Y_UNUSED(i); - UNIT_ASSERT(false); - } - - using TValueType = decltype(*emptyRange.begin()); + UNIT_ASSERT(false); + } + + using TValueType = decltype(*emptyRange.begin()); const TVector<TValueType> asVector = emptyRange; - UNIT_ASSERT(asVector.empty()); - } - } - + UNIT_ASSERT(asVector.empty()); + } + } + Y_UNIT_TEST(EmptySimpleRange) { - using TSimpleRange = decltype(xrange(1)); - - const TSimpleRange emptySimpleRanges[] = { - xrange(-1), - xrange(-10), - xrange(0, -5), - xrange(10, 10), - xrange(10, 9), - }; - - TestEmptyRanges(emptySimpleRanges); - } - + using TSimpleRange = decltype(xrange(1)); + + const TSimpleRange emptySimpleRanges[] = { + xrange(-1), + xrange(-10), + xrange(0, -5), + xrange(10, 10), + xrange(10, 9), + }; + + TestEmptyRanges(emptySimpleRanges); + } + Y_UNIT_TEST(EmptySteppedRange) { - using TSteppedRange = decltype(xrange(1, 10, 1)); - - const TSteppedRange emptySteppedRanges[] = { - xrange(5, 5, 1), - xrange(5, 0, 5), - xrange(0, -1, 5), + using TSteppedRange = decltype(xrange(1, 10, 1)); + + const TSteppedRange emptySteppedRanges[] = { + xrange(5, 5, 1), + xrange(5, 0, 5), + xrange(0, -1, 5), xrange(0, 1, -1), - xrange(0, -10, 10), - }; - - TestEmptyRanges(emptySteppedRanges); - } + xrange(0, -10, 10), + }; + + TestEmptyRanges(emptySteppedRanges); + } template <class TRange> static void TestIteratorDifferenceImpl(TRange range, int a, int b, TMaybe<int> step) { diff --git a/util/ysaveload.h b/util/ysaveload.h index 4941ffbc3e..02efb4049b 100644 --- a/util/ysaveload.h +++ b/util/ysaveload.h @@ -650,27 +650,27 @@ namespace NPrivate { } } -template <typename... Args> +template <typename... Args> struct TSerializer<std::variant<Args...>> { using TVar = std::variant<Args...>; - - static_assert(sizeof...(Args) < 256, "We use ui8 to store tag"); - - static void Save(IOutputStream* os, const TVar& v) { + + static_assert(sizeof...(Args) < 256, "We use ui8 to store tag"); + + static void Save(IOutputStream* os, const TVar& v) { ::Save<ui8>(os, v.index()); std::visit([os](const auto& data) { - ::Save(os, data); + ::Save(os, data); }, v); - } - - static void Load(IInputStream* is, TVar& v) { + } + + static void Load(IInputStream* is, TVar& v) { ui8 index; ::Load(is, index); if (Y_UNLIKELY(index >= sizeof...(Args))) { ::NPrivate::ThrowUnexpectedVariantTagException(index); } LoadImpl(is, v, index, std::index_sequence_for<Args...>{}); - } + } private: template <size_t... Is> @@ -679,8 +679,8 @@ private: constexpr TLoader loaders[] = {::NPrivate::LoadVariantAlternative<TVar, Args, Is>...}; loaders[index](is, v); } -}; - +}; + #endif template <class T> diff --git a/util/ysaveload_ut.cpp b/util/ysaveload_ut.cpp index 97ca40b916..723c68f391 100644 --- a/util/ysaveload_ut.cpp +++ b/util/ysaveload_ut.cpp @@ -15,7 +15,7 @@ #include <util/generic/buffer.h> #include <util/generic/hash_set.h> #include <util/generic/maybe.h> -#include <util/generic/variant.h> +#include <util/generic/variant.h> static inline char* AllocateFromPool(TMemoryPool& pool, size_t len) { return (char*)pool.Allocate(len); @@ -28,7 +28,7 @@ class TSaveLoadTest: public TTestBase { UNIT_TEST(TestNewNewStyle) UNIT_TEST(TestList) UNIT_TEST(TestTuple) - UNIT_TEST(TestVariant) + UNIT_TEST(TestVariant) UNIT_TEST(TestInheritNonVirtualClass) UNIT_TEST(TestInheritVirtualClass) UNIT_TEST_SUITE_END(); @@ -406,30 +406,30 @@ private: UNIT_ASSERT_VALUES_EQUAL(std::get<2>(toLoad), std::get<2>(toSave)); } - template <class TVariant, class T> - void TestVariantImpl(TVariant& v, const T& expected) { - v = expected; - - TBufferStream s; - ::Save(&s, v); - ::Load(&s, v); + template <class TVariant, class T> + void TestVariantImpl(TVariant& v, const T& expected) { + v = expected; + + TBufferStream s; + ::Save(&s, v); + ::Load(&s, v); UNIT_ASSERT_VALUES_EQUAL(std::get<T>(v), expected); - } - - void TestVariant() { + } + + void TestVariant() { std::variant<int, bool, TString, TVector<char>> v(1); - TestVariantImpl(v, 42); - TestVariantImpl(v, true); - TestVariantImpl(v, TString("foo")); + TestVariantImpl(v, 42); + TestVariantImpl(v, true); + TestVariantImpl(v, TString("foo")); TestVariantImpl(v, TVector<char>{'b', 'a', 'r'}); - - v = TString("baz"); - TBufferStream s; - ::Save(&s, v); - + + v = TString("baz"); + TBufferStream s; + ::Save(&s, v); + std::variant<char, bool> v2 = false; - UNIT_ASSERT_EXCEPTION(::Load(&s, v2), TLoadEOF); - } + UNIT_ASSERT_EXCEPTION(::Load(&s, v2), TLoadEOF); + } // tests serialization of class with three public string members template <class TDerived, class TInterface = TDerived> |