-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathenumTypeDescriptor.cc
More file actions
197 lines (174 loc) · 5.58 KB
/
Copy pathenumTypeDescriptor.cc
File metadata and controls
197 lines (174 loc) · 5.58 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
#include "clstepcore/enumTypeDescriptor.h"
#include <cctype>
#include <cstring>
#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>
namespace {
typedef std::vector<std::string> EnumElements;
typedef std::unordered_map<const EnumTypeDescriptor *, EnumElements>
EnumElementMap;
EnumElementMap & enumElementMap() {
static EnumElementMap elements;
return elements;
}
std::mutex & enumElementMutex() {
static std::mutex mutex;
return mutex;
}
EnumElements parseEnumDescription( const char * description ) {
EnumElements result;
if( !description ) {
return result;
}
const char * begin = strchr( description, '(' );
const char * end = strrchr( description, ')' );
if( !begin || !end || begin >= end ) {
return result;
}
++begin;
while( begin < end ) {
while( begin < end &&
( std::isspace( static_cast<unsigned char>( *begin ) ) ||
*begin == ',' ) ) {
++begin;
}
const char * itemEnd = begin;
while( itemEnd < end && *itemEnd != ',' ) {
++itemEnd;
}
const char * trimmed = itemEnd;
while( trimmed > begin &&
std::isspace( static_cast<unsigned char>( trimmed[-1] ) ) ) {
--trimmed;
}
if( trimmed > begin ) {
std::string item( begin, trimmed );
for( size_t i = 0; i < item.size(); ++i ) {
item[i] = static_cast<char>( std::toupper(
static_cast<unsigned char>( item[i] ) ) );
}
result.push_back( item );
}
begin = itemEnd + ( itemEnd < end ? 1 : 0 );
}
return result;
}
EnumElements descriptorElements( const EnumTypeDescriptor & descriptor ) {
std::lock_guard<std::mutex> lock( enumElementMutex() );
EnumElementMap::const_iterator i = enumElementMap().find( &descriptor );
if( i != enumElementMap().end() ) {
return i->second;
}
return parseEnumDescription( descriptor.Description() );
}
class DescriptorEnum : public SDAI_Enum {
std::string _name;
EnumElements _elements;
public:
explicit DescriptorEnum( const EnumTypeDescriptor & descriptor )
: _name( descriptor.Name() ),
_elements( descriptorElements( descriptor ) ) {
nullify();
}
virtual int no_elements() const {
return static_cast<int>( _elements.size() );
}
virtual const char * Name() const {
return _name.c_str();
}
virtual const char * element_at( int index ) const {
return index >= 0 && static_cast<size_t>( index ) < _elements.size() ?
_elements[index].c_str() : "UNSET";
}
};
}
/*
* why have EnumTypeDescriptor + EnumerationTypeDescriptor ???
* this was in ExpDict.cc before splitting it up
*/
#ifdef NOT_YET
EnumerationTypeDescriptor::EnumerationTypeDescriptor( ) {
_elements = new StringAggregate;
}
#endif
EnumTypeDescriptor::EnumTypeDescriptor( const char * nm, PrimitiveType ft,
Schema * origSchema,
const char * d, EnumCreator f )
: TypeDescriptor( nm, ft, origSchema, d ), CreateNewEnum( f ) {
}
SDAI_Enum * EnumTypeDescriptor::CreateEnum() const {
if( CreateNewEnum ) {
return CreateNewEnum();
}
if( NonRefType() == sdaiBOOLEAN ) {
return new SDAI_BOOLEAN;
}
if( NonRefType() == sdaiLOGICAL ) {
return new SDAI_LOGICAL;
}
return new DescriptorEnum( *this );
}
EnumTypeDescriptor::~EnumTypeDescriptor() {
std::lock_guard<std::mutex> lock( enumElementMutex() );
enumElementMap().erase( this );
}
void RegisterEnumDescriptorElements(
const EnumTypeDescriptor & descriptor,
const char * const * elements, size_t count ) {
EnumElements values;
values.reserve( count );
for( size_t i = 0; i < count; ++i ) {
values.push_back( elements[i] ? elements[i] : "" );
}
std::lock_guard<std::mutex> lock( enumElementMutex() );
enumElementMap()[&descriptor].swap( values );
}
const char * EnumTypeDescriptor::GenerateExpress( std::string & buf ) const {
char tmp[BUFSIZ+1];
buf = "TYPE ";
buf.append( StrToLower( Name(), tmp ) );
buf.append( " = ENUMERATION OF \n (" );
const char * desc = Description();
const char * ptr = &( desc[16] );
while( *ptr != '\0' ) {
if( *ptr == ',' ) {
buf.append( ",\n " );
} else if( isupper( *ptr ) ) {
buf += ( char )tolower( *ptr );
} else {
buf += *ptr;
}
ptr++;
}
buf.append( ";\n" );
///////////////
// count is # of WHERE rules
if( _where_rules != 0 ) {
int all_comments = 1;
int count = _where_rules->Count();
for( int i = 0; i < count; i++ ) { // print out each UNIQUE rule
if( !( *( _where_rules ) )[i]->_label.size() ) {
all_comments = 0;
}
}
if( all_comments ) {
buf.append( " (* WHERE *)\n" );
} else {
buf.append( " WHERE\n" );
}
for( int i = 0; i < count; i++ ) { // print out each WHERE rule
if( !( *( _where_rules ) )[i]->_comment.empty() ) {
buf.append( " " );
buf.append( ( *( _where_rules ) )[i]->comment_() );
}
if( ( *( _where_rules ) )[i]->_label.size() ) {
buf.append( " " );
buf.append( ( *( _where_rules ) )[i]->label_() );
}
}
}
buf.append( "END_TYPE;\n" );
return const_cast<char *>( buf.c_str() );
}