Giter Site home page Giter Site logo

Comments (3)

GoogleCodeExporter avatar GoogleCodeExporter commented on July 17, 2024
The parser isn't really intended to be used as a class member - more of a 
one-off
type thing. One problem is that the stream must stick around as long as you're
parsing. So if you did the very plausible:

struct foo {
   YAML::Parser parser;
   // ...
};

foo::init() {
   std::ifstream fin("file.yaml");
   parser.Load(fin);
}

foo::dostuff() {
   YAML::Node doc;
   parser.GetNextDocument(doc);  // FAILS!!!
}

because when `GetNextDocument` is called, the stream is no longer valid. Why do 
you
want the parser as a class member?

Original comment by [email protected] on 1 Oct 2009 at 2:18

  • Added labels: ****
  • Removed labels: ****

from yaml-cpp.

GoogleCodeExporter avatar GoogleCodeExporter commented on July 17, 2024
example code: (working now)

struct YAMLStreamer : BBStreamer {
    typedef std::vector<BoundingBox>::iterator iterator;
    boost::filesystem::ifstream file_in;
    boost::iostreams::filtering_stream<io::input> in;
    boost::scoped_ptr<YAML::Parser> parser;
    YAML::Node doc;
    GTImage img;
    YAMLStreamer(std::string const& filename) : file_in(filename) 
    {
        in.push(boost::iostreams::gzip_decompressor());
        in.push(file_in);
        parser.reset(new YAML::Parser(in));
    }
    bool advance() 
    {
        if (!*parser)
            return false;
        img = GTImage();
        parser->GetNextDocument(doc);
        doc >> img;
        return true;
    }
    iterator begin() 
    {
        return img.labels.begin();
    }
    iterator end() 
    {
        return img.labels.end();
    }
    std::string& get_filename()
    {
        return img.file;
    }
};

BBStreamer is just an abstract base class, there are streams for other file 
types
too. You basically give it a filename, it figures out if the file is compressed 
(code
for that is removed for clarity), applies decompression filters and passes the
filtered stream to the parser. The boost::scoped_ptr (deletes the parser when 
the ptr
goes out of scope) would be unneccessary if the parser could be used as a member
directly. So in this case, the stream is a member of the class too, but because 
i
must first prepare it before i can pass it to the parser i can't do so in the
initializer list ...

Original comment by stefan.walk on 1 Oct 2009 at 2:37

  • Added labels: ****
  • Removed labels: ****

from yaml-cpp.

GoogleCodeExporter avatar GoogleCodeExporter commented on July 17, 2024
OK, I implemented this in r277. You're right, it is useful to have a default
constructor; but I don't think I'm going to add this info to the wiki (yet) 
because I
want to encourage the single parameter construction. I'm not a huge fan of 
"zombie"
objects, but the operator bool() does take into account whether it's been 
initialized
yet.

Original comment by [email protected] on 19 Oct 2009 at 10:44

  • Changed state: Fixed
  • Added labels: ****
  • Removed labels: ****

from yaml-cpp.

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.