-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_write.rb
More file actions
276 lines (234 loc) · 6.38 KB
/
cpp_write.rb
File metadata and controls
276 lines (234 loc) · 6.38 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
require 'set'
# Fixes up single values and arrays of values to give them indexes
# Changes hash to sorted array of [key,value] tuples
# returns the sorted array of [[enum value, name],..]
def convert_values(values)
values_hash = {}
if(values.class != Hash)
values = [*values]
0.upto(values.length-1) { |index|
values_hash[values[index]] = index
}
else
values_hash = values
end
values_hash.sort_by {|k,v| v}
end
def validate_values(values)
if(!values)
raise "No values given."
end
return if values.class != Hash
value_types = Set.new
values.map { |k,v|
value_types.add(v.class)
if(v.class == String)
raise "Must use a single character for enum values" if v.length != 1
end
}
if(value_types.length != 1)
raise "Only one type of value allowed in enum, found: #{value_types.to_a.join(", ")}"
end
end
class Cpp_Writer
def initialize(enum)
@enum = enum
validate_values(@enum.values)
@values = convert_values(@enum.values)
end
def extra_interface_includes
[*@enum.interface_includes].map { |val| "#include #{val}" }.join("\n")
end
def extra_implementation_includes
[*@enum.implementation_includes].map { |val| "#include #{val}" }.join("\n")
end
def open_namespace
namespace_open = @enum.namespace.split("::").map { |name|
"namespace #{name} {"
}.join("\n")
end
def close_namespace
namespace_close = @enum.namespace.split("::").map { "}" }.join(" ")
end
def header_guard_begin
header_guard = @enum.namespace.split("::").map { |name|
"#{name}"
}.join("__") + "__" + @enum.name + "__hpp"
"#ifndef #{header_guard}\n#define #{header_guard}"
end
def header_guard_end
"#endif"
end
def interface_includes
["cstdint", "stdexcept", "map", "algorithm", "iosfwd", "string"].map { |file|
"#include <#{file}>"
}.join("\n")
end
def enum_class
%Q{enum class #{@enum.name} #{@enum.storage_type ? ": #{@enum.storage_type}" : ""}
{
#{@values.map { |k,v|
v.class == String ? "#{k}='#{v}'" : "#{k}=#{v}"
}.join(",\n ")
}
};}
end
def stream_interfaces
"std::ostream & operator<<(std::ostream &, #{@enum.name});\n" +
"std::istream & operator>>(std::istream &, #{@enum.name} &);"
end
def string_conversion_interfaces
"template <class Enum> Enum name_to_value(std::string const &);\n" +
"std::string const & value_to_name(#{@enum.name});"
end
def fully_qualified_name
fully_qualified_name =
(@enum.namespace ? @enum.namespace + "::" : '') + @enum.name
end
def fq_name_token
fq_name_token = fully_qualified_name.tr(":","_");
end
def name_to_value_exception
%Q{throw std::runtime_error("'" + name + "' is not a valid value.");}
end
def name_to_value_impl
%Q{
template <>
inline
#{@enum.name}
name_to_value(std::string const & name)
{
using namespace detail::#{@enum.name}_enum;
auto i = std::lower_bound(
types_map,
types_map_end,
name,
[] (Type_Map const & m, std::string const & n) { return m.first < n; });
if(i != types_map_end && i->first == name)
{
return i->second;
}
else
{
#{name_to_value_exception}
}
}}
end
def auto_gen_warning
"/**
* WARNING WARNING WARNING WARNING WARNING WARNING WARNING
* THIS FILE WAS AUTOMATICALLY GENERATED BY ENUMGEN
* DO NOT MODIFY DIRECTLY
* WARNING WARNING WARNING WARNING WARNING WARNING WARNING
*/"
end
def section_spacing
"\n\n"
end
def interface_code
@enum.interface_code
end
def header_file
hpp = ""
hpp += header_guard_begin + section_spacing
hpp += auto_gen_warning + section_spacing
hpp += interface_includes + section_spacing
hpp += extra_interface_includes + section_spacing
hpp += open_namespace + section_spacing
hpp += enum_class + section_spacing
hpp += string_and_value_holders + section_spacing
hpp += string_conversion_interfaces + section_spacing
hpp += stream_interfaces + section_spacing
hpp += interface_code + section_spacing
hpp += name_to_value_impl + section_spacing
hpp += value_to_name_impl + section_spacing
hpp += close_namespace + section_spacing
hpp += header_guard_end
end
def implementation_includes
%Q{#include "#{@enum.filename}.hpp"\n} +
["iostream", "map", "stdexcept"].map { |file|
"#include <#{file}>"
}.join("\n")
end
def type_map
sorted_values = @values.sort { |a,b| a[0] <=> b[0] }
%Q{namespace detail {
namespace #{@enum.name}_enum {
std::string const names[] =
{
#{@values.map { |k,v|
"\"#{k}\""
}.join(",\n ")}
};
Type_Map const types_map[] =
{
#{sorted_values.map { |k,v|
"{\"#{k}\" , #{fully_qualified_name}::#{k} }"
}.join(",\n ")
}
};
Type_Map const * types_map_end = types_map + (sizeof(types_map) / sizeof(types_map[0]));
}}}
end
def string_and_value_holders
%Q{namespace detail { namespace #{@enum.name}_enum {
extern std::string const names[];
typedef std::pair< std::string, #{fully_qualified_name} > Type_Map;
extern Type_Map const types_map[];
extern Type_Map const * types_map_end;
}}}
end
def value_to_name_exception
%Q{throw std::runtime_error("Invalid value given for #{@enum.name}");}
end
def value_to_name_impl
%Q{
inline
std::string const &
value_to_name(#{@enum.name} v)
{
switch (v)
{
#{i=-1; @values.map { |k,v|
" case #{@enum.name}::#{k}: return detail::#{@enum.name}_enum::names[#{i+=1}];"
}.join("\n ")}
}
#{value_to_name_exception}
}}
end
def stream_implementations
%Q{
std::ostream &
operator<<(std::ostream & os, #{@enum.name} v)
{
return os << value_to_name(v);
}
std::istream &
operator>>(std::istream & is, #{@enum.name} & v)
{
std::string tmp;
is >> tmp;
v = name_to_value<#{@enum.name}>(tmp);
return is;
}}
end
def implementation_code
@enum.implementation_code
end
def cpp_file
cpp = ""
cpp += implementation_includes + section_spacing
cpp += auto_gen_warning + section_spacing
cpp += extra_implementation_includes + section_spacing
cpp += open_namespace + section_spacing
cpp += type_map + section_spacing
cpp += stream_implementations + section_spacing
cpp += implementation_code + section_spacing
cpp += close_namespace
end
def write
File.open(@enum.filename + ".hpp", "w") {|f| f.write(header_file)}
File.open(@enum.filename + ".cpp", "w") {|f| f.write(cpp_file)}
end
end