diff options
author | ermolovd <ermolovd@yandex-team.ru> | 2022-02-10 16:47:53 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:53 +0300 |
commit | 2a4a975b112fa0fa138abc7457fe67e0e1e7fd02 (patch) | |
tree | c0748b5dcbade83af788c0abfa89c0383d6b779c /library/cpp/yson/node/node.cpp | |
parent | b91bcef7c99a119b0846479ef71bfab4beec72ca (diff) | |
download | ydb-2a4a975b112fa0fa138abc7457fe67e0e1e7fd02.tar.gz |
Restoring authorship annotation for <ermolovd@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/yson/node/node.cpp')
-rw-r--r-- | library/cpp/yson/node/node.cpp | 590 |
1 files changed, 295 insertions, 295 deletions
diff --git a/library/cpp/yson/node/node.cpp b/library/cpp/yson/node/node.cpp index 14c9aaefea..b39e070718 100644 --- a/library/cpp/yson/node/node.cpp +++ b/library/cpp/yson/node/node.cpp @@ -1,26 +1,26 @@ -#include "node.h" +#include "node.h" + +#include "node_io.h" + +#include <library/cpp/yson/writer.h> -#include "node_io.h" - -#include <library/cpp/yson/writer.h> - #include <util/generic/overloaded.h> -namespace NYT { +namespace NYT { -//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// -bool TNode::TNull::operator==(const TNull&) const { - return true; -} +bool TNode::TNull::operator==(const TNull&) const { + return true; +} //////////////////////////////////////////////////////////////////////////////// -bool TNode::TUndefined::operator==(const TUndefined&) const { - return true; -} +bool TNode::TUndefined::operator==(const TUndefined&) const { + return true; +} -//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// namespace NNodeCmp { @@ -91,17 +91,17 @@ bool operator>=(const TNode& lhs, const TNode& rhs) //////////////////////////////////////////////////////////////////////////////// -TNode::TNode() +TNode::TNode() : Value_(TUndefined{}) -{ } +{ } -TNode::TNode(const char* s) +TNode::TNode(const char* s) : Value_(TString(s)) -{ } +{ } TNode::TNode(TStringBuf s) : Value_(TString(s)) -{ } +{ } TNode::TNode(std::string_view s) : Value_(TString(s)) @@ -113,172 +113,172 @@ TNode::TNode(const std::string& s) TNode::TNode(TString s) : Value_(std::move(s)) -{ } +{ } -TNode::TNode(int i) +TNode::TNode(int i) : Value_(static_cast<i64>(i)) -{ } +{ } -TNode::TNode(unsigned int ui) +TNode::TNode(unsigned int ui) : Value_(static_cast<ui64>(ui)) -{ } +{ } -TNode::TNode(long i) +TNode::TNode(long i) : Value_(static_cast<i64>(i)) -{ } +{ } -TNode::TNode(unsigned long ui) +TNode::TNode(unsigned long ui) : Value_(static_cast<ui64>(ui)) -{ } +{ } -TNode::TNode(long long i) +TNode::TNode(long long i) : Value_(static_cast<i64>(i)) -{ } +{ } -TNode::TNode(unsigned long long ui) +TNode::TNode(unsigned long long ui) : Value_(static_cast<ui64>(ui)) -{ } +{ } -TNode::TNode(double d) +TNode::TNode(double d) : Value_(d) -{ } +{ } -TNode::TNode(bool b) +TNode::TNode(bool b) : Value_(b) -{ } +{ } TNode::TNode(TMapType map) : Value_(std::move(map)) { } -TNode::TNode(const TNode& rhs) - : TNode() -{ - if (rhs.Attributes_) { - CreateAttributes(); - *Attributes_ = *rhs.Attributes_; - } - Value_ = rhs.Value_; -} - -TNode& TNode::operator=(const TNode& rhs) -{ - if (this != &rhs) { - TNode tmp = rhs; - Move(std::move(tmp)); - } - return *this; -} +TNode::TNode(const TNode& rhs) + : TNode() +{ + if (rhs.Attributes_) { + CreateAttributes(); + *Attributes_ = *rhs.Attributes_; + } + Value_ = rhs.Value_; +} + +TNode& TNode::operator=(const TNode& rhs) +{ + if (this != &rhs) { + TNode tmp = rhs; + Move(std::move(tmp)); + } + return *this; +} TNode::TNode(TNode&& rhs) noexcept - : TNode() -{ - Move(std::move(rhs)); -} + : TNode() +{ + Move(std::move(rhs)); +} TNode& TNode::operator=(TNode&& rhs) noexcept -{ - if (this != &rhs) { - TNode tmp = std::move(rhs); - Move(std::move(tmp)); +{ + if (this != &rhs) { + TNode tmp = std::move(rhs); + Move(std::move(tmp)); } - return *this; -} + return *this; +} -TNode::~TNode() = default; +TNode::~TNode() = default; -void TNode::Clear() -{ - ClearAttributes(); - Value_ = TUndefined(); -} +void TNode::Clear() +{ + ClearAttributes(); + Value_ = TUndefined(); +} -bool TNode::IsString() const -{ +bool TNode::IsString() const +{ return std::holds_alternative<TString>(Value_); -} - -bool TNode::IsInt64() const -{ +} + +bool TNode::IsInt64() const +{ return std::holds_alternative<i64>(Value_); -} +} -bool TNode::IsUint64() const -{ +bool TNode::IsUint64() const +{ return std::holds_alternative<ui64>(Value_); -} +} -bool TNode::IsDouble() const -{ +bool TNode::IsDouble() const +{ return std::holds_alternative<double>(Value_); -} +} -bool TNode::IsBool() const -{ +bool TNode::IsBool() const +{ return std::holds_alternative<bool>(Value_); -} +} -bool TNode::IsList() const -{ +bool TNode::IsList() const +{ return std::holds_alternative<TListType>(Value_); -} +} -bool TNode::IsMap() const -{ +bool TNode::IsMap() const +{ return std::holds_alternative<TMapType>(Value_); -} +} -bool TNode::IsEntity() const -{ - return IsNull(); -} +bool TNode::IsEntity() const +{ + return IsNull(); +} -bool TNode::IsNull() const -{ +bool TNode::IsNull() const +{ return std::holds_alternative<TNull>(Value_); -} - -bool TNode::IsUndefined() const -{ +} + +bool TNode::IsUndefined() const +{ return std::holds_alternative<TUndefined>(Value_); -} - +} + bool TNode::HasValue() const { return !IsNull() && !IsUndefined(); } -bool TNode::Empty() const -{ - switch (GetType()) { - case String: +bool TNode::Empty() const +{ + switch (GetType()) { + case String: return std::get<TString>(Value_).empty(); - case List: + case List: return std::get<TListType>(Value_).empty(); - case Map: + case Map: return std::get<TMapType>(Value_).empty(); - default: + default: ythrow TTypeError() << "Empty() called for type " << GetType(); } -} +} -size_t TNode::Size() const -{ - switch (GetType()) { - case String: +size_t TNode::Size() const +{ + switch (GetType()) { + case String: return std::get<TString>(Value_).size(); - case List: + case List: return std::get<TListType>(Value_).size(); - case Map: + case Map: return std::get<TMapType>(Value_).size(); - default: + default: ythrow TTypeError() << "Size() called for type " << GetType(); } -} +} -TNode::EType TNode::GetType() const -{ +TNode::EType TNode::GetType() const +{ return std::visit(TOverloaded{ [](const TUndefined&) { return Undefined; }, [](const TString&) { return String; }, @@ -290,61 +290,61 @@ TNode::EType TNode::GetType() const [](const TMapType&) { return Map; }, [](const TNull&) { return Null; } }, Value_); -} +} const TString& TNode::AsString() const -{ - CheckType(String); +{ + CheckType(String); return std::get<TString>(Value_); -} +} -i64 TNode::AsInt64() const -{ - CheckType(Int64); +i64 TNode::AsInt64() const +{ + CheckType(Int64); return std::get<i64>(Value_); -} +} -ui64 TNode::AsUint64() const -{ - CheckType(Uint64); +ui64 TNode::AsUint64() const +{ + CheckType(Uint64); return std::get<ui64>(Value_); -} +} -double TNode::AsDouble() const -{ - CheckType(Double); +double TNode::AsDouble() const +{ + CheckType(Double); return std::get<double>(Value_); -} +} -bool TNode::AsBool() const -{ - CheckType(Bool); +bool TNode::AsBool() const +{ + CheckType(Bool); return std::get<bool>(Value_); -} +} const TNode::TListType& TNode::AsList() const -{ - CheckType(List); +{ + CheckType(List); return std::get<TListType>(Value_); -} +} const TNode::TMapType& TNode::AsMap() const -{ - CheckType(Map); +{ + CheckType(Map); return std::get<TMapType>(Value_); -} +} TNode::TListType& TNode::AsList() -{ - CheckType(List); +{ + CheckType(List); return std::get<TListType>(Value_); -} +} TNode::TMapType& TNode::AsMap() -{ - CheckType(Map); +{ + CheckType(Map); return std::get<TMapType>(Value_); -} +} const TString& TNode::UncheckedAsString() const noexcept { @@ -391,52 +391,52 @@ TNode::TMapType& TNode::UncheckedAsMap() noexcept return std::get<TMapType>(Value_); } -TNode TNode::CreateList() -{ - TNode node; +TNode TNode::CreateList() +{ + TNode node; node.Value_ = TListType{}; - return node; -} - -TNode TNode::CreateList(TListType list) -{ - TNode node; - node.Value_ = std::move(list); - return node; -} - -TNode TNode::CreateMap() -{ - TNode node; + return node; +} + +TNode TNode::CreateList(TListType list) +{ + TNode node; + node.Value_ = std::move(list); + return node; +} + +TNode TNode::CreateMap() +{ + TNode node; node.Value_ = TMapType{}; - return node; -} - -TNode TNode::CreateMap(TMapType map) -{ - TNode node; - node.Value_ = std::move(map); - return node; -} - -TNode TNode::CreateEntity() -{ - TNode node; + return node; +} + +TNode TNode::CreateMap(TMapType map) +{ + TNode node; + node.Value_ = std::move(map); + return node; +} + +TNode TNode::CreateEntity() +{ + TNode node; node.Value_ = TNull{}; - return node; -} + return node; +} -const TNode& TNode::operator[](size_t index) const -{ - CheckType(List); +const TNode& TNode::operator[](size_t index) const +{ + CheckType(List); return std::get<TListType>(Value_)[index]; -} +} -TNode& TNode::operator[](size_t index) -{ - CheckType(List); +TNode& TNode::operator[](size_t index) +{ + CheckType(List); return std::get<TListType>(Value_)[index]; -} +} const TNode& TNode::At(size_t index) const { CheckType(List); @@ -457,88 +457,88 @@ TNode& TNode::At(size_t index) { } TNode& TNode::Add() & -{ - AssureList(); +{ + AssureList(); return std::get<TListType>(Value_).emplace_back(); -} +} TNode TNode::Add() && -{ +{ return std::move(Add()); } TNode& TNode::Add(const TNode& node) & { - AssureList(); + AssureList(); std::get<TListType>(Value_).emplace_back(node); - return *this; -} + return *this; +} TNode TNode::Add(const TNode& node) && -{ +{ return std::move(Add(node)); } TNode& TNode::Add(TNode&& node) & { - AssureList(); + AssureList(); std::get<TListType>(Value_).emplace_back(std::move(node)); - return *this; -} + return *this; +} TNode TNode::Add(TNode&& node) && { return std::move(Add(std::move(node))); } -bool TNode::HasKey(const TStringBuf key) const -{ - CheckType(Map); +bool TNode::HasKey(const TStringBuf key) const +{ + CheckType(Map); return std::get<TMapType>(Value_).contains(key); -} +} TNode& TNode::operator()(const TString& key, const TNode& value) & -{ - AssureMap(); +{ + AssureMap(); std::get<TMapType>(Value_)[key] = value; - return *this; -} + return *this; +} TNode TNode::operator()(const TString& key, const TNode& value) && -{ +{ return std::move(operator()(key, value)); } TNode& TNode::operator()(const TString& key, TNode&& value) & { - AssureMap(); + AssureMap(); std::get<TMapType>(Value_)[key] = std::move(value); - return *this; -} + return *this; +} TNode TNode::operator()(const TString& key, TNode&& value) && { return std::move(operator()(key, std::move(value))); } -const TNode& TNode::operator[](const TStringBuf key) const -{ - CheckType(Map); - static TNode notFound; +const TNode& TNode::operator[](const TStringBuf key) const +{ + CheckType(Map); + static TNode notFound; const auto& map = std::get<TMapType>(Value_); TMapType::const_iterator i = map.find(key); - if (i == map.end()) { - return notFound; - } else { - return i->second; + if (i == map.end()) { + return notFound; + } else { + return i->second; } -} +} -TNode& TNode::operator[](const TStringBuf key) -{ - AssureMap(); +TNode& TNode::operator[](const TStringBuf key) +{ + AssureMap(); return std::get<TMapType>(Value_)[key]; -} +} const TNode& TNode::At(const TStringBuf key) const { CheckType(Map); @@ -778,101 +778,101 @@ TNode::TMapType& TNode::ChildAsMap(size_t index) { } } -bool TNode::HasAttributes() const -{ - return Attributes_ && !Attributes_->Empty(); -} +bool TNode::HasAttributes() const +{ + return Attributes_ && !Attributes_->Empty(); +} -void TNode::ClearAttributes() -{ - if (Attributes_) { - Attributes_.Destroy(); +void TNode::ClearAttributes() +{ + if (Attributes_) { + Attributes_.Destroy(); } -} +} -const TNode& TNode::GetAttributes() const -{ - static TNode notFound = TNode::CreateMap(); - if (!Attributes_) { - return notFound; +const TNode& TNode::GetAttributes() const +{ + static TNode notFound = TNode::CreateMap(); + if (!Attributes_) { + return notFound; } - return *Attributes_; -} + return *Attributes_; +} -TNode& TNode::Attributes() -{ - if (!Attributes_) { - CreateAttributes(); +TNode& TNode::Attributes() +{ + if (!Attributes_) { + CreateAttributes(); } - return *Attributes_; -} + return *Attributes_; +} -void TNode::MoveWithoutAttributes(TNode&& rhs) -{ - Value_ = std::move(rhs.Value_); - rhs.Clear(); -} +void TNode::MoveWithoutAttributes(TNode&& rhs) +{ + Value_ = std::move(rhs.Value_); + rhs.Clear(); +} -void TNode::Move(TNode&& rhs) -{ - Value_ = std::move(rhs.Value_); - Attributes_ = std::move(rhs.Attributes_); -} +void TNode::Move(TNode&& rhs) +{ + Value_ = std::move(rhs.Value_); + Attributes_ = std::move(rhs.Attributes_); +} -void TNode::CheckType(EType type) const -{ - Y_ENSURE_EX(GetType() == type, +void TNode::CheckType(EType type) const +{ + Y_ENSURE_EX(GetType() == type, TTypeError() << "TNode type " << type << " expected, actual type " << GetType(); - ); -} + ); +} -void TNode::AssureMap() -{ +void TNode::AssureMap() +{ if (std::holds_alternative<TUndefined>(Value_)) { Value_ = TMapType(); - } else { - CheckType(Map); + } else { + CheckType(Map); } -} +} -void TNode::AssureList() -{ +void TNode::AssureList() +{ if (std::holds_alternative<TUndefined>(Value_)) { Value_ = TListType(); - } else { - CheckType(List); + } else { + CheckType(List); } -} +} -void TNode::CreateAttributes() -{ +void TNode::CreateAttributes() +{ Attributes_ = MakeHolder<TNode>(); Attributes_->Value_ = TMapType(); -} +} void TNode::Save(IOutputStream* out) const -{ +{ NodeToYsonStream(*this, out, NYson::EYsonFormat::Binary); -} - +} + void TNode::Load(IInputStream* in) -{ - Clear(); +{ + Clear(); *this = NodeFromYsonStream(in, ::NYson::EYsonType::Node); -} - -//////////////////////////////////////////////////////////////////////////////// +} -bool operator==(const TNode& lhs, const TNode& rhs) -{ +//////////////////////////////////////////////////////////////////////////////// + +bool operator==(const TNode& lhs, const TNode& rhs) +{ if (std::holds_alternative<TNode::TUndefined>(lhs.Value_) || std::holds_alternative<TNode::TUndefined>(rhs.Value_)) { - // TODO: should try to remove this behaviour if nobody uses it. - return false; + // TODO: should try to remove this behaviour if nobody uses it. + return false; } - if (lhs.GetType() != rhs.GetType()) { + if (lhs.GetType() != rhs.GetType()) { return false; } @@ -890,15 +890,15 @@ bool operator==(const TNode& lhs, const TNode& rhs) } } - return rhs.Value_ == lhs.Value_; + return rhs.Value_ == lhs.Value_; } -bool operator!=(const TNode& lhs, const TNode& rhs) +bool operator!=(const TNode& lhs, const TNode& rhs) { return !(lhs == rhs); } -bool GetBool(const TNode& node) +bool GetBool(const TNode& node) { if (node.IsBool()) { return node.AsBool(); |