Spinning Topp Logo BlackTopp Studios
inc
actormanager.cpp
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef actormanager_cpp
41 #define actormanager_cpp
42 
43 #include "actormanager.h"
44 #include "actor.h"
45 #include "Physics/physicsmanager.h"
46 #include "entresol.h"
47 #include "world.h"
48 
49 #include <sstream>
50 #include <algorithm>
51 
52 namespace Mezzanine
53 {
54  ///////////////////////////////////////////////////////////////////////////////
55  // ActorUpdateWorkUnit Methods
56 
58  { }
59 
61  { return *this; }
62 
64  TargetManager(Target) { }
65 
67  { }
68 
69  ///////////////////////////////////////////////////////////////////////////////
70  // Utility
71 
73  {
74  for( ActorManager::ActorIterator ActIt = this->TargetManager->Actors.begin() ; ActIt != this->TargetManager->Actors.end() ; ++ActIt )
75  {
76  (*ActIt)->_Update();
77  }
78  }
79 
80  ///////////////////////////////////////////////////////////////////////////////
81  // ActorManager Methods
82 
83  const String ActorManager::ImplementationName = "DefaultActorManager";
84  const ManagerBase::ManagerType ActorManager::InterfaceType = ManagerBase::MT_ActorManager;
85 
87  WorldManager(Creator),
88  ActorUpdateWork(NULL),
89  ThreadResources(NULL)
90  {
91  this->ActorUpdateWork = new ActorUpdateWorkUnit(this);
92  }
93 
94  ActorManager::ActorManager(World* Creator, const XML::Node& XMLNode) :
95  WorldManager(Creator),
96  ActorUpdateWork(NULL),
97  ThreadResources(NULL)
98  {
99  /// @todo This class currently doesn't initialize anything from XML, if that changes this constructor needs to be expanded.
100 
101  this->ActorUpdateWork = new ActorUpdateWorkUnit(this);
102  }
103 
105  {
106  this->Deinitialize();
107  this->DestroyAllActors();
108 
109  delete this->ActorUpdateWork;
110  }
111 
112  ///////////////////////////////////////////////////////////////////////////////
113  // Prefab Actor Type Creation
114 
115  ///////////////////////////////////////////////////////////////////////////////
116  // Actor Management
117 
118  Actor* ActorManager::CreateActor(const String& TypeName, const String& InstanceName, const NameValuePairMap& Params)
119  {
120  FactoryIterator ActFactIt = this->ActorFactories.find( TypeName );
121  if( ActFactIt != this->ActorFactories.end() ) {
122  Actor* Ret = (*ActFactIt).second->CreateActor( InstanceName, this->ParentWorld, Params );
123  this->Actors.push_back( Ret );
124  return Ret;
125  }else{
126  MEZZ_EXCEPTION(ExceptionBase::INVALID_STATE_EXCEPTION,"Attempting to create an Actor of unknown type.");
127  }
128  }
129 
131  {
132  FactoryIterator ActFactIt = this->ActorFactories.find( SelfRoot.Name() );
133  if( ActFactIt != this->ActorFactories.end() ) {
134  Actor* Ret = (*ActFactIt).second->CreateActor( SelfRoot, this->ParentWorld );
135  this->Actors.push_back( Ret );
136  return Ret;
137  }else{
138  MEZZ_EXCEPTION(ExceptionBase::INVALID_STATE_EXCEPTION,"Attempting to create a Actor of unknown type.");
139  }
140  }
141 
142  Actor* ActorManager::GetActor(const Whole Index) const
143  {
144  return this->Actors.at(Index);
145  }
146 
147  Actor* ActorManager::GetActor(const String& Name) const
148  {
149  for( ConstActorIterator ActIt = this->Actors.begin() ; ActIt != this->Actors.end() ; ++ActIt )
150  {
151  if( (*ActIt)->GetName() == Name )
152  return (*ActIt);
153  }
154  return NULL;
155  }
156 
158  {
159  return this->Actors.size();
160  }
161 
163  {
164  ActorIterator ActIt = ( Index < this->GetNumActors() ? this->Actors.begin() + Index : this->Actors.end() );
165  if( ActIt != this->Actors.end() )
166  {
167  FactoryIterator ActFactIt = this->ActorFactories.find( (*ActIt)->GetDerivedSerializableName() );
168  if( ActFactIt != this->ActorFactories.end() ) {
169  (*ActFactIt).second->DestroyActor( (*ActIt) );
170  }else{
171  MEZZ_EXCEPTION(ExceptionBase::INVALID_STATE_EXCEPTION,"Attempting to destroy a Actor of unknown type.");
172  }
173 
174  this->Actors.erase(ActIt);
175  }
176  }
177 
178  void ActorManager::DestroyActor(Actor* ToBeDestroyed)
179  {
180  ActorIterator ActIt = std::find( this->Actors.begin(), this->Actors.end(), ToBeDestroyed );
181  if( ActIt != this->Actors.end() )
182  {
183  FactoryIterator ActFactIt = this->ActorFactories.find( (*ActIt)->GetDerivedSerializableName() );
184  if( ActFactIt != this->ActorFactories.end() ) {
185  (*ActFactIt).second->DestroyActor( (*ActIt) );
186  }else{
187  MEZZ_EXCEPTION(ExceptionBase::INVALID_STATE_EXCEPTION,"Attempting to destroy a Actor of unknown type.");
188  }
189 
190  this->Actors.erase(ActIt);
191  }
192  }
193 
195  {
196  for( ActorIterator ActIt = this->Actors.begin() ; ActIt != this->Actors.end() ; ++ActIt )
197  {
198  FactoryIterator ActFactIt = this->ActorFactories.find( (*ActIt)->GetDerivedSerializableName() );
199  if( ActFactIt != this->ActorFactories.end() ) {
200  (*ActFactIt).second->DestroyActor( (*ActIt) );
201  }else{
202  MEZZ_EXCEPTION(ExceptionBase::INVALID_STATE_EXCEPTION,"Attempting to destroy a Actor of unknown type.");
203  }
204  }
205  this->Actors.clear();
206  }
207 
209  { return this->Actors.begin(); }
210 
212  { return this->Actors.end(); }
213 
215  { return this->Actors.begin(); }
216 
218  { return this->Actors.end(); }
219 
220  ///////////////////////////////////////////////////////////////////////////////
221  // ActorFactory Management
222 
224  {
225  this->ActorFactories.insert(std::pair<String,ActorFactory*>(ToBeAdded->GetTypeName(),ToBeAdded));
226  }
227 
229  {
230  this->RemoveActorFactory(ToBeRemoved->GetTypeName());
231  }
232 
234  {
235  FactoryIterator ActFactIt = this->ActorFactories.find(ImplName);
236  if( ActFactIt != this->ActorFactories.end() )
237  { this->ActorFactories.erase(ActFactIt); }
238  }
239 
241  {
242  this->DestroyActorFactory(ToBeDestroyed->GetTypeName());
243  }
244 
246  {
247  FactoryIterator ActFactIt = this->ActorFactories.find(ImplName);
248  if( ActFactIt != this->ActorFactories.end() ) {
249  delete ActFactIt->second;
250  this->ActorFactories.erase(ActFactIt);
251  }
252  }
253 
255  {
256  for( FactoryIterator ActFactIt = this->ActorFactories.begin() ; ActFactIt != this->ActorFactories.end() ; ++ActFactIt )
257  { delete (*ActFactIt).second; }
258  this->ActorFactories.clear();
259  }
260 
261  ///////////////////////////////////////////////////////////////////////////////
262  // Utility
263 
265  {
266  // Do nothing currently
267  }
268 
270  {
271  if( !this->Initialized )
272  {
274 
275  this->TheEntresol->GetScheduler().AddWorkUnitMain( this->ActorUpdateWork, "ActorUpdateWork" );
276  Physics::PhysicsManager* PhysicsMan = static_cast<Physics::PhysicsManager*>( this->ParentWorld->GetManager(ManagerBase::MT_PhysicsManager) );
277  if( PhysicsMan ) {
278  this->ActorUpdateWork->AddDependency( PhysicsMan->GetSimulationWork() );
279  }
280 
281  this->Initialized = true;
282  }
283  }
284 
286  {
287  if( this->Initialized )
288  {
291 
292  this->Initialized = false;
293  }
294  }
295 
297  { return this->ActorUpdateWork; }
298 
299  ///////////////////////////////////////////////////////////////////////////////
300  // Type Identifier Methods
301 
303  { return ActorManager::InterfaceType; }
304 
307 
308  ///////////////////////////////////////////////////////////////////////////////
309  // DefaultActorManagerFactory Methods
310 
312  { }
313 
315  { }
316 
319 
321  { return ActorManager::InterfaceType; }
322 
324  { return new ActorManager(Creator); }
325 
327  { return new ActorManager(Creator,XMLNode); }
328 
330  { delete ToBeDestroyed; }
331 }//Mezzanine
332 
333 #endif
Actor * CreateActor(const String &TypeName, const String &InstanceName, const NameValuePairMap &Params)
Creates a new Actor.
virtual void AddDependency(iWorkUnit *NewDependency)
Force this WorkUnit to Start after another has completed.
Definition: workunit.cpp:99
This is the base class from which all the actors inherit.
Definition: actor.h:57
WorldManager * CreateManager(World *Creator, const NameValuePairList &Params)
Creates a manager of the type represented by this factory.
virtual void DestroyAllActors()
Destroys all Actors currently within this manager.
WorldManager * GetManager(const Whole ManagerToGet)
This is will find the manager of a given type.
Definition: world.cpp:324
ActorManager(World *Creator)
Class constructor.
virtual void AddWorkUnitMain(iWorkUnit *MoreWork, const String &WorkUnitName)
Add a normal Mezzanine::Threading::iWorkUnit to this For fcheduling.
virtual String GetImplementationTypeName() const
This Allows any manager name to be sent to a stream. Primarily used for logging.
virtual void Initialize()
Configures this manager for use prior to entering the main loop.
virtual void Deinitialize()
Removes this manager from any necessary configuration so it can be safely disposed of...
ManagerType
A listing of Manager Types.
Definition: managerbase.h:65
virtual Whole GetNumActors() const
Gets the number of Actors stored in this manager.
virtual void AddActorFactory(ActorFactory *ToBeAdded)
Adds/registers a Actor factory with this manager, allowing it to be constructed through this API...
virtual Actor * GetActor(const Whole Index) const
Gets an Actor by Index.
ActorUpdateWorkUnit(const ActorUpdateWorkUnit &Other)
Protected copy constructor. THIS IS NOT ALLOWED.
virtual void RemoveWorkUnitMain(iWorkUnit *LessWork)
Remove a WorkUnit from the main pool of WorkUnits (and not from the groups of Affinity or MonpolyWork...
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
ActorContainer::const_iterator ConstActorIterator
Const Iterator type for Actor instances stored by this class.
Definition: actormanager.h:108
virtual void DestroyActor(const Whole Index)
Destroys an Actor at the specified index.
static const ManagerBase::ManagerType InterfaceType
A ManagerType enum value used to describe the type of interface/functionality this manager provides...
Definition: actormanager.h:113
A base factory type for the creation of Actor objects.
Definition: actor.h:71
FactoryMap ActorFactories
A map containing all registered Actor type factories.
Definition: actormanager.h:119
ActorIterator EndActor()
Gets an iterator to one passed the last Actor in this manager.
String GetManagerImplName() const
Gets the name of the manager that is created by this factory.
virtual ~ActorManager()
Class destructor.
virtual void DestroyActorFactory(ActorFactory *ToBeDestroyed)
Removes and destroys a Actor factory in this manager.
Threading::FrameScheduler & GetScheduler()
Gets the core structure responsible for scheduling work in the Entresol main loop.
Definition: entresol.cpp:495
FactoryMap::iterator FactoryIterator
Iterator type for ActorFactory instances stored by this class.
Definition: actormanager.h:100
A thread specific collection of double-buffered and algorithm specific resources. ...
ActorContainer Actors
Container storing all Actors belonging to this manager.
Definition: actormanager.h:122
ActorContainer::iterator ActorIterator
Iterator type for Actor instances stored by this class.
Definition: actormanager.h:106
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
virtual void DoWork(Threading::DefaultThreadSpecificStorage::Type &CurrentThreadStorage)
This does any required update of the Actors stored by it's manager.
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
ManagerBase::ManagerType GetManagerType() const
Gets the type of manager that is created by this factory.
virtual ~DefaultActorManagerFactory()
Class destructor.
Entresol * TheEntresol
The actual pointer to the Entresol core class.
Definition: managerbase.h:108
DefaultActorManagerFactory()
Class constructor.
virtual void RemoveActorFactory(ActorFactory *ToBeRemoved)
Removes a Actor factory from this manager.
std::list< NameValuePair > NameValuePairList
This is a datatype mostly used for describing settings or parameters that can't be declared in advanc...
Definition: datatypes.h:206
Thrown when the available information should have worked but failed for unknown reasons.
Definition: exception.h:113
The base class for all Actors is defined here.
This is the base class for all managers that belong to a single world instance.
Definition: worldmanager.h:55
virtual ~ActorUpdateWorkUnit()
Class destructor.
virtual void ClearDependencies()
Drop any information about what work units this one depends on.
Definition: workunit.cpp:110
virtual void Initialize()
Configures this manager for use prior to entering the main loop.
This is simply a place for storing all the Physics Related functions.
ActorUpdateWorkUnit * ActorUpdateWork
The work unit that updates all the actors stored by this manager.
Definition: actormanager.h:126
virtual String GetTypeName() const =0
Gets the name of the Actor that is created by this factory.
Threading::DefaultWorkUnit * GetSimulationWork()
Gets a pointer to the work unit that steps the simulation.
This is a Mezzanine::Threading::iWorkUnit for the updating of actors.
Definition: actormanager.h:59
World * ParentWorld
A pointer to the world that created this manager.
Definition: worldmanager.h:60
ActorIterator BeginActor()
Gets an iterator to the first Actor in this manager.
static const String ImplementationName
A String containing the name of this manager implementation.
Definition: actormanager.h:111
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
This class represents a world for objects to interact within.
Definition: world.h:74
virtual ManagerType GetInterfaceType() const
This returns the type of this manager.
ActorUpdateWorkUnit & operator=(const ActorUpdateWorkUnit &Other)
Protected assignment operator. THIS IS NOT ALLOWED.
ActorUpdateWorkUnit * GetActorUpdateWork()
Gets the work unit responsible for updating actors stored by this manager.
virtual void DestroyAllActorFactories()
Destroys all Actor factories in this manager.
const Char8 * Name() const
ptrdiff_tGet the name of this Node.
std::map< String, String > NameValuePairMap
This is a datatype mostly used for describing settings or parameters that can't be declared in advanc...
Definition: datatypes.h:209
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
ActorManager * TargetManager
A pointer to the manager this work unit is processing.
Definition: actormanager.h:64
virtual void Pause(const UInt32 PL)
Sets the pause state of this manager, or has no effect depending on the value passed in...
void DestroyManager(WorldManager *ToBeDestroyed)
Destroys a Manager created by this factory.
A manager responsible for the storage and management of all actors that exist in a world...
Definition: actormanager.h:94
Boole Initialized
Simple Boole indicating whether or not this manager has been initialized.
Definition: managerbase.h:111