Guest User

Untitled

a guest
Jun 17th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. #ifndef ESC_ACTION_HPP
  2. #define ESC_ACTION_HPP
  3.  
  4. #include "EscCore.hpp"
  5.  
  6. #define DEBUG_OFF -1
  7. #define DEBUG_ON   1
  8. #define DEBUG_FUNC 2
  9.  
  10. static Atomic __LogDebugMode;
  11. static Atomic __LogTextOffset;
  12. static Atomic __LogInFunction;
  13. static String __LogLastFunction;
  14.  
  15. inline int  GetDebugMode()         { return AtomicRead(__LogDebugMode); }
  16. inline void SetDebugMode(int mode) { AtomicWrite(__LogDebugMode, mode); }
  17.  
  18. inline void FuncLog0(const String& msg, Atomic& offset, const String& func, Atomic& count)
  19. {
  20.     String esc;
  21.     for (int i = 0; i < AtomicRead(offset); ++i)
  22.         esc << "   ";
  23.     if (__LogLastFunction != func)
  24.     {
  25.         __LogLastFunction = func;
  26.         LOG((esc.GetCount() >= 3 ? esc.Right(esc.GetCount() - 3) : "") + func);
  27.     }
  28.     LOG(esc + msg);
  29.     AtomicInc(count);
  30. }
  31.  
  32. #define FuncLog(MSG)   FuncLog0(MSG, __LogTextOffset, __func, __LogInFunction)
  33. #define FuncError(MSG) { FuncLog0(String("Error: ") + MSG, __LogTextOffset, __func, \
  34.                        __LogInFunction); done = false; }
  35.  
  36. #define ActionHeader(ACTION, ARGC) \
  37. class A##ACTION : public EscAction \
  38. { \
  39. public: \
  40.         A##ACTION() { SetArgc(ARGC); } \
  41.         inline A##ACTION& SetParent(CLASSNAME *parent) \
  42.         { \
  43.                 Parent = parent; \
  44.                 return *this; \
  45.         } \
  46.         virtual bool Execute(EscValue& result, const Vector<Value>& args); \
  47.         CLASSNAME *Parent; \
  48. } I##ACTION; \
  49. inline void M##ACTION(bool& FUNC_result, EscValue& ESC_result, const Vector<Value>& args) \
  50. { \
  51.         FUNC_result = I##ACTION.Execute(ESC_result, args); \
  52. }
  53.  
  54. #define ActionMethod(PARENT, ACTION) \
  55. bool PARENT::A##ACTION::Execute(EscValue& result, const Vector<Value>& args) \
  56. { \
  57.         bool done = true; \
  58.         String __func = Format("-----\r\nFUNCTION: %s(", GetName()); \
  59.             for (int i = 0; i < args.GetCount(); ++i) \
  60.                 __func << args[i].ToString() << (i != args.GetCount() - 1 ? ", " : ")"); \
  61.         if (GetDebugMode() != DEBUG_OFF) \
  62.             FuncLog("DEBUG: enabled"); \
  63.         AtomicInc(__LogTextOffset);
  64.  
  65. #define EndActionMethod() \
  66.         AtomicDec(__LogTextOffset); \
  67.         int logInFunc = AtomicRead(__LogInFunction); \
  68.         if (logInFunc > 0) \
  69.         { \
  70.             AtomicWrite(__LogInFunction, logInFunc - 2); \
  71.             FuncLog(String("RETURN: ") + result.ToString() \
  72.                 + (done ? " (Success!)" : " (Failed!)") + "\r\n-----\r\n"); \
  73.         } \
  74.         if (GetDebugMode() == DEBUG_FUNC && GetName() != "DebugFunc") \
  75.             SetDebugMode(DEBUG_OFF); \
  76.         return done; \
  77. }
  78.  
  79. #define RegisterActionMethod(ACTION, FUNC_NAME) \
  80.     RegisterAction(&I##ACTION.SetParent(this).SetName(FUNC_NAME)); \
  81.     I##ACTION.SetAction(THISBACK(M##ACTION));
  82.  
  83. struct ActionInfo
  84. {
  85.     int Argc;
  86.     String Name;
  87.     String Description;
  88. };
  89.  
  90. class EscAction : Moveable<EscAction>
  91. {
  92. public:
  93.     EscAction() { SetArgc(0); }
  94.  
  95.     EscAction(const String& name, const Callback3<bool&, EscValue&, const Vector<Value>& >&
  96.         action, int argc = 0)
  97.     {
  98.         SetArgc(argc);
  99.         SetName(name);
  100.         SetAction(action);
  101.     }
  102.  
  103.     virtual bool Execute(EscValue& result, const Vector<Value>& args = Vector<Value>()) const
  104.     {
  105.         bool flag;
  106.         _action.Execute(flag, result, args);
  107.         return flag;
  108.     }
  109.  
  110.     EscAction& SetArgc(int argc)          { _info.Argc = argc > 0 ? argc : 0; return *this; }
  111.     EscAction& SetName(const String& name)        { _info.Name = name; return *this; }
  112.     EscAction& SetDescription(const String& desc) { _info.Description = desc; return *this; }
  113.  
  114.     int    GetArgc()        const { return _info.Argc; }
  115.     String GetName()        const { return _info.Name; }
  116.     String GetDescription() const { return _info.Description; }
  117.  
  118.     EscAction& SetAction(const Callback3<bool&, EscValue&, const Vector<Value>& >& action)
  119.     {
  120.         _action = action;
  121.         return *this;
  122.     }
  123.  
  124. protected:
  125.     ActionInfo _info;
  126.     Callback3<bool&, EscValue&, const Vector<Value>& > _action;
  127. };
  128.  
  129. #endif // .. ESC_ACTION_HPP
Add Comment
Please, Sign In to add comment