-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameBase.cpp
More file actions
165 lines (131 loc) · 4.97 KB
/
NameBase.cpp
File metadata and controls
165 lines (131 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//------------------------------------------------------------------------------
#include "NameBase.h"
#include "macros.h"
#include "OSFixes.hpp"
unsigned NameBase::uid = 0;
map<unsigned,NameBase *> NameBase::NBmap;
//==============================================================================
NameBase::NameBase()
{
npar = 0; // No name parent
name = string("**undefined**");
id = Uid(); // Unique numeric identifier
rtrap = false; // Recursion trap unset
}
//------------------------------------------------------------------------------
NameBase::~NameBase()
{
//printf("********* NameBase %s (%x) destructor\n",name.c_str,id); fflush(stdout);
}
//------------------------------------------------------------------------------
string NameBase::AutoName(string s)
// Generate the name automatically by appending the UID to the string s
{
char buf[32];
sprintf(buf,"%04u",Id());
string n = s+buf;
Name(n);
return n;
}
//------------------------------------------------------------------------------
void NameBase::Dump(FILE * fp)
{
fprintf(fp,"NameBase dump+++++++++++++++++++++++++++++++\n");
fprintf(fp,"this %" PTR_FMT "\n",reinterpret_cast<uint64_t>(this));
fprintf(fp,"Name %s\n",name.c_str());
fprintf(fp,"Id %10u(%#010x)\n",id,id);
fprintf(fp,"Parent %" PTR_FMT "\n",reinterpret_cast<uint64_t>(npar));
fprintf(fp,"Recursion trap %s\n",rtrap ? "Set" : "Unset");
fprintf(fp,"Unique id %u\n",uid);
fprintf(fp,"NameBase id Name\n");
if (NBmap.empty()) fprintf(fp," ** No map entries ** \n");
WALKMAP(unsigned,NameBase *,NBmap,i)
fprintf(fp,"%6u : %s\n",(*i).first,(*i).second->FullName(7).c_str());
fprintf(fp,"NameBase dump-------------------------------\n\n");
fflush(fp);
}
//------------------------------------------------------------------------------
NameBase * NameBase::Find(unsigned id)
// Locate the NameBase object with the provided id
{
if (NameBase::NBmap.find(id)==NameBase::NBmap.end()) return 0; // Not there?
return NameBase::NBmap[id];
}
//------------------------------------------------------------------------------
string NameBase::FullName(unsigned d)
// Return component full hierarchical name.
// Argument indicates amount of decoration:
// 0 - none - bit pointless asking, really
// 1 - string
// 2 - hex id
// 3 - string + hex id
// 4 - dec id
// 5 - string + dec id
// 6 - hex id + dec id
// 7 - string + hex id + dec id
{
// Been here before?
if (rtrap) return "Recursive name " + name + " trapped";
rtrap = true; // Set "I've been here" flag
char buff[256]; // Yes, I know. Get over it.
switch (d) { // OK, I got carried away
case 0 : buff[0] = '\0'; break;
case 1 : sprintf(buff,"%s",name.c_str()); break;
case 2 : sprintf(buff,"(%#010x)",id); break;
case 3 : sprintf(buff,"%s(%#010x)",name.c_str(),id); break;
case 4 : sprintf(buff,"(%010u)",id); break;
case 5 : sprintf(buff,"%s(%010u)",name.c_str(),id); break;
case 6 : sprintf(buff,"(%#010x=%010u)",id,id); break;
case 7 : sprintf(buff,"%s(%#010x=%010u)",name.c_str(),id,id); break;
default : sprintf(buff,"????"); break;
}
string s = buff; // Initialise build of full name
if (npar!=0) s = npar->FullName(d) + "." + s; // Add on the next bit
rtrap = false; // Unwind recursion trap for next time
return s;
}
//------------------------------------------------------------------------------
unsigned NameBase::Id()
// Return component numeric label
{
return id;
}
//------------------------------------------------------------------------------
void NameBase::Id(unsigned u)
// Set component numeric label and load search map
{
id = u;
NBmap[id] = this;
}
//------------------------------------------------------------------------------
string NameBase::Name()
// Return component short name
{
return name;
}
//------------------------------------------------------------------------------
void NameBase::Name(string s)
// Set component short name
{
name = s;
}
//------------------------------------------------------------------------------
NameBase * NameBase::Npar()
// Return component parent
{
return npar;
}
//------------------------------------------------------------------------------
void NameBase::Npar(NameBase * p)
// Set component parent
{
if (p==this) return; // Pre-emptively save us from ourselves
npar = p;
}
//------------------------------------------------------------------------------
unsigned NameBase::Uid()
// Return a NameBase-wide unique unsigned integer
{
return ++uid;
}
//==============================================================================