Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef ESC_ACTION_HPP
- #define ESC_ACTION_HPP
- #include "EscCore.hpp"
- #define DEBUG_OFF -1
- #define DEBUG_ON 1
- #define DEBUG_FUNC 2
- static Atomic __LogDebugMode;
- static Atomic __LogTextOffset;
- static Atomic __LogInFunction;
- static String __LogLastFunction;
- inline int GetDebugMode() { return AtomicRead(__LogDebugMode); }
- inline void SetDebugMode(int mode) { AtomicWrite(__LogDebugMode, mode); }
- inline void FuncLog0(const String& msg, Atomic& offset, const String& func, Atomic& count)
- {
- String esc;
- for (int i = 0; i < AtomicRead(offset); ++i)
- esc << " ";
- if (__LogLastFunction != func)
- {
- __LogLastFunction = func;
- LOG((esc.GetCount() >= 3 ? esc.Right(esc.GetCount() - 3) : "") + func);
- }
- LOG(esc + msg);
- AtomicInc(count);
- }
- #define FuncLog(MSG) FuncLog0(MSG, __LogTextOffset, __func, __LogInFunction)
- #define FuncError(MSG) { FuncLog0(String("Error: ") + MSG, __LogTextOffset, __func, \
- __LogInFunction); done = false; }
- #define ActionHeader(ACTION, ARGC) \
- class A##ACTION : public EscAction \
- { \
- public: \
- A##ACTION() { SetArgc(ARGC); } \
- inline A##ACTION& SetParent(CLASSNAME *parent) \
- { \
- Parent = parent; \
- return *this; \
- } \
- virtual bool Execute(EscValue& result, const Vector<Value>& args); \
- CLASSNAME *Parent; \
- } I##ACTION; \
- inline void M##ACTION(bool& FUNC_result, EscValue& ESC_result, const Vector<Value>& args) \
- { \
- FUNC_result = I##ACTION.Execute(ESC_result, args); \
- }
- #define ActionMethod(PARENT, ACTION) \
- bool PARENT::A##ACTION::Execute(EscValue& result, const Vector<Value>& args) \
- { \
- bool done = true; \
- String __func = Format("-----\r\nFUNCTION: %s(", GetName()); \
- for (int i = 0; i < args.GetCount(); ++i) \
- __func << args[i].ToString() << (i != args.GetCount() - 1 ? ", " : ")"); \
- if (GetDebugMode() != DEBUG_OFF) \
- FuncLog("DEBUG: enabled"); \
- AtomicInc(__LogTextOffset);
- #define EndActionMethod() \
- AtomicDec(__LogTextOffset); \
- int logInFunc = AtomicRead(__LogInFunction); \
- if (logInFunc > 0) \
- { \
- AtomicWrite(__LogInFunction, logInFunc - 2); \
- FuncLog(String("RETURN: ") + result.ToString() \
- + (done ? " (Success!)" : " (Failed!)") + "\r\n-----\r\n"); \
- } \
- if (GetDebugMode() == DEBUG_FUNC && GetName() != "DebugFunc") \
- SetDebugMode(DEBUG_OFF); \
- return done; \
- }
- #define RegisterActionMethod(ACTION, FUNC_NAME) \
- RegisterAction(&I##ACTION.SetParent(this).SetName(FUNC_NAME)); \
- I##ACTION.SetAction(THISBACK(M##ACTION));
- struct ActionInfo
- {
- int Argc;
- String Name;
- String Description;
- };
- class EscAction : Moveable<EscAction>
- {
- public:
- EscAction() { SetArgc(0); }
- EscAction(const String& name, const Callback3<bool&, EscValue&, const Vector<Value>& >&
- action, int argc = 0)
- {
- SetArgc(argc);
- SetName(name);
- SetAction(action);
- }
- virtual bool Execute(EscValue& result, const Vector<Value>& args = Vector<Value>()) const
- {
- bool flag;
- _action.Execute(flag, result, args);
- return flag;
- }
- EscAction& SetArgc(int argc) { _info.Argc = argc > 0 ? argc : 0; return *this; }
- EscAction& SetName(const String& name) { _info.Name = name; return *this; }
- EscAction& SetDescription(const String& desc) { _info.Description = desc; return *this; }
- int GetArgc() const { return _info.Argc; }
- String GetName() const { return _info.Name; }
- String GetDescription() const { return _info.Description; }
- EscAction& SetAction(const Callback3<bool&, EscValue&, const Vector<Value>& >& action)
- {
- _action = action;
- return *this;
- }
- protected:
- ActionInfo _info;
- Callback3<bool&, EscValue&, const Vector<Value>& > _action;
- };
- #endif // .. ESC_ACTION_HPP
Add Comment
Please, Sign In to add comment