MADNESS  version 0.9
typestuff.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 
36 #ifndef MADNESS_WORLD_TYPESTUFF_H__INCLUDED
37 #define MADNESS_WORLD_TYPESTUFF_H__INCLUDED
38 
41 
42 #include <cstddef>
43 #include <stdint.h>
44 #include <madness/madness_config.h>
45 
46 // Next 3 use TR1 or BOOST if available, otherwise a minimal
47 // version we support ... in the std:: namespace
49 
50 // Boost-like enable if stuff in the madness:: namespace
52 
53 // Boost-like stuff in the madnes::tr1::details namespace
55 
56 namespace madness {
57 
59  template <class A, class B>
60  struct is_derived_from {
61  typedef char yes;
62  typedef int no;
63  static no f(...);
64  static yes f(B*);
65  static const bool value = (sizeof(f((A*)0)) == sizeof(yes));
66  };
67 
69  template <class A>
70  struct is_derived_from<A,A> : public std::false_type {};
71 
72  template <typename> class Future;
73  template <typename> struct remove_future;
74 
75  // Remove Future, const, volatile, and reference qualifiers from the type
76  template <typename T>
77  struct remove_fcvr {
78  typedef typename remove_future<typename std::remove_cv<
80  };
81 
83  template <typename T>
84  struct is_serializable {
86  };
87 
89  template <class T, typename resultT>
91  private:
92  T* t_;
93  resultT(T::*op_)();
94  public:
95  BindNullaryMemFun(T* t, resultT(T::*op)()) : t_(t), op_(op) {}
96  resultT operator()() {
97  return (t_->*op_)();
98  }
99  };
100 
102  template <class T>
103  class BindNullaryMemFun<T,void> {
104  private:
105  T* t_;
106  void (T::*op_)();
107  public:
108  BindNullaryMemFun(T* t, void (T::*op)()) : t_(t), op_(op) {}
109  void operator()() {
110  (t_->*op_)();
111  }
112  };
113 
114 
116  template <class T, typename resultT>
118  private:
119  const T* t_;
120  resultT(T::*op_)() const;
121  public:
122  BindNullaryConstMemFun(const T* t, resultT(T::*op)() const) : t_(t), op_(op) {}
123  resultT operator()() const {
124  return (t_->*op_)();
125  }
126  };
127 
129  template <class T>
130  class BindNullaryConstMemFun<T,void> {
131  private:
132  const T* t_;
133  void (T::*op_)() const;
134  public:
135  BindNullaryConstMemFun(const T* t, void (T::*op)() const) : t_(t), op_(op) {}
136  void operator()() const {
137  (t_->*op_)();
138  }
139  };
140 
142  template <class T, typename resultT>
145  }
146 
148  template <class T, typename resultT>
149  inline BindNullaryConstMemFun<T,resultT> bind_nullary_mem_fun(const T* t, resultT(T::*op)() const) {
151  }
152 
154  struct Void {};
155 
157  static const Void None = Void();
158 
160  template <typename T>
161  struct ReturnWrapper {
162  typedef T type;
163  };
164 
166  template <>
167  struct ReturnWrapper<void> {
168  typedef Void type;
169  };
170 
171 
173  template <typename T> class Reference {
174  T* p;
175  public:
176  Reference(T* x) : p(x) {}
177  T& operator*() const {return *p;}
178  T* operator->() const {return p;}
179  };
180 
181  template <typename T>
183  // yes and no are guaranteed to have different sizes,
184  // specifically sizeof(yes) == 1 and sizeof(no) == 2
185  typedef char yes[1];
186  typedef char no[2];
187 
188  template <typename C>
189  static yes& test(typename C::result_type*);
190 
191  template <typename>
192  static no& test(...);
193 
194  // if the sizeof the result of calling test<T>(0) is equal to the sizeof(yes),
195  // the first overload worked and T has a nested type named type.
196  static const bool value = sizeof(test<T>(0)) == sizeof(yes);
197  };
198 
199 
200  /* Macros to make some of this stuff more readable */
201 
256 #define REMREF(TYPE) typename std::remove_reference< TYPE >::type
257 #define REMCONST(TYPE) typename std::remove_const< TYPE >::type
258 #define REMCONSTX(TYPE) std::remove_const< TYPE >::type
259 #define RETURN_WRAPPERT(TYPE) typename madness::ReturnWrapper< TYPE >::type
260 
261 #define MEMFUN_RETURNT(MEMFUN) typename madness::detail::memfunc_traits< MEMFUN >::result_type
262 #define MEMFUN_CONSTNESS(MEMFUN) madness::detail::memfunc_traits< MEMFUN >::constness
263 #define MEMFUN_OBJT(MEMFUN) typename madness::detail::memfunc_traits< MEMFUN >::obj_type
264 #define MEMFUN_ARITY(MEMFUN) madness::detail::memfunc_traits< MEMFUN >::arity
265 #define MEMFUN_ARG1T(MEMFUN) typename madness::detail::memfunc_traits< MEMFUN >::arg1_type
266 #define MEMFUN_ARG2T(MEMFUN) typename madness::detail::memfunc_traits< MEMFUN >::arg2_type
267 #define MEMFUN_ARG3T(MEMFUN) typename madness::detail::memfunc_traits< MEMFUN >::arg3_type
268 #define MEMFUN_ARG4T(MEMFUN) typename madness::detail::memfunc_traits< MEMFUN >::arg4_type
269 
270 #define FUNCTION_RETURNT(FUNCTION) typename madness::detail::function_traits< FUNCTION >::result_type
271 #define FUNCTION_ARITY(FUNCTION) madness::detail::function_traits< FUNCTION >::arity
272 #define FUNCTION_ARG1T(FUNCTION) typename madness::detail::function_traits< FUNCTION >::arg1_type
273 #define FUNCTION_ARG2T(FUNCTION) typename madness::detail::function_traits< FUNCTION >::arg2_type
274 #define FUNCTION_ARG3T(FUNCTION) typename madness::detail::function_traits< FUNCTION >::arg3_type
275 #define FUNCTION_ARG4T(FUNCTION) typename madness::detail::function_traits< FUNCTION >::arg4_type
276 
277 #define RESULT_OF(FUNCTION) typename madness::detail::result_of< FUNCTION >::type
278 
279 #define IS_SAME(A, B) std::is_same< A, B >
280 #define IS_EQ(A, B) std::is_eq< A, B >
281 
282 } // end of namespace madness
283 #endif // MADNESS_WORLD_TYPESTUFF_H__INCLUDED
Tensor< double > B
Definition: tdse1d.cc:167
Boost-type-trait-like mapping of Future to T.
Definition: typestuff.h:73
This defines stuff that is serialiable by default rules ... basically anything contiguous.
Definition: typestuff.h:84
Simple binder for const member functions with no arguments.
Definition: typestuff.h:117
T & operator*() const
Definition: typestuff.h:177
resultT operator()()
Definition: typestuff.h:96
T type
Definition: type_traits_bits.h:244
BindNullaryMemFun(T *t, resultT(T::*op)())
Definition: typestuff.h:95
static yes & test(typename C::result_type *)
Reference(T *x)
Definition: typestuff.h:176
Void type
Definition: typestuff.h:168
char yes[1]
Definition: typestuff.h:185
remove_future< typename std::remove_cv< typename std::remove_reference< T >::type >::type >::type type
Definition: typestuff.h:79
Definition: typestuff.h:182
is_fundamental::value will be true if T is a fundamental type
Definition: type_traits_bits.h:110
int no
Definition: typestuff.h:62
bool_constant< false > false_type
Definition: gtest-port.h:1617
BindNullaryMemFun< T, resultT > bind_nullary_mem_fun(T *t, resultT(T::*op)())
Factory function for BindNullaryMemFun.
Definition: typestuff.h:143
Used to provide rvalue references to support move semantics.
Definition: typestuff.h:173
void operator()()
Definition: typestuff.h:109
Wrapper so that can return something even if returning void.
Definition: typestuff.h:161
const T1 &f1 return GTEST_2_TUPLE_() T(f0, f1)
BindNullaryMemFun(T *t, void(T::*op)())
Definition: typestuff.h:108
Simple binder for member functions with no arguments.
Definition: typestuff.h:90
T type
Definition: typestuff.h:162
Definition: typestuff.h:77
static const bool value
Definition: typestuff.h:85
void operator()() const
Definition: typestuff.h:136
A future is a possibly yet unevaluated value.
Definition: ref.h:210
A type you can return when you want to return void ... use "return None".
Definition: typestuff.h:154
True if A is derived from B and is not B.
Definition: typestuff.h:60
Tensor< double > op(const Tensor< double > &x)
Definition: kain.cc:508
remove_cv::type or remove_cv::type will be T
Definition: type_traits_bits.h:221
is_function::value is true if T is a function pointer
Definition: type_traits_bits.h:85
BindNullaryConstMemFun(const T *t, void(T::*op)() const)
Definition: typestuff.h:135
static const bool value
Definition: typestuff.h:65
resultT operator()() const
Definition: typestuff.h:123
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45
char yes
Definition: typestuff.h:61
BindNullaryConstMemFun(const T *t, resultT(T::*op)() const)
Definition: typestuff.h:122
is_member_function_pointer::value is true if T is a member function pointer
Definition: type_traits_bits.h:91
T * operator->() const
Definition: typestuff.h:178