Hurricane VLSI Database


Commons.h
1 // -*- C++ -*-
2 //
3 // Copyright (c) BULL S.A. 2000-2018, All Rights Reserved
4 //
5 // This file is part of Hurricane.
6 //
7 // Hurricane is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
11 //
12 // Hurricane is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-
14 // TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the Lesser GNU General Public
18 // License along with Hurricane. If not, see
19 // <http://www.gnu.org/licenses/>.
20 //
21 // +-----------------------------------------------------------------+
22 // | H U R R I C A N E |
23 // | V L S I B a c k e n d D a t a - B a s e |
24 // | |
25 // | Author : Remy Escassut |
26 // | E-mail : Jean-Paul.Chaput@lip6.fr |
27 // | =============================================================== |
28 // | C++ Header : "./hurricane/Commons.h" |
29 // +-----------------------------------------------------------------+
30 
31 
32 #pragma once
33 #define HURRICANE_COMMONS_H
34 
35 #include <cstdio>
36 #include <cstdint>
37 #include <cassert>
38 #include <cmath>
39 #include <memory>
40 #include <string>
41 #include <list>
42 #include <set>
43 #include <map>
44 #include <stack>
45 #include <array>
46 #include <vector>
47 #include <iostream>
48 #include <iomanip>
49 #include <fstream>
50 #include <sstream>
51 
52 
53 // +-----------------------------------------------------------------+
54 // | Macros Definition |
55 // +-----------------------------------------------------------------+
56 
57 
58 namespace Hurricane {
59 
60  using namespace std;
61 
62  class Slot;
63 
64 
65  // +-------------------------------------------------------------+
66  // | shared_ptr<> support for DBo |
67  // +-------------------------------------------------------------+
68 
69 
70  template<typename DboType>
71  class DboDestroy {
72  public:
73  inline void operator() ( DboType* dbo ) { dbo->destroy(); }
74  };
75 
76 
77  template<typename DboType>
78  class dbo_ptr : public std::shared_ptr<DboType> {
79  public:
80  dbo_ptr ( DboType* dbo ) : std::shared_ptr<DboType>(dbo,DboDestroy<DboType>()) { }
81  };
82 
83 
84 
85 
86  // +-------------------------------------------------------------+
87  // | Miscellaneous Utilites |
88  // +-------------------------------------------------------------+
89 
90 
91  inline string _TName ( const string& s ) { return s; }
92  inline string _PName ( const string& s ) { return "Hurricane::" + s; }
93 
94  template<class Type>
95  inline Type abs ( const Type& value ) { return (value<0) ? -value : value; }
96 
97  string demangle ( const char* symbol );
98  inline string demangle ( string symbol ) { return demangle(symbol.c_str()); }
99  inline string demangle ( const type_info& info ) { return demangle(info.name()); }
100 
101  template<typename Element>
102  inline void erase_element ( vector<Element*>& v, const Element* e )
103  {
104  for ( auto ielement = v.begin() ; ielement != v.end() ; ++ielement )
105  if (*ielement == e) { v.erase( ielement ); return; }
106  }
107 
108 
109 #if DEPRECATED
110 // For a complete explanation of this function, please look at :
111 // http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
112 
113  inline int floatCompare ( float a, float b )
114  {
115  assert ( sizeof(float) == sizeof(int) );
116 
117  if ( a == b ) return 0;
118  return *(int*)&a - *(int*)&b;
119  }
120 
121  inline int floatDifference ( float a, float b, int threshold )
122  {
123  int difference = floatCompare(a,b);
124  if ( abs(difference) < threshold ) return 0;
125 
126  return (difference<0) ? -1 : 1;
127  }
128 
129 
130  inline void floatRound ( float& value, float precision )
131  {
132  float rounded = roundf ( value*precision );
133  value = rounded / precision;
134  }
135 #endif
136 
137  inline float roundfp ( float value, float precision=100.0 ) { return roundf(value*precision)/precision; }
138 
139 
140  template<typename Type> inline void order ( Type& a, Type& b ) { if (a>b) std::swap(a,b); }
141 
142  template<typename Type> inline Type setInBound ( Type lower, Type upper, Type& value )
143  {
144  if (value < lower) value = lower;
145  else if (value > upper) value = upper;
146  return value;
147  }
148 
149 
150  string& split ( std::string& );
151 
152 
153 // -------------------------------------------------------------------
154 // Function : Fowler-Noll-Vo hash.
155 //
156 // Reference:
157 // https://en.wikipedia.org/wiki/Fowler-Noll-Vo_hash_function
158 
159  inline unsigned int hashFNV ( unsigned int id )
160  {
161  // Assume unsigned int is 32 bits.
162  static const unsigned int FNVOffsetBasis = 0x811c9dc5;
163  static const unsigned int FNVPrime = 0x01000193;
164 
165  unsigned int hash = FNVOffsetBasis;
166  for ( unsigned int idBytes = id ; idBytes ; idBytes >>= 8 ) {
167  hash = hash * FNVPrime;
168  hash = hash xor (idBytes & 0xFF);
169  }
170  return hash;
171  }
172 
173  inline unsigned int iterFNV ( unsigned int hash, unsigned int id )
174  {
175  // Assume unsigned int is 32 bits.
176  static const unsigned int FNVPrime = 0x01000193;
177 
178  for ( unsigned int idBytes = id ; idBytes ; idBytes >>= 8 ) {
179  hash = hash * FNVPrime;
180  hash = hash xor (idBytes & 0xFF);
181  }
182  return hash;
183  }
184 
185 
186  inline unsigned int hashFNV ( std::string s )
187  {
188  // Assume unsigned int is 32 bits.
189  static const unsigned int FNVOffsetBasis = 0x811c9dc5;
190  static const unsigned int FNVPrime = 0x01000193;
191 
192  unsigned int hash = FNVOffsetBasis;
193  for ( size_t i=0 ; i<s.size() ; ++i ) {
194  hash = hash * FNVPrime;
195  hash = hash xor s[i];
196  }
197  return hash;
198  }
199 
200 
201  inline unsigned int hashFNV ( const char* s )
202  {
203  // Assume unsigned int is 32 bits.
204  static const unsigned int FNVOffsetBasis = 0x811c9dc5;
205  static const unsigned int FNVPrime = 0x01000193;
206 
207  unsigned int hash = FNVOffsetBasis;
208  for ( ; (int)(*s) != 0 ; ++s ) {
209  hash = hash * FNVPrime;
210  hash = hash xor *s;
211  }
212  return hash;
213  }
214 
215 
216 } // End of Hurricane namespace.
217 
218 
219 #include "hurricane/Record.h"
220 
221 
222 // +-----------------------------------------------------------------+
223 // | Functions for Inspector Support |
224 // +-----------------------------------------------------------------+
225 
226 // Note 1: Theses are specialized templates for "getString<>()" & "getRecord<>()".
227 // Note 2: we are outside the Hurricane namespace.
228 // Note 3: thoses templates manage all POD & STL types.
229 
230 
231 template<typename Data> inline Hurricane::Slot* getSlot ( std::string name, Data );
232 
233 
234 // -------------------------------------------------------------------
235 // Inspector Support for : "POD types".
236 
237 // Default match.
238 
239 template<typename Data> inline std::string getString ( Data data )
240 { return std::string("<type ")
241  + Hurricane::demangle(typeid(data).name())
242  + std::string(" unsupported by getString()>"); }
243 
244 template<> inline std::string getString<std::nullptr_t> ( std::nullptr_t )
245 { return "nullptr" ; }
246 
247 // "const &" flavors.
248 
249 template<> inline std::string getString<const bool&> ( const bool& b )
250 { return (b)?"True":"False" ; }
251 
252 template<> inline std::string getString<const int&> ( const int& i )
253 { std::ostringstream os (""); os << i; return os.str(); }
254 
255 template<> inline std::string getString<const long&> ( const long& l )
256 { std::ostringstream os (""); os << l; return os.str(); }
257 
258 template<> inline std::string getString<const unsigned int&> ( const unsigned int& u )
259 { std::ostringstream os (""); os << u; return os.str(); }
260 
261 template<> inline std::string getString<const unsigned long&> ( const unsigned long& ul )
262 { std::ostringstream os (""); os << ul; return os.str(); }
263 
264 template<> inline std::string getString<const unsigned long long&> ( const unsigned long long& ull )
265 { std::ostringstream os (""); os << ull; return os.str(); }
266 
267 template<> inline std::string getString<const unsigned short int&> ( const unsigned short int& us )
268 { std::ostringstream os (""); os << us; return os.str(); }
269 
270 template<> inline std::string getString<const float&> ( const float& f )
271 { std::ostringstream os (""); os << f; return os.str(); }
272 
273 template<> inline std::string getString<const double&> ( const double& d )
274 { std::ostringstream os; os << d; return os.str(); }
275 
276 template<> inline std::string getString<const std::string&> ( const std::string& s )
277 { return s; }
278 
279 // "const *" flavors.
280 
281 template<> inline std::string getString<const bool*> ( const bool* b )
282 { return (*b)?"True":"False" ; }
283 
284 template<> inline std::string getString<const char*> ( const char* c )
285 { return c; }
286 
287 template<> inline std::string getString<const int*> ( const int* i )
288 { std::ostringstream os (""); os << *i; return os.str(); }
289 
290 template<> inline std::string getString<const long*> ( const long* l )
291 { std::ostringstream os (""); os << *l; return os.str(); }
292 
293 template<> inline std::string getString<const unsigned int*> ( const unsigned int* u )
294 { std::ostringstream os (""); os << *u; return os.str(); }
295 
296 template<> inline std::string getString<const unsigned long*> ( const unsigned long* ul )
297 { std::ostringstream os (""); os << *ul; return os.str(); }
298 
299 template<> inline std::string getString<const unsigned long long*> ( const unsigned long long* ull )
300 { std::ostringstream os (""); os << *ull; return os.str(); }
301 
302 template<> inline std::string getString<const unsigned short int*> ( const unsigned short int* us )
303 { std::ostringstream os (""); os << *us; return os.str(); }
304 
305 template<> inline std::string getString<const float*> ( const float* f )
306 { std::ostringstream os (""); os << *f; return os.str(); }
307 
308 template<> inline std::string getString<const double*> ( const double* d )
309 { std::ostringstream os; os << *d; return os.str(); }
310 
311 template<> inline std::string getString<const void*> ( const void* p )
312 { std::ostringstream os ("0x"); os << std::hex << p; return os.str(); }
313 
314 template<> inline std::string getString<const std::string*> ( const std::string* s )
315 { return *s; }
316 
317 
318 // "*" flavors.
319 
320 template<> inline std::string getString<bool*> ( bool* b )
321 { return (*b)?"True":"False" ; }
322 
323 template<> inline std::string getString<char*> ( char* c )
324 { return c; }
325 
326 template<> inline std::string getString<int*> ( int* i )
327 { std::ostringstream os (""); os << *i; return os.str(); }
328 
329 template<> inline std::string getString<long*> ( long* l )
330 { std::ostringstream os (""); os << *l; return os.str(); }
331 
332 template<> inline std::string getString<unsigned int*> ( unsigned int* u )
333 { std::ostringstream os (""); os << *u; return os.str(); }
334 
335 template<> inline std::string getString<unsigned long*> ( unsigned long* ul )
336 { std::ostringstream os (""); os << *ul; return os.str(); }
337 
338 template<> inline std::string getString<unsigned long long*> ( unsigned long long* ull )
339 { std::ostringstream os (""); os << *ull; return os.str(); }
340 
341 template<> inline std::string getString<unsigned short int*> ( unsigned short int* us )
342 { std::ostringstream os (""); os << *us; return os.str(); }
343 
344 template<> inline std::string getString<float*> ( float* f )
345 { std::ostringstream os (""); os << *f; return os.str(); }
346 
347 template<> inline std::string getString<double*> ( double* d )
348 { std::ostringstream os; os << *d; return os.str(); }
349 
350 template<> inline std::string getString<void*> ( void* p )
351 { std::ostringstream os ("0x"); os << std::hex << p; return os.str(); }
352 
353 template<> inline std::string getString<std::string*> ( std::string* s )
354 { return *s; }
355 
356 
357 // "by value" flavors.
358 
359 template<> inline std::string getString<bool> ( bool b )
360 { return (b)?"True":"False" ; }
361 
362 template<> inline std::string getString<char> ( char c )
363 { return std::string(1,c); }
364 
365 template<> inline std::string getString<int> ( int i )
366 { std::ostringstream os (""); os << i; return os.str(); }
367 
368 template<> inline std::string getString<long> ( long l )
369 { std::ostringstream os (""); os << l; return os.str(); }
370 
371 template<> inline std::string getString<unsigned int> ( unsigned int u )
372 { std::ostringstream os (""); os << u; return os.str(); }
373 
374 template<> inline std::string getString<unsigned long> ( unsigned long ul )
375 { std::ostringstream os (""); os << ul; return os.str(); }
376 
377 template<> inline std::string getString<unsigned long long> ( unsigned long long ull )
378 { std::ostringstream os (""); os << ull; return os.str(); }
379 
380 template<> inline std::string getString<unsigned short int> ( unsigned short int us )
381 { std::ostringstream os (""); os << us; return os.str(); }
382 
383 template<> inline std::string getString<float> ( float f )
384 { std::ostringstream os (""); os << f; return os.str(); }
385 
386 template<> inline std::string getString<double> ( double d )
387 { std::ostringstream os; os << d; return os.str(); }
388 
389 template<> inline std::string getString<std::string> ( std::string s )
390 { return s; }
391 
392 
393 template<typename Data> inline Hurricane::Record* getRecord ( Data data )
394 {
395 //std::cerr << "::getRecord(Data) Data=" << Hurricane::demangle(typeid(data).name()) << std::endl;
396  return NULL;
397 }
398 
399 
400 // -------------------------------------------------------------------
401 // Inspector Support for : "[const] std::pair<T,U>&".
402 
403 template<typename T, typename U>
404 inline std::string getString ( const std::pair<T,U>& p )
405 {
406  return "const std::pair<T,U>";
407 }
408 
409 
410 template<typename T, typename U>
411 inline Hurricane::Record* getRecord ( const std::pair<T,U>& p )
412 {
413  Hurricane::Record* record = NULL;
414  record = new Hurricane::Record ( "const std::pair<T,U>" );
415  record->add( getSlot<const T>(std::string("first" ), &p.first ) );
416  record->add( getSlot<const U>(std::string("second"), &p.second) );
417  return record;
418 }
419 
420 
421 template<typename T, typename U>
422 inline std::string getString ( std::pair<T,U>& p )
423 {
424  return "std::pair<T,U>";
425 }
426 
427 
428 template<typename T, typename U>
429 inline Hurricane::Record* getRecord ( std::pair<T,U>& p )
430 {
431  Hurricane::Record* record = NULL;
432  record = new Hurricane::Record ( "std::pair<T,U>" );
433  record->add( getSlot<T>(std::string("first" ), &p.first ) );
434  record->add( getSlot<U>(std::string("second"), &p.second) );
435  return record;
436 }
437 
438 
439 // -------------------------------------------------------------------
440 // Inspector Support for : "[const] std::array<Element>*".
441 
442 
443 template<typename Element,size_t N>
444 inline std::string getString ( std::array<Element,N>* v )
445 {
446  std::string name = "const std::array<Element,N>:";
447  return name + getString<size_t>(v->size());
448 }
449 
450 
451 template<typename Element,size_t N>
452 inline Hurricane::Record* getRecord ( std::array<Element,N>* v )
453 {
454  Hurricane::Record* record = NULL;
455  if ( !v->empty() ) {
456  record = new Hurricane::Record ( "std::array<Element,N>" );
457  unsigned n = 0;
458  typename std::array<Element,N>::iterator iterator = v->begin();
459  while ( iterator != v->end() ) {
460  record->add ( getSlot<Element>(getString(n++), *iterator) );
461  ++iterator;
462  }
463  }
464  return record;
465 }
466 
467 
468 template<typename Element,size_t N>
469 inline std::string getString ( const std::array<Element,N>* v )
470 {
471  std::string name = "const std::array<Element,N>:";
472  return name + getString<size_t>(v->size());
473 }
474 
475 
476 template<typename Element,size_t N>
477 inline Hurricane::Record* getRecord ( const std::array<Element,N>* v )
478 {
479  Hurricane::Record* record = NULL;
480  if ( !v->empty() ) {
481  record = new Hurricane::Record ( "const std::array<Element,N>" );
482  unsigned n = 0;
483  typename std::array<Element,N>::const_iterator iterator = v->begin();
484  while ( iterator != v->end() ) {
485  record->add ( getSlot<const Element>(getString(n++), *iterator) );
486  ++iterator;
487  }
488  }
489  return record;
490 }
491 
492 
493 template<typename Element,size_t N>
494 inline std::string getString ( std::array<Element,N>& v )
495 {
496  std::string name = "std::array<Element,N>&:";
497  return name + getString<size_t>(v.size());
498 }
499 
500 
501 template<typename Element,size_t N>
502 inline Hurricane::Record* getRecord ( std::array<Element,N>& v )
503 {
504  Hurricane::Record* record = NULL;
505  if (not v.empty()) {
506  record = new Hurricane::Record ( "std::array<Element,N>&" );
507  unsigned n = 0;
508  for ( auto element : v )
509  record->add( getSlot<Element>(getString(n++), element) );
510  }
511  return record;
512 }
513 
514 
515 template<typename Element,size_t N>
516 inline std::string getString ( const std::array<Element,N>& v )
517 {
518  std::string name = "const std::array<Element,N>&:";
519  return name + getString<size_t>(v.size());
520 }
521 
522 
523 template<typename Element,size_t N>
524 inline Hurricane::Record* getRecord ( const std::array<Element,N>& v )
525 {
526  Hurricane::Record* record = NULL;
527  if (not v.empty()) {
528  record = new Hurricane::Record ( "const std::array<Element,N>&" );
529  unsigned n = 0;
530  for ( auto element : v )
531  record->add( getSlot<Element>(getString(n++), element) );
532  }
533  return record;
534 }
535 
536 
537 // -------------------------------------------------------------------
538 // Inspector Support for : "std::vector<Element>*".
539 
540 
541 template<typename Element>
542 inline std::string getString ( std::vector<Element>* v )
543 {
544  std::string name = "std::vector<Element>*:";
545  return name + getString<size_t>(v->size());
546 }
547 
548 
549 template<typename Element>
550 inline Hurricane::Record* getRecord ( std::vector<Element>* v )
551 {
552  Hurricane::Record* record = NULL;
553  if ( !v->empty() ) {
554  record = new Hurricane::Record ( "std::vector<Element>*" );
555  unsigned n = 0;
556  typename std::vector<Element>::iterator iterator = v->begin();
557  while ( iterator != v->end() ) {
558  record->add ( getSlot<const Element*>(getString(n++), &(*iterator)) );
559  ++iterator;
560  }
561  }
562  return record;
563 }
564 
565 
566 // -------------------------------------------------------------------
567 // Inspector Support for : "std::vector<Element*>*".
568 
569 
570 template<typename Element>
571 inline std::string getString ( std::vector<Element*>* v )
572 {
573  std::string name = "std::vector<Element*>*:";
574  return name + getString<size_t>(v->size());
575 }
576 
577 
578 template<typename Element>
579 inline Hurricane::Record* getRecord ( std::vector<Element*>* v )
580 {
581  Hurricane::Record* record = NULL;
582  if ( !v->empty() ) {
583  record = new Hurricane::Record ( "std::vector<Element*>*" );
584  unsigned n = 0;
585  typename std::vector<Element*>::iterator iterator = v->begin();
586  while ( iterator != v->end() ) {
587  record->add ( getSlot<Element*>(getString(n++), *iterator) );
588  ++iterator;
589  }
590  }
591  return record;
592 }
593 
594 
595 // -------------------------------------------------------------------
596 // Inspector Support for : "const std::vector<Element>*".
597 
598 
599 template<typename Element>
600 inline std::string getString ( const std::vector<Element>* v )
601 {
602  std::string name = "const std::vector<Element>*:";
603  return name + getString<size_t>(v->size());
604 }
605 
606 
607 template<typename Element>
608 inline Hurricane::Record* getRecord ( const std::vector<Element>* v )
609 {
610  Hurricane::Record* record = NULL;
611  if ( !v->empty() ) {
612  record = new Hurricane::Record ( "const std::vector<Element>*" );
613  unsigned n = 0;
614  typename std::vector<Element>::const_iterator iterator = v->begin();
615  while ( iterator != v->end() ) {
616  record->add ( getSlot<const Element*>(getString(n++), &(*iterator)) );
617  ++iterator;
618  }
619  }
620  return record;
621 }
622 
623 
624 // -------------------------------------------------------------------
625 // Inspector Support for : "const std::vector<Element*>*".
626 
627 
628 template<typename Element>
629 inline std::string getString ( const std::vector<Element*>* v )
630 {
631  std::string name = "const std::vector<Element*>*:";
632  return name + getString<size_t>(v->size());
633 }
634 
635 
636 template<typename Element>
637 inline Hurricane::Record* getRecord ( const std::vector<Element*>* v )
638 {
639  Hurricane::Record* record = NULL;
640  if (not v->empty()) {
641  record = new Hurricane::Record ( "const std::vector<Element*>*" );
642  size_t n = 0;
643  typename std::vector<Element*>::const_iterator iterator = v->begin();
644  while (iterator != v->end()) {
645  record->add ( getSlot<const Element*>(getString(n++), *iterator) );
646  ++iterator;
647  }
648  }
649  return record;
650 }
651 
652 
653 // -------------------------------------------------------------------
654 // Inspector Support for : "const std::list<Element>*".
655 
656 
657 template<typename Element>
658 inline std::string getString ( const std::list<Element>* l )
659 {
660  std::string name = "const std::list<Element>*:";
661  return name + getString<size_t>(l->size());
662 }
663 
664 
665 template<typename Element>
666 inline Hurricane::Record* getRecord ( const std::list<Element>* l )
667 {
668  Hurricane::Record* record = NULL;
669  if ( !l->empty() ) {
670  record = new Hurricane::Record ( "const std::list<Element>" );
671  unsigned n = 1;
672  typename std::list<Element>::const_iterator iterator = l->begin();
673  while ( iterator != l->end() ) {
674  record->add ( getSlot<const Element*>(getString(n++), &(*iterator)) );
675  ++iterator;
676  }
677  }
678  return record;
679 }
680 
681 
682 template<typename Element>
683 inline std::string getString ( std::list<Element>* l )
684 {
685  std::string name = "std::list<Element>*:";
686  return name + getString<size_t>(l->size());
687 }
688 
689 
690 template<typename Element>
691 inline Hurricane::Record* getRecord ( std::list<Element>* l )
692 {
693  Hurricane::Record* record = NULL;
694  if ( !l->empty() ) {
695  record = new Hurricane::Record ( "std::list<Element>" );
696  unsigned n = 1;
697  typename std::list<Element>::iterator iterator = l->begin();
698  while ( iterator != l->end() ) {
699  record->add ( getSlot<const Element*>(getString(n++), &(*iterator)) );
700  ++iterator;
701  }
702  }
703  return record;
704 }
705 
706 
707 // -------------------------------------------------------------------
708 // Inspector Support for : "[const] std::map<Key,Element,Compare>*.
709 
710 
711 template<typename Key, typename Element>
712 inline std::string getString ( std::map<Key,Element>* m )
713 {
714  std::string name = "std::map<Element>:";
715  return name + getString<size_t>(m->size());
716 }
717 
718 
719 template<typename Key, typename Element>
720 inline Hurricane::Record* getRecord ( std::map<Key,Element>* m )
721 {
722  Hurricane::Record* record = NULL;
723  if ( !m->empty() ) {
724  record = new Hurricane::Record ( "std::map<Element>" );
725  typename std::map<Key,Element>::iterator iterator = m->begin();
726  while ( iterator != m->end() ) {
727  record->add ( getSlot<Element>(getString(iterator->first), iterator->second) );
728  ++iterator;
729  }
730  }
731  return record;
732 }
733 
734 
735 template<typename Key, typename Element>
736 inline std::string getString ( const std::map<Key,Element>* m )
737 {
738  std::string name = "const std::map<Element>:";
739  return name + getString<size_t>(m->size());
740 }
741 
742 
743 template<typename Key, typename Element>
744 inline Hurricane::Record* getRecord ( const std::map<Key,Element>* m )
745 {
746  Hurricane::Record* record = NULL;
747  if ( !m->empty() ) {
748  record = new Hurricane::Record ( "const std::map<Element>" );
749  typename std::map<Key,Element>::const_iterator iterator = m->begin();
750  while ( iterator != m->end() ) {
751  record->add ( getSlot<const Element>(getString(iterator->first), iterator->second) );
752  ++iterator;
753  }
754  }
755  return record;
756 }
757 
758 
759 // -------------------------------------------------------------------
760 // Inspector Support for : "[const] std::map<Key,Element,Compare>*.
761 
762 
763 template<typename Key, typename Element, typename Compare>
764 inline std::string getString ( std::map<Key,Element,Compare>* m )
765 {
766  std::string name = "std::map<Element>:";
767  return name + getString<size_t>(m->size());
768 }
769 
770 
771 template<typename Key, typename Element, typename Compare>
772 inline Hurricane::Record* getRecord ( std::map<Key,Element,Compare>* m )
773 {
774  Hurricane::Record* record = NULL;
775  if ( !m->empty() ) {
776  record = new Hurricane::Record ( "std::map<Element>" );
777  typename std::map<Key,Element,Compare>::iterator iterator = m->begin();
778  while ( iterator != m->end() ) {
779  record->add ( getSlot<Element>(getString(iterator->first), iterator->second) );
780  ++iterator;
781  }
782  }
783  return record;
784 }
785 
786 
787 template<typename Key, typename Element, typename Compare>
788 inline std::string getString ( const std::map<Key,Element,Compare>* m )
789 {
790  std::string name = "const std::map<Element>:";
791  return name + getString<size_t>(m->size());
792 }
793 
794 
795 template<typename Key, typename Element, typename Compare>
796 inline Hurricane::Record* getRecord ( const std::map<Key,Element,Compare>* m )
797 {
798  Hurricane::Record* record = NULL;
799  if ( !m->empty() ) {
800  record = new Hurricane::Record ( "const std::map<Element>" );
801  typename std::map<Key,Element,Compare>::const_iterator iterator = m->begin();
802  while ( iterator != m->end() ) {
803  record->add ( getSlot<const Element>(getString(iterator->first), iterator->second) );
804  ++iterator;
805  }
806  }
807  return record;
808 }
809 
810 
811 // -------------------------------------------------------------------
812 // Inspector Support for : "const std::multimap<Key,Element,Compare>*".
813 
814 
815 template<typename Key, typename Element, typename Compare>
816 inline std::string getString ( const std::multimap<Key,Element,Compare>* m )
817 {
818  std::string name = "const std::multimap<Element>:";
819  return name + getString<size_t>(m->size());
820 }
821 
822 
823 template<typename Key, typename Element, typename Compare>
824 inline Hurricane::Record* getRecord ( const std::multimap<Key,Element,Compare>* m )
825 {
826  Hurricane::Record* record = NULL;
827  if ( !m->empty() ) {
828  record = new Hurricane::Record ( "const std::multimap<Element>" );
829  typename std::multimap<Key,Element,Compare>::const_iterator iterator = m->begin();
830  while ( iterator != m->end() ) {
831  record->add ( getSlot<const Element>(getString(iterator->first), iterator->second) );
832  ++iterator;
833  }
834  }
835  return record;
836 }
837 
838 
839 template<typename Key, typename Element, typename Compare>
840 inline std::string getString ( std::multimap<Key,Element,Compare>* m )
841 {
842  std::string name = "std::multimap<Element>:";
843  return name + getString<size_t>(m->size());
844 }
845 
846 
847 template<typename Key, typename Element, typename Compare>
848 inline Hurricane::Record* getRecord ( std::multimap<Key,Element,Compare>* m )
849 {
850  Hurricane::Record* record = NULL;
851  if ( !m->empty() ) {
852  record = new Hurricane::Record ( "std::multimap<Element>" );
853  typename std::multimap<Key,Element,Compare>::iterator iterator = m->begin();
854  while ( iterator != m->end() ) {
855  record->add ( getSlot<Element>(getString(iterator->first), iterator->second) );
856  ++iterator;
857  }
858  }
859  return record;
860 }
861 
862 
863 // -------------------------------------------------------------------
864 // Inspector Support for : "[const] std::set<Element,Compare>*".
865 
866 
867 template<typename Element, typename Compare>
868 inline std::string getString ( const std::set<Element,Compare>* s )
869 {
870  std::string name = "const std::set<Element>:";
871  return name + getString<size_t>(s->size());
872 }
873 
874 
875 template<typename Element, typename Compare>
876 inline Hurricane::Record* getRecord ( const std::set<Element,Compare>* s )
877 {
878  Hurricane::Record* record = NULL;
879  if ( !s->empty() ) {
880  record = new Hurricane::Record ( "const std::set<Element>" );
881  unsigned n = 1;
882  typename std::set<Element,Compare>::const_iterator iterator = s->begin();
883  while ( iterator != s->end() ) {
884  record->add ( getSlot<const Element>(getString(n++), *iterator) );
885  ++iterator;
886  }
887  }
888  return record;
889 }
890 
891 
892 template< typename Element, typename Compare, typename Allocator >
893 inline std::string getString ( std::set<Element,Compare,Allocator>* s )
894 {
895  std::string name = "std::set<Element>:";
896  return name + getString<size_t>(s->size());
897 }
898 
899 
900 template< typename Element, typename Compare, typename Allocator >
901 inline Hurricane::Record* getRecord ( std::set<Element,Compare,Allocator>* s )
902 {
903  Hurricane::Record* record = NULL;
904  if (not s->empty()) {
905  record = new Hurricane::Record ( "std::set<Element>" );
906  unsigned n = 1;
907  typename std::set<Element,Compare,Allocator>::iterator iterator = s->begin();
908  while ( iterator != s->end() ) {
909  record->add( getSlot<Element>(getString(n++), *iterator) );
910  ++iterator;
911  }
912  }
913  return record;
914 }
915 
916 // -------------------------------------------------------------------
917 // Inspector Support for : "[const] std::set<Element,Compare>&".
918 
919 
920 template<typename Element, typename Compare>
921 inline std::string getString ( const std::set<Element,Compare>& s )
922 {
923  std::string name = "const std::set<Element>:";
924  return name + getString<size_t>(s.size());
925 }
926 
927 
928 template<typename Element, typename Compare>
929 inline Hurricane::Record* getRecord ( const std::set<Element,Compare>& s )
930 {
931  Hurricane::Record* record = NULL;
932  if ( !s.empty() ) {
933  record = new Hurricane::Record ( "const std::set<Element>" );
934  unsigned n = 1;
935  typename std::set<Element,Compare>::const_iterator iterator = s.begin();
936  while ( iterator != s.end() ) {
937  record->add ( getSlot<Element>(getString(n++), *iterator) );
938  ++iterator;
939  }
940  }
941  return record;
942 }
943 
944 // -------------------------------------------------------------------
945 // Inspector Support for : "const std::multiset<Element,Compare>*".
946 
947 
948 template<typename Element, typename Compare>
949 inline std::string getString ( const std::multiset<Element,Compare>* s )
950 {
951  std::string name = "std::multiset<Element>:";
952  return name + getString<size_t>(s->size());
953 }
954 
955 
956 template<typename Element, typename Compare>
957 inline Hurricane::Record* getRecord ( const std::multiset<Element,Compare>* s )
958 {
959  Hurricane::Record* record = NULL;
960  if ( !s->empty() ) {
961  record = new Hurricane::Record ( "std::multiset<Element>" );
962  unsigned n = 1;
963  typename std::multiset<Element,Compare>::const_iterator iterator = s->begin();
964  while ( iterator != s->end() ) {
965  record->add ( getSlot<Element>(getString(n++), *iterator) );
966  ++iterator;
967  }
968  }
969  return record;
970 }
971 
972 
973 #include "hurricane/Tabulation.h"
974 
975 
976 // -------------------------------------------------------------------
977 // Class : "::cdebug()".
978 //
979 // Wrapper around the STL ostream which to print debugging messages.
980 
981 class tstream : public std::ostream {
982  public:
983  inline int getMinLevel () const;
984  inline int getMaxLevel () const;
985  inline int setMinLevel ( int );
986  inline int setMaxLevel ( int );
987  inline int getLevel () const;
988  inline int setLevel ( int );
989  inline bool enabled () const;
990  inline bool enabled ( int ) const;
991  inline tstream& log ( int level, int count=0 );
992  inline tstream& tabw ( int level, int count );
993  inline tstream ( std::ostream & );
994  inline tstream& put ( char c );
995  inline tstream& flush ();
996  private:
997  inline tstream& _tab ();
998  inline tstream& _tabw ( int count );
999  public:
1000  // Overload for manipulators.
1001  inline tstream& operator<< ( std::ostream& (*pf)(std::ostream &) );
1002  private:
1003  int _minLevel;
1004  int _maxLevel;
1005  int _level;
1006  Hurricane::Tabulation _tabulation;
1007 };
1008 
1009 
1010 inline tstream::tstream ( std::ostream& s )
1011  : std::ostream(s.rdbuf())
1012  , _minLevel (100000)
1013  , _maxLevel (0)
1014  , _level (0)
1015  , _tabulation(" ")
1016 { }
1017 
1018 inline int tstream::getMinLevel () const { return _minLevel; }
1019 inline int tstream::getMaxLevel () const { return _maxLevel; }
1020 inline int tstream::setMinLevel ( int l ) { int pl=_minLevel; _minLevel=l; return pl; }
1021 inline int tstream::setMaxLevel ( int l ) { int pl=_maxLevel; _maxLevel=l; return pl; }
1022 inline int tstream::getLevel () const { return _level; }
1023 inline int tstream::setLevel ( int l ) { int pl=_level; _level=l; return pl; }
1024 inline bool tstream::enabled () const { return (_level >= _minLevel) and (_level < _maxLevel); }
1025 inline bool tstream::enabled ( int l ) const { return (l >= _minLevel) and (l < _maxLevel); }
1026 inline tstream& tstream::tabw ( int level, int count ) { setLevel(level); return _tabw(count); }
1027 inline tstream& tstream::put ( char c ) { if (enabled()) static_cast<std::ostream*>(this)->put(c); return *this; }
1028 inline tstream& tstream::flush () { if (enabled()) static_cast<std::ostream*>(this)->flush(); return *this; }
1029 inline tstream& tstream::operator<< ( std::ostream& (*pf)(std::ostream&) ) { if (enabled()) (*pf)(*this); return *this; }
1030 inline tstream& operator<< ( tstream& o, const Hurricane::Tabulation& t )
1031 { static_cast<std::ostream&>(o) << t; return o; }
1032 
1033 inline tstream& tstream::_tab () { if (enabled()) (* static_cast<std::ostream*>(this)) << _tabulation; return *this; }
1034 inline tstream& tstream::_tabw ( int count )
1035 {
1036  if (enabled()) {
1037  if (count > 0) while(count--) _tabulation++;
1038  else if (count < 0) while(count++) _tabulation--;
1039  }
1040  return *this;
1041 }
1042 
1043 inline tstream& tstream::log ( int level, int count )
1044 { setLevel(level); _tab(); return _tabw(count); }
1045 
1046 // For STL Types.
1047 inline tstream& operator<< ( tstream& o, const std::string s )
1048 { if (o.enabled()) { static_cast<std::ostream&>(o) << s; } return o; };
1049 
1050 // For POD Types.
1051 // template<typename T>
1052 // inline tstream& operator<< ( tstream& o, T& t )
1053 // { if (o.enabled()) { static_cast<std::ostream&>(o) << getString<T&>(t); } return o; };
1054 
1055 template<typename T>
1056 inline tstream& operator<< ( tstream& o, T* t )
1057 { if (o.enabled()) { static_cast<std::ostream&>(o) << getString<T*>(t); } return o; };
1058 
1059 // template<typename T>
1060 // inline tstream& operator<< ( tstream& o, const T& t )
1061 // { if (o.enabled()) { static_cast<std::ostream&>(o) << getString<const T&>(t); } return o; };
1062 
1063 template<typename T>
1064 inline tstream& operator<< ( tstream& o, T t )
1065 { if (o.enabled()) { static_cast<std::ostream&>(o) << getString<T>(t); } return o; };
1066 
1067 template<typename T>
1068 inline tstream& operator<< ( tstream& o, const T* t )
1069 { if (o.enabled()) { static_cast<std::ostream&>(o) << getString<const T*>(t); } return o; };
1070 
1071 template<>
1072 inline tstream& operator<< ( tstream& o, std::ios_base& (*pf)(std::ios_base&) )
1073 { if (o.enabled()) { static_cast<std::ostream&>(o) << pf; } return o; };
1074 
1075 struct _Tsetw { int n_; };
1076 inline _Tsetw tsetw ( int n ) { return { n }; }
1077 
1078 struct _Tsetf { int n_; };
1079 inline _Tsetf tsetf ( int n ) { return { n }; }
1080 
1081 template<>
1082 inline tstream& operator<< ( tstream& o, _Tsetw manip )
1083 { if (o.enabled()) { static_cast<std::ostream&>(o) << std::setw(manip.n_); } return o; }
1084 
1085 extern tstream cdebug;
1086 
1087 
1088 #define cdebug_log(level,indent) if (cdebug.enabled(level)) cdebug.log(level,indent)
1089 #define cdebug_tabw(level,indent) cdebug.tabw(level,indent)
1090 
1091 
1092 # define GETSTRING_POINTER_SUPPORT(Data) \
1093  template<> inline std::string getString<Data*>( Data* data ) \
1094  { \
1095  if (!data) return "NULL [" #Data "]"; \
1096  return data->_getString(); \
1097  } \
1098  \
1099  template<> inline std::string getString<const Data*>( const Data* data ) \
1100  { if (!data) return "NULL [const " #Data "]"; return data->_getString(); }
1101 
1102 
1103 # define IOSTREAM_POINTER_SUPPORT(Data) \
1104  inline std::ostream& operator<< ( std::ostream& o, Data* d ) \
1105  { \
1106  if (!d) return o << "NULL [" #Data "]"; \
1107  return o << "&" << getString<const Data*>(d); \
1108  } \
1109  inline std::ostream& operator<< ( std::ostream& o, const Data* d ) \
1110  { \
1111  if (!d) return o << "NULL [const " #Data "]"; \
1112  return o << "&" << getString<const Data*>(d); \
1113  } \
1114 
1115 
1116 # define TSTREAM_POINTER_SUPPORT(Data) \
1117  inline tstream& operator<< ( tstream& o, Data* d ) \
1118  { return o << "&" << getString<const Data*>(d); } \
1119  inline tstream& operator<< ( tstream& o, const Data* d ) \
1120  { return o << "&" << getString<const Data*>(d); }
1121 
1122 
1123 # define GETRECORD_POINTER_SUPPORT(Data) \
1124  template<> inline Hurricane::Record* getRecord<Data*>( Data* data ) \
1125  { if (!data) return NULL; return data->_getRecord(); } \
1126  \
1127  template<> inline Hurricane::Record* getRecord<const Data*>( const Data* data ) \
1128  { if (!data) return NULL; return data->_getRecord(); }
1129 
1130 
1131 # define GETSTRING_REFERENCE_SUPPORT(Data) \
1132  template<> inline std::string getString<Data&>( Data& data ) \
1133  { return data._getString(); } \
1134  \
1135  template<> inline std::string getString<const Data&>( const Data& data ) \
1136  { return data._getString(); }
1137 
1138 
1139 # define IOSTREAM_REFERENCE_SUPPORT(Data) \
1140  inline std::ostream& operator<< ( std::ostream& o, Data& d ) \
1141  { return o << getString<Data&>(d); } \
1142  \
1143  inline std::ostream& operator<< ( std::ostream& o, const Data& d ) \
1144  { return o << getString<const Data&>(d); } \
1145  \
1146 
1147 # define GETRECORD_REFERENCE_SUPPORT(Data) \
1148  template<> inline Hurricane::Record* getRecord<Data&>( Data& data ) \
1149  { return data._getRecord(); } \
1150  \
1151  template<> inline Hurricane::Record* getRecord<const Data&>( const Data& data ) \
1152  { return data._getRecord(); }
1153 
1154 
1155 # define GETSTRING_VALUE_SUPPORT(Data) \
1156  template<> inline std::string getString<Data>( Data data ) \
1157  { return data._getString(); }
1158 
1159 
1160 # define IOSTREAM_VALUE_SUPPORT(Data) \
1161  inline std::ostream& operator<< ( std::ostream& o, Data d ) \
1162  { return o << getString<Data>(d); }
1163 
1164 
1165 # define GETRECORD_VALUE_SUPPORT(Data) \
1166  template<> inline Hurricane::Record* getRecord<Data>( Data data ) \
1167  { return data._getRecord(); }
1168 
1169 
1170 # define INSPECTOR_P_SUPPORT(Data) \
1171  GETRECORD_POINTER_SUPPORT(Data) \
1172  GETSTRING_POINTER_SUPPORT(Data) \
1173  IOSTREAM_POINTER_SUPPORT(Data) \
1174  TSTREAM_POINTER_SUPPORT(Data)
1175 
1176 
1177 # define INSPECTOR_R_SUPPORT(Data) \
1178  GETRECORD_REFERENCE_SUPPORT(Data) \
1179  GETSTRING_REFERENCE_SUPPORT(Data) \
1180  IOSTREAM_REFERENCE_SUPPORT(Data)
1181 
1182 
1183 # define INSPECTOR_PR_SUPPORT(Data) \
1184  GETSTRING_POINTER_SUPPORT(Data) \
1185  GETSTRING_REFERENCE_SUPPORT(Data) \
1186  GETSTRING_VALUE_SUPPORT(Data) \
1187  IOSTREAM_POINTER_SUPPORT(Data) \
1188  IOSTREAM_REFERENCE_SUPPORT(Data) \
1189  GETRECORD_POINTER_SUPPORT(Data) \
1190  GETRECORD_REFERENCE_SUPPORT(Data)
1191 
1192 
1193 # define INSPECTOR_PV_SUPPORT(Data) \
1194  GETSTRING_POINTER_SUPPORT(Data) \
1195  GETSTRING_VALUE_SUPPORT(Data) \
1196  IOSTREAM_POINTER_SUPPORT(Data) \
1197  IOSTREAM_VALUE_SUPPORT(Data) \
1198  GETRECORD_POINTER_SUPPORT(Data) \
1199  GETRECORD_VALUE_SUPPORT(Data)
1200 
1201 
1202 // x-----------------------------------------------------------------x
1203 // | Classes Neededs in All Hurricane Modules |
1204 // x-----------------------------------------------------------------x
1205 
1206 #include "hurricane/Slot.h"
1207 #include "hurricane/Initializer.h"
1208 #include "hurricane/JsonWriter.h"
1209 #include "hurricane/JsonObject.h"
1210 
1211 
1212 namespace Hurricane {
1213 
1214 // -------------------------------------------------------------------
1215 // Class : FastRTTI
1216 //
1217 // Reference:
1218 // https://tinodidriksen.com/2010/04/cpp-dynamic-cast-performance/
1219 
1220 
1221  class FastRTTI {
1222  private:
1223  static uint64_t _classBit;
1224  static std::map<uint64_t,std::string> _classNames;
1225  std::string _className;
1226  const FastRTTI* _baseRTTI;
1227  uint64_t _baseIds;
1228  uint64_t _classId;
1229  public:
1230  inline FastRTTI ( std::string className, const FastRTTI* baseRTTI );
1231  public:
1232  inline uint64_t classId () const;
1233  inline bool hasBaseClass ( uint64_t ) const;
1234  inline void initialize ();
1235  inline std::string _getString () const;
1236  inline Record* _getRecord () const;
1237  };
1238 
1239 
1240  FastRTTI::FastRTTI ( std::string className, const FastRTTI* baseRTTI )
1241  : _className("<"+className+">")
1242  , _baseRTTI(baseRTTI)
1243  , _baseIds(0)
1244  {
1245  if (baseRTTI) const_cast<FastRTTI*>( baseRTTI )->initialize();
1246  initialize();
1247  }
1248 
1249  inline uint64_t FastRTTI::classId () const { return _classId; }
1250 
1251  inline bool FastRTTI::hasBaseClass ( uint64_t baseClassId ) const
1252  { return (_baseIds bitand baseClassId); }
1253 
1254  inline void FastRTTI::initialize ()
1255  {
1256  if (classId()) return;
1257  _classId = (1 << _classBit++);
1258 
1259  _classNames[ _classId ] = _className;
1260  if (_baseRTTI) _baseIds = _baseRTTI->_baseIds;
1261  _baseIds |= _classId;
1262  }
1263 
1264 
1265  template<typename Derived>
1266  class derived_cast {
1267  private:
1268  Derived* _derivedPointer;
1269  public:
1270  template<typename Base>
1271  inline derived_cast ( Base* basePointer )
1272  : _derivedPointer(nullptr)
1273  {
1274  if (not basePointer) return;
1275  if (basePointer->vfastRTTI().hasBaseClass( Derived::fastRTTI().classId() ))
1276  _derivedPointer = static_cast<Derived*>( basePointer );
1277  }
1278  inline operator Derived* () const;
1279  };
1280 
1281  template<typename Derived>
1282  inline derived_cast<Derived>::operator Derived* () const { return _derivedPointer; }
1283 
1284 
1285 } // Hurricane namespace.
1286 
1287 
1288 INSPECTOR_P_SUPPORT(Hurricane::FastRTTI);
1289 
1290 
1291 namespace Hurricane {
1292 
1293 
1294  inline std::string FastRTTI::_getString () const { return _className + ":" + getString(_baseIds); }
1295 
1296  inline Record* FastRTTI::_getRecord () const
1297  {
1298  Record* record = new Record ( getString(this) );
1299  for ( size_t bit=0 ; bit<64 ; ++bit ) {
1300  uint64_t classMask = (((uint64_t)1) << bit);
1301  if (_baseIds & classMask) {
1302  std::string baseName = getString( classMask ) + " -> " + _classNames[ classMask ];
1303  record->add( getSlot( getString(bit), baseName ));
1304  }
1305  }
1306  return record;
1307  }
1308 
1309 
1310 } // Hurricane namespace.
Tabulation description (API)
Definition: Tabulation.h:33
Trace & indentation enabled stream.
Definition: Commons.h:981
bool enabled() const
Definition: Commons.h:1024
int getLevel() const
Definition: Commons.h:1022
int setMinLevel(int)
Definition: Commons.h:1020
tstream & log(int level, int count=0)
Definition: Commons.h:1043
int setLevel(int)
Definition: Commons.h:1023
tstream & tabw(int level, int count)
Definition: Commons.h:1026
int setMaxLevel(int)
Definition: Commons.h:1021
int getMinLevel() const
Definition: Commons.h:1018
int getMaxLevel() const
Definition: Commons.h:1019
Contains Almost Everything.
Definition: BasicLayer.h:39
string demangle(const char *symbol)


Generated by doxygen 1.9.1 on Sun Dec 14 2025 Return to top of page
Hurricane VLSI Database Copyright © 2000-2020 Bull S.A. All rights reserved