aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/scheme/scimpl_private.h
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/scheme/scimpl_private.h
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/scheme/scimpl_private.h')
-rw-r--r--library/cpp/scheme/scimpl_private.h114
1 files changed, 114 insertions, 0 deletions
diff --git a/library/cpp/scheme/scimpl_private.h b/library/cpp/scheme/scimpl_private.h
new file mode 100644
index 0000000000..b92badabde
--- /dev/null
+++ b/library/cpp/scheme/scimpl_private.h
@@ -0,0 +1,114 @@
+#pragma once
+
+#include "scheme.h"
+
+#include <util/thread/singleton.h>
+
+namespace NSc {
+ namespace NImpl {
+ template <typename TContext>
+ static inline TContext& GetTlsInstance() {
+ return *FastTlsSingleton<TContext>();
+ }
+
+ template <typename TContext>
+ class TContextGuard : TNonCopyable {
+ using TElement = typename TContext::TElement;
+ using TTarget = typename TContext::TTarget;
+ using TVectorType = TVector<TElement>;
+
+ public:
+ TContextGuard(TContext& ctx, TTarget& target)
+ : Ctx(ctx)
+ , Active(TContext::Needed(target))
+ {
+ if (Active) {
+ Begin = Ctx.Vector.size();
+ Ok = Ctx.Process(target);
+ End = Ctx.Vector.size();
+ }
+ }
+
+ ~TContextGuard() noexcept {
+ if (Active) {
+ Ctx.Vector.resize(Begin);
+ }
+ }
+
+ const TVectorType& GetVector() const {
+ return Ctx.Vector;
+ }
+
+ using const_iterator = size_t;
+
+ size_t begin() const {
+ return Begin;
+ }
+
+ size_t end() const {
+ return End;
+ }
+
+ bool Ok = true;
+
+ private:
+ TContext& Ctx;
+ size_t Begin = 0;
+ size_t End = 0;
+ bool Active = false;
+ };
+
+ template <typename TElem, typename TTgt>
+ class TBasicContext {
+ public:
+ using TElement = TElem;
+ using TTarget = TTgt;
+
+ TBasicContext() {
+ Vector.reserve(64);
+ }
+
+ TVector<TElement> Vector;
+ };
+
+ class TKeySortContext: public TBasicContext<TStringBuf, const TDict> {
+ public:
+ using TGuard = TContextGuard<TKeySortContext>;
+
+ bool Process(const TDict& self);
+
+ static bool Needed(const TDict& self) {
+ return self.size();
+ }
+ };
+
+ class TSelfOverrideContext: public TBasicContext<TValue, TValue::TScCore> {
+ public:
+ using TGuard = TContextGuard<TSelfOverrideContext>;
+
+ bool Process(TValue::TScCore& self);
+
+ static bool Needed(const TValue::TScCore& self) {
+ return self.HasChildren();
+ }
+ };
+
+ class TSelfLoopContext: public TBasicContext<const void*, const TValue::TScCore> {
+ public:
+ enum class EMode {
+ Assert, Throw, Abort, Stderr
+ };
+
+ using TGuard = TContextGuard<TSelfLoopContext>;
+
+ bool Process(const TValue::TScCore& self);
+
+ static bool Needed(const TValue::TScCore& self) {
+ return self.HasChildren();
+ }
+
+ public:
+ EMode ReportingMode = EMode::Assert;
+ };
+ }
+}