A platform for high-performance distributed tool and library development written in C++. It can be deployed in two different cluster modes: standalone or distributed. API for v0.5.0, released on June 13, 2018.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
MethodCallLambdaCreationFunctions.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * *
3  * Copyright 2018 Rice University *
4  * *
5  * Licensed under the Apache License, Version 2.0 (the "License"); *
6  * you may not use this file except in compliance with the License. *
7  * You may obtain a copy of the License at *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14  * See the License for the specific language governing permissions and *
15  * limitations under the License. *
16  * *
17  *****************************************************************************/
18 
19 #ifndef PDB_METHODCALLLAMBDA_H
20 #define PDB_METHODCALLLAMBDA_H
21 
22 #include <MethodCallLambda.h>
23 
24 namespace pdb {
25 
43 template <typename ReturnType, typename ClassType>
45  std::string methodName,
46  Handle<ClassType>& var,
47  std::string returnTypeName,
48  ReturnType (ClassType::*arg)(),
49  std::function<bool(std::string&, TupleSetPtr, int)> columnBuilder,
50  std::function<SimpleComputeExecutorPtr(TupleSpec&, TupleSpec&, TupleSpec&)> getExecutor) {
51  PDB_COUT << "makeLambdaFromMethod: input type code is " << var.getExactTypeInfoValue() << std::endl;
52  return LambdaTree<Ptr<typename std::remove_reference<ReturnType>::type>>(std::make_shared<MethodCallLambda<Ptr<typename std::remove_reference<ReturnType>::type>, ClassType>>(inputTypeName,
53  methodName,
54  returnTypeName,
55  var,
56  columnBuilder,
57  getExecutor));
58 }
59 
73 template <typename ReturnType, typename ClassType>
75  std::string methodName,
76  Handle<ClassType>& var,
77  std::string returnTypeName,
78  ReturnType (ClassType::*arg)(),
79  std::function<bool(std::string&, TupleSetPtr, int)> columnBuilder,
80  std::function<SimpleComputeExecutorPtr(TupleSpec&, TupleSpec&, TupleSpec&)> getExecutor) {
81 
82  PDB_COUT << "makeLambdaFromMethod: input type code is " << var.getExactTypeInfoValue() << std::endl;
83  return LambdaTree<ReturnType>(std::make_shared<MethodCallLambda<ReturnType, ClassType>>(inputTypeName,
84  methodName,
85  returnTypeName,
86  var,
87  columnBuilder,
88  getExecutor));
89 }
90 
91 
99 template <bool B, typename InputType>
100 auto tryReference(InputType& arg) -> typename std::enable_if_t<B, InputType*> {
101  return &arg;
102 }
103 
111 template <bool B, typename InputType>
112 auto tryReference(InputType arg) -> typename std::enable_if_t<!B, InputType*> {
113  InputType* temp = nullptr;
114  return temp;
115 }
116 
122 #define makeLambdaFromMethod(VAR, METHOD) \
123  (makeLambdaUsingMethod( \
124  getTypeName<typename std::remove_reference<decltype(*VAR)>::type>(), \
125  std::string(#METHOD), \
126  VAR, \
127  getTypeName<typename std::remove_reference<decltype(*VAR)>::type>(), \
128  &std::remove_reference<decltype(*VAR)>::type::METHOD, \
129  [](std::string& pleaseCreateThisType, TupleSetPtr input, int outAtt) { \
130  if (pleaseCreateThisType == \
131  getTypeName<typename std::remove_reference<decltype(VAR->METHOD())>::type>()) { \
132  std::vector<typename std::remove_reference<decltype(VAR->METHOD())>::type>* \
133  outColumn = new std::vector< \
134  typename std::remove_reference<decltype(VAR->METHOD())>::type>; \
135  input->addColumn(outAtt, outColumn, true); \
136  return true; \
137  } \
138  \
139  if (pleaseCreateThisType == \
140  getTypeName< \
141  Ptr<typename std::remove_reference<decltype(VAR->METHOD())>::type>>()) { \
142  std::vector<Ptr<typename std::remove_reference<decltype(VAR->METHOD())>::type>>* \
143  outColumn = new std::vector< \
144  Ptr<typename std::remove_reference<decltype(VAR->METHOD())>::type>>; \
145  input->addColumn(outAtt, outColumn, true); \
146  return true; \
147  } \
148  \
149  return false; \
150  }, \
151  [](TupleSpec& inputSchema, TupleSpec& attsToOperateOn, TupleSpec& attsToIncludeInOutput) { \
152  \
153  /* create the output tuple set */ \
154  TupleSetPtr output = std::make_shared<TupleSet>(); \
155  \
156  /* create the machine that is going to setup the output tuple set, using the input \
157  * tuple set */ \
158  TupleSetSetupMachinePtr myMachine = \
159  std::make_shared<TupleSetSetupMachine>(inputSchema, attsToIncludeInOutput); \
160  \
161  /* this is the input attribute that we will process */ \
162  std::vector<int> matches = myMachine->match(attsToOperateOn); \
163  int whichAtt = matches[0]; \
164  \
165  /* this is the output attribute */ \
166  int outAtt = attsToIncludeInOutput.getAtts().size(); \
167  \
168  return std::make_shared<SimpleComputeExecutor>(output, [=](TupleSetPtr input) { \
169  \
170  /* if the method returns a reference, then we transform the method output into a \
171  * pointer */ \
172  if (std::is_reference<decltype(VAR->METHOD())>::value) { \
173  \
174  /* set up the output tuple set */ \
175  myMachine->setup(input, output); \
176  \
177  /* get the column to operate on... the input type is taken from the argument \
178  * VAR */ \
179  std::vector<typename std::remove_reference<decltype(VAR)>::type>& \
180  inputColumn = \
181  input->getColumn<typename std::remove_reference<decltype(VAR)>::type>( \
182  whichAtt); \
183  \
184  /* setup the output column, if it is not already set up */ \
185  if (!output->hasColumn(outAtt)) { \
186  std::vector< \
187  Ptr<typename std::remove_reference<decltype(VAR->METHOD())>::type>>* \
188  outColumn = new std::vector<Ptr< \
189  typename std::remove_reference<decltype(VAR->METHOD())>::type>>; \
190  output->addColumn(outAtt, outColumn, true); \
191  } \
192  \
193  /* get the output column */ \
194  std::vector< \
195  Ptr<typename std::remove_reference<decltype(VAR->METHOD())>::type>>& \
196  outColumn = output->getColumn< \
197  Ptr<typename std::remove_reference<decltype(VAR->METHOD())>::type>>( \
198  outAtt); \
199  \
200  /* loop down the column, setting the output */ \
201  int numTuples = inputColumn.size(); \
202  outColumn.resize(numTuples); \
203  for (int i = 0; i < numTuples; i++) { \
204  outColumn[i] = \
205  tryReference<std::is_reference<decltype(VAR->METHOD())>::value>( \
206  inputColumn[i]->METHOD()); \
207  } \
208  outColumn = output->getColumn< \
209  Ptr<typename std::remove_reference<decltype(VAR->METHOD())>::type>>( \
210  outAtt); \
211  return output; \
212  \
213  /* if the method does not return a reference, then we just go ahead and store \
214  * a copy of what was returned */ \
215  } else { \
216  \
217  /* set up the output tuple set */ \
218  myMachine->setup(input, output); \
219  \
220  /* get the column to operate on... the input type is taken from the argument \
221  * VAR */ \
222  std::vector<typename std::remove_reference<decltype(VAR)>::type>& \
223  inputColumn = \
224  input->getColumn<typename std::remove_reference<decltype(VAR)>::type>( \
225  whichAtt); \
226  \
227  /* setup the output column, if it is not already set up */ \
228  if (!output->hasColumn(outAtt)) { \
229  std::vector<typename std::remove_reference<decltype( \
230  VAR->METHOD())>::type>* outColumn = \
231  new std::vector< \
232  typename std::remove_reference<decltype(VAR->METHOD())>::type>; \
233  output->addColumn(outAtt, outColumn, true); \
234  } \
235  \
236  /* get the output column */ \
237  std::vector<typename std::remove_reference<decltype(VAR->METHOD())>::type>& \
238  outColumn = output->getColumn< \
239  typename std::remove_reference<decltype(VAR->METHOD())>::type>( \
240  outAtt); \
241  \
242  /* loop down the column, setting the output */ \
243  int numTuples = inputColumn.size(); \
244  outColumn.resize(numTuples); \
245  for (int i = 0; i < numTuples; i++) { \
246  outColumn[i] = inputColumn[i]->METHOD(); \
247  } \
248  return output; \
249  } \
250  }); \
251  }))
252 
253 }
254 
255 #endif //PDB_METHODCALLLAMBDA_H
std::shared_ptr< SimpleComputeExecutor > SimpleComputeExecutorPtr
std::shared_ptr< TupleSet > TupleSetPtr
Definition: TupleSet.h:64
#define PDB_COUT
Definition: PDBDebug.h:31
auto tryReference(InputType &arg) -> typename std::enable_if_t< B, InputType * >
LambdaTree< std::enable_if_t< std::is_reference< ReturnType >::value, Ptr< typename std::remove_reference< ReturnType >::type > > > makeLambdaUsingMethod(std::string inputTypeName, std::string methodName, Handle< ClassType > &var, std::string returnTypeName, ReturnType(ClassType::*arg)(), std::function< bool(std::string &, TupleSetPtr, int)> columnBuilder, std::function< SimpleComputeExecutorPtr(TupleSpec &, TupleSpec &, TupleSpec &)> getExecutor)
Definition: Ptr.h:32