Emergency 20 Dokumentation  4.2.0
ComponentsRandomEnumerator.h
Go to the documentation of this file.
1 // Copyright (C) 2012-2018 Promotion Software GmbH
2 
3 
4 //[-------------------------------------------------------]
5 //[ Header guard ]
6 //[-------------------------------------------------------]
7 #pragma once
8 
9 
10 //[-------------------------------------------------------]
11 //[ Includes ]
12 //[-------------------------------------------------------]
14 
17 
18 
19 //[-------------------------------------------------------]
20 //[ Namespace ]
21 //[-------------------------------------------------------]
22 namespace em5
23 {
24 
25 
26  //[-------------------------------------------------------]
27  //[ Classes ]
28  //[-------------------------------------------------------]
37  template<typename T>
39  {
40 
41 
42  //[-------------------------------------------------------]
43  //[ Public methods ]
44  //[-------------------------------------------------------]
45  public:
50  inline explicit ComponentsRandomEnumerator(uint32 maxTries = std::numeric_limits<uint32>::max()) :
51  RandomEnumerator<T>(maxTries)
52  {
53  // Fill our component list with all component instances in the main map
54  const auto& components = qsf::ComponentMapQuery(QSF_MAINMAP).getAllInstances<T>();
55  if (!components.empty())
56  {
57  components.copyToVector(this->mElements, true);
58  }
59  }
60 
65  inline explicit ComponentsRandomEnumerator(const std::vector<T*>& inputToCopy, uint32 maxTries = std::numeric_limits<uint32>::max()) :
66  RandomEnumerator<T>(inputToCopy, maxTries)
67  {
68  // Nothing here
69  }
70 
71  // This constructor must not be used, see comment inside
72  inline explicit ComponentsRandomEnumerator(std::vector<T*>& inputToCopy, uint32 maxTries = std::numeric_limits<uint32>::max())
73  {
74  // If you get there, you used a ComponentsRandomEnumerator constructor with a non-const std::vector reference as first argument.
75  // In this case, use the constructor with "constructionpolicy::Policy" argument to specify whether to copy or swap contents with the given std::vector.
76  // Both may be beneficial depending on your case:
77  // - constructionpolicy::COPY is safer, but produces overhead due to copying of data
78  // - constructionpolicy::SWAP will swap contents and consequently leave the given std::vector empty afterwards, which is fine if it is not used afterwards any more
79  T::Used_ambiguous_ComponentsRandomEnumerator_constructor;
80  }
81 
86  inline ComponentsRandomEnumerator(constructionpolicy::Policy policy, std::vector<T*>& inputToSwapWith, uint32 maxTries = std::numeric_limits<uint32>::max()) :
87  RandomEnumerator<T>(policy, inputToSwapWith, maxTries)
88  {
89  // Nothing here
90  }
91 
100  {
101  qsf::Component* component = this->getNext();
102  return (nullptr == component) ? nullptr : &component->getEntity();
103  }
104 
105 
106  };
107 
108 
109 //[-------------------------------------------------------]
110 //[ Namespace ]
111 //[-------------------------------------------------------]
112 } // em5
Definition: ActionPriority.h:13
ComponentsRandomEnumerator(std::vector< T * > &inputToCopy, uint32 maxTries=std::numeric_limits< uint32 >::max())
Definition: ComponentsRandomEnumerator.h:72
Helper class for iterating over a list of instances of a specific type, in a random order...
Definition: RandomEnumerator.h:47
Abstract component class.
Definition: Component.h:105
Entity class.
Definition: Entity.h:46
unsigned int uint32
Definition: PlatformTypes.h:181
Entity & getEntity() const
Return a reference to the entity this component is in.
Helper class for iterating over a list of component instances of a specific type, in a random order...
Definition: ComponentsRandomEnumerator.h:38
const ComponentCollection::ComponentList< T > & getAllInstances() const
Return all component instances of a certain type found in any entity.
Definition: ComponentMapQuery-inl.h:32
ComponentsRandomEnumerator(constructionpolicy::Policy policy, std::vector< T * > &inputToSwapWith, uint32 maxTries=std::numeric_limits< uint32 >::max())
Constructor to pass a vector of components, with the option to swap contents.
Definition: ComponentsRandomEnumerator.h:86
Component map query interface.
Definition: ComponentMapQuery.h:54
T * getNext()
Randomly choose an element that was not returned before.
Definition: RandomEnumerator.h:106
ComponentsRandomEnumerator(const std::vector< T * > &inputToCopy, uint32 maxTries=std::numeric_limits< uint32 >::max())
Constructor to pass a vector of components, which will be copied.
Definition: ComponentsRandomEnumerator.h:65
ComponentsRandomEnumerator(uint32 maxTries=std::numeric_limits< uint32 >::max())
Constructor that will use all of main map's components to chose from.
Definition: ComponentsRandomEnumerator.h:50
Policy
Definition: RandomEnumerator.h:28
std::vector< T * > mElements
Definition: RandomEnumerator.h:144
#define QSF_MAINMAP
Ease of use macro returning a reference to the QSF main map instance.
Definition: QsfHelper.h:286
qsf::Entity * getNextEntity()
Randomly choose a component that was not chosen already, and return its entity.
Definition: ComponentsRandomEnumerator.h:99