Develop Biology
The language of life
Perspective.h
Go to the documentation of this file.
1/*
2 * This file is a part of the Biology project by eons LLC.
3 * Biology (aka Develop Biology) is a framework for approaching software
4 * development from a natural sciences perspective.
5 *
6 * Copyright (C) 2021 Séon O'Shannon & eons LLC
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
25#include "bio/common/Types.h"
26#include "bio/common/String.h"
28#include "bio/common/Cast.h"
29#include <sstream>
30#include <cstring>
31
32//@formatter:off
33#if BIO_CPP_VERSION < 11
34 #include <stdint.h>
35#else
36 #include <cstdint>
37#endif
38//@formatter:on
39
40namespace bio {
41namespace physical {
42
43class Wave;
44
50{
51 static Wave* Clone(const Wave* toClone);
52
53 static void Delete(Wave* toDelete);
54};
55
69template < typename DIMENSION >
71 virtual public ThreadSafe
72{
73public:
74 typedef DIMENSION Id;
75 typedef std::vector< Id > Ids;
76
81 class Hadit
82 {
83 public:
85 Id id,
86 Name name,
87 Wave* type
88 )
89 :
90 m_id(id),
91 m_name(name),
92 m_type(type)
93 {
94 }
95
99 };
100
101 typedef std::vector< Hadit > Hadits;
102
107 :
108 m_nextId(1)
109 {
110 }
111
115 virtual ~Perspective()
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 }
138
143 static Id InvalidId()
144 {
145 return 0;
146 }
147
153 {
154 return "INVALID_NAME";
155 }
156
162 typename Hadits::iterator Find(Id id)
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 }
181
187 typename Hadits::const_iterator Find(Id id) const
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 }
205
206
212 virtual Id GetIdFromName(Name name)
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 }
246
247
253 virtual Name GetNameFromId(Id id) const
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 }
267
268
274 virtual Id GetUniqueIdFor(Name name)
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 }
303
304
310 virtual Id GetIdWithoutCreation(Name name) const
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 }
336
340 virtual Id GetNumUsedIds() const
341 {
342 return this->m_nextId - 1;
343 }
344
345
354 virtual bool AssociateType(
355 Id id,
356 Wave* type
357 )
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 }
373
379 virtual bool DisassociateType(Id id)
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 }
395
396
402 virtual const Wave* GetTypeFromId(Id id) const
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 }
414
420 virtual const Wave* GetTypeFromName(Name name) const
421 {
423 }
424
430 virtual Wave* GetNewObjectFromId(Id id) const
431 {
432 const Wave* ret = GetTypeFromId(id);
433 if (ret)
434 {
435 return PerspectiveUtilities::Clone(ret);
436 }
437 return NULL;
438 }
439
446 {
447 return this->GetNewObjectFromId(this->GetIdFromName(name));
448 }
449
456 template < typename T >
457 const T GetTypeFromIdAs(Id id) const
458 {
460 BIO_SINGLE_ARG(return ForceCast< T, const Wave* >(RESULT)),
461 return NULL);
462 }
463
470 template < typename T >
471 const T GetTypeFromNameAs(Name name) const
472 {
474 BIO_SINGLE_ARG(return ForceCast< T, const Wave* >(RESULT)),
475 return NULL);
476 }
477
484 template < typename T >
486 {
488 BIO_SINGLE_ARG(return ForceCast< T, Wave* >(RESULT)),
489 return NULL);
490 }
491
498 template < typename T >
500 {
502 BIO_SINGLE_ARG(return ForceCast< T, Wave* >(RESULT)),
503 return NULL);
504 }
505
506
507protected:
510};
511
512} //physical namespace
513} //bio namespace
#define BIO_SANITIZE_AT_SAFETY_LEVEL_2(test, success, failure)
#define BIO_SANITIZE(test, success, failure)
#define BIO_SANITIZE_WITH_CACHE(test, success, failure)
void LockThread() const
Definition: ThreadSafe.cpp:84
void UnlockThread() const
Definition: ThreadSafe.cpp:97
Hadit(Id id, Name name, Wave *type)
Definition: Perspective.h:84
virtual Id GetNumUsedIds() const
Definition: Perspective.h:340
T GetNewObjectFromNameAs(Name name)
Definition: Perspective.h:499
const T GetTypeFromIdAs(Id id) const
Definition: Perspective.h:457
virtual const Wave * GetTypeFromName(Name name) const
Definition: Perspective.h:420
virtual Id GetIdWithoutCreation(Name name) const
Definition: Perspective.h:310
Hadits::iterator Find(Id id)
Definition: Perspective.h:162
std::vector< Hadit > Hadits
Definition: Perspective.h:101
virtual Wave * GetNewObjectFromName(Name name)
Definition: Perspective.h:445
virtual bool AssociateType(Id id, Wave *type)
Definition: Perspective.h:354
const T GetTypeFromNameAs(Name name) const
Definition: Perspective.h:471
virtual Id GetIdFromName(Name name)
Definition: Perspective.h:212
virtual const Wave * GetTypeFromId(Id id) const
Definition: Perspective.h:402
Hadits::const_iterator Find(Id id) const
Definition: Perspective.h:187
virtual bool DisassociateType(Id id)
Definition: Perspective.h:379
virtual Wave * GetNewObjectFromId(Id id) const
Definition: Perspective.h:430
virtual Id GetUniqueIdFor(Name name)
Definition: Perspective.h:274
std::vector< Id > Ids
Definition: Perspective.h:75
virtual Name GetNameFromId(Id id) const
Definition: Perspective.h:253
#define BIO_SINGLE_ARG(...)
Definition: Macros.h:66
void CloneInto(const char *source, const char *&target)
Definition: String.cpp:236
Definition: Cell.h:31
const char * Name
Definition: Types.h:46
static void Delete(Wave *toDelete)
Definition: Perspective.cpp:33
static Wave * Clone(const Wave *toClone)
Definition: Perspective.cpp:28