Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
spe/binary.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_FUN_SPE_BINARY_HH
27 # define MLN_FUN_SPE_BINARY_HH
28 
29 # include <mln/core/concept/function.hh>
30 # include <mln/trait/next/solve.hh>
31 # include <mln/trait/functions.hh>
32 
34 
35 namespace mln
36 {
37 
38  namespace fun
39  {
40 
41  namespace spe
42  {
43 
44  // Forward declaration
45  template <typename Fun, typename T1, typename T2>
46  struct binary;
47 
48  namespace impl
49  {
50 
51  template <bool has_param, typename Fun, typename T1, typename T2>
52  struct binary_impl;
53 
54  template <typename Fun, typename T1, typename T2>
55  struct binary_impl<false, Fun, T1, T2>
56  : mln::Function_v2v< binary<Fun, T1, T2> >
57  {
58  typedef Fun flag;
59  typedef mln_trait_nbinary(flag, T1, T2) def;
60 
61  typedef typename def::argument1 argument1;
62  typedef typename def::argument2 argument2;
63  typedef typename def::result result;
64 
65  result operator () (const argument1& a, const argument2& b) const
66  {
67  return def::read(a, b);
68  }
69 
70 
71  template <typename U>
72  void init(const U& value)
73  {
74  (void) value;
75  }
76 
77  };
78 
79  template <typename Fun, typename T1, typename T2>
80  struct binary_impl<true, Fun, T1, T2>
81  : mln::Function_v2v< binary<Fun, T1, T2> >
82  {
83  typedef Fun flag;
84  typedef mln_trait_nbinary(flag, T1, T2) def;
85 
86  typedef typename def::argument1 argument1;
87  typedef typename def::argument2 argument2;
88  typedef typename def::result result;
89 
90  typedef mln_trait_fun_param(def) param;
91  typedef mln_trait_fun_storage(def) storage;
92 
93  result operator () (const argument1& a, const argument2& b) const
94  {
95  return def::read(state_, a, b);
96  }
97 
98  template <typename U>
99  void init(const U& value)
100  {
101  state_ = mln::trait::function::internal::introspect::has_storage_t<def, void>::compute(value);
102  }
103 
104  stored<storage>& state()
105  {
106  return state_;
107  }
108 
109  const stored<storage>& state() const
110  {
111  return state_;
112  }
113 
114  protected:
115  mln::fun::stored<storage> state_;
116  };
117 
118  } // end of namespace mln::fun::spe::impl
119 
120  template <typename Fun, typename T1, typename T2>
121  struct binary
122  : impl::binary_impl<mln_trait_fun_is_parametrable_(Fun)::value, Fun, T1, T2>
123  {
124  typedef impl::binary_impl<mln_trait_fun_is_parametrable_(Fun)::value, Fun, T1, T2> super;
125 
126  binary()
127  {
128  }
129 
130  template <typename U>
131  binary(const U& param)
132  {
133  this->super::init(param);
134  }
135 
136  using super::operator();
137  };
138 
139  } // end of namespace mln::fun::spe
140 
141  } // end of namespace mln::fun
142 
143 } // end of namespace mln
144 
145 #endif // ! MLN_FUN_SPE_BINARY_HH