RepastHPC  2.3.1
AgentId.h
1 /*
2  * Repast for High Performance Computing (Repast HPC)
3  *
4  * Copyright (c) 2010 Argonne National Laboratory
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with
8  * or without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * Neither the name of the Argonne National Laboratory nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  *
35  * AgentId.h
36  *
37  * Created on: Dec 23, 2008
38  * Author: nick
39  */
40 
41 #ifndef AGENTID_H_
42 #define AGENTID_H_
43 
44 #include <iostream>
45 #include <boost/serialization/access.hpp>
46 #include <boost/mpi.hpp>
47 
48 namespace repast {
49 
60 class AgentId {
61 
62  friend std::ostream& operator<<(std::ostream& os, const AgentId& id);
63  friend bool operator==(const AgentId &one, const AgentId &two);
64  friend bool operator<(const AgentId &one, const AgentId &two);
65  friend class boost::serialization::access;
66 
67 private:
68  int id_, startProc_, agentType_, currentProc_;
69  std::size_t hash;
70 
71  template<class Archive>
72  void serialize(Archive& ar, const unsigned int version) {
73  ar & id_;
74  ar & startProc_;
75  ar & agentType_;
76  ar & currentProc_;
77  ar & hash;
78  }
79 
80 public:
81 
85  AgentId() {
86  }
87 
97  AgentId(int id, int startProc, int agentType, int currentProc = -1);
98 
99  virtual ~AgentId();
100 
106  int id() const {
107  return id_;
108  }
109 
115  int startingRank() const {
116  return startProc_;
117  }
118 
124  int agentType() const {
125  return agentType_;
126  }
127 
135  int currentRank() const {
136  return currentProc_;
137  }
138 
146  void currentRank(int val) {
147  currentProc_ = val;
148  }
149 
155  std::size_t hashcode() const {
156  return hash;
157  }
158 };
159 
163 std::ostream& operator<<(std::ostream& os, const AgentId& id);
164 
168 bool operator==(const AgentId &one, const AgentId &two);
169 
173 bool operator!=(const AgentId &one, const AgentId &two);
174 
175 
179 bool operator<(const AgentId &one, const AgentId &two);
180 
185 struct HashId {
186  std::size_t operator()(const AgentId& id) const {
187  return id.hashcode();
188  }
189 };
190 
195 template<typename AgentType>
196 struct AgentHashId {
197  std::size_t operator()(const AgentType* agent) const {
198  return agent->getId().hashcode();
199  }
200 };
201 
206 class Agent {
207 
208 public:
209  virtual ~Agent() {
210  }
211 
217  virtual AgentId& getId() = 0;
218 
224  virtual const AgentId& getId() const = 0;
225 };
226 
227 
231 template<typename T>
232 struct IsAgentType {
233  int _typeId;
234 
235  IsAgentType(){
236  _typeId = 0;
237  }
238 
239  IsAgentType(int typeId) :
240  _typeId(typeId) {
241  }
242 
243  bool operator()(const boost::shared_ptr<T>& ptr) {
244  return ptr->getId().agentType() == _typeId;
245  }
246 
247  bool operator()(const T* agent) {
248  return agent->getId().agentType() == _typeId;
249  }
250 };
251 
252 
256 template<typename T>
257 struct IsNotType {
258  int _typeId;
259 
260  IsNotType(){
261  _typeId = 0;
262  }
263 
264  IsNotType(int typeId) :
265  _typeId(typeId) {
266  }
267 
268  bool operator()(const boost::shared_ptr<T>& ptr) {
269  return ptr->getId().agentType() != _typeId;
270  }
271 
272  bool operator()(const T* agent) {
273  return agent->getId().agentType() != _typeId;
274  }
275 
276 };
277 
278 }
279 
280 #endif /* AGENTID_H_ */
repast::AgentId::operator==
friend bool operator==(const AgentId &one, const AgentId &two)
Equality operator.
Definition: AgentId.cpp:57
repast::Agent::getId
virtual AgentId & getId()=0
Gets the AgentId for this Agent.
repast::AgentId::operator<
friend bool operator<(const AgentId &one, const AgentId &two)
A comparison operator for use with std::set.
Definition: AgentId.cpp:65
repast::AgentId
Agent identity information.
Definition: AgentId.h:60
repast::AgentId::id
int id() const
Gets the id component of this AgentId.
Definition: AgentId.h:106
repast::IsNotType
Struct that allows filtering by !(Agent Type)
Definition: AgentId.h:257
repast::IsAgentType
Struct that allows filtering by Agent Type.
Definition: AgentId.h:232
repast::AgentId::currentRank
int currentRank() const
Gets the current process rank of this AgentId.
Definition: AgentId.h:135
repast::Agent
Interface for agent classes.
Definition: AgentId.h:206
repast::AgentId::hashcode
std::size_t hashcode() const
Gets the hashcode for this AgentId.
Definition: AgentId.h:155
repast::AgentId::agentType
int agentType() const
Gets the agent type component of this AgentId.
Definition: AgentId.h:124
repast::AgentId::AgentId
AgentId()
No-arg constructor necessary for serialization.
Definition: AgentId.h:85
repast::AgentHashId
operator() implementation that returns the hashcode of an agent via its AgentId.
Definition: AgentId.h:196
repast::AgentId::startingRank
int startingRank() const
Gets the starting rank component of this AgentId.
Definition: AgentId.h:115
repast::AgentId::operator<<
friend std::ostream & operator<<(std::ostream &os, const AgentId &id)
Writes the agent id to the ostream.
Definition: AgentId.cpp:74
repast::HashId
operator() implementation that returns the hashcode of an AgentId.
Definition: AgentId.h:185
repast::AgentId::currentRank
void currentRank(int val)
Sets the current process rank of this AgentId.
Definition: AgentId.h:146