rttrorg / rttr

C++ Reflection Library
https://www.rttr.org
MIT License
3.11k stars 429 forks source link

not support shared_ptr #352

Open doudouodong opened 1 year ago

doudouodong commented 1 year ago

ifndef PSDLAYERH

define PSDLAYERH

include "rttr/registration"

class PSDLayer { public: PSDLayer(); virtual ~PSDLayer();

public: int left; int top; int right; int bottom;

RTTR_ENABLE()

};

endif //PSDLAYERH

include "core/psdlayer.h"

PSDLayer::PSDLayer():left(0),top(0),right(0),bottom(0){}

PSDLayer::~PSDLayer(){}

RTTRREGISTRATION { rttr::registration::class("PSDLayer") .property("left", &PSDLayer::left) .property("top", &PSDLayer::top) .property("right", &PSDLayer::right) .property("bottom", &PSDLayer::bottom) ; }

ifndef PSDTYPELAYERH

define PSDTYPELAYERH

include

include "core/psdlayer.h"

class PSDTypeLayer : public PSDLayer { public: PSDTypeLayer(); ~PSDTypeLayer();

public: RTTR_ENABLE(PSDLayer) public: std::string vips_text; };

endif // PSDTYPELAYERH

include "core/psdtypelayer.h"

PSDTypeLayer::PSDTypeLayer() {

}

PSDTypeLayer::~PSDTypeLayer() {

}

RTTRREGISTRATION { rttr::registration::class("PSDTypeLayer") .property("vips_text", &PSDTypeLayer::vips_text) ; }

ifndef PSDLAYERVECTORH

define PSDLAYERVECTORH

include

include "core/psdlayer.h"

class PSDLayerVector { public: PSDLayerVector(); ~PSDLayerVector();

RTTR_ENABLE()

public: std::vector<std::shared_ptr> layers; };

endif //PSDLAYERVECTORH

include "core/psdlayervector.h"

PSDLayerVector::PSDLayerVector() {

}

PSDLayerVector::~PSDLayerVector() {

}

RTTRREGISTRATION { rttr::registration::class("PSDLayerVector") .property("layers", &PSDLayerVector::layers) ; }

int main(int argc, char** argv) { PSDLayerVector layers;

auto p1 = std::make_shared<PSDLayer>();
p1->left = 100;
auto p2 = std::make_shared<PSDTypeLayer>();
p2->vips_text = "hello world";
layers.layers.push_back(p1);
layers.layers.push_back(p2);

std::string json_string;
json_string = io::to_json(layers);
std::cout << json_string << std::endl;

PSDLayerVector objlayers;
io::from_json(json_string, objlayers); 
return 0;

}

btgoodwin commented 1 year ago

I've run into something similar while trying to follow the example rapidjson to/from that you appear to be using here. My library uses JsonGLIB as its target type, and I'm currently testing how to handle registered object members that are shared pointers to other RTTR_ENABLE'd structures. I've found that in order for my variation of the fromjson_recursive function to work, I have to get the properties off the obj.get_type().get_raw_type().get_properties(); rather than obj.get_derived_type().get_properties() (which seems to work fine for value -type object members.

Despite being able to now pull the property list and apparently read and set the various members on this passed in rttr instance, the output structure does not have the created instance set on its member (I.e., target.shared_ptr_member.get() is nullptr.)

Did you ever make headway with this?