Is there an easier way to display the struct fields and their corresponding values in RichEdit control?
This is what I am doing now:
<
There is no way to iterate over the members of a plain struct. You must supply this information outside your struct declaration.
You can do it at compile time, as some of the previous answers have shown. However, you can do it at run-time, too. This is similar to the way some "Serialization" libraries work.
You may have the following class:
class MemberStore
{
public:
template
MemberStore(const Base &base) :
m_basePtr(reinterpret_cast(&base))
{}
template
MemberStore& operator&(const Member &classMember){
DataInfo curMember;
curMember.m_offset = reinterpret_cast(&classMember) - m_basePtr;
curMember.m_func = &CvtChar;
m_members.push_back(curMember);
return *this;
}
std::string convert(size_t index) {
return m_members[index].m_func(m_basePtr + m_members[index].m_offset);
}
size_t size() const {
return m_members.size();
}
protected:
template
static std::string CvtChar(const void *data) {
std::stringstream str;
str << *reinterpret_cast(data);
return str.str();
}
private:
struct DataInfo {
size_t m_offset;
std::string (*m_func)(const void *data);
};
std::vector m_members;
const char *m_basePtr;
};
This class contains a vector of "class members" (MemberStore::DataInfo), each of one has:
You can add elements to this class using the & operator (you can concatenate several & operators). After that, you can iterate over to members and convert them them to std::string using its index:
struct StructureIWantToPrint
{
char a;
int b;
double c;
};
int main(int argc, wchar_t* argv[])
{
StructureIWantToPrint myData;
myData.a = 'b';
myData.b = 18;
myData.c = 3.9;
MemberStore myDataMembers(myData);
myDataMembers & myData.a & myData.b & myData.c;
for(size_t i=0;i
It should be possible to modify the MemberStore class so that, instead of store a method to convert member to std::string, automatically inserts the data to the TextList.