MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Id.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment,
4 ** also known as GENESIS 3 base code.
5 ** copyright (C) 2003-2006 Upinder S. Bhalla. and NCBS
6 ** It is made available under the terms of the
7 ** GNU Lesser General Public License version 2.1
8 ** See the file COPYING.LIB for the full notice.
9 **********************************************************************/
10 
11 #include "header.h"
12 #include "../shell/Shell.h"
13 
15 // Id creation
17 
19 // : id_( 0 ), index_( 0 )
20  : id_( 0 )
21 {
22  ;
23 }
24 
25 Id::Id( unsigned int id )
26  : id_( id )
27 {
28  ;
29 }
30 
31 Id::Id( const string& path, const string& separator )
32 {
33  Shell* shell = reinterpret_cast< Shell* >( Id().eref().data() );
34  assert( shell );
35  id_ = shell->doFind( path ).id.id_;
36 }
37 
38 Id::Id( const ObjId& oi )
39  : id_( oi.id.id_ )
40 {
41  ;
42 }
43 
49 Id Id::str2Id( const std::string& s )
50 {
51  // unsigned int val = atoi( s.c_str() );
52  return Id( s );
53 }
54 
56 // Element array static access function. Private.
58 
59 vector< Element* >& Id::elements()
60 {
61  static vector< Element* > e;
62  return e;
63 }
64 
66 // Id info
68 
69 // static func to convert id into a string.
70 string Id::id2str( Id id )
71 {
72  return id.path();
73 }
74 
75 // Function to convert it into its fully separated path.
76 string Id::path( const string& separator) const
77 {
78  string ret = Neutral::path( eref() );
79  // Trim off trailing []
80  assert( ret.length() > 0 );
81  // the 'back' operation is not supported by pre 2011 compilers
82 
83  while ( ret[ ret.length() - 1 ] == ']' )
84  {
85  size_t pos = ret.find_last_of( '[' );
86  if ( pos != string::npos && pos > 0 )
87  {
88  ret = ret.substr( 0, pos );
89  }
90  }
91 
92  return ret;
93 }
94 
112 Element* Id::element() const
114 {
115  return elements()[ id_ ];
116 }
117 
118 /*
119 unsigned int Id::index() const
120 {
121  return index_;
122 }
123 */
124 
125 Eref Id::eref() const
126 {
127  return Eref( elements()[ id_ ], 0 );
128  // return Eref( elements()[ id_ ], index_ );
129 }
130 
131 // Static func.
133 {
134  // Should really look up 'available' list.
135  // Should really put the returned value onto the 'reserved' list
136  // so they don't go dangling.
137  Id ret( elements().size() );
138  elements().push_back( 0 );
139  return ret;
140 }
141 
142 // Static func.
143 unsigned int Id::numIds()
144 {
145  return elements().size();
146 }
147 
149 {
150  if ( elements().size() <= id_ )
151  {
152  if ( elements().size() % 1000 == 0 )
153  {
154  elements().reserve( elements().size() + 1000 );
155  }
156  elements().resize( id_ + 1, 0 );
157  }
158  assert( elements()[ id_ ] == 0 );
159  /*
160  if ( elements()[ id_ ] != 0 )
161  cout << "Warning: assigning Element to existing id " << id_ << "\n";
162  */
163  elements()[ id_ ] = e;
164  // cout << "Id::bindIdToElement '" << e->getName() << "' = " << id_ << endl;
165 }
166 
167 /*
168 Id Id::create( Element* e )
169 {
170  Id ret( elements().size() );
171  elements().push_back( e );
172  return ret;
173 }
174 */
175 
176 void Id::destroy() const
177 {
178  if ( elements()[ id_ ] )
179  {
180  // cout << "Id::destroy '" << elements()[ id_ ]->getName() << "' = " << id_ << endl;
181  delete elements()[ id_ ];
182  elements()[ id_ ] = 0;
183  // Put id_ on 'available' list
184  }
185  else
186  {
187  cout << "Warning: Id::destroy: " << id_ << " already zeroed\n";
188  }
189 }
190 
191 void Id::zeroOut() const
192 {
193  assert ( id_ < elements().size() );
194  elements()[ id_ ] = 0;
195 }
196 
197 unsigned int Id::value() const
198 {
199  return id_;
200 }
201 
203 {
204  for ( vector< Element* >::iterator
205  i = elements().begin(); i != elements().end(); ++i )
206  {
207  if ( *i )
208  {
209  (*i)->clearAllMsgs();
210  delete *i;
211  }
212  }
213 }
214 
216 // Id utility
218 
219 ostream& operator <<( ostream& s, const Id& i )
220 {
221  s << i.id_;
222  /*
223  if ( i.index_ == 0 )
224  s << i.id_;
225  else
226  s << i.id_ << "[" << i.index_ << "]";
227  */
228  return s;
229 }
230 
231 istream& operator >>( istream& s, Id& i )
232 {
233  s >> i.id_;
234  return s;
235 }
236 
237 /*
238 Id::Id( unsigned int id, unsigned int index )
239  : id_( id ), index_( index )
240 {
241  ;
242 }
243 */
ostream & operator<<(ostream &s, const Id &i)
Definition: Id.cpp:219
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
Id id
Definition: ObjId.h:98
Definition: ObjId.h:20
Eref eref() const
Definition: Id.cpp:125
ObjId doFind(const string &path) const
Definition: Shell.cpp:549
Id()
Definition: Id.cpp:18
static string path(const Eref &e)
Definition: Neutral.cpp:725
void zeroOut() const
Definition: Id.cpp:191
void destroy() const
Definition: Id.cpp:176
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
istream & operator>>(istream &s, Id &i)
Definition: Id.cpp:231
Definition: Id.h:17
static char id[]
Definition: mfield.cpp:404
unsigned int id_
Definition: Id.h:172
static char path[]
Definition: mfield.cpp:403
static std::string id2str(Id id)
Definition: Id.cpp:70
static Id str2Id(const std::string &s)
Definition: Id.cpp:49
Definition: Shell.h:43