Search code examples
c++boostboost-spirit

Using Boost Karma to print boost::posix_time::time_duration


I am trying to write a simple generator for boost::posix_time::duration to use in another generator. The sticking point for me now is the "+" or "-" which I wanted printed. What I have so far is:

struct GetSign
{
    template<typename> struct result { typedef char type; };

    template<typename TimeDur>
    const char operator()(const TimeDur& dur) const
    {
        return dur.is_negative() ? '-' : '+';
    }
};

boost::phoenix::function<GetSign>   phx_getsign;

struct TimeDurationGenerator
    : boost::spirit::karma::grammar<boost::spirit::ostream_iterator, boost::posix_time::time_duration()>
{
    TimeDurationGenerator()
        : TimeDurationGenerator::base_type(start_)
    {
        namespace bsk = boost::spirit::karma;
        namespace bpt = boost::posix_time;

        start_
            = sign_[bsk::_1 = phx_getsign(bsk::_val)]
            << bsk::right_align(2,'0')[bsk::int_[bsk::_1 = boost::phoenix::bind(&bpt::time_duration::hours, bsk::_val)]]
            << ':'
            << bsk::right_align(2,'0')[bsk::int_[bsk::_1 = boost::phoenix::bind(&bpt::time_duration::minutes,bsk::_val)]];
    }

    boost::spirit::karma::rule<boost::spirit::ostream_iterator, char()> sign_;
    boost::spirit::karma::rule<boost::spirit::ostream_iterator, boost::posix_time::time_duration()> start_;
};

While this does compile (at least on clang) it does not work, as there is no output. When I include this generator in another generator, output always stops when it gets to here. If I remove the sign_[...] portion of the rule, then it work.

How can I get this to work?


Solution

  • You never defined sign_. In fact, you don't need it:

            char_[_1 = phx_getsign(_val)]
    

    But I'd refrain from forcing the square peg into the round hole. If you need that level of control, make a primitive generator that does it. In fact, IO stream manipulators have you covered:

    Live On Coliru

    #include <boost/date_time/posix_time/posix_time.hpp>
    #include <boost/spirit/include/karma.hpp>
    #include <iomanip>
    namespace bsk = boost::spirit::karma;
    namespace bpt = boost::posix_time;
    
    template <typename It = boost::spirit::ostream_iterator>
    struct TimeDurationGenerator : bsk::grammar<It, bpt::time_duration()>
    {
        TimeDurationGenerator() : TimeDurationGenerator::base_type(start_) {
            duration_ = bsk::stream;
            start_    = duration_;
        }
      private:
        struct wrap {
            bpt::time_duration d;
            wrap(bpt::time_duration const& d) : d(d) {}
            friend std::ostream& operator<<(std::ostream& os, wrap const& w) {
                return os << std::setfill('0') << std::internal
                    << std::setw(3) << std::showpos   << w.d.hours() << ":"
                    << std::setw(2) << std::noshowpos << std::abs(w.d.minutes());
            }
        };
        bsk::rule<It, bpt::time_duration()> start_;
        bsk::rule<It, wrap()> duration_;
    };
    
    int main() {
        for (auto str : { "-7:30", "7", "323:87:13" }) {
            std::cout << format(TimeDurationGenerator<>{}, bpt::duration_from_string(str)) << "\n";
        }
    }
    

    Prints

    -07:30
    +07:00
    +324:27