Nektar++
OperatorGenerators.hpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: OperatorGenerators
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Generates global operators for classes based upon traits.
33 //
34 //
35 //
36 ///////////////////////////////////////////////////////////////////////////////
37 
38 #ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_OPERATOR_GENERATORS_HPP
39 #define NEKTAR_LIB_UTILITIES_BASIC_UTILS_OPERATOR_GENERATORS_HPP
40 
41 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
42 #include <boost/preprocessor/control/expr_if.hpp>
43 #include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
44 #include <boost/preprocessor/control/if.hpp>
45 #include <boost/preprocessor/logical/and.hpp>
46 #include <boost/preprocessor/logical/or.hpp>
47 #include <boost/preprocessor/comparison/greater.hpp>
48 #include <boost/preprocessor/repetition/enum_params.hpp>
49 #include <boost/preprocessor/stringize.hpp>
50 #include <boost/preprocessor/tuple/eat.hpp>
51 
52 #include <ExpressionTemplates/Operators.hpp>
53 
54 #ifdef NEKTAR_USE_EXPRESSION_TEMPLATES
55 #include <ExpressionTemplates/Node.hpp>
56 #endif //NEKTAR_USE_EXPRESSION_TEMPLATES
57 
58 namespace Nektar
59 {
60 
61  #define TEMPLATE() template<
62  #define CLOSE_TEMPLATE() >
63 
64  #define NEKTAR_TYPENAME() typename
65  #define SHOW_TYPENAME(LhsNumber, RhsNumber) \
66  BOOST_PP_IF( BOOST_PP_OR(BOOST_PP_GREATER(LhsNumber,0), BOOST_PP_GREATER(RhsNumber,0)), NEKTAR_TYPENAME, BOOST_PP_EMPTY)()
67 
68  #define PP_ENUM_TWO_SETS_OF_TYPES(LhsTypeName, LhsNumber, RhsTypeName, RhsNumber) \
69  BOOST_PP_IF( BOOST_PP_OR(BOOST_PP_GREATER(LhsNumber,0), BOOST_PP_GREATER(RhsNumber,0)), TEMPLATE, BOOST_PP_EMPTY)() \
70  BOOST_PP_ENUM_PARAMS(LhsNumber, typename LhsTypeName) \
71  BOOST_PP_COMMA_IF(BOOST_PP_AND(BOOST_PP_GREATER(LhsNumber,0), BOOST_PP_GREATER(RhsNumber,0))) \
72  BOOST_PP_ENUM_PARAMS(RhsNumber, typename RhsTypeName) \
73  BOOST_PP_IF( BOOST_PP_OR(BOOST_PP_GREATER(LhsNumber,0), BOOST_PP_GREATER(RhsNumber,0)), CLOSE_TEMPLATE, BOOST_PP_EMPTY)()
74 
75  #define GET_TEMPLATED_TYPE(TypeName, TemplateTypeName, Number) TypeName<BOOST_PP_ENUM_PARAMS(Number, TemplateTypeName)>
76 
77  #define GET_TYPE(TypeName, TemplateTypeName, Number) \
78  BOOST_PP_IF(BOOST_PP_GREATER(Number, 0), GET_TEMPLATED_TYPE, TypeName BOOST_PP_TUPLE_EAT(3))(TypeName, TemplateTypeName, Number)
79 
80  #define DECLARE_MULTIPLICATION_OPERATOR(LeftType, NumLeftParams, RightType, NumRightParams) \
81  PP_ENUM_TWO_SETS_OF_TYPES(LhsType, NumLeftParams, RhsType, NumRightParams) \
82  expt::Node<expt::Node<GET_TYPE(LeftType, LhsType, NumLeftParams) >, expt::MultiplyOp, expt::Node<GET_TYPE(RightType, RhsType, NumRightParams) > > \
83  operator*(const GET_TYPE(LeftType, LhsType, NumLeftParams)& lhs, \
84  const GET_TYPE(RightType, RhsType, NumRightParams)& rhs);
85 
86  #define DECLARE_DIVISION_OPERATOR(LeftType, NumLeftParams, RightType, NumRightParams) \
87  PP_ENUM_TWO_SETS_OF_TYPES(LhsType, NumLeftParams, RhsType, NumRightParams) \
88  expt::Node<expt::Node<GET_TYPE(LeftType, LhsType, NumLeftParams) >, expt::DivideOp, expt::Node<GET_TYPE(RightType, RhsType, NumRightParams) > > \
89  operator/(const GET_TYPE(LeftType, LhsType, NumLeftParams)& lhs, \
90  const GET_TYPE(RightType, RhsType, NumRightParams)& rhs);
91 
92  #define DECLARE_ADDITION_OPERATOR(LeftType, NumLeftParams, RightType, NumRightParams) \
93  PP_ENUM_TWO_SETS_OF_TYPES(LhsType, NumLeftParams, RhsType, NumRightParams) \
94  expt::Node<expt::Node<GET_TYPE(LeftType, LhsType, NumLeftParams) >, expt::AddOp, expt::Node<GET_TYPE(RightType, RhsType, NumRightParams) > > \
95  operator+(const GET_TYPE(LeftType, LhsType, NumLeftParams)& lhs, \
96  const GET_TYPE(RightType, RhsType, NumRightParams)& rhs);
97 
98  #define DECLARE_SUBTRACTION_OPERATOR(LeftType, NumLeftParams, RightType, NumRightParams) \
99  PP_ENUM_TWO_SETS_OF_TYPES(LhsType, NumLeftParams, RhsType, NumRightParams) \
100  expt::Node<expt::Node<GET_TYPE(LeftType, LhsType, NumLeftParams) >, expt::SubtractOp, expt::Node<GET_TYPE(RightType, RhsType, NumRightParams) > > \
101  operator-(const GET_TYPE(LeftType, LhsType, NumLeftParams)& lhs, \
102  const GET_TYPE(RightType, RhsType, NumRightParams)& rhs);
103 
104  #ifdef NEKTAR_USE_EXPRESSION_TEMPLATES
105  #define GENERATE_MULTIPLICATION_OPERATOR(LeftType, NumLeftParams, RightType, NumRightParams) \
106  PP_ENUM_TWO_SETS_OF_TYPES(LhsType, NumLeftParams, RhsType, NumRightParams) \
107  expt::Node<expt::Node<GET_TYPE(LeftType, LhsType, NumLeftParams) >, expt::MultiplyOp, expt::Node<GET_TYPE(RightType, RhsType, NumRightParams) > > \
108  operator*(const GET_TYPE(LeftType, LhsType, NumLeftParams)& lhs, \
109  const GET_TYPE(RightType, RhsType, NumRightParams)& rhs) \
110  { \
111  return expt::CreateBinaryExpression<expt::MultiplyOp>(lhs, rhs); \
112  }
113 
114  #define GENERATE_DIVISION_OPERATOR(LeftType, NumLeftParams, RightType, NumRightParams) \
115  PP_ENUM_TWO_SETS_OF_TYPES(LhsType, NumLeftParams, RhsType, NumRightParams) \
116  expt::Node<expt::Node<GET_TYPE(LeftType, LhsType, NumLeftParams) >, expt::DivideOp, expt::Node<GET_TYPE(RightType, RhsType, NumRightParams) > > \
117  operator/(const GET_TYPE(LeftType, LhsType, NumLeftParams)& lhs, \
118  const GET_TYPE(RightType, RhsType, NumRightParams)& rhs) \
119  { \
120  return expt::CreateBinaryExpression<expt::DivideOp>(lhs, rhs); \
121  }
122 
123  #define GENERATE_ADDITION_OPERATOR(LeftType, NumLeftParams, RightType, NumRightParams) \
124  PP_ENUM_TWO_SETS_OF_TYPES(LhsType, NumLeftParams, RhsType, NumRightParams) \
125  expt::Node<expt::Node<GET_TYPE(LeftType, LhsType, NumLeftParams) >, expt::AddOp, expt::Node<GET_TYPE(RightType, RhsType, NumRightParams) > > \
126  operator+(const GET_TYPE(LeftType, LhsType, NumLeftParams)& lhs, \
127  const GET_TYPE(RightType, RhsType, NumRightParams)& rhs) \
128  { \
129  return expt::CreateBinaryExpression<expt::AddOp>(lhs, rhs); \
130  }
131 
132  #define GENERATE_SUBTRACTION_OPERATOR(LeftType, NumLeftParams, RightType, NumRightParams) \
133  PP_ENUM_TWO_SETS_OF_TYPES(LhsType, NumLeftParams, RhsType, NumRightParams) \
134  expt::Node<expt::Node<GET_TYPE(LeftType, LhsType, NumLeftParams) >, expt::SubtractOp, expt::Node<GET_TYPE(RightType, RhsType, NumRightParams) > > \
135  operator-(const GET_TYPE(LeftType, LhsType, NumLeftParams)& lhs, \
136  const GET_TYPE(RightType, RhsType, NumRightParams)& rhs) \
137  { \
138  return expt::CreateBinaryExpression<expt::SubtractOp>(lhs, rhs); \
139  }
140 
141  #else //NEKTAR_USE_EXPRESSION_TEMPLATES
142 
143  #define GENERATE_MULTIPLICATION_OPERATOR(LeftType, NumLeftParams, RightType, NumRightParams) \
144  PP_ENUM_TWO_SETS_OF_TYPES(LhsType, NumLeftParams, RhsType, NumRightParams) \
145  SHOW_TYPENAME(NumLeftParams, NumRightParams) expt::MultiplyOp::ResultType<GET_TYPE(LeftType, LhsType, NumLeftParams), GET_TYPE(RightType, RhsType, NumRightParams)>::type \
146  operator*(const GET_TYPE(LeftType, LhsType, NumLeftParams)& lhs, \
147  const GET_TYPE(RightType, RhsType, NumRightParams)& rhs) \
148  { \
149  return Multiply(lhs, rhs); \
150  }
151 
152 
153  #define GENERATE_DIVISION_OPERATOR(LeftType, NumLeftParams, RightType, NumRightParams) \
154  PP_ENUM_TWO_SETS_OF_TYPES(LhsType, NumLeftParams, RhsType, NumRightParams) \
155  SHOW_TYPENAME(NumLeftParams, NumRightParams) expt::DivideOp::ResultType<GET_TYPE(LeftType, LhsType, NumLeftParams), GET_TYPE(RightType, RhsType, NumRightParams)>::type \
156  operator/(const GET_TYPE(LeftType, LhsType, NumLeftParams)& lhs, \
157  const GET_TYPE(RightType, RhsType, NumRightParams)& rhs) \
158  { \
159  return Divide(lhs, rhs); \
160  }
161 
162  #define GENERATE_ADDITION_OPERATOR(LeftType, NumLeftParams, RightType, NumRightParams) \
163  PP_ENUM_TWO_SETS_OF_TYPES(LhsType, NumLeftParams, RhsType, NumRightParams) \
164  SHOW_TYPENAME(NumLeftParams, NumRightParams) expt::AddOp::ResultType<GET_TYPE(LeftType, LhsType, NumLeftParams), GET_TYPE(RightType, RhsType, NumRightParams)>::type \
165  operator+(const GET_TYPE(LeftType, LhsType, NumLeftParams)& lhs, \
166  const GET_TYPE(RightType, RhsType, NumRightParams)& rhs) \
167  { \
168  return Add(lhs, rhs); \
169  }
170 
171 
172  #define GENERATE_SUBTRACTION_OPERATOR(LeftType, NumLeftParams, RightType, NumRightParams) \
173  PP_ENUM_TWO_SETS_OF_TYPES(LhsType, NumLeftParams, RhsType, NumRightParams) \
174  SHOW_TYPENAME(NumLeftParams, NumRightParams) expt::SubtractOp::ResultType<GET_TYPE(LeftType, LhsType, NumLeftParams), GET_TYPE(RightType, RhsType, NumRightParams)>::type \
175  operator-(const GET_TYPE(LeftType, LhsType, NumLeftParams)& lhs, \
176  const GET_TYPE(RightType, RhsType, NumRightParams)& rhs) \
177  { \
178  return Subtract(lhs, rhs); \
179  }
180 
181 #endif //NEKTAR_USE_EXPRESSION_TEMPLATES
182 }
183 
184 #endif //NEKTAR_LIB_UTILITIES_BASIC_UTILS_OPERATOR_GENERATORS_HPP
185