Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_OrdinalTraits.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 // Kris
43 // 07.08.03 -- Move into Teuchos package/namespace
44 
45 #ifndef _TEUCHOS_ORDINALTRAITS_HPP_
46 #define _TEUCHOS_ORDINALTRAITS_HPP_
47 
52 #include "Teuchos_ConfigDefs.hpp"
53 #include <limits>
54 
68 /* This is the default structure used by OrdinalTraits<T> to produce a compile time
69  error when the specialization does not exist for type <tt>T</tt>.
70 */
71 namespace Teuchos {
72 
73 template<class T>
74 struct UndefinedOrdinalTraits
75 {
77  static inline T notDefined() { return T::this_type_is_missing_a_specialization(); }
78 };
79 
80 template<class T>
81 struct OrdinalTraits {
82 
84  static const bool hasMachineParameters = false;
85 
87  static inline T zero() { return UndefinedOrdinalTraits<T>::notDefined(); }
88 
90  static inline T one() { return UndefinedOrdinalTraits<T>::notDefined(); }
91 
93 
96  static inline T max() { return UndefinedOrdinalTraits<T>::notDefined(); }
97 
99  static inline T invalid() { return UndefinedOrdinalTraits<T>::notDefined(); }
100 
102  static inline std::string name() { return UndefinedOrdinalTraits<T>::notDefined(); }
103 };
104 
105 #ifndef DOXYGEN_SHOULD_SKIP_THIS
106 
107 template<>
108 struct OrdinalTraits<char> {
109  static const bool hasMachineParameters = false;
110  static inline char zero() {return(0);}
111  static inline char one() {return(1);}
112  static inline char invalid() {return(std::numeric_limits<char>::max());}
113  static inline char max() {return(std::numeric_limits<char>::max()-one());}
114  static inline std::string name() {return("char");}
115 };
116 
117 template<>
118 struct OrdinalTraits<short int> {
119  static const bool hasMachineParameters = false;
120  static inline short int zero() {return(0);}
121  static inline short int one() {return(1);}
122  static inline short int invalid() {return(-1);}
123  static inline short int max() {return(std::numeric_limits<short int>::max());}
124  static inline std::string name() {return("short int");}
125 };
126 
127 template<>
128 struct OrdinalTraits<int> {
129  static const bool hasMachineParameters = false;
130  static inline int zero() {return(0);}
131  static inline int one() {return(1);}
132  static inline int invalid() {return(-1);}
133  static inline int max() {return(std::numeric_limits<int>::max());}
134  static inline std::string name() {return("int");}
135 };
136 
137 template<>
138 struct OrdinalTraits<unsigned int> {
139  static const bool hasMachineParameters = false;
140  static inline unsigned int zero() {return(0);}
141  static inline unsigned int one() {return(1);}
142  static inline unsigned int invalid() {return(std::numeric_limits<unsigned int>::max());}
143  static inline unsigned int max() {return(std::numeric_limits<unsigned int>::max()-1);}
144  static inline std::string name() {return("unsigned int");}
145 };
146 
147 template<>
148 struct OrdinalTraits<long int> {
149  static const bool hasMachineParameters = false;
150  static inline long int zero() {return(static_cast<long int>(0));}
151  static inline long int one() {return(static_cast<long int>(1));}
152  static inline long int invalid() {return(static_cast<long int>(-1));}
153  static inline long int max() {return(std::numeric_limits<long int>::max());}
154  static inline std::string name() {return("long int");}
155 };
156 
157 template<>
158 struct OrdinalTraits<long unsigned int> {
159  static const bool hasMachineParameters = false;
160  static inline long unsigned int zero() {return(static_cast<long unsigned int>(0));}
161  static inline long unsigned int one() {return(static_cast<long unsigned int>(1));}
162  static inline long unsigned int invalid() {return(std::numeric_limits<long unsigned int>::max());}
163  static inline long unsigned int max() {return(std::numeric_limits<long unsigned int>::max()-1);}
164  static inline std::string name() {return("long unsigned int");}
165 };
166 
167 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
168 template<>
169 struct OrdinalTraits<long long int> {
170  static const bool hasMachineParameters = false;
171  static inline long long int zero() {return(static_cast<long long int>(0));}
172  static inline long long int one() {return(static_cast<long long int>(1));}
173  static inline long long int invalid() {return(static_cast<long long int>(-1));}
174  static inline long long int max() {return(LLONG_MAX);}
175  static inline std::string name() {return("long long int");}
176 };
177 
178 template<>
179 struct OrdinalTraits<unsigned long long int> {
180  static const bool hasMachineParameters = false;
181  static inline unsigned long long int zero() {return(static_cast<unsigned long long int>(0));}
182  static inline unsigned long long int one() {return(static_cast<unsigned long long int>(1));}
183  static inline unsigned long long int invalid() {return(std::numeric_limits<unsigned long long int>::max());}
184  static inline unsigned long long int max() {return(std::numeric_limits<unsigned long long int>::max()-1);}
185  static inline std::string name() {return("unsigned long long int");}
186 };
187 #endif // HAVE_TEUCHOS_LONG_LONG_INT
188 
189 #ifdef HAVE_TEUCHOS___INT64
190 
191 template<>
192 struct OrdinalTraits<__int64> {
193  static const bool hasMachineParameters = false;
194  static inline __int64 zero() {return(static_cast<__int64>(0));}
195  static inline __int64 one() {return(static_cast<__int64>(1));}
196  static inline __int64 invalid() {return(std::numeric_limits<__int64>::max());}
197  static inline __int64 max() {return(std::numeric_limits<__int64>::max()-1);}
198  static inline std::string name() {return("__int64");}
199 };
200 
201 template<>
202 struct OrdinalTraits<unsigned __int64> {
203  static const bool hasMachineParameters = false;
204  static inline unsigned __int64 zero() {return(static_cast<unsigned __int64>(0));}
205  static inline unsigned __int64 one() {return(static_cast<unsigned __int64>(1));}
206  static inline unsigned __int64 invalid() {return(std::numeric_limits<unsigned __int64>::max());}
207  static inline unsigned __int64 max() {return(std::numeric_limits<unsigned __int64>::max()-1);}
208  static inline std::string name() {return("unsigned __int64");}
209 };
210 
211 #endif // HAVE_TEUCHOS___INT64
212 
213 #endif // DOXYGEN_SHOULD_SKIP_THIS
214 
215 } // namespace Teuchos
216 
217 #endif // _TEUCHOS_ORDINALTRAITS_HPP_
static T one()
Returns representation of one for this ordinal type.
static T zero()
Returns representation of zero for this ordinal type.
static T invalid()
Returns a value designating an invalid number. For signed types, this is typically negative one; for ...
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
static T max()
Returns a value designating the maximum value accessible by code using OrdinalTraits.
This structure defines some basic traits for the ordinal field type.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
static std::string name()
Returns name of this ordinal type.