Coder Social home page Coder Social logo

Comments (2)

ToruNiina avatar ToruNiina commented on May 28, 2024

Unfortunately, it does not work in that way.

You converted the data from toml::value to foo::device. This conversion inevitably copies the value because the converted types are not a reference nor pointer. You cannot modify the original through copied value because they are located in different regions of memory.

If you want to modify the value, you need to modify the original data. There are several ways to do that.

  1. you can modify the value directly.
int main()
{
    auto file = toml::parse("cfg.toml");

    file["device"]["heart"] = 1;

    std::ofstream ofs("cfg1.toml");
    ofs << file;
    ofs.close();
    return 0;
}
  1. you can take a non-const (mutable) reference to the data
int main()
{
    auto file = toml::parse("cfg.toml");

    auto& heart = toml::find(file, "device", "heart");

    heart = 1;

    std::ofstream ofs("cfg1.toml");
    ofs << file;
    ofs.close();
    return 0;
}
  1. you can substitute modified foo::device to "device" table.
int main()
{
    auto file = toml::parse("cfg.toml");

    auto f = toml::find<foo::device>(file, "device");
    f.heart = 1;

    file["device"] = f;

    std::ofstream ofs("cfg1.toml");
    ofs << file;
    ofs.close();
    return 0;
}

NOTE: The third example overwrites the whole device table by foo::device. Since foo::device does not have port member variable, file["device"] = f; removes port value in toml table.

from toml11.

keepitsimle avatar keepitsimle commented on May 28, 2024

thanks, it will be more convenient if i can do it in my way.

from toml11.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.