MADNESS  version 0.9
ref.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2007,2010 Oak Ridge National Laboratory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 
31 
32  $Id$
33 */
34 
35 #ifndef MADNESS_WORLD_REF_H__INCLUDED
36 #define MADNESS_WORLD_REF_H__INCLUDED
37 
38 #include <madness/world/worldexc.h>
40 
41 // ref.hpp - ref/cref, useful helper functions
42 //
43 // Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
44 // Copyright (C) 2001, 2002 Peter Dimov
45 // Copyright (C) 2002 David Abrahams
46 //
47 // Distributed under the Boost Software License, Version 1.0. (See
48 // accompanying file LICENSE_1_0.txt or copy at
49 // http://www.boost.org/LICENSE_1_0.txt)
50 //
51 // See http://www.boost.org/libs/bind/ref.html for documentation.
52 //
53 // Ported by JAC into madness 6/27/2012
54 
55 namespace madness {
56  namespace detail {
57 
59 
68  template<class T>
70  private:
71  T* t_;
72 
73  public:
74  typedef T type;
75 
77 
79  ReferenceWrapper() : t_(NULL) { }
80 
82 
84  explicit ReferenceWrapper(T& t) : t_(&t) { }
85 
86  // Compiler generated copy constructor and assignment operator are OK here
87 
89 
92  T& get() const {
93  MADNESS_ASSERT(t_);
94  return *t_;
95  }
96 
98 
102  operator T& () const { return get(); }
103 
105 
108  T* get_pointer() const { return t_; }
109 
111 
116  template <typename Archive>
117  void serialize(const Archive&) {
118  MADNESS_EXCEPTION("ReferenceWrapper serialization not supported.", 0);
119  }
120 
121  }; // class ReferenceWrapper
122 
123  } // namespace detail
124 
125 
127 
131  template<class T>
133 
135 
139  template<class T>
141 
142 
144 
146  template<typename T>
148 
150  template<typename T>
152  public:
153  typedef T type;
154  }; // class UnwrapReference
155 
156  template<typename T>
157  class is_reference_wrapper<detail::ReferenceWrapper<T> > : public std::true_type { };
158 
159  template<typename T>
160  class is_reference_wrapper<detail::ReferenceWrapper<T> const> : public std::true_type { };
161 
162  template<typename T>
163  class is_reference_wrapper<detail::ReferenceWrapper<T> volatile> : public std::true_type { };
164 
165  template<typename T>
166  class is_reference_wrapper<detail::ReferenceWrapper<T> const volatile> : public std::true_type { };
167 
168  template<typename T>
169  class UnwrapReference<detail::ReferenceWrapper<T> > {
170  public:
171  typedef T type;
172  }; // class UnwrapReference<ReferenceWrapper<T> >
173 
174  template<typename T>
175  class UnwrapReference<detail::ReferenceWrapper<T> const> {
176  public:
177  typedef T type;
178  }; // class UnwrapReference<ReferenceWrapper<T> const>
179 
180  template<typename T>
181  class UnwrapReference<detail::ReferenceWrapper<T> volatile> {
182  public:
183  typedef T type;
184  }; // class UnwrapReference<ReferenceWrapper<T> volatile>
185 
186  template<typename T>
187  class UnwrapReference<detail::ReferenceWrapper<T> const volatile> {
188  public:
189  typedef T type;
190  }; // class UnwrapReference<ReferenceWrapper<T> const volatile>
191 
192 
194 
198  template <class T>
199  inline typename UnwrapReference<T>::type& unwrap_ref(T& t) { return t; }
200 
202 
206  template<class T>
207  inline T* get_pointer(const detail::ReferenceWrapper<T>& r ) { return r.get_pointer(); }
208 
209  // Forward declairation
210  template <typename> class Future;
211 
213 
217  template <typename T>
218  class Future<detail::ReferenceWrapper<T> > {
219  private:
221 
222  // Not needed
226 
227  public:
229 
232  ref_(ref)
233  { }
234 
235  // Not needed right now. Uncomment these lines if that changes.
236 // Future() : ref_() { }
237 // Future(const Future<detail::ReferenceWrapper<T> >& other) :
238 // ref_(other.ref_)
239 // { }
240 // Future<detail::ReferenceWrapper<T> >&
241 // operator=(const Future<detail::ReferenceWrapper<T> >& other) {
242 // ref_ = other.ref_;
243 // return *this;
244 // }
245 
247 
249  bool probe() const { return true; }
250 
252 
257  callback->notify();
258  }
259 
261  operator T&() const { return ref_.get(); }
262 
263  }; // class Future<detail::ReferenceWrapper<T> >
264 
265 } // namespace madness
266 
267 #endif // MADNESS_WORLD_REF_H__INCLUDED
Future(const detail::ReferenceWrapper< T > &ref)
Constructor.
Definition: ref.h:231
Future< T > & operator=(const Future< T > &other)
Assignment future = future makes a shallow copy just like copy constructor.
Definition: worldfut.h:448
detail::ReferenceWrapper< T > const ref(T &t)
Reference wrapper factory function.
Definition: ref.h:132
This class used for callbacks (e.g., for dependency tracking)
Definition: worlddep.h:51
void serialize(const Archive &)
Serialization.
Definition: ref.h:117
bool_constant< true > true_type
Definition: gtest-port.h:1618
T * get_pointer() const
Obect pointer accessor.
Definition: ref.h:108
bool_constant< false > false_type
Definition: gtest-port.h:1617
Reference wrapper class.
Definition: ref.h:69
ReferenceWrapper(T &t)
Constructor.
Definition: ref.h:84
const T1 &f1 return GTEST_2_TUPLE_() T(f0, f1)
Type trait for reference wrapper.
Definition: ref.h:147
void register_callback(CallbackInterface *callback)
Register a callback.
Definition: ref.h:256
ReferenceWrapper type trait accessor
Definition: ref.h:151
Future()
Makes an unassigned future.
Definition: worldfut.h:397
detail::ReferenceWrapper< const T > const cref(const T &t)
Constant reference wrapper factory function.
Definition: ref.h:140
UnwrapReference< T >::type & unwrap_ref(T &t)
Function for retreaving the referenced object.
Definition: ref.h:199
ReferenceWrapper()
Default constructor.
Definition: ref.h:79
bool probe() const
Probe set state of the future.
Definition: ref.h:249
T & get() const
Reference accessor.
Definition: ref.h:92
A future is a possibly yet unevaluated value.
Definition: ref.h:210
Implements MadnessException.
#define MADNESS_EXCEPTION(msg, value)
Definition: worldexc.h:88
T type
The reference type.
Definition: ref.h:74
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45
T * get_pointer(const detail::ReferenceWrapper< T > &r)
Function for retreaving a pointer to the referenced object.
Definition: ref.h:207
T type
The reference type.
Definition: ref.h:153
virtual void notify()=0