Develop Biology
The language of life
bio::physical::Perspective< DIMENSION > Class Template Reference

#include <Perspective.h>

+ Inheritance diagram for bio::physical::Perspective< DIMENSION >:
+ Collaboration diagram for bio::physical::Perspective< DIMENSION >:

Classes

class  Hadit
 

Public Types

typedef std::vector< HaditHadits
 
typedef DIMENSION Id
 
typedef std::vector< IdIds
 

Public Member Functions

 Perspective ()
 
virtual ~Perspective ()
 
virtual bool AssociateType (Id id, Wave *type)
 
virtual bool DisassociateType (Id id)
 
Hadits::iterator Find (Id id)
 
Hadits::const_iterator Find (Id id) const
 
virtual Id GetIdFromName (Name name)
 
virtual Id GetIdWithoutCreation (Name name) const
 
virtual Name GetNameFromId (Id id) const
 
virtual WaveGetNewObjectFromId (Id id) const
 
template<typename T >
GetNewObjectFromIdAs (Id id)
 
virtual WaveGetNewObjectFromName (Name name)
 
template<typename T >
GetNewObjectFromNameAs (Name name)
 
virtual Id GetNumUsedIds () const
 
virtual const WaveGetTypeFromId (Id id) const
 
template<typename T >
const T GetTypeFromIdAs (Id id) const
 
virtual const WaveGetTypeFromName (Name name) const
 
template<typename T >
const T GetTypeFromNameAs (Name name) const
 
virtual Id GetUniqueIdFor (Name name)
 
- Public Member Functions inherited from bio::ThreadSafe
 ThreadSafe ()
 
 ThreadSafe (const ThreadSafe &toCopy)
 
virtual ~ThreadSafe ()
 
void LockThread () const
 
void UnlockThread () const
 

Static Public Member Functions

static Id InvalidId ()
 
static Name InvalidName ()
 

Protected Attributes

Hadits m_hadits
 
Id m_nextId
 

Detailed Description

template<typename DIMENSION>
class bio::physical::Perspective< DIMENSION >

A Perspective keeps track of Names and Ids for a certain set of objects within a DIMENSION and ensures a unique Id <-> Name pairing for all objects it "observes". A DIMENSION is a numeric space in which objects may be defined. The size of the space determines how many objects may be "observed" (i.e. kept track of) by a single Perspective. Together, a Perspective and DIMENSION can be thought to define the "size" of the "lens" used to "observe" "objects", if you'll permit the physical analogy of what is purely imaginative. Only objects that share a DIMENSION may be derived from, combined, etc. You cannot have a child class that derives from 2 different Identifiable templates (without solving diamond inheritance and some other caveats). Thus, functionally, you can think of each DIMENSION as a different library, with its source code hidden, such that only objects within that library, that DIMENSION, may inherit from each other. An example DIMENSION would be uint32_t, with up to 4,294,967,295 unique object names per perspective.

An example use case can be seen with Neurons and Synapses. Both are distinct objects and both can be tracked through different Perspectives. This means a Neuron of Id 1 can have the name "MyNeuron" and a Synapse of Id 1 can have the name "MySynapse". However, Neurons and Synapses share a lot of code and should exist within the same DIMENSION (e.g. in case you wanted to make some strange Neuron/Synapse hybrid). If your DIMENSION is a uint8_t, you could have 255 Neurons and 255 Synapses using a different Perspective for each. Using a single Respective, you could only have 255 uniquely identified Neurons OR Connections, total. Therefore, you'd likely want multiple Perspectives and a much larger DIMENSION (uint32_t, for instance) in order to accommodate a more total objects. See below for a macro for creating singleton of Perspectives.

Definition at line 70 of file Perspective.h.

Member Typedef Documentation

◆ Hadits

template<typename DIMENSION >
typedef std::vector< Hadit > bio::physical::Perspective< DIMENSION >::Hadits

Definition at line 101 of file Perspective.h.

◆ Id

template<typename DIMENSION >
typedef DIMENSION bio::physical::Perspective< DIMENSION >::Id

Definition at line 74 of file Perspective.h.

◆ Ids

template<typename DIMENSION >
typedef std::vector< Id > bio::physical::Perspective< DIMENSION >::Ids

Definition at line 75 of file Perspective.h.

Constructor & Destructor Documentation

◆ Perspective()

template<typename DIMENSION >
bio::physical::Perspective< DIMENSION >::Perspective ( )
inline

Definition at line 106 of file Perspective.h.

107 :
108 m_nextId(1)
109 {
110 }

◆ ~Perspective()

template<typename DIMENSION >
virtual bio::physical::Perspective< DIMENSION >::~Perspective ( )
inlinevirtual

Definition at line 115 of file Perspective.h.

116 {
117 LockThread();
118 for (
119 typename Hadits::iterator itt = m_hadits.begin();
120 itt != m_hadits.end();
121 ++itt
122 )
123 {
124 if (itt->m_name)
125 {
126 delete[] itt->m_name;
127 itt->m_name = NULL;
128 }
129 if (itt->m_type)
130 {
131 PerspectiveUtilities::Delete(itt->m_type);
132 itt->m_type = NULL;
133 }
134 }
135 m_hadits.clear();
136 UnlockThread();
137 }
void LockThread() const
Definition: ThreadSafe.cpp:84
void UnlockThread() const
Definition: ThreadSafe.cpp:97
static void Delete(Wave *toDelete)
Definition: Perspective.cpp:33

References bio::physical::PerspectiveUtilities::Delete(), bio::ThreadSafe::LockThread(), bio::physical::Perspective< DIMENSION >::m_hadits, and bio::ThreadSafe::UnlockThread().

Member Function Documentation

◆ AssociateType()

template<typename DIMENSION >
virtual bool bio::physical::Perspective< DIMENSION >::AssociateType ( Id  id,
Wave type 
)
inlinevirtual

Associates the given Wave type with the given id. This is only necessary if you want to use GetTypeFromId later on. NOTE: There is no GetIdFromType(). For that behavior, see chemical::PeriodicTable and chemical::Atom.

Parameters
id
type
Returns
true if the association completed successfully else false

Definition at line 354 of file Perspective.h.

358 {
359 typename Hadits::iterator hdt = Find(id);
360 if (hdt == m_hadits.end() || hdt->m_type)
361 {
362 return false;
363 }
364
365 LockThread();
366 BIO_SANITIZE(type,
367 hdt->m_type = PerspectiveUtilities::Clone(type),
368 hdt->m_type = type);
369 UnlockThread();
370
371 return true;
372 }
#define BIO_SANITIZE(test, success, failure)
Hadits::iterator Find(Id id)
Definition: Perspective.h:162
static Wave * Clone(const Wave *toClone)
Definition: Perspective.cpp:28

References BIO_SANITIZE, bio::physical::PerspectiveUtilities::Clone(), bio::physical::Perspective< DIMENSION >::Find(), bio::ThreadSafe::LockThread(), bio::physical::Perspective< DIMENSION >::m_hadits, and bio::ThreadSafe::UnlockThread().

◆ DisassociateType()

template<typename DIMENSION >
virtual bool bio::physical::Perspective< DIMENSION >::DisassociateType ( Id  id)
inlinevirtual

Removes the type association created by AssociateType().

Parameters
id
Returns
true if the association was removed else false.

Definition at line 379 of file Perspective.h.

380 {
381 typename Hadits::iterator hdt = Find(id);
382 if (hdt == m_hadits.end())
383 {
384 return false;
385 }
386
387 LockThread();
389 PerspectiveUtilities::Delete(hdt->m_type),);
390 hdt->m_type = NULL;
391 UnlockThread();
392
393 return true;
394 }
#define BIO_SANITIZE_AT_SAFETY_LEVEL_2(test, success, failure)

References BIO_SANITIZE_AT_SAFETY_LEVEL_2, bio::physical::PerspectiveUtilities::Delete(), bio::physical::Perspective< DIMENSION >::Find(), bio::ThreadSafe::LockThread(), bio::physical::Perspective< DIMENSION >::m_hadits, and bio::ThreadSafe::UnlockThread().

◆ Find() [1/2]

template<typename DIMENSION >
Hadits::iterator bio::physical::Perspective< DIMENSION >::Find ( Id  id)
inline

Gives an iterator fos the given id.

Parameters
id
Returns
a Hadits::iterator or m_hadits.end();

Definition at line 162 of file Perspective.h.

163 {
164
165 LockThread();
166 typename Hadits::iterator hdt = m_hadits.begin();
167 for (
168 ; hdt != m_hadits.end();
169 ++hdt
170 )
171 {
172 if (hdt->m_id == id)
173 {
174 UnlockThread();
175 return hdt;
176 }
177 }
178 UnlockThread();
179 return hdt;
180 }

References bio::ThreadSafe::LockThread(), bio::physical::Perspective< DIMENSION >::m_hadits, and bio::ThreadSafe::UnlockThread().

Referenced by bio::physical::Perspective< DIMENSION >::AssociateType(), bio::physical::Perspective< DIMENSION >::DisassociateType(), bio::physical::Perspective< DIMENSION >::GetNameFromId(), and bio::physical::Perspective< DIMENSION >::GetTypeFromId().

◆ Find() [2/2]

template<typename DIMENSION >
Hadits::const_iterator bio::physical::Perspective< DIMENSION >::Find ( Id  id) const
inline

Gives an iterator fos the given id.

Parameters
id
Returns
a Hadits::const_iterator or m_hadits.end();

Definition at line 187 of file Perspective.h.

188 {
189 LockThread();
190 typename Hadits::const_iterator hdt = m_hadits.begin();
191 for (
192 ; hdt != m_hadits.end();
193 ++hdt
194 )
195 {
196 if (hdt->m_id == id)
197 {
198 UnlockThread();
199 return hdt;
200 }
201 }
202 UnlockThread();
203 return hdt;
204 }

References bio::ThreadSafe::LockThread(), bio::physical::Perspective< DIMENSION >::m_hadits, and bio::ThreadSafe::UnlockThread().

◆ GetIdFromName()

template<typename DIMENSION >
virtual Id bio::physical::Perspective< DIMENSION >::GetIdFromName ( Name  name)
inlinevirtual

This will create a new Id for the given name if one does not exist.

Parameters
name
Returns
the Id associated with the given name

Definition at line 212 of file Perspective.h.

213 {
214 if (strcmp(
215 InvalidName(),
216 name
217 ))
218 {
219 return InvalidId();
220 }
221
222 Id ret = GetIdWithoutCreation(name);
223 if (ret)
224 {
225 return ret;
226 }
227
228 Name usedName;
230 name,
231 usedName
232 );
233
234 LockThread();
235 ret = m_nextId++;
236 m_hadits.push_back(
237 Hadit(
238 ret,
239 usedName,
240 NULL
241 ));
242 UnlockThread();
243
244 return ret;
245 }
virtual Id GetIdWithoutCreation(Name name) const
Definition: Perspective.h:310
void CloneInto(const char *source, const char *&target)
Definition: String.cpp:236
const char * Name
Definition: Types.h:46

References bio::string::CloneInto(), bio::physical::Perspective< DIMENSION >::GetIdWithoutCreation(), bio::physical::Perspective< DIMENSION >::InvalidId(), bio::physical::Perspective< DIMENSION >::InvalidName(), bio::ThreadSafe::LockThread(), bio::physical::Perspective< DIMENSION >::m_hadits, bio::physical::Perspective< DIMENSION >::m_nextId, and bio::ThreadSafe::UnlockThread().

Referenced by bio::physical::Perspective< DIMENSION >::GetNewObjectFromName(), bio::chemical::LinearMotif< CONTENT_TYPE >::GetOrCreateByNameImplementation(), bio::physical::Perspective< DIMENSION >::GetUniqueIdFor(), and bio::genetic::Plasmid::TranscribeFor().

◆ GetIdWithoutCreation()

template<typename DIMENSION >
virtual Id bio::physical::Perspective< DIMENSION >::GetIdWithoutCreation ( Name  name) const
inlinevirtual

the same as GetIdFromName but will RETURN 0 instead of making a new association, if name is not found.

Parameters
name
Returns
the Id associated with name else InvalidId().

Definition at line 310 of file Perspective.h.

311 {
312 if (strcmp(
313 InvalidName(),
314 name
315 ))
316 {
317 return InvalidId();
318 }
319
320 typename Hadits::const_iterator itt = m_hadits.begin();
321 for (
322 ; itt != m_hadits.end();
323 ++itt
324 )
325 {
326 if (!strcmp(
327 itt->m_name,
328 name
329 ))
330 {
331 return itt->m_id;
332 }
333 }
334 return InvalidId();
335 }

References bio::physical::Perspective< DIMENSION >::InvalidId(), bio::physical::Perspective< DIMENSION >::InvalidName(), and bio::physical::Perspective< DIMENSION >::m_hadits.

Referenced by bio::physical::Perspective< DIMENSION >::GetIdFromName(), bio::physical::Perspective< DIMENSION >::GetTypeFromName(), and bio::physical::Perspective< DIMENSION >::GetUniqueIdFor().

◆ GetNameFromId()

template<typename DIMENSION >
virtual Name bio::physical::Perspective< DIMENSION >::GetNameFromId ( Id  id) const
inlinevirtual

This requires that the Id has been previously associated with the name, perhaps from a call to GetIdFromName.

Parameters
id
Returns
the Name associated with the given Id

Definition at line 253 of file Perspective.h.

254 {
255 if (id == InvalidId())
256 {
257 return InvalidName();
258 }
259
260 typename Hadits::const_iterator result = Find(id);
261 if (result == m_hadits.end())
262 {
263 return InvalidName();
264 }
265 return result->m_name;
266 }

References bio::physical::Perspective< DIMENSION >::Find(), bio::physical::Perspective< DIMENSION >::InvalidId(), bio::physical::Perspective< DIMENSION >::InvalidName(), and bio::physical::Perspective< DIMENSION >::m_hadits.

◆ GetNewObjectFromId()

template<typename DIMENSION >
virtual Wave * bio::physical::Perspective< DIMENSION >::GetNewObjectFromId ( Id  id) const
inlinevirtual

Creates a new object by Clone()ing the associated type.

Parameters
id
Returns
a Clone() of the Wave* associated with the given id else NULL.

Definition at line 430 of file Perspective.h.

431 {
432 const Wave* ret = GetTypeFromId(id);
433 if (ret)
434 {
435 return PerspectiveUtilities::Clone(ret);
436 }
437 return NULL;
438 }
virtual const Wave * GetTypeFromId(Id id) const
Definition: Perspective.h:402

References bio::physical::PerspectiveUtilities::Clone(), and bio::physical::Perspective< DIMENSION >::GetTypeFromId().

Referenced by bio::physical::Perspective< DIMENSION >::GetNewObjectFromIdAs(), and bio::physical::Perspective< DIMENSION >::GetNewObjectFromName().

◆ GetNewObjectFromIdAs()

template<typename DIMENSION >
template<typename T >
T bio::physical::Perspective< DIMENSION >::GetNewObjectFromIdAs ( Id  id)
inline

Ease of use method for casting the result of GetNewObjectFromId()

Template Parameters
T
Parameters
id
Returns
a new T* from Clone()ing the type associated with the given id else NULL.

Definition at line 485 of file Perspective.h.

486 {
488 BIO_SINGLE_ARG(return ForceCast< T, Wave* >(RESULT)),
489 return NULL);
490 }
#define BIO_SANITIZE_WITH_CACHE(test, success, failure)
virtual Wave * GetNewObjectFromId(Id id) const
Definition: Perspective.h:430
#define BIO_SINGLE_ARG(...)
Definition: Macros.h:66

References BIO_SANITIZE_WITH_CACHE, BIO_SINGLE_ARG, and bio::physical::Perspective< DIMENSION >::GetNewObjectFromId().

◆ GetNewObjectFromName()

template<typename DIMENSION >
virtual Wave * bio::physical::Perspective< DIMENSION >::GetNewObjectFromName ( Name  name)
inlinevirtual

Creates a new object by Clone()ing the associated type.

Parameters
name
Returns
a Clone() of the Wave* associated with the given name else NULL.

Definition at line 445 of file Perspective.h.

446 {
447 return this->GetNewObjectFromId(this->GetIdFromName(name));
448 }
virtual Id GetIdFromName(Name name)
Definition: Perspective.h:212

References bio::physical::Perspective< DIMENSION >::GetIdFromName(), and bio::physical::Perspective< DIMENSION >::GetNewObjectFromId().

Referenced by bio::physical::Perspective< DIMENSION >::GetNewObjectFromNameAs().

◆ GetNewObjectFromNameAs()

template<typename DIMENSION >
template<typename T >
T bio::physical::Perspective< DIMENSION >::GetNewObjectFromNameAs ( Name  name)
inline

Ease of access method for casting the result of GetNewObjectFromName()

Template Parameters
T
Parameters
name
Returns
a new T* from Clone()ing the type associated with the given name else NULL.

Definition at line 499 of file Perspective.h.

500 {
502 BIO_SINGLE_ARG(return ForceCast< T, Wave* >(RESULT)),
503 return NULL);
504 }
virtual Wave * GetNewObjectFromName(Name name)
Definition: Perspective.h:445

References BIO_SANITIZE_WITH_CACHE, BIO_SINGLE_ARG, and bio::physical::Perspective< DIMENSION >::GetNewObjectFromName().

◆ GetNumUsedIds()

template<typename DIMENSION >
virtual Id bio::physical::Perspective< DIMENSION >::GetNumUsedIds ( ) const
inlinevirtual
Returns
the number of ids stored in *this.

Definition at line 340 of file Perspective.h.

341 {
342 return this->m_nextId - 1;
343 }

References bio::physical::Perspective< DIMENSION >::m_nextId.

◆ GetTypeFromId()

template<typename DIMENSION >
virtual const Wave * bio::physical::Perspective< DIMENSION >::GetTypeFromId ( Id  id) const
inlinevirtual

Only works if AssociateType has been called with the given id.

Parameters
id
Returns
the pointer to the Wave type associated with the given id else NULL.

Definition at line 402 of file Perspective.h.

403 {
404 BIO_SANITIZE(id == InvalidId(), ,
405 return NULL)
406
407 typename Hadits::const_iterator result = Find(id);
408 if (result == m_hadits.end())
409 {
410 return NULL;
411 }
412 return result->m_type;
413 }

References BIO_SANITIZE, bio::physical::Perspective< DIMENSION >::Find(), bio::physical::Perspective< DIMENSION >::InvalidId(), and bio::physical::Perspective< DIMENSION >::m_hadits.

Referenced by bio::physical::Perspective< DIMENSION >::GetNewObjectFromId(), bio::physical::Perspective< DIMENSION >::GetTypeFromIdAs(), and bio::physical::Perspective< DIMENSION >::GetTypeFromName().

◆ GetTypeFromIdAs()

template<typename DIMENSION >
template<typename T >
const T bio::physical::Perspective< DIMENSION >::GetTypeFromIdAs ( Id  id) const
inline

Ease of access method for casting the result of GetTypeFromId().

Template Parameters
T
Parameters
id
Returns
a T* associated with the given name id NULL.

Definition at line 457 of file Perspective.h.

458 {
460 BIO_SINGLE_ARG(return ForceCast< T, const Wave* >(RESULT)),
461 return NULL);
462 }

References BIO_SANITIZE_WITH_CACHE, BIO_SINGLE_ARG, and bio::physical::Perspective< DIMENSION >::GetTypeFromId().

◆ GetTypeFromName()

template<typename DIMENSION >
virtual const Wave * bio::physical::Perspective< DIMENSION >::GetTypeFromName ( Name  name) const
inlinevirtual

Only works if AssociateType has been called with the given id.

Parameters
name
Returns
the pointer to the Wave type associated with the given id else NULL.

Definition at line 420 of file Perspective.h.

421 {
423 }

References bio::physical::Perspective< DIMENSION >::GetIdWithoutCreation(), and bio::physical::Perspective< DIMENSION >::GetTypeFromId().

Referenced by bio::physical::Perspective< DIMENSION >::GetTypeFromNameAs().

◆ GetTypeFromNameAs()

template<typename DIMENSION >
template<typename T >
const T bio::physical::Perspective< DIMENSION >::GetTypeFromNameAs ( Name  name) const
inline

Ease of access method for casting the result of GetTypeFromId().

Template Parameters
T
Parameters
name
Returns
a T* associated with the given name id NULL.

Definition at line 471 of file Perspective.h.

472 {
474 BIO_SINGLE_ARG(return ForceCast< T, const Wave* >(RESULT)),
475 return NULL);
476 }
virtual const Wave * GetTypeFromName(Name name) const
Definition: Perspective.h:420

References BIO_SANITIZE_WITH_CACHE, BIO_SINGLE_ARG, and bio::physical::Perspective< DIMENSION >::GetTypeFromName().

◆ GetUniqueIdFor()

template<typename DIMENSION >
virtual Id bio::physical::Perspective< DIMENSION >::GetUniqueIdFor ( Name  name)
inlinevirtual

There can be up to 256 additional names.

Parameters
name
Returns
a new Id for the given Name. However, the Name associated with the returned Id may not be the one provided. For example, consider: GetNameFromId(GetUniqueIdFor("MyName")); //Returns "MyName" GetNameFromId(GetUniqueIdFor("MyName")); //Returns "MyName_1"

Definition at line 274 of file Perspective.h.

275 {
276 if (strcmp(
277 InvalidName(),
278 name
279 ))
280 {
281 return InvalidId();
282 }
283
284 std::ostringstream usedName;
285 usedName.str(""); //TODO: can we do this all in the ctor?
286 usedName << name;
287
288 Id ret = GetIdWithoutCreation(usedName.str().c_str());
289
290 uint8_t nameCount = 0;
291 while (!ret)
292 {
293 usedName.clear();
294 usedName.str(""); //TODO: is this right?
295 usedName << name;
296 usedName << "_" << nameCount++;
297 ret = GetIdWithoutCreation(usedName.str().c_str());
298 }
299
300 //this creates the unique id.
301 return GetIdFromName(usedName.str().c_str());
302 }

References bio::physical::Perspective< DIMENSION >::GetIdFromName(), bio::physical::Perspective< DIMENSION >::GetIdWithoutCreation(), bio::physical::Perspective< DIMENSION >::InvalidId(), and bio::physical::Perspective< DIMENSION >::InvalidName().

◆ InvalidId()

template<typename DIMENSION >
static Id bio::physical::Perspective< DIMENSION >::InvalidId ( )
inlinestatic

When overloading other methods of *this, make sure to check your inputs for invalid Ids. See the functions below for examples.

Returns
0.

Definition at line 143 of file Perspective.h.

144 {
145 return 0;
146 }

Referenced by bio::physical::Perspective< DIMENSION >::GetIdFromName(), bio::physical::Perspective< DIMENSION >::GetIdWithoutCreation(), bio::physical::Perspective< DIMENSION >::GetNameFromId(), bio::physical::Perspective< DIMENSION >::GetTypeFromId(), and bio::physical::Perspective< DIMENSION >::GetUniqueIdFor().

◆ InvalidName()

template<typename DIMENSION >
static Name bio::physical::Perspective< DIMENSION >::InvalidName ( )
inlinestatic

When overloading other methods of *this, make sure to check your inputs for invalid Names. See the functions below for examples.

Returns
"INVALID_NAME".

Definition at line 152 of file Perspective.h.

153 {
154 return "INVALID_NAME";
155 }

Referenced by bio::physical::Perspective< DIMENSION >::GetIdFromName(), bio::physical::Perspective< DIMENSION >::GetIdWithoutCreation(), bio::physical::Perspective< DIMENSION >::GetNameFromId(), and bio::physical::Perspective< DIMENSION >::GetUniqueIdFor().

Member Data Documentation

◆ m_hadits

◆ m_nextId

template<typename DIMENSION >
Id bio::physical::Perspective< DIMENSION >::m_nextId
protected

The documentation for this class was generated from the following file: