C++ Array Support to ThorsAnvil_MakeTrait class
dpate117 opened this issue · comments
#include <iostream>
#include <sstream>
#include <vector>
//Including Thor Serialize Library
#include "ThorSerialize/Traits.h"
#include "ThorSerialize/JsonThor.h"
#include "ThorSerialize\SerUtil.h"
struct Shirt {
public: int red;
int green;
int blue;
};
class TeamMember {
public:
std::string name = "Empty";
int score = 0;
int damage = 0;
Shirt team[3];
TeamMember() {};
// Define the trait as a friend to get accesses to private
// Members.
//friend class ThorsAnvil::Serialize::Traits<TeamMember>;
};
// Declare the traits.
// Specifying what members need to be serialized.
ThorsAnvil_MakeTrait(Shirt, red, green, blue);
ThorsAnvil_MakeTrait(TeamMember, name, score, damage, team);
int main() {
using ThorsAnvil::Serialize::jsonExport;
TeamMember john; // setting Default value 0 for all class members
std::cout << "-----------------Before---------------" << "\n";
std::cout << jsonExport(john) << "\n"; //Printing all the member data
std::stringstream input(R "({" name ": "John ","score ": 100,"team ":{"red ": 1,"green ": 2,"blue ": 3}})");//json string
input >> jsonImport(john); //Assigning JSON data to class members
std::cout << "-----------------After----------------" << "\n";
std::cout << jsonExport(john) << "\n"; //Printing all the member data after assigning JSON data
std::cout << "--------------------------------------" << "\n";
}
Does library support c style array inside of the class to serialize the data?
Library throws error shown in error.txt file
error.txt
The problem with C arrays is that the decay to pointers when passed as parameters. Once the array has decayed to a pointer it can not be converted back to an array and you can not find the size of the array via a pointer. So there are all sorts of corner cases where we could incorrectly use the wrong size of C array.
As a result I made the deliberate choice of not supporting C arrays.
I think that this was a valid choice as basic C arrays are rarely used in modern C++ code as the language has provided a better alternative with std::array
class TeamMember {
public:
std::string name = "Empty";
int score = 0;
int damage = 0;
std::array<3, Shirt> team;
TeamMember() {};
};
This issue already has been discussed I think. I saw it after I asked question. #25