-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.erb
More file actions
131 lines (113 loc) · 4.02 KB
/
classes.erb
File metadata and controls
131 lines (113 loc) · 4.02 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
namespace <%= namespace[:name] %>;
class ISerializable
{
public:
virtual bool hasChanged() = 0;
virtual bool hasChanged(const std::string& field) = 0;
virtual boost::any getField(const std::string& field) = 0;
virtual void setField(const std::string& field, boost::any value) = 0;
virtual void serialize(flatbuffers::FlatbufferBuilder& fbb) = 0;
};
<% tables.each do |clazz| %>
class <%= clazz[:fullname].gsub(".","::") %> {
public:
enum class Fields {
<%= clazz[:fields].collect{|i| i[:name].to_s.upcase}.join(",") %>
};
static const std::vector<std::string> FieldNames{<%= clazz[:fields].collect{|i| "\"#{i[:name].to_s}\""}.join(",") %>};
void serialize(flatbuffers::FlatbufferBuilder& fbb)
{
<%= clazz[:name] %>Builder builder;
<% clazz[:fields].each_with_index do |field,i| %>
if(hasChanged(<%= i %>))
{
builder.add_<%= field[:name] %>(<%= field[:name] %>());
}
<% end %>
fbb.Finish(builder.Finish());
}
<% clazz[:fields].each_with_index do |field,i| %>
<% typeName = (field[:dtype] == :array) ? "std::vector<#{field[:type][:array]}>" : field[:type] %>
<%= typeName %> <%= field[:name] %>()
{
return <%= field[:name] %>_;
}
void <%= field[:name] %>(const <%= typeName %>& value)
{
<%= field[:name] %>_ = value;
changed_[<%=i%>] = true;
}
<% if field[:dtype] == :array %>
<%= field[:type][:array] %> <%= field[:name] %>(size_t index)
{
return <%= field[:name] %>_[index];
}
void <%= field[:name] %>(size_t index, const <%= field[:type][:array] %>& value)
{
<%= field[:name] %>_[index] = value;
changed_[<%=i%>] = true;
}
<% end %>
<% end %>
void hasChanged(unsigned int field)
{
return changed_[field];
}
void hasChanged()
{
return changed_.any();
}
void clearChanges()
{
changed_.clear();
}
const std::vector<std::string>& fields() const
{
return std::ref(FieldNames);
}
boost::any getField(const std::string& field)
{
<% clazz[:fields].each_with_index do |field,i| %>
if(field == "<%=field[:name]%>")
return <%=field[:name]%>_;
<% end %>
}
boost::any getField(unsigned int field)
{
<% clazz[:fields].each_with_index do |field,i| %>
if(field == i)
return <%= field[:name] %>_;
<% end %>
}
void setField(const std::string& field, boost::any value)
{
<% clazz[:fields].each_with_index do |field,i| %>
if(field == "<%=field[:name]%>")
<%=field[:name]%>_ = value;
<% end %>
}
void iterateChanges(const std::function<void(const std::string& field, boost::any& value)>& handler)
{
for(unsigned int i = 0; i < bitset_.size(); ++i)
{
if(changed_[i])
{
handler("TODO", getField(i));
}
}
}
private:
std::bitset<<%= clazz[:fields].count %>> changed_;
<% clazz[:fields].each_with_index do |field,i| %>
<% if field[:dtype] == :array %>
std::vector<<%= field[:type][:array] %>> <%= field[:name] %>_;
<% else %>
<%= field[:type] %> <%= field[:name] %>_;
<% end %>
<% end %>
}
<% end %>
Structs:
<% structs.each do |clazz| %>
<%= clazz[:name] %>
<% end %>