Atoms Crowd  4.1.0
AgentsPool.h
1 #pragma once
2 // ===========================================================================
3 // Copyright (c) 2015 Toolchefs Ltd. All rights reserved.
4 //
5 // Use of this software is subject to the terms of the Toolchefs license
6 // agreement provided at the time of installation or download, or which
7 // otherwise accompanies this software in either electronic or hard copy form.
8 // ===========================================================================
9 
10 #include <Atoms/Globals.h>
11 #include <Atoms/Agent.h>
12 #include <mutex>
13 #include <atomic>
14 
15 namespace Atoms
16 {
18 
23  class ATOMS_EXPORT AgentsPool
24  {
25  public:
26 
28  static AgentsPool& instance();
29 
31 
35  std::vector<Agent*> reserve(unsigned int numAgents, const std::string& affinity = "");
36 
38  void clear();
39 
41  void reset();
42 
44  void clearCache();
45 
47 
50  Agent* agent(unsigned int id);
51 
53  std::vector<int> agentIds();
54 
56  void deleteAgent(unsigned int id);
57 
59  void deleteAgents(const std::vector<unsigned int>& ids);
60 
62  size_t numAgents() const;
63 
65  AtomsPtr<Agent> createEmptyAgent() const;
66 
67  void setNumAgentsOffset(unsigned int offset);
68 
69  unsigned int numAgentsOffset() const;
70 
71  private:
72 
74  AgentsPool();
75 
77  ~AgentsPool();
78 
80  AgentsPool(const AgentsPool&);
81 
83  AgentsPool& operator=(const AgentsPool&);
84 
85  private:
86 
88  std::unordered_map<int, Agent*> m_agents;
89 
91  std::map<std::string, std::vector<Agent*>> m_agentCache;
92 
94  std::mutex m_mutex;
95 
97  unsigned int m_nextFreeId;
98 
99  unsigned int m_numAgentsOffset;
100  };
101 }
Atoms::AgentsPool::deleteAgent
void deleteAgent(unsigned int id)
Delete an agent.
Atoms::AgentsPool::agent
Agent * agent(unsigned int id)
Gets an agent by its global id.
Atoms::Agent
Agent.
Definition: Agent.h:38
Atoms::AgentsPool::instance
static AgentsPool & instance()
Singleton access.
Atoms::AgentsPool::deleteAgents
void deleteAgents(const std::vector< unsigned int > &ids)
Delete agents.
Atoms::AgentsPool::agentIds
std::vector< int > agentIds()
Gets all agent global ids.
Atoms::AgentsPool::createEmptyAgent
AtomsPtr< Agent > createEmptyAgent() const
Create and agent without saving it inside the pool.
Atoms
Atoms namespace.
Definition: Agent.h:28
Atoms::AgentsPool::clear
void clear()
Clears all the agents.
Atoms::AgentsPool::clearCache
void clearCache()
Delete the cache.
Atoms::AgentsPool::reset
void reset()
Reset the map and move the agents to the cache.
Atoms::AgentsPool::reserve
std::vector< Agent * > reserve(unsigned int numAgents, const std::string &affinity="")
Creates the given number of agents and returns them.
Atoms::AgentsPool
Agents pool.
Definition: AgentsPool.h:24
Atoms::AgentsPool::numAgents
size_t numAgents() const
Gets the number of agents.