Mana
Actor.h
Go to the documentation of this file.
1 
8 #pragma once
9 #include "Buffer.h"
10 #include "Event.h"
11 #include "Stack.h"
12 
13 #include <bitset>
14 #include <map>
15 #include <string>
16 #include <unordered_map>
17 
18 namespace mana
19 {
20  class VM;
21 
25  class Actor : Noncopyable, public std::enable_shared_from_this<Actor>
26  {
27  friend class VM;
28 
29  public:
31  explicit Actor(const std::shared_ptr<VM>& vm, const address_t variableSize);
32 
33  Actor(const Actor&) = delete;
34  Actor(Actor&&) noexcept = delete;
35  Actor& operator=(const Actor&) = delete;
36  Actor& operator=(Actor&&) noexcept = delete;
37  virtual ~Actor() = default;
38 
39  std::shared_ptr<Actor> Clone() const;
40 
41  bool Run();
42  bool SyncCall(const int32_t priority, const char* action, const std::shared_ptr<Actor>& sender);
43  bool AsyncCall(const int32_t priority, const char* action, const std::shared_ptr<Actor>& sender);
44 
45  bool Request(const int32_t priority, const char* action, const std::shared_ptr<Actor>& sender);
46  void Rollback(const int32_t priority);
47  void Restart();
48 
49  std::string_view GetName();
50  uint32_t GetAction(const std::string_view& actionName) const;
51 
52  [[nodiscard]] int32_t GetArgumentCount() const;
53  [[nodiscard]] int32_t GetArgumentCountByAddress(const uint32_t address) const;
54  [[nodiscard]] int32_t GetArgumentSize(const uint32_t address) const;
55  [[nodiscard]] bool HasReturnValue(const uint32_t address) const;
56  [[nodiscard]] int32_t GetParameterInteger(const int32_t value) const;
57  [[nodiscard]] float GetParameterFloat(const int32_t value) const;
58  [[nodiscard]] const char* GetParameterString(const int32_t value) const;
59  [[nodiscard]] std::shared_ptr<Actor> GetParameterActor(const int32_t value) const;
60  [[nodiscard]] Actor* GetParameterActorPointer(const int32_t value) const;
61  [[nodiscard]] void* GetParameterPointer(const int32_t value) const;
62  [[nodiscard]] void* GetParameterAddress(const int32_t value) const;
63  void SetReturnInteger(const int32_t value);
64  void SetReturnFloat(const float value);
65  void SetReturnString(const char* string);
66  void SetReturnActor(const std::shared_ptr<Actor>& actor);
67  void SetReturnPointer(void* pointer);
68  void SetReturnData(const void* pointer, const int32_t size);
69 
70 #if MANA_BUILD_TARGET < MANA_BUILD_RELEASE
71  [[nodiscard]] std::string_view GetActorName() const;
72  void SetActorName(const std::string_view& name);
73  [[nodiscard]] std::string GetActionName() const;
74  [[nodiscard]] std::string_view GetFunctionName() const;
75 #endif
76 
77  std::shared_ptr<VM> GetVirtualMachine() const;
78  [[nodiscard]] bool IsCommandInitialized() const;
79  [[nodiscard]] bool IsCommandRepeat() const;
80  [[nodiscard]] bool IsRunning() const;
81  void Repeat(const bool initialComplete);
82  void Again();
83  void Halt();
84  void Stop();
85  void yield();
86  void Accept();
87  void Refuse();
88  [[nodiscard]] int32_t GetInterruptPriority() const;
89  [[nodiscard]] bool IsSynchronized() const;
90  void SetSynchronized(const bool synchronized);
91  void SetSynchronizedWithPriority(const int32_t priority, const bool synchronized);
92 
93  [[nodiscard]] EventNameType AddPriorityChangedEvent(const std::function<void(int32_t, int32_t)>& function);
94  void RemovePriorityChangedEvent(const EventNameType eventName);
95 
96  [[nodiscard]] Stack& GetStack();
97  [[nodiscard]] const Stack& GetStack() const;
98 
99  private:
100  void SetAction(const std::string_view& actionName, const uint32_t address);
101  //void Initialize(const ActionInfoHeader* actionInfo);
102 
103  private:
104  static constexpr int32_t LowestInterruptPriority = std::numeric_limits<int32_t>::lowest();
105  static constexpr uint32_t Nil = static_cast<uint32_t>(~0);
106 
108  struct Interrupt final
109  {
110  std::shared_ptr<Actor> mSender;
111  uint32_t mAddress = Nil;
112  uint32_t mReturnAddress = Nil;
113  address_t mFramePointer = Nil;
114  address_t mStackPointer = Nil;
115  std::bitset<8> mFlag = 0;
116 #if MANA_BUILD_TARGET < MANA_BUILD_RELEASE
117  std::string mActionName;
118 #endif
119 
120  enum class Flag : uint8_t
121  {
122  Initialized,
123  IsInSyncCall,
124  Repeat,
125  Suspend,
126  Synchronized
127  };
128  };
129 
131  struct ReturnValue final
132  {
133  union
134  {
135  int_t mIntegerValue;
136  float_t mFloatValue;
137  const char* mStringValue;
138  void* mPointerValue;
139  Actor* mActorValue;
140  } mValues;
141  int32_t mSize;
142 
143  static constexpr int32_t Invalid = 0;
144  static constexpr int32_t Actor = -1;
145  static constexpr int32_t Integer = -2;
146  static constexpr int32_t Float = -3;
147  static constexpr int32_t String = -4;
148 
149  ReturnValue()
150  : mSize(0)
151  {
152  mValues.mPointerValue = nullptr;
153  }
154  };
155 
156  std::weak_ptr<VM> mVM;
157  std::unordered_map <std::string_view, uint32_t> mActions;
158  Buffer mFrame;
159  Stack mStack;
160  std::map<int32_t, Interrupt> mInterrupts;
161  ReturnValue mReturnValue;
162  Event<int32_t, int32_t> mOnPriorityChanged;
163  Buffer mVariable;
164  address_t mPc = Nil;
165  int32_t mInterruptPriority = LowestInterruptPriority;
166  std::bitset<8> mFlag = 0;
167  enum class Flag : uint8_t
168  {
169  Halt,
170  Running,
171  Touched,
172  Hided,
173  Requested,
174  Refused
175  };
176 
177 #if MANA_BUILD_TARGET < MANA_BUILD_RELEASE
178  std::string_view mName;
179 #endif
180 
181  private:
182  static void CommandRestart(const std::shared_ptr<VM>& vm, Actor& self);
183  static void CommandHalt(const std::shared_ptr<VM>& vm, Actor& self);
184  static void CommandYield(const std::shared_ptr<VM>& vm, Actor& self);
185  static void CommandSetNonPreemptive(const std::shared_ptr<VM>& vm, Actor& self);
186  static void CommandSetPreemptive(const std::shared_ptr<VM>& vm, Actor& self);
187  static void CommandPushZeroInteger(const std::shared_ptr<VM>& vm, Actor& self);
188  static void CommandPushZeroFloat(const std::shared_ptr<VM>& vm, Actor& self);
189  static void CommandPushChar(const std::shared_ptr<VM>& vm, Actor& self);
190  static void CommandPushShort(const std::shared_ptr<VM>& vm, Actor& self);
191  static void CommandPushInteger(const std::shared_ptr<VM>& vm, Actor& self);
192  static void CommandPushFloat(const std::shared_ptr<VM>& vm, Actor& self);
193  static void CommandPushString(const std::shared_ptr<VM>& vm, Actor& self);
194  static void CommandPushPriority(const std::shared_ptr<VM>& vm, Actor& self);
195  static void CommandPushSelf(const std::shared_ptr<VM>& vm, Actor& self);
196  static void CommandPushSender(const std::shared_ptr<VM>& vm, Actor& self);
197  static void CommandAllocate(const std::shared_ptr<VM>& vm, Actor& self);
198  static void CommandRelease(const std::shared_ptr<VM>& vm, Actor& self);
199  static void CommandDuplicate(const std::shared_ptr<VM>& vm, Actor& self);
200  static void CommandRemove(const std::shared_ptr<VM>& vm, Actor& self);
201  static void CommandLoadStaticAddress(const std::shared_ptr<VM>& vm, Actor& self);
202  static void CommandLoadGlobalAddress(const std::shared_ptr<VM>& vm, Actor& self);
203  static void CommandLoadFrameAddress(const std::shared_ptr<VM>& vm, Actor& self);
204  static void CommandLoadSelfAddress(const std::shared_ptr<VM>& vm, Actor& self);
205  static void CommandLoadInt8(const std::shared_ptr<VM>& vm, Actor& self);
206  static void CommandLoadInt16(const std::shared_ptr<VM>& vm, Actor& self);
207  static void CommandLoadInt32(const std::shared_ptr<VM>& vm, Actor& self);
208  static void CommandLoadFloat(const std::shared_ptr<VM>& vm, Actor& self);
209  static void CommandLoadReference(const std::shared_ptr<VM>& vm, Actor& self);
210  static void CommandStoreInt8(const std::shared_ptr<VM>& vm, Actor& self);
211  static void CommandStoreInt16(const std::shared_ptr<VM>& vm, Actor& self);
212  static void CommandStoreInt32(const std::shared_ptr<VM>& vm, Actor& self);
213  static void CommandStoreFloat(const std::shared_ptr<VM>& vm, Actor& self);
214  static void CommandStoreReference(const std::shared_ptr<VM>& vm, Actor& self);
215  static void CommandBranchEqual(const std::shared_ptr<VM>& vm, Actor& self);
216  static void CommandBranchNotEqual(const std::shared_ptr<VM>& vm, Actor& self);
217  static void CommandBranchAway(const std::shared_ptr<VM>& vm, Actor& self);
218  static void CommandBranchSubRoutine(const std::shared_ptr<VM>& vm, Actor& self);
219  static void CommandCall(const std::shared_ptr<VM>& vm, Actor& self);
220  static void CommandRequest(const std::shared_ptr<VM>& vm, Actor& self);
221  static void CommandRequestWaitStarting(const std::shared_ptr<VM>& vm, Actor& self);
222  static void CommandRequestWaitEnding(const std::shared_ptr<VM>& vm, Actor& self);
223  static void CommandDynamicRequest(const std::shared_ptr<VM>& vm, Actor& self);
224  static void CommandDynamicRequestWaitStarting(const std::shared_ptr<VM>& vm, Actor& self);
225  static void CommandDynamicRequestWaitEnded(const std::shared_ptr<VM>& vm, Actor& self);
226  static void CommandJoin(const std::shared_ptr<VM>& vm, Actor& self);
227  static void CommandComply(const std::shared_ptr<VM>& vm, Actor& self);
228  static void CommandRefuse(const std::shared_ptr<VM>& vm, Actor& self);
229  static void CommandLoadReturnAddress(const std::shared_ptr<VM>& vm, Actor& self);
230  static void CommandStoreReturnAddress(const std::shared_ptr<VM>& vm, Actor& self);
231  static void CommandReturnFromFunction(const std::shared_ptr<VM>& vm, Actor& self);
232  static void CommandReturnFromAction(const std::shared_ptr<VM>& vm, Actor& self);
233  static void CommandRollback(const std::shared_ptr<VM>& vm, Actor& self);
234  static void CommandAddInteger(const std::shared_ptr<VM>& vm, Actor& self);
235  static void CommandAddFloat(const std::shared_ptr<VM>& vm, Actor& self);
236  static void CommandDivideInteger(const std::shared_ptr<VM>& vm, Actor& self);
237  static void CommandDivideFloat(const std::shared_ptr<VM>& vm, Actor& self);
238  static void CommandMinusInteger(const std::shared_ptr<VM>& vm, Actor& self);
239  static void CommandMinusFloat(const std::shared_ptr<VM>& vm, Actor& self);
240  static void CommandModInteger(const std::shared_ptr<VM>& vm, Actor& self);
241  static void CommandModFloat(const std::shared_ptr<VM>& vm, Actor& self);
242  static void CommandMultiplyInteger(const std::shared_ptr<VM>& vm, Actor& self);
243  static void CommandMultiplyFloat(const std::shared_ptr<VM>& vm, Actor& self);
244  static void CommandSubtractInteger(const std::shared_ptr<VM>& vm, Actor& self);
245  static void CommandSubtractFloat(const std::shared_ptr<VM>& vm, Actor& self);
246  static void CommandAnd(const std::shared_ptr<VM>& vm, Actor& self);
247  static void CommandExclusiveOr(const std::shared_ptr<VM>& vm, Actor& self);
248  static void CommandLogicalAnd(const std::shared_ptr<VM>& vm, Actor& self);
249  static void CommandLogicalOr(const std::shared_ptr<VM>& vm, Actor& self);
250  static void CommandNot(const std::shared_ptr<VM>& vm, Actor& self);
251  static void CommandLogicalNot(const std::shared_ptr<VM>& vm, Actor& self);
252  static void CommandOr(const std::shared_ptr<VM>& vm, Actor& self);
253  static void CommandShiftLeft(const std::shared_ptr<VM>& vm, Actor& self);
254  static void CommandShiftRight(const std::shared_ptr<VM>& vm, Actor& self);
255  static void CommandCompareEqualInteger(const std::shared_ptr<VM>& vm, Actor& self);
256  static void CommandCompareEqualFloat(const std::shared_ptr<VM>& vm, Actor& self);
257  static void CommandCompareGreaterEqualInteger(const std::shared_ptr<VM>& vm, Actor& self);
258  static void CommandCompareGreaterEqualFloat(const std::shared_ptr<VM>& vm, Actor& self);
259  static void CommandCompareGreaterInteger(const std::shared_ptr<VM>& vm, Actor& self);
260  static void CommandCompareGreaterFloat(const std::shared_ptr<VM>& vm, Actor& self);
261  static void CommandCompareNotEqualInteger(const std::shared_ptr<VM>& vm, Actor& self);
262  static void CommandCompareNotEqualFloat(const std::shared_ptr<VM>& vm, Actor& self);
263  static void CommandCompareLessEqualInteger(const std::shared_ptr<VM>& vm, Actor& self);
264  static void CommandCompareLessEqualFloat(const std::shared_ptr<VM>& vm, Actor& self);
265  static void CommandCompareLessInteger(const std::shared_ptr<VM>& vm, Actor& self);
266  static void CommandCompareLessFloat(const std::shared_ptr<VM>& vm, Actor& self);
267  static void CommandIntegerToFloat(const std::shared_ptr<VM>& vm, Actor& self);
268  static void CommandFloatToInteger(const std::shared_ptr<VM>& vm, Actor& self);
269  static void CommandPushActor(const std::shared_ptr<VM>& vm, Actor& self);
270  static void CommandLoadData(const std::shared_ptr<VM>& vm, Actor& self);
271  static void CommandStoreData(const std::shared_ptr<VM>& vm, Actor& self);
272  static void CommandDuplicateData(const std::shared_ptr<VM>& vm, Actor& self);
273  static void CommandRemoveData(const std::shared_ptr<VM>& vm, Actor& self);
274  static void CommandCompareEqualData(const std::shared_ptr<VM>& vm, Actor& self);
275  static void CommandCompareGreaterEqualData(const std::shared_ptr<VM>& vm, Actor& self);
276  static void CommandCompareGreaterData(const std::shared_ptr<VM>& vm, Actor& self);
277  static void CommandCompareNotEqualData(const std::shared_ptr<VM>& vm, Actor& self);
278  static void CommandCompareLessEqualData(const std::shared_ptr<VM>& vm, Actor& self);
279  static void CommandCompareLessData(const std::shared_ptr<VM>& vm, Actor& self);
280  static void CommandPrint(const std::shared_ptr<VM>& vm, Actor& self);
281  };
282 }
283 
284 #if MANA_BUILD_TARGET > MANA_BUILD_DEBUG
286 #define MANA_ASSERT_PARAMETER(P, I) { \
287  if(P->GetArgumentCount() != I) \
288  return; \
289 }
291 #define MANA_ASSERT_ILLIGAL_CALL_IN_INIT_ACTION(P) { \
292  if(P->GetVirtualMachine()->IsInInitAction()){ \
293  return; \
294 }
295 #else
297 #define MANA_ASSERT_PARAMETER(P, I) \
298  if((P)->GetArgumentCount() != (I)){ \
299  MANA_PRINT({ "ERROR: ", (P)->GetName(), ": function ", (P)->GetFunctionName(), " number of arguments ", std::to_string((P)->GetArgumentCount()), " correct ", std::to_string(I), "\n" });\
300  return; \
301  }
303 #define MANA_ASSERT_CANT_CALL_IN_INIT_ACTION(P) \
304  if((P)->GetVirtualMachine()->IsInInitAction()){ \
305  MANA_PRINT({ "ERROR: ", (P)->GetName(), ": init action ", (P)->GetFunctionName()," can not call\n" });\
306  return; \
307  }
308 #endif
309 
310 #include "Actor.inl"
Definition: Actor.h:26
Actor(const std::shared_ptr< VM > &vm, const address_t variableSize)
Constructor.
Definition: Actor.inl:14
bool Run()
Definition: Actor.inl:29
int32_t GetInterruptPriority() const
Definition: Actor.inl:752
void SetReturnActor(const std::shared_ptr< Actor > &actor)
Definition: Actor.inl:616
void Again()
Definition: Actor.inl:709
bool IsRunning() const
Definition: Actor.inl:688
void SetReturnString(const char *string)
Definition: Actor.inl:610
void Repeat(const bool initialComplete)
Definition: Actor.inl:695
void yield()
Definition: Actor.inl:734
int32_t GetArgumentSize(const uint32_t address) const
Definition: Actor.inl:546
void Stop()
Definition: Actor.inl:729
void Restart()
Definition: Actor.inl:502
bool SyncCall(const int32_t priority, const char *action, const std::shared_ptr< Actor > &sender)
Definition: Actor.inl:224
void SetSynchronized(const bool synchronized)
Definition: Actor.inl:765
void SetSynchronizedWithPriority(const int32_t priority, const bool synchronized)
Definition: Actor.inl:776
bool IsCommandRepeat() const
Definition: Actor.inl:680
void SetReturnFloat(const float value)
Definition: Actor.inl:604
bool Request(const int32_t priority, const char *action, const std::shared_ptr< Actor > &sender)
Definition: Actor.inl:260
std::string_view GetName()
Definition: Actor.inl:518
bool IsCommandInitialized() const
Definition: Actor.inl:672
EventNameType AddPriorityChangedEvent(const std::function< void(int32_t, int32_t)> &function)
Definition: Actor.inl:787
int32_t GetArgumentCountByAddress(const uint32_t address) const
Definition: Actor.inl:540
bool IsSynchronized() const
Definition: Actor.inl:757
Actor(const Actor &)=delete
void SetReturnPointer(void *pointer)
Definition: Actor.inl:622
float GetParameterFloat(const int32_t value) const
Definition: Actor.inl:562
bool HasReturnValue(const uint32_t address) const
Definition: Actor.inl:551
void SetReturnInteger(const int32_t value)
Definition: Actor.inl:598
void SetReturnData(const void *pointer, const int32_t size)
Definition: Actor.inl:628
void * GetParameterAddress(const int32_t value) const
Definition: Actor.inl:592
Actor(Actor &&) noexcept=delete
void * GetParameterPointer(const int32_t value) const
Definition: Actor.inl:586
void Rollback(const int32_t priority)
Definition: Actor.inl:383
void Refuse()
Definition: Actor.inl:747
std::shared_ptr< Actor > Clone() const
Definition: Actor.inl:21
void RemovePriorityChangedEvent(const EventNameType eventName)
Definition: Actor.inl:792
bool AsyncCall(const int32_t priority, const char *action, const std::shared_ptr< Actor > &sender)
Definition: Actor.inl:242
void Accept()
Definition: Actor.inl:742
int32_t GetArgumentCount() const
Definition: Actor.inl:534
Stack & GetStack()
Definition: Actor.inl:797
std::shared_ptr< VM > GetVirtualMachine() const
Definition: Actor.inl:667
std::shared_ptr< Actor > GetParameterActor(const int32_t value) const
Definition: Actor.inl:574
void Halt()
Definition: Actor.inl:715
int32_t GetParameterInteger(const int32_t value) const
Definition: Actor.inl:556
uint32_t GetAction(const std::string_view &actionName) const
Definition: Actor.inl:523
Actor * GetParameterActorPointer(const int32_t value) const
Definition: Actor.inl:581
const char * GetParameterString(const int32_t value) const
Definition: Actor.inl:568
Definition: Stack.h:23
Definition: VM.h:21
Definition: CodeBuffer.cpp:12
std::uint32_t address_t
Definition: Type.h:30
std::int32_t int_t
Definition: Type.h:28
float float_t
Definition: Type.h:27
uint32_t EventNameType
Definition: Event.h:15
Definition: Noncopyable.h:18