1
This commit is contained in:
parent
801ee99ec3
commit
458a24b4c6
62
f8/utils.h
62
f8/utils.h
@ -1,5 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <a8/reflect.h>
|
||||||
|
#include <a8/stringlist.h>
|
||||||
|
#include <a8/csvreader.h>
|
||||||
|
|
||||||
namespace google
|
namespace google
|
||||||
{
|
{
|
||||||
namespace protobuf
|
namespace protobuf
|
||||||
@ -44,6 +48,64 @@ namespace f8
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
bool ReadCsvMetaFile(const std::string& filename, std::vector<T*>& meta_list)
|
||||||
|
{
|
||||||
|
T dummy;
|
||||||
|
a8::reflect::Class* cls = dummy.GetClass();
|
||||||
|
|
||||||
|
a8::CsvReader reader;
|
||||||
|
reader.Load(filename);
|
||||||
|
while (reader.NextLine()) {
|
||||||
|
a8::reflect::Field* fields = cls->GetDeclaredFields();
|
||||||
|
for (int i = 0; i < cls->FieldNum(); ++i) {
|
||||||
|
a8::reflect::Field& field = fields[i];
|
||||||
|
if (field.subtype != a8::reflect::EST_SIMPLE) {
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
T* obj = new T();
|
||||||
|
void* p = (void*)obj + field.offset;
|
||||||
|
switch (field.type) {
|
||||||
|
case a8::reflect::ET_INT32:
|
||||||
|
{
|
||||||
|
*((int*)p) = reader.GetValue(field.field_name);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case a8::reflect::ET_UINT32:
|
||||||
|
{
|
||||||
|
*((unsigned int*)p) = reader.GetValue(field.field_name);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case a8::reflect::ET_INT64:
|
||||||
|
{
|
||||||
|
*((long long*)p) = reader.GetValue(field.field_name);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case a8::reflect::ET_FLOAT:
|
||||||
|
{
|
||||||
|
*((float*)p) = reader.GetValue(field.field_name);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case a8::reflect::ET_DOUBLE:
|
||||||
|
{
|
||||||
|
*((double*)p) = reader.GetValue(field.field_name);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case a8::reflect::ET_STRING:
|
||||||
|
{
|
||||||
|
*((std::string*)p) = reader.GetValue(field.field_name).GetString();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
meta_list.push_back(obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool ReadJsonMetaFile(const std::string& filename,
|
bool ReadJsonMetaFile(const std::string& filename,
|
||||||
google::protobuf::Message* prototype,
|
google::protobuf::Message* prototype,
|
||||||
std::function<void (google::protobuf::Message*)> push_back_func);
|
std::function<void (google::protobuf::Message*)> push_back_func);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user