diff options
author | karpik <karpik@yandex-team.ru> | 2022-02-10 16:49:23 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:23 +0300 |
commit | eb4b8b8ee0d18f168ae14f4d88a6efe2498e0f78 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 | |
parent | 9cb2cb2fdceae44848ab4504f5e445015b0eca6e (diff) | |
download | ydb-eb4b8b8ee0d18f168ae14f4d88a6efe2498e0f78.tar.gz |
Restoring authorship annotation for <karpik@yandex-team.ru>. Commit 2 of 2.
-rw-r--r-- | library/cpp/binsaver/bin_saver.cpp | 52 | ||||
-rw-r--r-- | library/cpp/binsaver/bin_saver.h | 204 | ||||
-rw-r--r-- | library/cpp/binsaver/buffered_io.cpp | 4 | ||||
-rw-r--r-- | library/cpp/binsaver/buffered_io.h | 52 | ||||
-rw-r--r-- | library/cpp/binsaver/class_factory.h | 36 | ||||
-rw-r--r-- | library/cpp/binsaver/mem_io.h | 10 | ||||
-rw-r--r-- | util/generic/guid.cpp | 2 | ||||
-rw-r--r-- | util/generic/guid.h | 10 |
8 files changed, 185 insertions, 185 deletions
diff --git a/library/cpp/binsaver/bin_saver.cpp b/library/cpp/binsaver/bin_saver.cpp index fffcb78b8c..fe0775af9f 100644 --- a/library/cpp/binsaver/bin_saver.cpp +++ b/library/cpp/binsaver/bin_saver.cpp @@ -1,20 +1,20 @@ #include "bin_saver.h" - + TClassFactory<IObjectBase>* pSaverClasses; -void StartRegisterSaveload() { +void StartRegisterSaveload() { if (!pSaverClasses) pSaverClasses = new TClassFactory<IObjectBase>; -} -struct SBasicChunkInit { +} +struct SBasicChunkInit { ~SBasicChunkInit() { if (pSaverClasses) delete pSaverClasses; } -} initSaver; - -////////////////////////////////////////////////////////////////////////// +} initSaver; + +////////////////////////////////////////////////////////////////////////// void IBinSaver::StoreObject(IObjectBase* pObject) { - if (pObject) { + if (pObject) { Y_ASSERT(pSaverClasses->GetObjectTypeID(pObject) != -1 && "trying to save unregistered object"); } @@ -39,17 +39,17 @@ void IBinSaver::StoreObject(IObjectBase* pObject) { if (!Objects.Get()) Objects.Reset(new CObjectsHash); if (ptrId != 0 && Objects->find(ptrId) == Objects->end()) { - ObjectQueue.push_back(pObject); + ObjectQueue.push_back(pObject); (*Objects)[ptrId]; - int typeId = pSaverClasses->GetObjectTypeID(pObject); + int typeId = pSaverClasses->GetObjectTypeID(pObject); if (typeId == -1) { fprintf(stderr, "IBinSaver: trying to save unregistered object\n"); abort(); } - DataChunk(&typeId, sizeof(typeId)); - } -} - + DataChunk(&typeId, sizeof(typeId)); + } +} + IObjectBase* IBinSaver::LoadObject() { ui64 ptrId = 0; DataChunk(&ptrId, sizeof(ptrId)); @@ -58,9 +58,9 @@ IObjectBase* IBinSaver::LoadObject() { Objects.Reset(new CObjectsHash); CObjectsHash::iterator pFound = Objects->find(ptrId); if (pFound != Objects->end()) - return pFound->second; - int typeId; - DataChunk(&typeId, sizeof(typeId)); + return pFound->second; + int typeId; + DataChunk(&typeId, sizeof(typeId)); IObjectBase* pObj = pSaverClasses->CreateObject(typeId); Y_ASSERT(pObj != nullptr); if (pObj == nullptr) { @@ -68,14 +68,14 @@ IObjectBase* IBinSaver::LoadObject() { abort(); } (*Objects)[ptrId] = pObj; - ObjectQueue.push_back(pObj); - return pObj; - } + ObjectQueue.push_back(pObj); + return pObj; + } return nullptr; -} - -IBinSaver::~IBinSaver() { +} + +IBinSaver::~IBinSaver() { for (size_t i = 0; i < ObjectQueue.size(); ++i) { - AddPolymorphicBase(1, ObjectQueue[i]); - } -} + AddPolymorphicBase(1, ObjectQueue[i]); + } +} diff --git a/library/cpp/binsaver/bin_saver.h b/library/cpp/binsaver/bin_saver.h index 5b60154d83..412424889f 100644 --- a/library/cpp/binsaver/bin_saver.h +++ b/library/cpp/binsaver/bin_saver.h @@ -1,5 +1,5 @@ -#pragma once - +#pragma once + #include "buffered_io.h" #include "class_factory.h" @@ -14,7 +14,7 @@ #include <util/generic/ylimits.h> #include <util/memory/blob.h> #include <util/digest/murmur.h> - + #include <array> #include <bitset> #include <list> @@ -27,9 +27,9 @@ enum ESaverMode { SAVER_MODE_READ = 1, SAVER_MODE_WRITE = 2, - SAVER_MODE_WRITE_COMPRESSED = 3, -}; - + SAVER_MODE_WRITE_COMPRESSED = 3, +}; + namespace NBinSaverInternals { // This lets explicitly control the overload resolution priority // The higher P means higher priority in overload resolution order @@ -42,13 +42,13 @@ namespace NBinSaverInternals { }; } -////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////// struct IBinSaver { -public: - typedef unsigned char chunk_id; +public: + typedef unsigned char chunk_id; typedef ui32 TStoredSize; // changing this will break compatibility -private: +private: // This overload is required to avoid infinite recursion when overriding serialization in derived classes: // struct B { // virtual int operator &(IBinSaver& f) { @@ -82,22 +82,22 @@ private: DataChunk(p, sizeof(T)); } - // vector + // vector template <class T, class TA> void DoVector(TVector<T, TA>& data) { TStoredSize nSize; - if (IsReading()) { - data.clear(); - Add(2, &nSize); - data.resize(nSize); - } else { + if (IsReading()) { + data.clear(); + Add(2, &nSize); + data.resize(nSize); + } else { nSize = data.size(); CheckOverflow(nSize, data.size()); - Add(2, &nSize); - } + Add(2, &nSize); + } for (TStoredSize i = 0; i < nSize; i++) - Add(1, &data[i]); - } + Add(1, &data[i]); + } template <class T, int N> void DoArray(T (&data)[N]) { @@ -118,34 +118,34 @@ private: void DoDataVector(TVector<T, TA>& data) { TStoredSize nSize = data.size(); CheckOverflow(nSize, data.size()); - Add(1, &nSize); - if (IsReading()) { - data.clear(); - data.resize(nSize); - } - if (nSize > 0) - DataChunk(&data[0], sizeof(T) * nSize); - } + Add(1, &nSize); + if (IsReading()) { + data.clear(); + data.resize(nSize); + } + if (nSize > 0) + DataChunk(&data[0], sizeof(T) * nSize); + } template <class AM> void DoAnyMap(AM& data) { - if (IsReading()) { - data.clear(); + if (IsReading()) { + data.clear(); TStoredSize nSize; - Add(3, &nSize); + Add(3, &nSize); TVector<typename AM::key_type, typename std::allocator_traits<typename AM::allocator_type>::template rebind_alloc<typename AM::key_type>> indices; - indices.resize(nSize); + indices.resize(nSize); for (TStoredSize i = 0; i < nSize; ++i) - Add(1, &indices[i]); + Add(1, &indices[i]); for (TStoredSize i = 0; i < nSize; ++i) Add(2, &data[indices[i]]); - } else { + } else { TStoredSize nSize = data.size(); CheckOverflow(nSize, data.size()); - Add(3, &nSize); - + Add(3, &nSize); + TVector<typename AM::key_type, typename std::allocator_traits<typename AM::allocator_type>::template rebind_alloc<typename AM::key_type>> indices; - indices.resize(nSize); + indices.resize(nSize); TStoredSize i = 1; for (auto pos = data.begin(); pos != data.end(); ++pos, ++i) indices[nSize - i] = pos->first; @@ -153,8 +153,8 @@ private: Add(1, &indices[j]); for (TStoredSize j = 0; j < nSize; ++j) Add(2, &data[indices[j]]); - } - } + } + } // hash_multimap template <class AMM> @@ -209,40 +209,40 @@ private: // 2D array template <class T> void Do2DArray(TArray2D<T>& a) { - int nXSize = a.GetXSize(), nYSize = a.GetYSize(); - Add(1, &nXSize); - Add(2, &nYSize); - if (IsReading()) - a.SetSizes(nXSize, nYSize); - for (int i = 0; i < nXSize * nYSize; i++) + int nXSize = a.GetXSize(), nYSize = a.GetYSize(); + Add(1, &nXSize); + Add(2, &nYSize); + if (IsReading()) + a.SetSizes(nXSize, nYSize); + for (int i = 0; i < nXSize * nYSize; i++) Add(3, &a[i / nXSize][i % nXSize]); - } + } template <class T> void Do2DArrayData(TArray2D<T>& a) { - int nXSize = a.GetXSize(), nYSize = a.GetYSize(); - Add(1, &nXSize); - Add(2, &nYSize); - if (IsReading()) - a.SetSizes(nXSize, nYSize); - if (nXSize * nYSize > 0) - DataChunk(&a[0][0], sizeof(T) * nXSize * nYSize); - } + int nXSize = a.GetXSize(), nYSize = a.GetYSize(); + Add(1, &nXSize); + Add(2, &nYSize); + if (IsReading()) + a.SetSizes(nXSize, nYSize); + if (nXSize * nYSize > 0) + DataChunk(&a[0][0], sizeof(T) * nXSize * nYSize); + } // strings template <class TStringType> void DataChunkStr(TStringType& data, i64 elemSize) { - if (bRead) { + if (bRead) { TStoredSize nCount = 0; File.Read(&nCount, sizeof(TStoredSize)); - data.resize(nCount); - if (nCount) + data.resize(nCount); + if (nCount) File.Read(&*data.begin(), nCount * elemSize); - } else { + } else { TStoredSize nCount = data.size(); CheckOverflow(nCount, data.size()); File.Write(&nCount, sizeof(TStoredSize)); File.Write(data.c_str(), nCount * elemSize); - } - } + } + } void DataChunkString(std::string& data) { DataChunkStr(data, sizeof(char)); } @@ -265,14 +265,14 @@ private: } } - // storing/loading pointers to objects + // storing/loading pointers to objects void StoreObject(IObjectBase* pObject); IObjectBase* LoadObject(); - - bool bRead; + + bool bRead; TBufferedStream<> File; - // maps objects addresses during save(first) to addresses during load(second) - during loading - // or serves as a sign that some object has been already stored - during storing + // maps objects addresses during save(first) to addresses during load(second) - during loading + // or serves as a sign that some object has been already stored - during storing bool StableOutput; typedef THashMap<void*, ui32> PtrIdHash; @@ -283,24 +283,24 @@ private: TVector<IObjectBase*> ObjectQueue; -public: +public: bool IsReading() { return bRead; } void AddRawData(const chunk_id, void* pData, i64 nSize) { DataChunk(pData, nSize); } - + // return type of Add() is used to detect specialized serializer (see HasNonTrivialSerializer below) template <class T> char Add(const chunk_id, T* p) { CallObjectSerialize(p, NBinSaverInternals::TOverloadPriority<2>()); return 0; - } + } int Add(const chunk_id, std::string* pStr) { - DataChunkString(*pStr); + DataChunkString(*pStr); return 0; - } + } int Add(const chunk_id, TString* pStr) { DataChunkStroka(*pStr); return 0; @@ -330,9 +330,9 @@ public: if (HasNonTrivialSerializer<T1>(0u)) DoVector(*pVec); else - DoDataVector(*pVec); + DoDataVector(*pVec); return 0; - } + } template <class T, int N> int Add(const chunk_id, T (*pVec)[N]) { @@ -352,7 +352,7 @@ public: int Add(const chunk_id, THashMap<T1, T2, T3, T4, T5>* pHash) { DoAnyMap(*pHash); return 0; - } + } template <class T1, class T2, class T3, class T4, class T5> int Add(const chunk_id, THashMultiMap<T1, T2, T3, T4, T5>* pHash) { DoAnyMultiMap(*pHash); @@ -374,32 +374,32 @@ public: if (HasNonTrivialSerializer<T1>(0u)) Do2DArray(*pArr); else - Do2DArrayData(*pArr); + Do2DArrayData(*pArr); return 0; - } + } template <class T1> int Add(const chunk_id, TList<T1>* pList) { TList<T1>& data = *pList; if (IsReading()) { - int nSize; - Add(2, &nSize); - data.clear(); - data.insert(data.begin(), nSize, T1()); + int nSize; + Add(2, &nSize); + data.clear(); + data.insert(data.begin(), nSize, T1()); } else { - int nSize = data.size(); - Add(2, &nSize); - } - int i = 1; + int nSize = data.size(); + Add(2, &nSize); + } + int i = 1; for (typename TList<T1>::iterator k = data.begin(); k != data.end(); ++k, ++i) Add(i + 2, &(*k)); return 0; - } + } template <class T1, class T2> int Add(const chunk_id, std::pair<T1, T2>* pData) { - Add(1, &(pData->first)); - Add(2, &(pData->second)); + Add(1, &(pData->first)); + Add(2, &(pData->second)); return 0; - } + } template <class T1, size_t N> int Add(const chunk_id, std::array<T1, N>* pData) { @@ -492,8 +492,8 @@ public: void AddPolymorphicBase(chunk_id, IObjectBase* pObject) { - (*pObject) & (*this); - } + (*pObject) & (*this); + } template <class T1, class T2> void DoPtr(TPtrBase<T1, T2>* pData) { @@ -502,8 +502,8 @@ public: if (IsReading()) pData->Set(CastToUserObject(LoadObject(), (T1*)nullptr)); else - StoreObject(pData->GetBarePtr()); - } + StoreObject(pData->GetBarePtr()); + } template <class T, class TPolicy> int Add(const chunk_id, TMaybe<T, TPolicy>* pData) { TMaybe<T, TPolicy>& data = *pData; @@ -523,7 +523,7 @@ public: } return 0; } - + template <typename TOne> void AddMulti(TOne& one) { Add(0, &one); @@ -545,7 +545,7 @@ public: return sizeof(std::declval<IBinSaver*>()->Add(0, std::declval<T*>())) != 1; } -public: +public: IBinSaver(IBinaryStream& stream, bool _bRead, bool stableOutput = false) : bRead(_bRead) , File(_bRead, stream) @@ -556,19 +556,19 @@ public: bool IsValid() const { return File.IsValid(); } -}; - -// realisation of forward declared serialisation operator +}; + +// realisation of forward declared serialisation operator template <class TUserObj, class TRef> int TPtrBase<TUserObj, TRef>::operator&(IBinSaver& f) { - f.DoPtr(this); - return 0; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// + f.DoPtr(this); + return 0; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// extern TClassFactory<IObjectBase>* pSaverClasses; -void StartRegisterSaveload(); +void StartRegisterSaveload(); template <class TReg> struct TRegisterSaveLoadType { diff --git a/library/cpp/binsaver/buffered_io.cpp b/library/cpp/binsaver/buffered_io.cpp index 30090372dd..dd88b04bc5 100644 --- a/library/cpp/binsaver/buffered_io.cpp +++ b/library/cpp/binsaver/buffered_io.cpp @@ -1,8 +1,8 @@ #include "buffered_io.h" - + i64 IBinaryStream::LongWrite(const void* userBuffer, i64 size) { Y_VERIFY(size >= 0, "IBinaryStream::Write() called with a negative buffer size."); - + i64 leftToWrite = size; while (leftToWrite != 0) { int writeSz = static_cast<int>(Min<i64>(leftToWrite, std::numeric_limits<int>::max())); diff --git a/library/cpp/binsaver/buffered_io.h b/library/cpp/binsaver/buffered_io.h index 58a22e7c78..75465c9c5c 100644 --- a/library/cpp/binsaver/buffered_io.h +++ b/library/cpp/binsaver/buffered_io.h @@ -1,10 +1,10 @@ -#pragma once +#pragma once #include <util/system/yassert.h> #include <util/generic/utility.h> #include <util/generic/ylimits.h> #include <string.h> - + struct IBinaryStream { virtual ~IBinaryStream() = default; ; @@ -25,7 +25,7 @@ struct IBinaryStream { } } - virtual bool IsValid() const = 0; + virtual bool IsValid() const = 0; virtual bool IsFailed() const = 0; private: @@ -34,15 +34,15 @@ private: i64 LongRead(void* userBuffer, i64 size); i64 LongWrite(const void* userBuffer, i64 size); -}; - +}; + template <int N_SIZE = 16384> class TBufferedStream { - char Buf[N_SIZE]; + char Buf[N_SIZE]; i64 Pos, BufSize; IBinaryStream& Stream; bool bIsReading, bIsEof, bFailed; - + void ReadComplex(void* userBuffer, i64 size) { if (bIsEof) { memset(userBuffer, 0, size); @@ -82,7 +82,7 @@ class TBufferedStream { void operator=(const TBufferedStream&) { } -public: +public: TBufferedStream(bool bRead, IBinaryStream& stream) : Pos(0) , BufSize(0) @@ -92,10 +92,10 @@ public: , bFailed(false) { } - ~TBufferedStream() { - if (!bIsReading) + ~TBufferedStream() { + if (!bIsReading) Flush(); - } + } void Flush() { Y_ASSERT(!bIsReading); if (bIsReading) @@ -109,26 +109,26 @@ public: } inline void Read(void* userBuffer, i64 size) { Y_ASSERT(bIsReading); - if (!bIsEof && size + Pos <= BufSize) { - memcpy(userBuffer, Buf + Pos, size); - Pos += size; - return; - } - ReadComplex(userBuffer, size); - } + if (!bIsEof && size + Pos <= BufSize) { + memcpy(userBuffer, Buf + Pos, size); + Pos += size; + return; + } + ReadComplex(userBuffer, size); + } inline void Write(const void* userBuffer, i64 size) { Y_ASSERT(!bIsReading); - if (Pos + size < N_SIZE) { - memcpy(Buf + Pos, userBuffer, size); - Pos += size; - return; - } - WriteComplex(userBuffer, size); - } + if (Pos + size < N_SIZE) { + memcpy(Buf + Pos, userBuffer, size); + Pos += size; + return; + } + WriteComplex(userBuffer, size); + } bool IsValid() const { return Stream.IsValid(); } bool IsFailed() const { return bFailed; } -}; +}; diff --git a/library/cpp/binsaver/class_factory.h b/library/cpp/binsaver/class_factory.h index 704ae9fb4b..e83512331b 100644 --- a/library/cpp/binsaver/class_factory.h +++ b/library/cpp/binsaver/class_factory.h @@ -1,27 +1,27 @@ -#pragma once +#pragma once -#include <typeinfo> +#include <typeinfo> #include <util/generic/hash.h> #include <util/generic/vector.h> #include <util/ysafeptr.h> - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// factory is using RTTI -// objects should inherit T and T must have at least 1 virtual function -template <class T> + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// factory is using RTTI +// objects should inherit T and T must have at least 1 virtual function +template <class T> class TClassFactory { -public: +public: typedef const std::type_info* VFT; -private: +private: typedef T* (*newFunc)(); typedef THashMap<int, newFunc> CTypeNewHash; // typeID->newFunc() typedef THashMap<VFT, int> CTypeIndexHash; // vftable->typeID - + CTypeIndexHash typeIndex; CTypeNewHash typeInfo; - + void RegisterTypeBase(int nTypeID, newFunc func, VFT vft); static VFT GetObjectType(T* pObject) { return &typeid(*pObject); @@ -39,7 +39,7 @@ private: return -1; } -public: +public: template <class TT> void RegisterType(int nTypeID, newFunc func, TT*) { RegisterTypeBase(nTypeID, func, &typeid(TT)); @@ -72,9 +72,9 @@ public: typeIds.push_back(iter->first); } } -}; -//////////////////////////////////////////////////////////////////////////////////////////////////// -template <class T> +}; +//////////////////////////////////////////////////////////////////////////////////////////////////// +template <class T> void TClassFactory<T>::RegisterTypeBase(int nTypeID, newFunc func, VFT vft) { if (typeInfo.find(nTypeID) != typeInfo.end()) { TObj<IObjectBase> o1 = typeInfo[nTypeID](); @@ -97,9 +97,9 @@ void TClassFactory<T>::RegisterTypeBase(int nTypeID, newFunc func, VFT vft) { } typeIndex[vft] = nTypeID; typeInfo[nTypeID] = func; -} -//////////////////////////////////////////////////////////////////////////////////////////////////// -// macro for registering CFundament derivatives +} +//////////////////////////////////////////////////////////////////////////////////////////////////// +// macro for registering CFundament derivatives #define REGISTER_CLASS(factory, N, name) factory.RegisterType(N, name::New##name, (name*)0); #define REGISTER_TEMPL_CLASS(factory, N, name, className) factory.RegisterType(N, name::New##className, (name*)0); #define REGISTER_CLASS_NM(factory, N, name, nmspace) factory.RegisterType(N, nmspace::name::New##name, (nmspace::name*)0); diff --git a/library/cpp/binsaver/mem_io.h b/library/cpp/binsaver/mem_io.h index 1d64db4fe5..2a9e36fe68 100644 --- a/library/cpp/binsaver/mem_io.h +++ b/library/cpp/binsaver/mem_io.h @@ -1,5 +1,5 @@ -#pragma once - +#pragma once + #include "bin_saver.h" namespace NMemIoInternals { @@ -22,7 +22,7 @@ namespace NMemIoInternals { bool IsFailed() const override { return false; } - + private: int WriteImpl(const void* userBuffer, int size) override { if (size == 0) @@ -63,8 +63,8 @@ namespace NMemIoInternals { *reinterpret_cast<T*>(&(*data)[0]) = c; } } - } - + } + //////////////////////////////////////////////////////////////////////////// class THugeMemoryStream: public IBinaryStream { TVector<TVector<char>>& Data; diff --git a/util/generic/guid.cpp b/util/generic/guid.cpp index 1225c0f44a..8b907457bc 100644 --- a/util/generic/guid.cpp +++ b/util/generic/guid.cpp @@ -7,7 +7,7 @@ #include <util/stream/format.h> #include <util/system/unaligned_mem.h> #include <util/random/easy.h> - + namespace { inline void LowerCaseHex(TString& s) { for (auto&& c : s) { diff --git a/util/generic/guid.h b/util/generic/guid.h index 94557e6f99..2bf6c8ad99 100644 --- a/util/generic/guid.h +++ b/util/generic/guid.h @@ -1,5 +1,5 @@ -#pragma once - +#pragma once + #include "fwd.h" #include <util/str_stl.h> @@ -16,7 +16,7 @@ */ struct TGUID { ui32 dw[4] = {}; - + constexpr bool IsEmpty() const noexcept { return (dw[0] | dw[1] | dw[2] | dw[3]) == 0; } @@ -42,7 +42,7 @@ struct TGUID { * https://datatracker.ietf.org/doc/html/rfc4122#section-4.1 **/ static TGUID CreateTimebased(); -}; +}; constexpr bool operator==(const TGUID& a, const TGUID& b) noexcept { return a.dw[0] == b.dw[0] && a.dw[1] == b.dw[1] && a.dw[2] == b.dw[2] && a.dw[3] == b.dw[3]; @@ -56,7 +56,7 @@ struct TGUIDHash { constexpr int operator()(const TGUID& a) const noexcept { return a.dw[0] + a.dw[1] + a.dw[2] + a.dw[3]; } -}; +}; template <> struct THash<TGUID> { |