logo
down
shadow

Boost Karma generator for composition of classes


Boost Karma generator for composition of classes

By : Mike
Date : November 21 2020, 03:00 PM
wish help you to fix your issue I'd advise against using Karma to generate JSON. I'd advise strongly against ADAPT_ADT (it's prone to very subtle UB bugs and it means you're trying to adapt something that wasn't designed for it. Just say no).
Here's my take on it. Let's take the high road and be as unintrusive as possible. That means
code :
#include <boost/variant.hpp>
namespace json {
    // adhoc JSON rep
    struct Null {};
    using String = std::string;

    using Value = boost::make_recursive_variant<
        Null,
        String,
        std::vector<boost::recursive_variant_>,
        std::vector<std::pair<String, boost::recursive_variant_> >
    >::type;

    using Property = std::pair<String, Value>;
    using Object = std::vector<Property>;
    using Array = std::vector<Value>;
}
#include <iomanip>
namespace json {

    // pretty print it
    struct pretty_io {
        using result_type = void;

        template <typename Ref>
        struct manip {
            Ref ref;
            friend std::ostream& operator<<(std::ostream& os, manip const& m) {
                pretty_io{os,""}(m.ref);
                return os;
            }
        };

        std::ostream& _os;
        std::string _indent;

        void operator()(Value const& v) const {
            boost::apply_visitor(*this, v);
        }
        void operator()(Null) const {
            _os << "null";
        }
        void operator()(String const& s) const {
            _os << std::quoted(s);
        }
        void operator()(Property const& p) const {
            _os << '\n' << _indent; operator()(p.first);
            _os << ": ";            operator()(p.second);
        }
        void operator()(Object const& o) const {
            pretty_io nested{_os, _indent+"  "};
            _os << "{";
            bool first = true;
            for (auto& p : o) { first||_os << ","; nested(p); first = false; }
            _os << "\n" << _indent << "}";
        }
        void operator()(Array const& o) const {
            pretty_io nested{_os, _indent+"  "};
            _os << "[\n" << _indent << "  ";
            bool first = true;
            for (auto& p : o) { first||_os << ",\n" << _indent << "  "; nested(p); first = false; }
            _os << "\n" << _indent << "]";
        }
    };

    Value to_json(Value const& v) { return v; }

    template <typename T, typename V = decltype(to_json(std::declval<T const&>()))>
    pretty_io::manip<V> pretty(T const& v) { return {to_json(v)}; }
}
std::cout << json::pretty("hello world"); // prints as a JSON String
std::cout << json::pretty(plus1);
auto to_json(Expression const* expression) {
    return serialization::call(expression);
}
struct serialization {
    static json::Value call(Expression const* e) {
        if (auto* f = dynamic_cast<Function const*>(e)) {
            json::Array args;
            for (auto& a : f->m_arguments)
                args.push_back(call(a));
            return json::Object {
                { "name", f->getName() },
                { "type", "Function" },
                { "arguments", args },
            };
        }

        if (auto* v = dynamic_cast<Value const*>(e)) {
            return json::Object {
                { "name", v->getName() },
                { "type", "Value" },
                { "value", v->getValue() },
            };
        }

        return {}; // Null in case we didn't implement a node type
    }
};
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <iomanip>
#include <vector>

struct Expression {
    virtual std::string getName() const = 0;
};

struct Value : Expression {
    virtual std::string getValue() const = 0;
};

struct IntegerValue : Value {
    IntegerValue(int value) : m_value(value) {}
    virtual std::string getName() const override { return "IntegerValue"; }
    virtual std::string getValue() const override { return boost::lexical_cast<std::string>(m_value); }

  private:
    int m_value;
};

struct Function : Expression {
    void addArgument(Expression *expression) { m_arguments.push_back(expression); }
    virtual std::string getName() const override { return m_name; }

  protected:
    std::vector<Expression *> m_arguments;
    std::string m_name;

    friend struct serialization;
};

struct Plus : Function {
    Plus() : Function() { m_name = "Plus"; }
};

///////////////////////////////////////////////////////////////////////////////
// A simple JSON facility
#include <boost/variant.hpp>
namespace json {
    // adhoc JSON rep
    struct Null {};
    using String = std::string;

    using Value = boost::make_recursive_variant<
        Null,
        String,
        std::vector<boost::recursive_variant_>,
        std::vector<std::pair<String, boost::recursive_variant_> >
    >::type;

    using Property = std::pair<String, Value>;
    using Object = std::vector<Property>;
    using Array = std::vector<Value>;
}

///////////////////////////////////////////////////////////////////////////////
// Pretty Print manipulator
#include <iomanip>
namespace json {

    // pretty print it
    struct pretty_io {
        using result_type = void;

        template <typename Ref>
        struct manip {
            Ref ref;
            friend std::ostream& operator<<(std::ostream& os, manip const& m) {
                pretty_io{os,""}(m.ref);
                return os;
            }
        };

        std::ostream& _os;
        std::string _indent;

        void operator()(Value const& v) const {
            boost::apply_visitor(*this, v);
        }
        void operator()(Null) const {
            _os << "null";
        }
        void operator()(String const& s) const {
            _os << std::quoted(s);
        }
        void operator()(Property const& p) const {
            _os << '\n' << _indent; operator()(p.first);
            _os << ": ";            operator()(p.second);
        }
        void operator()(Object const& o) const {
            pretty_io nested{_os, _indent+"  "};
            _os << "{";
            bool first = true;
            for (auto& p : o) { first||_os << ","; nested(p); first = false; }
            _os << "\n" << _indent << "}";
        }
        void operator()(Array const& o) const {
            pretty_io nested{_os, _indent+"  "};
            _os << "[\n" << _indent << "  ";
            bool first = true;
            for (auto& p : o) { first||_os << ",\n" << _indent << "  "; nested(p); first = false; }
            _os << "\n" << _indent << "]";
        }
    };

    Value to_json(Value const& v) { return v; }

    template <typename T, typename V = decltype(to_json(std::declval<T const&>()))>
    pretty_io::manip<V> pretty(T const& v) { return {to_json(v)}; }
}

///////////////////////////////////////////////////////////////////////////////
// Expression -> JSON
struct serialization {
    static json::Value call(Expression const* e) {
        if (auto* f = dynamic_cast<Function const*>(e)) {
            json::Array args;
            for (auto& a : f->m_arguments)
                args.push_back(call(a));
            return json::Object {
                { "name", f->getName() },
                { "type", "Function" },
                { "arguments", args },
            };
        }

        if (auto* v = dynamic_cast<Value const*>(e)) {
            return json::Object {
                { "name", v->getName() },
                { "type", "Value" },
                { "value", v->getValue() },
            };
        }

        return {};
    }
};

auto to_json(Expression const* expression) {
    return serialization::call(expression);
}

int main() {
    // Build expression 4 + 5 + 6 as 4 + (5 + 6)
    Function *plus1 = new Plus();
    Function *plus2 = new Plus();
    Value *iv4 = new IntegerValue(4);
    Value *iv5 = new IntegerValue(5);
    Value *iv6 = new IntegerValue(6);
    plus2->addArgument(iv5);
    plus2->addArgument(iv6);
    plus1->addArgument(iv4);
    plus1->addArgument(plus2);

    // Generate json string here, but how?

    std::cout << json::pretty(plus1);
}
{
  "name": "Plus",
  "type": "Function",
  "arguments": [
    {
      "name": "IntegerValue",
      "type": "Value",
      "value": "4"
    },
    {
      "name": "Plus",
      "type": "Function",
      "arguments": [
        {
          "name": "IntegerValue",
          "type": "Value",
          "value": "5"
        },
        {
          "name": "IntegerValue",
          "type": "Value",
          "value": "6"
        }
      ]
    }
  ]
}


Share : facebook icon twitter icon
boost::spirit::karma Generator Semantic Actions in rules

boost::spirit::karma Generator Semantic Actions in rules


By : Carrie Carlton
Date : March 29 2020, 07:55 AM
it helps some times That is a problem with boost::phoenix V2 (don't ask which ;-)) So, using V3 will work.
Additionally one has to give the attribute to the int generator and reference the lineNum when printing it.
code :
#include <iostream>
#include <string>

#define BOOST_SPIRIT_USE_PHOENIX_V3

#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>

int main() {
  using boost::spirit::karma::eps;
  using boost::spirit::karma::int_;
  using boost::spirit::karma::lit;
  using boost::spirit::karma::eol;
  using boost::spirit::karma::_1;
  using boost::spirit::karma::_val;
  using boost::phoenix::val;
  using boost::phoenix::ref;
  using boost::spirit::karma::generate;
  using boost::spirit::karma::rule;

  typedef std::back_insert_iterator<std::string> OutputIteratorType;

  std::string s;
  std::back_insert_iterator<std::string> sink(s);
  int lineNum = 0;

  rule<OutputIteratorType, int()> testRule = eps[ref(lineNum) += 10]
      << lit("Line number ") << lit(ref(lineNum)) << lit(": ")
      << int_[_1 = _val] << eol;
  generate(sink, testRule, 123);
  generate(sink, testRule, 123);

  std::cout << s;
  return 0;
}
How to tel a boost::karma::rule not to consume its attribute without providing a valid generator?

How to tel a boost::karma::rule not to consume its attribute without providing a valid generator?


By : TonyStark
Date : March 29 2020, 07:55 AM
hope this fix your issue You seem to have stumbled on the inverse of the infamous single-element fusion sequence conundrum[1] :(
I noticed, because the error emanates from the code trying to verify that the input string matches the attribute (lit.hpp):
code :
// fail if attribute isn't matched by immediate literal
typedef typename attribute<Context>::type attribute_type;

typedef typename spirit::result_of::extract_from<attribute_type, Attribute>::type
    extracted_string_type;

using spirit::traits::get_c_string;
if (!detail::string_compare(
        get_c_string(
            traits::extract_from<attribute_type>(attr, context))
      , get_c_string(str_), char_encoding(), Tag()))
{
    return false;
}
query = karma::eps << "yeah";
boost spirit karma real generator performance

boost spirit karma real generator performance


By : Ven
Date : March 29 2020, 07:55 AM
Hope that helps It's not a regression if you compare apples and pears. In this case, twice.
First apple/pear pair
code :
    static int floatfield(T n)
    {
        if (traits::test_zero(n))
            return fmtflags::fixed;

        T abs_n = traits::get_absolute_value(n);
        return (abs_n >= 1e5 || abs_n < 1e-3) 
          ? fmtflags::scientific : fmtflags::fixed;
    }
clock resolution: mean is 16.9199 ns (40960002 iterations)

benchmarking format_performance_direct_string
collecting 100 samples, 1 iterations each, in estimated 4.7784 ms
mean: 238.81 ns, lb 187.22 ns, ub 493.46 ns, ci 0.95
std dev: 507.559 ns, lb 5.36317 ns, ub 1111.94 ns, ci 0.95
found 11 outliers among 100 samples (11%)
variance is severely inflated by outliers

benchmarking format_performance_direct_string_with_policy
collecting 100 samples, 96 iterations each, in estimated 1699.2 μs
mean: 173.927 ns, lb 172.764 ns, ub 176.939 ns, ci 0.95
std dev: 8.33706 ns, lb 0.256875 ns, ub 16.9312 ns, ci 0.95
found 2 outliers among 100 samples (2%)
variance is moderately inflated by outliers

benchmarking format_performance_string
collecting 100 samples, 84 iterations each, in estimated 1705.2 μs
mean: 312.646 ns, lb 311.027 ns, ub 314.819 ns, ci 0.95
std dev: 9.42479 ns, lb 7.32668 ns, ub 15.2546 ns, ci 0.95
found 1 outliers among 100 samples (1%)
variance is moderately inflated by outliers

benchmarking format_performance_string_with_policy
collecting 100 samples, 31 iterations each, in estimated 1736 μs
mean: 193.572 ns, lb 192.257 ns, ub 200.032 ns, ci 0.95
std dev: 12.8586 ns, lb 0.322008 ns, ub 30.6708 ns, ci 0.95
found 4 outliers among 100 samples (4%)
variance is severely inflated by outliers
clock resolution: mean is 18.6041 ns (40960002 iterations)

benchmarking format_performance_direct_string_with_policy
collecting 100 samples, 1 iterations each, in estimated 1892.9 μs
mean: 228.83 ns, lb 179.9 ns, ub 471.84 ns, ci 0.95
std dev: 483.67 ns, lb 2.29965 ns, ub 1153.98 ns, ci 0.95
found 14 outliers among 100 samples (14%)
variance is severely inflated by outliers

benchmarking format_performance_direct_string_with_policy15
collecting 100 samples, 45 iterations each, in estimated 1858.5 μs
mean: 418.697 ns, lb 410.976 ns, ub 438.865 ns, ci 0.95
std dev: 58.0984 ns, lb 24.1313 ns, ub 115.549 ns, ci 0.95
found 6 outliers among 100 samples (6%)
variance is severely inflated by outliers

benchmarking format_performance_string_with_policy
collecting 100 samples, 87 iterations each, in estimated 1870.5 μs
mean: 262.057 ns, lb 254.73 ns, ub 269.354 ns, ci 0.95
std dev: 37.2502 ns, lb 31.1261 ns, ub 50.5813 ns, ci 0.95
found 17 outliers among 100 samples (17%)
variance is severely inflated by outliers

benchmarking format_performance_string_with_policy15
collecting 100 samples, 42 iterations each, in estimated 1898.4 μs
mean: 458.505 ns, lb 453.626 ns, ub 481.044 ns, ci 0.95
std dev: 45.5401 ns, lb 4.30147 ns, ub 108.045 ns, ci 0.95
found 4 outliers among 100 samples (4%)
variance is severely inflated by outliers
Inconsistent Generator directive column behavior in boost karma

Inconsistent Generator directive column behavior in boost karma


By : gkanchan_4
Date : March 29 2020, 07:55 AM
Hope this helps I think this comes close to what you want to achieve. Like I said, the last (incomplete) run will still be "terminated" with an nth_delimiter tag:
Live On Coliru
code :
#include <boost/spirit/include/karma.hpp>
#include <boost/fusion/include/struct.hpp>
#include <boost/fusion/include/nview.hpp>

///////////////////////////////////////////////////////////////////////////////
namespace client {
    struct employee
    {
        int num;
        std::string datatype;
        std::string dataname;
        std::string inputicon;
    };

    typedef std::back_insert_iterator<std::string> iterator_type;
}

BOOST_FUSION_ADAPT_STRUCT(client::employee, /*num,*/ datatype, dataname, inputicon)

///////////////////////////////////////////////////////////////////////////////
int main() {
    // some employees
    std::vector<client::employee> const employees {
        { 25, "int",    "sra_command","fa fa-wrench" },
        { 26, "float",  "swt_command","fa fa-wrench" },
        { 27, "double", "msc_command","fa fa-mobile" },
        { 28, "int",    "sra_command","fa fa-wrench" },
        { 29, "float",  "swt_command","fa fa-wrench" },
        { 30, "double", "msc_command","fa fa-mobile" },
        { 31, "int",    "sra_command","fa fa-wrench" },
        { 32, "float",  "swt_command","fa fa-wrench" },
        { 33, "double", "msc_command","fa fa-mobile" },
    };

    // now print them all
    std::string generated;
    {
        using namespace boost::spirit::karma;

        using Sink = client::iterator_type;
        //using Attr = boost::fusion::result_of::as_nview<client::employee const, 1, 2, 3>::type;
        using Attr = client::employee;
        rule<Sink, Attr()> small_box  = "<B>" << string << "<1>" << string << "<2>" << string << "<3>";

        generate(Sink(generated), columns(2, "<nth_delimiter>\n") [+small_box], employees);
    }

    std::cout << generated << std::endl;
}
<B>int<1>sra_command<2>fa fa-wrench<3><B>float<1>swt_command<2>fa fa-wrench<3><nth_delimiter>
<B>double<1>msc_command<2>fa fa-mobile<3><B>int<1>sra_command<2>fa fa-wrench<3><nth_delimiter>
<B>float<1>swt_command<2>fa fa-wrench<3><B>double<1>msc_command<2>fa fa-mobile<3><nth_delimiter>
<B>int<1>sra_command<2>fa fa-wrench<3><B>float<1>swt_command<2>fa fa-wrench<3><nth_delimiter>
<B>double<1>msc_command<2>fa fa-mobile<3><nth_delimiter>
    generate(Sink(generated), columns(4, "<nth_delimiter>") [small_box % eol], employees);
<B>int<1>sra_command<2>fa fa-wrench<3>
<B>float<1>swt_command<2>fa fa-wrench<3>
<nth_delimiter><B>double<1>msc_command<2>fa fa-mobile<3>
<B>int<1>sra_command<2>fa fa-wrench<3>
<nth_delimiter><B>float<1>swt_command<2>fa fa-wrench<3>
<B>double<1>msc_command<2>fa fa-mobile<3>
<nth_delimiter><B>int<1>sra_command<2>fa fa-wrench<3>
<B>float<1>swt_command<2>fa fa-wrench<3>
<nth_delimiter><B>double<1>msc_command<2>fa fa-mobile<3><nth_delimiter>
boost::spirit::karma alternative generator with boost::variant consisting a string and string alias

boost::spirit::karma alternative generator with boost::variant consisting a string and string alias


By : user1983660
Date : March 29 2020, 07:55 AM
it helps some times I have a boost::variant consisting out of several types, including string type aliases and a string type. The string type aliases work as aspected with the boost::spirit::qi alternative parser, but the boost::spirit::karma alternative generator does not only work in a unwanted but also unexpected way, by not using the wanted string type alias generator rule, but also by not even using the built in string generator, when the variant includes the string type: , Where to start.
A. Unspecified Behaviour
code :
boost::variant<mode, std::string> v;
boost::variant<int, mode, std::string> v;
const auto gen = mode_gen<std::back_insert_iterator<std::string> > | uint_ | string;
#include <boost/spirit/include/karma.hpp>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

struct mode : std::string {
    using std::string::string;
};

namespace karma = boost::spirit::karma;

template <typename Out = boost::spirit::ostream_iterator>
karma::rule<Out, mode()> mode_gen = "mode=\"" << karma::string << "\"";

int main() {
    using Variant = boost::variant<mode, std::string, unsigned>;

    Variant foo = std::string("foo"),
            bar = mode("bar"),
            i = 42;

    for (Variant v : { foo, bar, i })
        std::cout << "Output: " << format(mode_gen<> | karma::uint_ | karma::string, v) << "\n";
}
Output: foo
Output: mode="bar"
Output: 42
#include <boost/serialization/strong_typedef.hpp>
namespace hack {
    template <typename Char, typename Tag>
    struct my_traits : std::char_traits<Char> {};
}

using mode = std::basic_string<char, hack::my_traits<char, struct ModeTag> >;
Output: foo
Output: mode="bar"
Output: 42
struct mode : std::string {
    using std::string::string;

    friend std::ostream& operator<<(std::ostream& os, mode const& m) {
        return os << "mode=" << std::quoted(m);
    }
};
std::cout << mode("yo") << std::endl;
std::cout << mode("y\"!\"o") << std::endl;
mode="yo"
mode="y\"!\"o"
#include <boost/spirit/include/karma.hpp>
#include <iostream>
#include <iomanip>

struct mode : std::string {
    using std::string::string;

    friend std::ostream& operator<<(std::ostream& os, mode const& m) {
        return os << "mode=" << std::quoted(m);
    }
};

int main() {
    boost::variant<mode, std::string, unsigned> 
        foo = std::string("foo"),
        bar = mode("bar"),
        i = 42;

    for (auto v : { foo, bar, i })
        std::cout << "Output: " << karma::format(karma::stream, v) << "\n";
}
#include <boost/variant.hpp>
#include <iostream>
#include <iomanip>

namespace hack {
    template <typename Char, typename Tag>
    struct my_traits : std::char_traits<Char> {};
}

namespace mylib {
    struct ModeTag{};
    struct ValueTag{};

    static inline std::ostream& operator<<(std::ostream& os, ModeTag)  { return os << "mode"; }
    static inline std::ostream& operator<<(std::ostream& os, ValueTag) { return os << "value"; }

    template <typename Char, typename Tag>
    static inline std::ostream& operator<<(std::ostream& os, hack::my_traits<Char, Tag>)
        { return os << Tag{}; }

    template <typename Char, typename CharT, typename Alloc>
    std::ostream& operator<<(std::ostream& os, std::basic_string<Char, CharT, Alloc> const& s) {
        return os << CharT{} << "=" << std::quoted(s);
    }
}

using mode = std::basic_string<char, hack::my_traits<char, struct mylib::ModeTag> >;
using value = std::basic_string<char, hack::my_traits<char, struct mylib::ValueTag> >;

int main() {
    boost::variant<mode, value, unsigned> 
        foo = value("foo"),
        bar = mode("bar"),
        i = 42;

    std::cout << foo << std::endl;
    std::cout << bar << std::endl;
    std::cout << i << std::endl;
}
value="foo"
mode="bar"
42
Related Posts Related Posts :
  • Find parent node function for binary tree
  • Rotate Binary Tree
  • Boost asio async_read_until stop reading on matched condition
  • How can I change the QGraphicView selected Item color
  • Understanding object slicing
  • How to change pointer so that it point to somewhere else?
  • Dynamically add overlay widgets
  • Compressing 3 number data in a byte
  • OpenGL - Uniform not being passed properly?
  • C++ Visual Studio "Non-standard syntax; use '&' to create a pointer to member"
  • Doxygen won't resolve reference to typedef in namespace
  • How to test for assert in debug build, but returned value in release?
  • use boost spirit parse int pair to vector
  • How #define works in Programming when define has value with operator?
  • C++: Passing objects by value to a member function of the same class
  • Confusion in C++ constructor
  • Implicit conversion operator doesn't kick in with operator overloading
  • From tuple of N elements to tuple of N/2 pairs
  • Operators Not Working With Class Objects?
  • GDAL Not Linking
  • C++ aligning characters to display neatly with any input implemented
  • c++, method to display content in hash table.Using double linked list
  • How to apply a function to a variadic argument list and cat them to a tuple
  • Doubly linked list c++ crashing after 3rd node
  • Transform a specific type of argument using templates
  • Function with recursion is causing stack overflow
  • working with multi-word typedefs
  • Calling std::to_string or std::to_wstring based on given typedef
  • Ensure a `std::vector` won't move its pointer
  • Appopriate Container that acts like normal static array?
  • Using enable_if to disable a template constructor of a template class
  • define implicit conversion from template class to primitive type based on other template
  • C++ class template, how to overload [ ] operator in a specific stuation?
  • What is the purpose of using an iterator in this function's return value?
  • C++ overriding the value of inherited static const variables
  • C++: what is special about punning character arrays according to the standard?
  • Producer consumer pthreads program not finishing
  • How do I start two thread teams with different thread functions at the same time?
  • OpenGL 3.3: GL_INVALID_OPERATION when calling glBindBuffer
  • Can the storage of trivially copyable objects be safely reallocated with realloc?
  • Can I force a C++ class to be non-abstract?
  • The QPropertyAnimation is automatically deleted when using QParallelAnimationGroup?
  • Failing to specialize function for function pointer
  • Check if two (smart) pointers point to the same derived class
  • Getting an error when using delete[] cpp
  • recursion in variadic template function of different argument types
  • QTWidgets QTableWidget crash with segfault
  • clang and clang++ with ASAN generate different output
  • How to map BYTE array as FILE * on Windows
  • Logic Error With Function to Manipulate Linked List
  • Expected primary-expression before numeric constant
  • Pre-Decrement Operator
  • C++: how to input values separated by comma(,)
  • Correct way to write operator+ using rvalues
  • How to ignore whitespace and punctuation?
  • Prevent duplicated code on different data types (uint16_t/ uint32_t)
  • Writing Custom FPrintF
  • How to read from keyboard a complete random text, filter and cast
  • SIGSEGV when trying to change string in structure C/C++
  • c++: tuple with tag-type access
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org