forked from grantrostig/cpp_by_example
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
184 lines (169 loc) · 6.87 KB
/
main.cpp
File metadata and controls
184 lines (169 loc) · 6.87 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* Generated as part of Grant's password manager effort to show
* how to load a data record, or struct or data row
*/
#include <iostream>
#include <string>
#include <vector>
#include <any>
#include <variant>
#include <memory>
#include <typeinfo>
using std::endl, std::cout, std::string, std::vector;
enum class Data_type {
boolean,
integer,
long_integer,
floating_point,
alphanumeric
};
class Target_data_row {
public:
bool b1 {true};
int i1 {-9};
int i2 {-99};
long l1 {-999};
float f1 {-999.9};
string s1 {"Empty"};
};
enum Data_member_index :int {
B1,
I1,
I2,
L1,
F1,
S1
};
/* template <typename T> // NOT USED
class Location_type_map_templated {
public:
T* location;
T member_type;
Location_type_map_templated( T * ptr) {
location = ptr;
}
void set_location( T * ptr ) {
location = ptr;
}
T * get_location() {
return location;
}
};
*/
int main()
{
{
class Type_map {
public:
void * location;
Data_type member_type;
};
Target_data_row data_fields1 {};
vector<Type_map> location_type_map1 {
{ &data_fields1.b1, Data_type::boolean },
{ &data_fields1.i1, Data_type::integer },
{ &data_fields1.i2, Data_type::integer },
{ &data_fields1.l1, Data_type::long_integer },
{ &data_fields1.f1, Data_type::floating_point },
{ &data_fields1.s1, Data_type::alphanumeric }
};
/* class Type_map2 {
public:
void * location;
Data_type member_type;
// https://stackoverflow.com/questions/13906570/how-to-use-typeid-to-store-a-type-info-object
// also see: https://stackoverflow.com/questions/30159933/how-do-i-store-and-access-a-type-dynamically-in-c
std::type_info * type_info_name; // redundant, but we don't have to code each one specifically. likely UNDEFINED BEHAVIOR // TODO what is going on here and how stable is the value and what about the ABI presuming gcc and all compiled at the same time.
};
Target_data_row data_fields21 {};
vector<Type_map2> location_type_map21 {
{ &data_fields1.b1, Data_type::boolean, const_cast<std::type_info *>( &typeid ( decltype (data_fields21.b1))) },
{ &data_fields1.i1, Data_type::integer, const_cast<std::type_info *>( &typeid ( decltype (data_fields21.i1))) },
{ &data_fields1.i2, Data_type::integer, const_cast<std::type_info *>( &typeid ( decltype (data_fields21.i2))) },
{ &data_fields1.l1, Data_type::long_integer, const_cast<std::type_info *>( &typeid ( decltype (data_fields21.l1))) },
{ &data_fields1.f1, Data_type::floating_point, const_cast<std::type_info *>( &typeid ( decltype (data_fields21.f1))) },
{ &data_fields1.s1, Data_type::alphanumeric, const_cast<std::type_info *>( &typeid ( decltype (data_fields21.s1))) },
};
*/
bool bool_val1 {false};
int int_val1 {4};
int int_val2 {42};
long long_val1 {442};
float float_val1 {442.4};
string string_val2 {"NOT empty."};
// *(bool *)(location_type_map1.at(0).location) = bool_val1 ;
*( decltype(bool_val1) * )(location_type_map1.at(0).location) = bool_val1 ;
cout << "df1.b1: " << data_fields1.b1 << ", "<< bool_val1 << endl;
*( decltype(float_val1) *)(location_type_map1.at(4).location) = float_val1 ;
cout << "df1.f1: " << data_fields1.f1 << ", "<< float_val1 << endl;
// *((bool *)(location_type_map1.at(0).location)) =+ bool_val1 ;
// cout << "df1.b1: " << data_fields1.b1 << ", "<< bool_val1 << endl;
// *((float *)(location_type_map1.at(4).location)) =+ float_val1 ; // todo: minor, this does nothing?
// cout << "df1.f1: " << data_fields1.f1 << ", "<< float_val1 << endl;
}
{
class Type_map {
public:
std::any location;
Data_type member_type;
};
Target_data_row data_fields1 {};
// Type_map a;
// a.location = &data_fields1.b1;
vector<Type_map> location_type_map1 {
{ &data_fields1.b1, Data_type::boolean },
{ &data_fields1.i1, Data_type::integer },
{ &data_fields1.i2, Data_type::integer },
{ &data_fields1.l1, Data_type::long_integer },
{ &data_fields1.f1, Data_type::floating_point },
{ &data_fields1.s1, Data_type::alphanumeric }
};
bool bool_val1 {false};
int int_val1 {4};
int int_val2 {42};
long long_val1 {442};
float float_val1 {442.4};
string string_val2 {"NOT empty."};
// *(bool *)(location_type_map1.at(0).location) = bool_val1 ;
*( std::any_cast< decltype(bool_val1) *>(location_type_map1.at(0).location)) = bool_val1 ;
cout << "df1.b1: " << data_fields1.b1 << ", "<< bool_val1 << endl;
*( std::any_cast< decltype(float_val1) *>(location_type_map1.at(4).location)) = float_val1 ;
cout << "df1.f1: " << data_fields1.f1 << ", "<< float_val1 << endl;
}
{
class Type_map {
public:
std::variant< bool *, int *, long *, float *, string * > location;
Data_type member_type;
};
Target_data_row data_fields1 {};
// Type_map a;
// a.location = &data_fields1.b1;
vector<Type_map> location_type_map1 {
{ &data_fields1.b1, Data_type::boolean },
{ &data_fields1.i1, Data_type::integer },
{ &data_fields1.i2, Data_type::integer },
{ &data_fields1.l1, Data_type::long_integer },
{ &data_fields1.f1, Data_type::floating_point },
{ &data_fields1.s1, Data_type::alphanumeric }
};
bool bool_val1 {false};
int int_val1 {4};
int int_val2 {42};
long long_val1 {442};
float float_val1 {442.4};
string string_val2 {"NOT empty."};
if ( auto location = std::holds_alternative<bool *> (location_type_map1.at(B1).location))
cout << "holds_bool_location\n";
if ( auto location = std::holds_alternative<string *> (location_type_map1.at(B1).location))
cout << "holds_string_location\n";
// *(bool *)(location_type_map1.at(0).location) = bool_val1 ;
auto location = std::get_if< decltype(bool_val1) *>( &location_type_map1.at( B1 ).location );
**location = bool_val1;
cout << "df1.b1: " << data_fields1.b1 << ", "<< bool_val1 << endl;
// *(std::any_cast<float *>(location_type_map1.at(4).location)) = float_val1 ;
**std::get_if< decltype(float_val1) *>( &location_type_map1.at( F1 ).location ) = float_val1;
cout << "df1.f1: " << data_fields1.f1 << ", "<< float_val1 << endl;
}
cout << "###" << endl;
return 0;
}