#pragma once #include "probe.h" #include #include #include namespace NLWTrace { class TSession; // Custom action can save any stuff (derived from IResource) in TSession object // IMPORTANT: Derived class will be used from multiple threads! (see example3) class IResource: public TAtomicRefCount { public: virtual ~IResource() { } }; using TResourcePtr = TIntrusivePtr; // Trace resources that is used to hold/get/create any stuff class TTraceResources: public THashMap { public: template T& Get(const TString& name) { auto iter = find(name); if (iter == end()) { iter = insert(value_type(name, TResourcePtr(new T()))).first; } return *static_cast(iter->second.Get()); } template const T* GetOrNull(const TString& name) const { auto iter = find(name); if (iter == end()) { return nullptr; } return *iter->second; } }; // Base class of all custom actions class TCustomActionExecutor: public IExecutor { protected: TProbe* const Probe; bool Destructive; public: TCustomActionExecutor(TProbe* probe, bool destructive) : IExecutor() , Probe(probe) , Destructive(destructive) { } bool IsDestructive() { return Destructive; } }; // Factory to produce custom action executors class TCustomActionFactory { public: using TCallback = std::function; TCustomActionExecutor* Create(TProbe* probe, const TCustomAction& action, TSession* trace) const; void Register(const TString& name, const TCallback& callback); private: THashMap Callbacks; }; }