Search code examples
c++boosttreeflagsboost-propertytree

Using Multiple Flags When using read_xml() in Boost Property Tree 1.50


So I found the 'pretty printing' trimming white space flag referenced here: boost::property_tree XML pretty printing

And I found the documentation for Boost::Property_Tree 1.50's read_xml() function: http://www.boost.org/doc/libs/1_50_0/doc/html/boost/property_tree/xml_parser/read_xml_id1073248.html

But I can't for the life of me figure out how to enable both the ignore comments and the trim whitespace flags when reading, then writing the XML! Any help would be appreciated.

I think my main issue is with the function prototype. How do the flags used translate to ints, like the function prototype seems to ask for?


Solution

  • You have to connect the flags together with a bitwise OR (the vertical bar character |). It's a fairly common way to specify multiple flags using a single argument. For example:

    #include <boost/property_tree/ptree.hpp>
    #include <boost/property_tree/xml_parser.hpp>
    
    int main(int argc, char* argv[])
    {
        ptree pt;
        using namespace boost::property_tree::xml_parser; // just to keep the flags nice and short
    
        read_xml("file.xml", pt, trim_whitespace | no_comments);
        return 0;
    }    
    

    As for your second question, the flags are declared as ints. When you do a bitwise or, the bits that represent the ints are combined to make a new int with a unique value. For example, the number 2 has a bit (binary) representation 00000010, and the number 4 has a bitwise representation 00000100 (assuming you're using 8 bits to represent an integer). The bitwise or operation compares each bit, and if either is 1 or both are 1, the result is 1. Otherwise it's zero. In our case:

      00000010
    | 00000100
    ----------
      00000110
    

    which is the number 6 in bit notation.