Skip to content

Question on the lifetime managment of objects at the lower levels #449

Closed

Description

@RiccardoRossi

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.

thank you in advance for any hint
Riccardo

Activity

  1. nlohmann commented on Feb 12, 2017

    @nlohmann
    Owner

    You could use an iterator to your object element:

    #include <json.hpp>
    
    using nlohmann::json;
    
    int main()
    {
        json j = {{"a", 1}, {"level1", "some_value"}};
    
        json::iterator my_lev1 = j.find("level1");
    
        std::cout << j << std::endl;
        // {"a":1,"level1":"some_value"}
    
        *my_lev1 = "another value";
    
        std::cout << j << std::endl;
        // {"a":1,"level1":"another value"}
    }

    Internally, objects are stored as std::map, so iterators should be stable.

  2. RiccardoRossi commented on Feb 12, 2017

    @RiccardoRossi
    Author

    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)

     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

    if you have any better idea let me know

    cheers
    Riccardo

  3. nlohmann commented on Feb 13, 2017

    @nlohmann
    Owner

    Hi Riccardo, unfortunately, I do not understand what you are trying to do and where the library falls short. 😞

  4. RiccardoRossi commented on Feb 14, 2017

    @RiccardoRossi
    Author

    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

  5. nlohmann commented on Feb 14, 2017

    @nlohmann
    Owner

    Thanks for checking back!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions