Traits.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2004 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9// epsic/src/util/Traits.h
10
11/*
12 * For a useful discussion on traits, please see
13 *
14 * "Traits: a new and useful template technique" by Nathan C. Meyers
15 * C++ Report, June 1995 http://www.cantrip.org/traits.html
16 *
17 * These traits were implemented in order that data types such as Jones
18 * matrices and Quaternions can be passed to algorithms designed to work
19 * with scalar values like float and double.
20 *
21 * Willem van Straten - 26 October 2004
22 *
23 */
24
25#ifndef __Traits_h
26#define __Traits_h
27
28#include <complex>
29
31template< class E > struct ElementTraits
32{
33 typedef E real;
34
36 template< class T >
37 static inline E from_complex (const std::complex<T>& value)
38 { return value.real(); }
39
41 static inline double to_real (const E& element)
42 { return element; }
43
45 static inline E conjugate (const E& element)
46 { return element; }
47};
48
50template< class E > struct ElementTraits< std::complex<E> >
51{
52 typedef E real;
53
55 template< class T >
56 static inline std::complex<E> from_complex (const std::complex<T>& value)
57 { return value; }
58
60 static inline double to_real (const std::complex<E>& element)
61 { return element.real(); }
62
64 template< class T >
65 static inline std::complex<E> conjugate (const std::complex<T>& value)
66 { return std::conj(value); }
67};
68
70template< class T, class E = ElementTraits<T> > struct DatumTraits
71{
74
76 typedef T element_type;
77
78 static inline unsigned ndim () { return 1; }
79 static inline T& element (T& t, unsigned idim) { return t; }
80 static inline const T& element (const T& t, unsigned idim) { return t; }
81};
82
84template< class T >
85struct DatumTraits< std::complex<T> >
86{
87 ElementTraits<T> element_traits;
88 typedef T element_type;
89
90 static inline unsigned ndim () { return 2; }
91 static inline T& element (std::complex<T>& t, unsigned idim)
92 { return reinterpret_cast<T*>(&t)[idim]; }
93
94 static inline const T& element (const std::complex<T>& t, unsigned idim)
95 { return reinterpret_cast<const T*>(&t)[idim]; }
96};
97
98
100template<typename T>
101std::complex<T> ci (const std::complex<T>& c)
102{
103 return std::complex<T> (-c.imag(), c.real());
104}
105
107template<typename T>
108std::complex<T> ci (const T& real)
109{
110 return std::complex<T> (0.0, real);
111}
112
114template<typename T>
115T myconj (const T& x) { return ElementTraits<T>::conjugate(x); }
116
118template<typename T>
119typename ElementTraits<T>::real
120myreal (const T& x) { return ElementTraits<T>::to_real(x); }
121
122#endif
123
STL namespace.
E element_traits
Definition Traits.h:73
static double to_real(const E &element)
static E conjugate(const E &element)
static E from_complex(const std::complex< T > &value)

Generated using doxygen 1.14.0