-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdescriptor.cpp
More file actions
279 lines (216 loc) · 8.62 KB
/
descriptor.cpp
File metadata and controls
279 lines (216 loc) · 8.62 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//
// Mach7: Pattern Matching Library for C++
//
// Copyright 2011-2013, Texas A&M University.
// Copyright 2014 Yuriy Solodkyy.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the names of Mach7 project nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
///
/// \file
///
/// This file is a part of Mach7 library test suite.
///
/// \author Yuriy Solodkyy <yuriy.solodkyy@gmail.com>
///
/// \see https://bit.ly/Mach7
/// \see https://github.com/solodon4/Mach7
/// \see https://github.com/solodon4/SELL
/// \see https://parasol.tamu.edu/mach7/
///
#include "descriptor.hpp"
#include <algorithm>
#include <cassert>
#include <iostream>
//------------------------------------------------------------------------------
ClassDescriptor* get_class(const std::string& name)
{
std::string n = fix_name(name);
ClassDescriptor*& result = all_classes[n];
if (!result)
result = new ClassDescriptor(n);
return result;
}
//------------------------------------------------------------------------------
std::string fix_name(std::string s)
{
for (std::string::iterator p = s.begin(); p != s.end(); ++p)
{
if (!(isalpha(*p) || isdigit(*p) || *p == '_'))
*p = '_';
}
return s;
}
//------------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const ClassDescriptor& cd)
{
os << "// Class c_" << cd.name
<< " is at depth " << cd.depth()
<< " and has " << cd.derived.size() << " children"
<< std::endl;
if (!cd.derived.empty())
{
os << "// Children: ";
for (class_set::const_iterator p = cd.derived.begin(); p != cd.derived.end(); ++p)
os << (p == cd.derived.begin() ? "c_" : ", c_") << (*p)->name;
os << std::endl;
}
os << "struct c_" << cd.name;
if (!cd.bases.empty())
for (ClassDescriptor::bases_type::const_iterator p = cd.bases.begin(); p != cd.bases.end(); ++p)
os << (p == cd.bases.begin() ? " : XTL_VIRTUAL c_" : ", XTL_VIRTUAL c_") << (*p)->name;
os << "\n{\n";
if (cd.is_root())
os << " virtual ~c_" << cd.name << "() {};\n";
for (ClassDescriptor::attributes_type::const_iterator p = cd.attributes.begin(); p != cd.attributes.end(); ++p)
os << " int m_" << *p << ";\n";
os << std::endl;
for (ClassDescriptor::methods_type::const_iterator p = cd.methods.begin(); p != cd.methods.end(); ++p)
os << " virtual int f_" << *p << "() \t{ return " << rand() % 100 << "; }\n";
return os << "};\n";
}
//------------------------------------------------------------------------------
size_t ClassDescriptor::depth() const
{
if (is_root()) return 1;
size_t result = 0;
for (bases_type::const_iterator p = bases.begin(); p != bases.end(); ++p)
{
size_t base_depth = (*p)->depth();
if (base_depth > result)
result = base_depth;
}
return result+1;
}
//------------------------------------------------------------------------------
bool ClassDescriptor::is_derived_from(const ClassDescriptor* c) const
{
if (c == this || std::find(bases.begin(), bases.end(), c) != bases.end())
return true;
for (bases_type::const_iterator p = bases.begin(); p != bases.end(); ++p)
{
if ((*p)->is_derived_from(c))
return true;
}
return false;
}
//------------------------------------------------------------------------------
path_set ClassDescriptor::derivation_paths_to(const ClassDescriptor* c) const
{
class_seq path;
path_set paths;
derivation_paths_to_recursive(c, path, paths);
return paths;
}
//------------------------------------------------------------------------------
void ClassDescriptor::derivation_paths_to_recursive(const ClassDescriptor* c, class_seq& path, path_set& paths) const
{
path.push_back(this);
if (c == this)
paths.insert(path);
for (bases_type::const_iterator p = bases.begin(); p != bases.end(); ++p)
(*p)->derivation_paths_to_recursive(c, path, paths);
path.pop_back();
}
//------------------------------------------------------------------------------
void ClassDescriptor::add_base(ClassDescriptor* b)
{
if (is_defined)
{
std::cerr << "Warning: Class `c_" << name << "' has already been defined. Ignoring addition of base class " << b->name << std::endl;
return;
}
// b->add_derived();
b->add_derived(this);
bases.push_back(b);
}
//------------------------------------------------------------------------------
void ClassDescriptor::add_attr(const std::string& nm)
{
std::string n = fix_name(nm);
if (is_defined)
{
std::cerr << "Warning: Class `c_" << name << "' has already been defined. Ignoring addition of attribute " << n << std::endl;
return;
}
if (std::find(attributes.begin(), attributes.end(), n) == attributes.end())
attributes.push_back(n);
//else
// std::cerr << "Warning: Class `c_" << name << "' already has attribute " << n << std::endl;
}
//------------------------------------------------------------------------------
void ClassDescriptor::add_mthd(const std::string& nm)
{
std::string n = fix_name(nm);
if (is_defined)
{
std::cerr << "Warning: Class `c_" << name << "' has already been defined. Ignoring addition of method " << n << std::endl;
return;
}
if (std::find(methods.begin(), methods.end(), n) == methods.end())
methods.push_back(n);
//else
// std::cerr << "Warning: Class `c_" << name << "' already has method " << n << std::endl;
}
//------------------------------------------------------------------------------
//void ClassDescriptor::add_derived() const
//{
// ++derived_count;
//
// for (bases_type::const_iterator p = bases.begin(); p != bases.end(); ++p)
// (*p)->add_derived();
//}
//------------------------------------------------------------------------------
void ClassDescriptor::add_derived(const ClassDescriptor* c)
{
derived.insert(c);
}
//------------------------------------------------------------------------------
void ClassDescriptor::remove_inaccessible_bases()
{
assert(is_defined); // This can only be called once the class and all its bases are finalized
for (bases_type::iterator p = bases.begin(); p != bases.end();)
{
assert((*p)->is_defined); // This can only be called once the class and all its bases are finalized
bool is_ambiguous_base = false;
for (bases_type::const_iterator q = bases.begin(); q != bases.end(); ++q)
if (*p != *q && (*q)->is_derived_from(*p))
{
is_ambiguous_base = true;
std::cerr << "Warning: base class `c_" << (*p)->name
<< "' is an ambiguous (inaccessible) base class of `c_" << name
<< "' because `c_" << (*q)->name
<< "' already derives from it. Removed from base classes."
<< std::endl;
break;
}
if (is_ambiguous_base)
p = bases.erase(p);
else
++p;
}
}
//------------------------------------------------------------------------------