MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Id.h
Go to the documentation of this file.
1 /**********************************************************************
2 ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment.
4 ** Copyright (C) 2003-2007 Upinder S. Bhalla. and NCBS
5 ** It is made available under the terms of the
6 ** GNU Lesser General Public License version 2.1
7 ** See the file COPYING.LIB for the full notice.
8 **********************************************************************/
9 
10 #ifndef _ID_H
11 #define _ID_H
12 
17 class Id
18 {
19 public:
21  // Id creation
23 
26  Id();
27 
31  Id( unsigned int id );
32 
36  Id( const std::string& path, const std::string& separator = "/" );
37 
41  Id( const ObjId& oi );
42 
46  ~Id() {}
47 
53  static Id nextId();
54 
55 
59  static unsigned int numIds();
60 
64  void bindIdToElement( Element* e );
65 
71  void destroy() const;
72 
74  // Id info
76 
79  std::string path( const std::string& separator = "/" ) const;
80 
81 
99  Element* element() const;
100 
101 // unsigned int index() const;
102 
106  Eref eref() const;
107 
112  static Id str2Id( const std::string& s );
113 
117  static std::string id2str( Id id );
118 
119  unsigned int value() const;
120 
122  // Comparisons between ids
124  bool operator==( const Id& other ) const
125  {
126  // return id_ == other.id_ && index_ == other.index_;
127  return id_ == other.id_;
128  }
129 
130  bool operator!=( const Id& other ) const
131  {
132  // return id_ != other.id_ || index_ != other.index_;
133  return id_ != other.id_;
134  }
135 
136  bool operator<( const Id& other ) const
137  {
138  // return ( id_ < other.id_ ) ||
139  // ( id_ == other.id_ && index_ < other.index_ );
140  return ( id_ < other.id_ );
141  }
142 
143  // The follwoing two functions check if the Id is associated with
144  // an existing element. Needed for handling objects that have been destroyed.
145  static bool isValid(Id id)
146  {
147  return (id.id_ < elements().size()) && (elements()[id.id_] != 0);
148  }
149 
150  static bool isValid(unsigned int id)
151  {
152  return (id < elements().size()) && (elements()[id] != 0);
153  }
154 
156 
159  static void clearAllElements();
160 
164  void zeroOut() const;
166 
167  friend ostream& operator <<( ostream& s, const Id& i );
168  friend istream& operator >>( istream& s, Id& i );
169 
170 private:
171  // static void setManager( Manager* m );
172  unsigned int id_; // Unique identifier for Element*
173 // unsigned int index_; // Index of array entry within element.
174  static vector< Element* >& elements();
175 };
176 
177 // User defined hash function.
178 // See https://en.cppreference.com/w/cpp/utility/hash for more details.
179 namespace std
180 {
181  template <>
182  struct hash<Id>
183  {
184  public :
185  size_t operator()(const Id &x ) const
186  {
187  return std::hash<unsigned int>()( x.value() );
188  }
189  };
190 }
191 
192 #endif // _ID_H
size_t operator()(const Id &x) const
Definition: Id.h:185
void bindIdToElement(Element *e)
Definition: Id.cpp:148
static void clearAllElements()
Definition: Id.cpp:202
Element * element() const
Synonym for Id::operator()()
Definition: Id.cpp:113
std::string path(const std::string &separator="/") const
Definition: Id.cpp:76
unsigned int value() const
Definition: Id.cpp:197
Definition: ObjId.h:20
Eref eref() const
Definition: Id.cpp:125
Id()
Definition: Id.cpp:18
void zeroOut() const
Definition: Id.cpp:191
friend istream & operator>>(istream &s, Id &i)
~Id()
Definition: Id.h:46
void destroy() const
Definition: Id.cpp:176
bool operator!=(const Id &other) const
Definition: Id.h:130
bool operator<(const Id &other) const
Definition: Id.h:136
bool operator==(const Id &other) const
Definition: Id.h:124
static Id nextId()
Definition: Id.cpp:132
static vector< Element * > & elements()
Definition: Id.cpp:59
static unsigned int numIds()
Definition: Id.cpp:143
Definition: Eref.h:26
static bool isValid(unsigned int id)
Definition: Id.h:150
static bool isValid(Id id)
Definition: Id.h:145
Definition: Id.h:17
static char id[]
Definition: mfield.cpp:404
unsigned int id_
Definition: Id.h:172
friend ostream & operator<<(ostream &s, const Id &i)
static std::string id2str(Id id)
Definition: Id.cpp:70
static Id str2Id(const std::string &s)
Definition: Id.cpp:49