SpatialOps
NeboTypeInference.h
1 /*
2  * Copyright (c) 2014 The University of Utah
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */
22 
23 #ifndef NEBO_TYPE_INFERENCE_H
24 #define NEBO_TYPE_INFERENCE_H
25 namespace SpatialOps {
26 
27  template<typename SubExpr, typename FieldType>
28  inline SubExpr normalize(NeboExpression<SubExpr, FieldType> arg) {
29  return arg.expr();
30  }
31 
32  inline NeboScalar<Initial, double> normalize(double arg) {
33  NeboScalar<Initial, double> typedef ReturnType;
34  return ReturnType(arg);
35  }
36 
37  template<typename FieldType>
38  inline NeboConstField<Initial, FieldType> normalize(FieldType const & arg) {
39  NeboConstField<Initial, FieldType> typedef ReturnType;
40  return ReturnType(arg);
41  }
42 
43  template<typename Arg>
44  struct FinalType {
45  NeboConstField<Initial, Arg> typedef Result;
46  };
47 
48  template<>
49  struct FinalType<double> {
50  NeboScalar<Initial, double> typedef Result;
51  };
52 
53  template<typename SubExpr, typename FieldType>
54  struct FinalType<NeboExpression<SubExpr, FieldType> > {
55  SubExpr typedef Result;
56  };
57 
58  template<typename ValueType>
60  typedef ValueType value_type;
61  };
62 
63  template<typename Arg>
64  struct FindFieldType {
65  Arg typedef Result;
66  };
67 
68  template<>
69  struct FindFieldType<double> {
70  GenericFieldType<double> typedef Result;
71  };
72 
73  template<typename SubExpr, typename FieldType>
74  struct FindFieldType<NeboExpression<SubExpr, FieldType> > {
75  FieldType typedef Result;
76  };
77 
78  // Need something that only compiles if value types match, but returns the (non-generic) field type
79  template<typename ValueType1, typename ValueType2, typename FieldType>
81 
82  template<typename ValueType, typename FieldType>
83  struct ValueTypeCheck<ValueType, ValueType, FieldType> {
84  FieldType typedef Result;
85  };
86 
87  template<typename FieldType1, typename FieldType2>
89 
90  // Covers Generic + Generic (if matching value_types) and Type + Type
91  template<typename FieldType>
92  struct RefineFieldType<FieldType, FieldType> {
93  FieldType typedef Result;
94  };
95 
96  template<typename ValueType>
97  struct RefineFieldType<GenericFieldType<ValueType>, GenericFieldType<ValueType> > {
98  GenericFieldType<ValueType> typedef Result;
99  };
100 
101  template<typename FieldType, typename ValueType>
102  struct RefineFieldType<GenericFieldType<ValueType>, FieldType> {
104  };
105 
106  template<typename FieldType, typename ValueType>
107  struct RefineFieldType<FieldType, GenericFieldType<ValueType> > {
109  };
110 
111 } /* SpatialOps */
112 
113 #endif
114 /* NEBO_TYPE_INFERENCE_H */