TrueReality  v0.1.1912
SystemManager.h
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License as published by the Free
7 * Software Foundation; either version 3.0 of the License, or (at your option)
8 * any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * @author Maxim Serebrennik
20 */
21 
22 #pragma once
23 
24 #include "Export.h"
25 
27 #include <trManager/MessageBase.h>
28 #include <trManager/EntityBase.h>
29 #include <trUtil/HashMap.h>
30 #include <trBase/UniqueId.h>
31 #include <trBase/SmrtPtr.h>
32 #include <trBase/Base.h>
33 
34 #include <utility>
35 #include <string>
36 #include <vector>
37 #include <queue>
38 #include <list>
39 
40 namespace trManager
41 {
47  {
48  public:
49 
51 
52  const static trUtil::RefStr CLASS_TYPE;
53 
61  static trManager::SystemManager& GetInstance();
62 
70  virtual const std::string& GetType() const override;
71 
81  virtual bool SendMessage(const trManager::MessageBase& message);
82 
92  virtual bool SendNetworkMessage(const trManager::MessageBase& message);
93 
102  virtual void ProcessMessage(const trManager::MessageBase& message);
103 
109  virtual void ProcessMessages();
110 
117  virtual void ProcessNetworkMessages();
118 
128  virtual void RegisterForMessage(const std::string& messageType, EntityBase& listeningActor, const std::string& invokableName);
129 
138  virtual void UnregisterFromMessage(const std::string& messageType, EntityBase& listeningActor);
139 
149  virtual void RegisterForMessagesAboutEntity(EntityBase& listeningEntity, const trBase::UniqueId& aboutEntityId, const std::string& invokableName);
150 
159  virtual void UnregisterFromMessagesAboutEntity(EntityBase& listeningEntity, const trBase::UniqueId& aboutEntityId);
160 
170  virtual bool RegisterActor(trManager::EntityBase& actor);
171 
181  virtual bool UnregisterActor(trManager::EntityBase& actor);
182 
192  virtual bool UnregisterActor(const trBase::UniqueId& id);
193 
201  virtual bool UnregisterAllActors();
202 
212  virtual trManager::EntityBase* FindActor(const trBase::UniqueId& id);
213 
223  virtual std::vector<trManager::EntityBase*> FindActorsByType(const std::string& actorType);
224 
234  virtual std::vector<trManager::EntityBase*> FindActorsByName(const std::string& actorName);
235 
249  virtual bool RegisterDirector(trManager::EntityBase& director, trManager::DirectorPriority& priority = trManager::DirectorPriority::NORMAL);
250 
260  virtual bool UnregisterDirector(trManager::EntityBase& director);
261 
271  virtual bool UnregisterDirector(const trBase::UniqueId& id);
272 
278  virtual void UnregisterAllDirectors();
279 
289  virtual trManager::EntityBase* FindDirector(const trBase::UniqueId& id) const;
290 
301  virtual trManager::EntityBase* FindDirector(const std::string& name) const;
302 
312  virtual std::vector<trManager::EntityBase*> FindDirectors(const std::string& type) const;
313 
322  virtual void RemoveMarkedEntities();
323 
329  virtual void ShutDown();
330 
331  protected:
332 
340  SystemManager(const std::string name = CLASS_TYPE);
341 
347  ~SystemManager();
348 
356  virtual void SendMessageToDirectors(const trManager::MessageBase& message);
357 
365  virtual void SendMessageToActors(const trManager::MessageBase& message);
366 
374  virtual void SendMessageToListeners(const trManager::MessageBase& message);
375 
383  virtual void SendGlobalyRegisteredMessage(const trManager::MessageBase& message);
384 
394  virtual void CallInvokable(const trManager::MessageBase& message, const std::string& invokableName, trManager::EntityBase& entity);
395 
403  virtual void UnregisterActorFromGlobalMessages(trManager::EntityBase& actor);
404 
412  virtual void UnregisterDirectorFromGlobalMessages(trManager::EntityBase& director);
413 
421  virtual void UnregisterEntityFromAboutMessages(trManager::EntityBase& listeningEntity);
422 
423  private:
424 
426  std::queue<trBase::SmrtPtr<const trManager::MessageBase>> mMessageQueue;
427  std::queue<trBase::SmrtPtr<const trManager::MessageBase>> mNetworkMessageQueue;
428 
429  // Storage for all the registered Directors
430  using DirectorList = std::list<trBase::SmrtPtr<trManager::EntityBase>>; //Needs to be a std::list so the directors can be priority sorted
436 
437  //Message registration structures..
438  using EntityInvokablePair = std::pair<trBase::SmrtPtr<trManager::EntityBase>, std::string>; //<entity, invokableName>
439  using MessageRegistrationVectorMap = trUtil::HashMap<std::string*, std::vector<EntityInvokablePair>>; //<messageName, vector of registered entityPairs>
441  using EntityInvokableMap = trUtil::HashMap<trBase::SmrtPtr<trManager::EntityBase>, std::string>; //<entity, invokableName>
442  using MessageRegistrationMap = trUtil::HashMap<std::string*, EntityInvokableMap>; //<messageName, <entity, invokableName>>
446 
447  //Storage for all registered Actors and Actor Modules
448  using ActorList = std::vector<trBase::SmrtPtr<trManager::EntityBase>>;
452 
453  std::vector<trBase::SmrtPtr<trManager::EntityBase>> mEntityDeleteList; //List of entities that will be deleted at the end of the frame
454 
465  void RegisterMsgWithMsgVectorMap(const std::string& messageType, EntityBase& listeningEntity, const std::string& invokableName, MessageRegistrationVectorMap& messageMap);
466 
476  void UnregisterMsgFromMsgVectorMap(const std::string& messageType, EntityBase& listeningEntity, MessageRegistrationVectorMap& messageMap);
477 
488  void RegisterMsgWithMsgMap(const std::string& messageType, EntityBase& listeningEntity, const std::string& invokableName, MessageRegistrationMap& messageMap);
489 
499  void UnregisterMsgFromMsgMap(const std::string& messageType, EntityBase& listeningEntity, MessageRegistrationMap& messageMap);
500  };
501 }
std::vector< trBase::SmrtPtr< trManager::EntityBase > > ActorList
Sets the priority for a Director instance.
This class creates a GUID, or a Unique ID that is used through out TR to identify and distinguish one...
Definition: UniqueId.h:42
std::list< trBase::SmrtPtr< trManager::EntityBase > > DirectorList
A string wrapper that will make sure that all of the strings with the same value will point to the sa...
Definition: RefStr.h:50
static const trUtil::RefStr CLASS_TYPE
Adds an easy and swappable access to the base class.
Definition: SystemManager.h:52
DirectorIDMap mDirectorIDMap
System Manager class is a singleton that is responsible for all message routing and basic operations ...
Definition: SystemManager.h:46
DirectorNameMap mDirectorNameMap
This class is part of the internal garbage collection system.
Definition: SmrtClass.h:38
This is a Base class that carries the Class name and Unique ID of all the derived classes...
Definition: trBase/Base.h:36
static DirectorPriority NORMAL
Normal priority.
std::queue< trBase::SmrtPtr< const trManager::MessageBase > > mMessageQueue
UUIDRegistrationVectorMap mListenerRegistrationMap
MessageRegistrationMap mDirectorGlobalMsgRegistrationMap
MessageRegistrationVectorMap mEntityGlobalMsgRegistrationMap
std::pair< trBase::SmrtPtr< trManager::EntityBase >, std::string > EntityInvokablePair
This serves as the base class for the Entity class and removes a circular dependency between Entity a...
Definition: EntityBase.h:46
This is the base class for all the messages in TR.
Definition: MessageBase.h:40
std::vector< trBase::SmrtPtr< trManager::EntityBase > > mEntityDeleteList
#define TR_MANAGER_EXPORT
static trBase::SmrtPtr< trManager::SystemManager > mInstance
std::queue< trBase::SmrtPtr< const trManager::MessageBase > > mNetworkMessageQueue