| 9 | namespace Piccolo |
| 10 | { |
| 11 | void metaExample() |
| 12 | { |
| 13 | Test1 test1_in; |
| 14 | test1_in.m_int = 12; |
| 15 | test1_in.m_char = 'g'; |
| 16 | int i = 1; |
| 17 | test1_in.m_int_vector.emplace_back(&i); |
| 18 | |
| 19 | Test1 test1_out; |
| 20 | // test on array |
| 21 | Test2 test2_in; |
| 22 | test2_in.m_test_base_array.emplace_back("Test1", &test1_in); |
| 23 | Test1 Test2_temp; |
| 24 | test2_in.m_test_base_array.emplace_back("Test1", &Test2_temp); |
| 25 | |
| 26 | // serializer & deserializer |
| 27 | |
| 28 | // write Test1_in (object) to Test1_json_in (json) |
| 29 | auto test1_json_in = PSerializer::write(test1_in); |
| 30 | |
| 31 | std::string test1_context = test1_json_in.dump(); |
| 32 | |
| 33 | // read Test1_context (json) to Test1_out (object) |
| 34 | std::string err; |
| 35 | |
| 36 | auto&& Test1_json = PJson::parse(test1_context, err); |
| 37 | PSerializer::read(Test1_json, test1_out); |
| 38 | LOG_INFO(test1_context); |
| 39 | |
| 40 | auto Test2_json_in = PSerializer::write(test2_in); |
| 41 | std::string test2_context = Test2_json_in.dump(); |
| 42 | |
| 43 | std::fstream out_put("out.txt", std::ios::out); |
| 44 | out_put << test2_context; |
| 45 | out_put.flush(); |
| 46 | out_put.close(); |
| 47 | |
| 48 | Test2 test2_out; |
| 49 | auto&& test2_json = PJson::parse(test2_context, err); |
| 50 | PSerializer::read(test2_json, test2_out); |
| 51 | LOG_INFO(test2_context.c_str()); |
| 52 | |
| 53 | // reflection |
| 54 | auto meta = TypeMetaDef(Test2, &test2_out); |
| 55 | Reflection::FieldAccessor* fields; |
| 56 | int fields_count = meta.m_meta.getFieldsList(fields); |
| 57 | for (int i = 0; i < fields_count; ++i) |
| 58 | { |
| 59 | auto filed_accesser = fields[i]; |
| 60 | std::cout << filed_accesser.getFieldTypeName() << " " << filed_accesser.getFieldName() << " " |
| 61 | << (char*)filed_accesser.get(meta.m_instance) << std::endl; |
| 62 | if (filed_accesser.isArrayType()) |
| 63 | { |
| 64 | Reflection::ArrayAccessor array_accesser; |
| 65 | if (Reflection::TypeMeta::newArrayAccessorFromName(filed_accesser.getFieldTypeName(), array_accesser)) |
| 66 | { |
| 67 | void* field_instance = filed_accesser.get(meta.m_instance); |
| 68 | int count = array_accesser.getSize(field_instance); |
nothing calls this directly
no test coverage detected