You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Question on the lifetime managment of objects at the lower levels #449
Dear Niels,
first of all let me say that your library is very cool.
I am writing here since i am considering the possibility of switching our current implementation from using rapidjson to nlohmann.
Within our code (KratosMultiphysics) we made the choice of isolating ourselves from the json lib of choice by defining an object "Parameters" that essentially hides the details of the underlying implementation.
the point is that if i have something like
Parameters general( " { "a" : 1, "level1" : {... another level in here ... }" }
i need the capability of creating a pointer to the level1, something like
Parameters my_lev1 = general[ "level1" ];
so that if i change my_lev1, the general1 is also changed.
the problem here is of course how to manage the lifetime of the different objects.
before going for ugly solutions (for example for rapidjson in each "Parameters" i was storing a shared_ptr to the root object, i could do something of the sort, or some even uglier trick)
i was wondering if you were using internally some sort of shared_ptr to manage the lifetyme of your descendants, which would allow me a much cleaner solution.
well this is a good information, however i would say it does not solve my problem, since the object "Parameters" has to hold the json inside. (I am trying to modularize the library so that it only knowns Parameters and not the underlying implementation
i could do something like the following, however i agree beforehand that it is not really clean
(and it pays the penalty of a bool per each Parameter i generate)
class Parameters:
{
private:
json* mpdata;
bool is_owner=false;
void Parameters(json& other) //of course this could accept an iterator as you pointed out
{
this->mpdata = other.mpdata;
is_owner = false;
}
public:
void Parameters(std::string params)
{
mpdata = new json(params);
is_owner = true;
}
//this would not be like this ... just to explain the idea
void Get(std::string key)
{
return Parameter((*mpData)[key])
}
//here destroy only if owner, otherwise do nothing
~Parameters()
{
if(is_owner)
mpdata->~json();
}
}
an alternative could be to derive Parameters from the json class...
not sure of how this would pan out
Activity
nlohmann commentedon Feb 12, 2017
You could use an iterator to your object element:
Internally, objects are stored as
std::map, so iterators should be stable.RiccardoRossi commentedon Feb 12, 2017
Hi Niels,
well this is a good information, however i would say it does not solve my problem, since the object "Parameters" has to hold the json inside. (I am trying to modularize the library so that it only knowns Parameters and not the underlying implementation
i could do something like the following, however i agree beforehand that it is not really clean
(and it pays the penalty of a bool per each Parameter i generate)
an alternative could be to derive Parameters from the json class...
not sure of how this would pan out
if you have any better idea let me know
cheers
Riccardo
nlohmann commentedon Feb 13, 2017
Hi Riccardo, unfortunately, I do not understand what you are trying to do and where the library falls short. 😞
RiccardoRossi commentedon Feb 14, 2017
Hi Niels,
to clarify it is NOT your library to fall short. It is the requirement i have on my side that obliges me to a hack.
i am almost through anyway...so thanks a lot for your time, the confirmations that iterators are stable was fundamental
cheers
Riccardo
nlohmann commentedon Feb 14, 2017
Thanks for checking back!