cgv
Loading...
Searching...
No Matches
binary_io_reflection_handlers.cxx
1#include "binary_io_reflection_handlers.h"
2
3using namespace cgv::reflect;
4
5namespace cgv {
6 namespace data {
7
8
9bool binary_reflection_handler::reflect_header()
10{
11 in_header = true;
12 bool res = reflect_member("version", version) &&
13 reflect_member("content", file_content);
14 in_header = false;
15 return res;
16}
17
20{
21 return true;
22}
23
24binary_reflection_handler::binary_reflection_handler(const std::string& _content, unsigned _ver) : io_reflection_handler(_content, _ver)
25{
26}
27
29void binary_reflection_handler::close()
30{
31 fclose(fp);
32}
33
34bool binary_read_reflection_handler::read_reflect_header(const std::string& _content, unsigned _ver)
35{
36 reflect_header();
37 if (failed())
38 return false;
39 if (_content != file_content)
40 last_error = RE_CONTENT_MISMATCH;
41 else if (_ver != version)
42 last_error = RE_VERSION_MISMATCH;
43 else return true;
44 return false;
45}
46
47binary_read_reflection_handler::binary_read_reflection_handler(const std::string& file_name, const std::string& _content, unsigned _ver) :
48 binary_reflection_handler(_content, _ver)
49{
50 fp = fopen(file_name.c_str(), "rb");
51 if (!fp)
52 last_error = RE_FILE_OPEN_ERROR;
53 else
54 read_reflect_header(_content, _ver);
55}
56
58binary_read_reflection_handler::binary_read_reflection_handler(FILE* _fp, const std::string& _content, unsigned _ver) :
59 binary_reflection_handler(_content, _ver)
60{
61 fp = _fp;
62 read_reflect_header(_content, _ver);
63}
65bool binary_read_reflection_handler::reflect_member_void(const std::string& member_name,
66 void* member_ptr, abst_reflection_traits* rt)
67{
68 switch (rt->get_type_id()) {
70 {
71 std::string& str = *((std::string*)member_ptr);
73 if (fread(&s, sizeof(cgv::type::uint32_type), 1, fp) != 1) {
74 last_error = RE_FILE_READ_ERROR;
75 return false;
76 }
77 str.resize(s);
78 if (fread(&str[0], sizeof(char), s, fp) != s) {
79 last_error = RE_FILE_READ_ERROR;
80 return false;
81 }
82 break;
83 }
85 {
86 std::wstring& str = *((std::wstring*)member_ptr);
88 if (fread(&s, sizeof(cgv::type::uint32_type), 1, fp) != 1) {
89 last_error = RE_FILE_READ_ERROR;
90 return false;
91 }
92 str.resize(s);
93 if (fread(&str[0], sizeof(cgv::type::wchar_type), s, fp) != s) {
94 last_error = RE_FILE_READ_ERROR;
95 return false;
96 }
97 break;
98 }
99 default:
100 if (fread(member_ptr, rt->size(), 1, fp) != 1) {
101 last_error = RE_FILE_READ_ERROR;
102 return false;
103 }
104 break;
105 }
106 return true;
107}
108
109binary_write_reflection_handler::binary_write_reflection_handler(const std::string& file_name, const std::string& _content, unsigned _ver) :
110 binary_reflection_handler(_content, _ver)
111{
112 fp = fopen(file_name.c_str(), "wb");
113 if (!fp)
114 last_error = RE_FILE_OPEN_ERROR;
115 else
116 reflect_header();
117}
118
119binary_write_reflection_handler::binary_write_reflection_handler(FILE* _fp, const std::string& _content, unsigned _ver) :
120 binary_reflection_handler(_content, _ver)
121{
122 fp = _fp;
123 reflect_header();
124}
125
127bool binary_write_reflection_handler::reflect_member_void(const std::string& member_name,
128 void* member_ptr, abst_reflection_traits* rt)
129{
130 switch (rt->get_type_id()) {
132 {
133 const std::string& str = *((std::string*)member_ptr);
135 if (fwrite(&s, sizeof(cgv::type::uint32_type), 1, fp) != 1) {
136 last_error = RE_FILE_WRITE_ERROR;
137 return false;
138 }
139 if (fwrite(&str[0], sizeof(char), s, fp) != s) {
140 last_error = RE_FILE_WRITE_ERROR;
141 return false;
142 }
143 break;
144 }
146 {
147 const std::wstring& str = *((std::wstring*)member_ptr);
149 if (fwrite(&s, sizeof(cgv::type::uint32_type), 1, fp) != 1) {
150 last_error = RE_FILE_WRITE_ERROR;
151 return false;
152 }
153 if (fwrite(&str[0], sizeof(cgv::type::wchar_type), s, fp) != s) {
154 last_error = RE_FILE_WRITE_ERROR;
155 return false;
156 }
157 break;
158 }
159 default:
160 if (fwrite(member_ptr, rt->size(), 1, fp) != 1) {
161 last_error = RE_FILE_WRITE_ERROR;
162 return false;
163 }
164 break;
165 }
166 return true;
167}
168
169 }
170}
171
bool reflect_member_void(const std::string &member_name, void *member_ptr, cgv::reflect::abst_reflection_traits *rt)
abstract interface to reflect a member variable, where the member type is specified as a string.
binary_write_reflection_handler(const std::string &file_name, const std::string &_content, unsigned _ver)
construct from file_name by opening file in ascii mode
bool reflect_member_void(const std::string &member_name, void *member_ptr, cgv::reflect::abst_reflection_traits *rt)
abstract interface to reflect a member variable, where the member type is specified as a string.
common base for all io reflection handlers
bool reflect_member(const std::string &member_name, T &member_ref, bool hard_cast=false)
call this to reflect a member by member name and reference to the member.
in this namespace reflection of types is implemented
@ TI_STRING
wide character type
Definition type_id.h:31
@ TI_WSTRING
string type
Definition type_id.h:32
wchar_t wchar_type
wide character type
unsigned int uint32_type
this type provides an 32 bit unsigned integer type
the cgv namespace
Definition print.h:11
abstract interface for type reflection with basic type management and optional string conversion
virtual unsigned size() const =0
return the size of the type
virtual cgv::type::info::TypeId get_type_id() const =0
return the type id