Parsing a Parsed String Stream

The acceleration block library provides a useful check of the unit type of measurement units. It also provides io stream operations for serializing units. However, I am struggling with the bit of parsing the lines. For example, the following lines:

boost::units::quantity<boost::units::si::force> f(2.0 * boost::units::si::newton);
std::cout << "Force = " << f << std::endl;

      

outputs the result:

Force = 2.0 N

      

Can anyone point me to an example that parses this standard serialization to raise units?

// f.parse_string("2.0 N");  or using stream operators??

      

Thank!

+3


source to share


1 answer


It is not directly supported by the library.

Here's an example that might provide some inspiration: http://www.boost.org/doc/libs/1_56_0/doc/html/boost_units/Examples.html#boost_units.Examples.RuntimeUnits



This example demonstrates how to implement an interface that units at run time while maintaining type safety for internal calculations.

namespace {

using namespace boost::units;
using imperial::foot_base_unit;

std::map<std::string, quantity<si::length> > known_units;

}

quantity<si::length> calculate(const quantity<si::length>& t)
{
    return(boost::units::hypot(t, 2.0 * si::meters));
}

int main()
{
    known_units["meter"] = 1.0 * si::meters;
    known_units["centimeter"] = .01 * si::meters;
    known_units["foot"] =
        conversion_factor(foot_base_unit::unit_type(), si::meter) * si::meter;

    std::string output_type("meter");
    std::string input;

    while((std::cout << "> ") && (std::cin >> input))
    {
        if(!input.empty() && input[0] == '#')
        {
            std::getline(std::cin, input);
        }
        else if(input == "exit")
        {
            break;
        }
        else if(input == "help")
        {
            std::cout << "type \"exit\" to exit\n"
                "type \"return 'unit'\" to set the return units\n"
                "type \"'number' 'unit'\" to do a simple calculation"
                << std::endl;
        }
        else if(input == "return")
        {
            if(std::cin >> input)
            {
                if(known_units.find(input) != known_units.end())
                {
                    output_type = input;
                    std::cout << "Done." << std::endl;
                }
                else
                {
                    std::cout << "Unknown unit \"" << input << "\""
                         << std::endl;
                }
            }
            else
            {
                break;
            }
        }
        else
        {
            try
            {
                double value = boost::lexical_cast<double>(input);

                if(std::cin >> input)
                {
                    if(known_units.find(input) != known_units.end())
                    {
                        std::cout << static_cast<double>(
                            calculate(value * known_units[input]) /
                            known_units[output_type])
                            << ' ' << output_type << std::endl;
                    }
                    else
                    {
                        std::cout << "Unknown unit \"" << input << "\""
                            << std::endl;
                    }
                }
                else
                {
                    break;
                }
            }
            catch(...)
            {
                std::cout << "Input error" << std::endl;
            }
        }
    }
}

      

+2


source







All Articles