Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
pseudo_site.hh
1 // Copyright (C) 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_CORE_CONCEPT_PSEUDO_SITE_HH
27 # define MLN_CORE_CONCEPT_PSEUDO_SITE_HH
28 
36 
37 # include <mln/core/concept/site_proxy.hh>
38 # include <mln/metal/is_a.hh>
39 
40 
41 
42 namespace mln
43 {
44 
45  // Fwd decl.
46  template <typename E> struct Pseudo_Site;
47 
48 
49 
51  template <>
52  struct Pseudo_Site<void>
53  {
54  typedef Site_Proxy<void> super;
55  };
56 
57 
63  template <typename E>
64  struct Pseudo_Site : public Site_Proxy<E>
65  {
66  typedef Pseudo_Site<void> category;
67 
68  // typedef target;
69 
70  // void change_target(const target& new_target);
71  // const target* target_() const; // Hook to the target.
72 
73  bool has_target() const;
74 
75  protected:
76  Pseudo_Site();
77  };
78 
79 
80  namespace if_possible
81  {
82  // Nota: This procedure is used in
83  // internal::site_set_iterator_base and
84  // site_relative_iterator_base.
85 
86  template <typename O, typename T>
87  void change_target(Object<O>&, const T&);
88 
89  } // end of namespace mln::if_possible
90 
91 
92 # ifndef MLN_INCLUDE_ONLY
93 
94  template <typename E>
95  inline
96  Pseudo_Site<E>::Pseudo_Site()
97  {
98  typedef mln_target(E) target;
99 
100  void (E::*m1)(const target&) = & E::change_target;
101  m1 = 0;
102  const target* (E::*m2)() const = & E::target_;
103  m2 = 0;
104  }
105 
106  template <typename E>
107  inline
108  bool
109  Pseudo_Site<E>::has_target() const
110  {
111  return exact(this)->target_() != 0;
112  }
113 
114 
115 
116  namespace if_possible
117  {
118 
119  namespace internal
120  {
121 
122  template <bool b> struct helper;
123 
124  template <>
125  struct helper< /* is an Object */ true >
126  {
127 
128  template <typename P>
129  void change_target(Pseudo_Site<P>& p,
130  const mln_target(P)& new_target) const
131  {
132  exact(p).change_target(new_target);
133  }
134 
135  template <typename O, typename D>
136  void change_target(Object<O>&, const D&) const
137  {
138  // No-op.
139  }
140 
141  };
142 
143  template <>
144  struct helper< /* NOT an Object */ false >
145  {
146  template <typename O, typename D>
147  void change_target(O&, const D&) const
148  {
149  // No-op.
150  }
151  };
152 
153  } // namespace mln::if_possible::internal
154 
155 
156  template <typename O, typename D>
157  void change_target(O& o, const D& d)
158  {
159  enum { is_object = mlc_is_a(O, Object)::value };
160  mln::if_possible::internal::helper< is_object >().change_target(exact(o),
161  d);
162  }
163 
164  } // end of namespace mln::if_possible
165 
166 # endif // ! MLN_INCLUDE_ONLY
167 
168 } // end of namespace mln
169 
170 
171 #endif // ! MLN_CORE_CONCEPT_PSEUDO_SITE_HH