说明

c++没有静态反射机制,这里采用暴力解析头文件,并生成代码的形式

有struct定义解析结构体

/*
---- From XDU's mzb
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
namespace automation
{
using func_type = function<ll(string)>;
map<ll,func_type> trance;
void init()
{
trance.clear();
}
void add(ll now,func_type func)
{
trance[now] = func;
}
ll run(ll now,string ch)
{
return trance[now](ch);
}
}
namespace state
{
struct s1
{
ll operator ()(string ch)
{
if (ch == "struct")
return 2;
else
return 0;
}
};
static string class_name;
struct s2
{
ll operator ()(string ch)
{
if (ch == "{")
{
class_name.clear();
return 3;
}
else
{
class_name = ch;
return 2;
}
}
};
static string type,value_name;
struct s3
{

ll operator ()(string ch)
{
if (ch == ",")
{
value_name.clear();
return 3;
}
else if (ch == ";")
{
type.clear();
value_name.clear();
return 3;
}
else if (ch == "}")
{
type.clear();
value_name.clear();
return 4;
}
else
{
if (type.size() == 0)
{
type = ch;
}
else
{
value_name = ch;
}
return 3;
}
}
};
struct s4
{
ll operator ()(string ch)
{
if (ch == ";")
return 5;
else
return 0;
}
};
struct s5
{
ll operator ()(string ch)
{
return 0;
}
};
}
struct class_info
{
string class_name;
vector<pair<string,string>> member; //(type,member_name)
vector<string> get_token(string text)
{
vector<string> ret;
string now;
text += " ";
for (auto it : text)
{
if (isalpha(it) or isdigit(it))
{
now.push_back(it);
}
else
{
if (now.size())
{
ret.emplace_back(now);
now.clear();
}
if (it != ' ')
ret.emplace_back(string(1,it));
}
}
return ret;
}
vector<pair<string,string>> set(vector<string> token)
{
cout << "+ " <<"\n";
vector<pair<string,string>> ret;
automation::add(1,state::s1());
automation::add(2,state::s2());
automation::add(3,state::s3());
automation::add(4,state::s4());
automation::add(5,state::s5());
ll now = 1;
for (auto it : token)
{
//cout << "debug+ " << now;
now = automation::run(now,it);
//cout << " -> " << it << " -> " << now << "\n";
if (now == 2)
{
if (state::class_name.size())
class_name = state::class_name.size();
}
else if (now == 3)
{
if (state::type.size() and
state::value_name.size())
{
ret.emplace_back(state::type,
state::value_name);
}
}

}
return ret;
}
}info;
int main()
{
string text;
for (string s;getline(cin,s);text += s + " ");
auto token = info.get_token(text);
auto ret = info.set(token);

for (auto it : ret)
cout << left << setw(20) << it.first << " " << setw(20) << it.second << "\n";
return 0;
}