Boost.Proto

  • Boost.Xpressiveに含まれ、Expression Templateの実装を支援する
  • 似たようなライブラリと思われるもの・・・PETE, Daixtrose


[]#include[] []<boost/assert.hpp>[]
[]#include[] []<boost/xpressive/detail/proto/proto.hpp>[]
[]#include[] []<boost/xpressive/detail/proto/compiler/fold.hpp>[]

[]namespace[] []proto[][] = [][]boost[][]::[][]proto[][];[]

[]struct[] []poost_tag[][] { };[]

[]struct[] []complex_visitor[]
[]{[]
[]int[] []m_real[][], [][]m_imag[][];[]

[]complex_visitor[][]() : [][]m_real[][]([][]0[][]), [][]m_imag[][]([][]0[][])[]
[] { }[]

[]template[][]< [][]class[] []ComplexT[][] >[]
[]struct[] []apply[]
[] {[]
[]typedef[] []int[] []type[][]; [][]// int is dummy, don't care[]
[] };[]

[]template[][]< [][]class[] []ComplexT[][] >[]
[]static[] []int[] []call[][]([][]ComplexT[] []const[][]& [][]x[][])[]
[] {[]
[]m_real[][] += [][]x[][].[][]m_real[][];[]
[]m_imag[][] += [][]x[][].[][]m_imag[][];[]
[]return[] []0[][];[]
[] }[]
[]};[]

[]struct[] []zero_t[]
[]{[]
[]int[] []m_real[][], [][]m_imag[][];[]

[]zero_t[][]() : [][]m_real[][]([][]0[][]), [][]m_imag[][]([][]0[][])[]
[] { }[]
[]};[]

[]// trigger proto-overloadings[]
[]// xpressive/detail/regex_operators.hpp shows how to remove this.[]
[]proto[][]::[][]unary_op[][]<[][]zero_t[][], [][]proto[][]::[][]noop_tag[][]> [][]const[] []zero[][];[]

[]struct[] []complex_t[]
[]{[]
[]int[] []m_real[][], [][]m_imag[][];[]

[]template[][]< [][]class[] []OpT[][] >[]
[]complex_t[][]([][]OpT[] []const[][]& [][]op[][])[]
[] {[]
[]complex_visitor[] []vtor[][];[]
[]proto[][]::[][]compile[][]([][]op[][], [][]zero[][], [][]vtor[][], [][]poost_tag[][]());[]
[]m_real[][] = [][]vtor[][].[][]m_real[][];[]
[]m_imag[][] = [][]vtor[][].[][]m_imag[][];[]
[] }[]

[]complex_t[][]([][]int[] []real[][], [][]int[] []imag[][]) :[]
[]m_real[][]([][]real[][]), [][]m_imag[][]([][]imag[][])[]
[] { }[]
[]};[]

[]// compiler specializations[]
[]namespace[] []boost[][] { [][]namespace[] []proto[][] {[]

[]template[][]<>[]
[]struct[] []compiler[][]<[][]add_tag[][], [][]poost_tag[][]>[]
[] : [][]fold_compiler[][]<[][]add_tag[][], [][]poost_tag[][]>[]
[] { };[]

[]template[][]<>[]
[]struct[] []compiler[][]<[][]noop_tag[][], [][]poost_tag[][]>[]
[] {[]
[]template[][]< [][]class[] []OpT[][], [][]class[] []StateT[][], [][]class[] []VisitorT[][] >[]
[]struct[] []apply[]
[] {[]
[]typedef[] []int[] []type[][]; [][]// also dummy[]
[] };[]

[]template[][]< [][]class[] []OpT[][], [][]class[] []StateT[][], [][]class[] []VisitorT[][] >[]
[]static[] []int[] []call[][]([][]OpT[] []const[][]& [][]op[][], [][]StateT[] []const[][]&, [][]VisitorT[][]& [][]vtor[][])[]
[] {[]
[]vtor[][].[][]m_real[][] += [][]proto[][]::[][]arg[][]([][]op[][]).[][]m_real[][];[]
[]vtor[][].[][]m_imag[][] += [][]proto[][]::[][]arg[][]([][]op[][]).[][]m_imag[][];[]
[]return[] []0[][];[]
[] }[]
[] };[]

[]} } [][]// namespace boost::proto[]

[]int[] []main[][]()[]
[]{[]
[]complex_t[] []x[][]( [][]zero[][] + [][]complex_t[][]([][]1[][],[][]2[][]) + [][]complex_t[][]([][]3[][],[][]4[][]) + [][]complex_t[][]([][]5[][],[][]6[][]) );[]

[]BOOST_ASSERT[][]( [][]x[][].[][]m_real[][] == [][]9[][] && [][]x[][].[][]m_imag[][] == [][]12[][] );[]
[]return[] []0[][];[]
[]}[]