Boost Karma 对象方法调用
Boost Karma object method call
作为我的问题的扩展和 我想在生成输出时调用一个函数。
我添加了方法 bool isRoby()
并修改了 emp 规则。
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <map>
namespace karma = boost::spirit::karma;
namespace phx = boost::phoenix;
enum TYPEX { AUTHOR1, AUTHOR2, AUTHOR3, AUTHOR4 };
std::map<TYPEX, std::string> author2name;
struct Emp {
std::string name;
TYPEX author;
bool isRoby()
{
return name == "roby";
};
};
BOOST_FUSION_ADAPT_STRUCT(Emp, name, author) // boost 1_59
//BOOST_FUSION_ADAPT_STRUCT(Emp, (std::string, name)(std::string, author)) // older boost
int main() {
using it = boost::spirit::ostream_iterator;
karma::rule<it, std::string()> quote;
karma::rule<it, TYPEX()> author;
karma::rule<it, Emp()> emp;
{
using namespace karma;
quote %= '"' << string << '"';
author = quote [ _1 = phx::ref(author2name)[ _val ] ];
emp %= delimit('\t')[ quote << author << bool_[ BIND? ] ];
}
Emp x { "one", AUTHOR2 };
author2name[AUTHOR2] = "TWO!";
std::cout << karma::format(emp, x);
}
我刚找到 Phoenix Lazy,但似乎不匹配。
假设您使 isRoby
const:
bool isRoby() const { return name == "roby"; };
您可以使用 phx::bind
:
bool_ [ _1 = phx::bind(&Emp::isRoby, _val) ]
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <map>
namespace karma = boost::spirit::karma;
namespace phx = boost::phoenix;
enum TYPEX { AUTHOR1, AUTHOR2, AUTHOR3, AUTHOR4 };
std::map<TYPEX, std::string> author2name;
struct Emp {
std::string name;
TYPEX author;
bool isRoby() const { return name == "roby"; };
};
BOOST_FUSION_ADAPT_STRUCT(Emp, name, author) // boost 1_59
// BOOST_FUSION_ADAPT_STRUCT(Emp, (std::string, name)(std::string, author)) // older boost
int main() {
using it = boost::spirit::ostream_iterator;
karma::rule<it, std::string()> quote;
karma::rule<it, TYPEX()> author;
karma::rule<it, Emp()> emp;
{
using namespace karma;
quote %= '"' << string << '"';
author = quote[_1 = phx::ref(author2name)[_val]];
emp %= delimit('\t')[ quote << author << bool_ [ _1 = phx::bind(&Emp::isRoby, _val) ] ];
}
Emp x{ "one", AUTHOR2 };
author2name[AUTHOR2] = "TWO!";
std::cout << karma::format(emp, x);
}
作为我的问题的扩展和
我添加了方法 bool isRoby()
并修改了 emp 规则。
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <map>
namespace karma = boost::spirit::karma;
namespace phx = boost::phoenix;
enum TYPEX { AUTHOR1, AUTHOR2, AUTHOR3, AUTHOR4 };
std::map<TYPEX, std::string> author2name;
struct Emp {
std::string name;
TYPEX author;
bool isRoby()
{
return name == "roby";
};
};
BOOST_FUSION_ADAPT_STRUCT(Emp, name, author) // boost 1_59
//BOOST_FUSION_ADAPT_STRUCT(Emp, (std::string, name)(std::string, author)) // older boost
int main() {
using it = boost::spirit::ostream_iterator;
karma::rule<it, std::string()> quote;
karma::rule<it, TYPEX()> author;
karma::rule<it, Emp()> emp;
{
using namespace karma;
quote %= '"' << string << '"';
author = quote [ _1 = phx::ref(author2name)[ _val ] ];
emp %= delimit('\t')[ quote << author << bool_[ BIND? ] ];
}
Emp x { "one", AUTHOR2 };
author2name[AUTHOR2] = "TWO!";
std::cout << karma::format(emp, x);
}
我刚找到 Phoenix Lazy,但似乎不匹配。
假设您使 isRoby
const:
bool isRoby() const { return name == "roby"; };
您可以使用 phx::bind
:
bool_ [ _1 = phx::bind(&Emp::isRoby, _val) ]
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <map>
namespace karma = boost::spirit::karma;
namespace phx = boost::phoenix;
enum TYPEX { AUTHOR1, AUTHOR2, AUTHOR3, AUTHOR4 };
std::map<TYPEX, std::string> author2name;
struct Emp {
std::string name;
TYPEX author;
bool isRoby() const { return name == "roby"; };
};
BOOST_FUSION_ADAPT_STRUCT(Emp, name, author) // boost 1_59
// BOOST_FUSION_ADAPT_STRUCT(Emp, (std::string, name)(std::string, author)) // older boost
int main() {
using it = boost::spirit::ostream_iterator;
karma::rule<it, std::string()> quote;
karma::rule<it, TYPEX()> author;
karma::rule<it, Emp()> emp;
{
using namespace karma;
quote %= '"' << string << '"';
author = quote[_1 = phx::ref(author2name)[_val]];
emp %= delimit('\t')[ quote << author << bool_ [ _1 = phx::bind(&Emp::isRoby, _val) ] ];
}
Emp x{ "one", AUTHOR2 };
author2name[AUTHOR2] = "TWO!";
std::cout << karma::format(emp, x);
}