export_function.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
27 
36 
37 using namespace std;
38 
40 
41 
42 //
43 // PUBLIC MEMBER FUNCTIONS:
44 //
45 
46 ExportFunction::ExportFunction( const std::string& _name,
47  const ExportArgument& _argument1,
48  const ExportArgument& _argument2,
49  const ExportArgument& _argument3,
50  const ExportArgument& _argument4,
51  const ExportArgument& _argument5,
52  const ExportArgument& _argument6,
53  const ExportArgument& _argument7,
54  const ExportArgument& _argument8,
55  const ExportArgument& _argument9
57 {
58  returnAsPointer = false;
59  flagPrivate = false;
60 
62 
63  init( _name,_argument1,_argument2,_argument3,
64  _argument4,_argument5,_argument6,
65  _argument7,_argument8,_argument9 );
66 }
67 
69 {}
70 
72 {
73  return new ExportFunction(*this);
74 }
75 
76 returnValue ExportFunction::init( const std::string& _name,
77  const ExportArgument& _argument1,
78  const ExportArgument& _argument2,
79  const ExportArgument& _argument3,
80  const ExportArgument& _argument4,
81  const ExportArgument& _argument5,
82  const ExportArgument& _argument6,
83  const ExportArgument& _argument7,
84  const ExportArgument& _argument8,
85  const ExportArgument& _argument9
86  )
87 {
89  clear( );
90 
91  setName( fcnPrefix + "_" + _name );
92 
93  addArgument( _argument1,_argument2,_argument3,
94  _argument4,_argument5,_argument6,
95  _argument7,_argument8,_argument9 );
96 
97  return SUCCESSFUL_RETURN;
98 }
99 
100 
101 ExportFunction& ExportFunction::setup( const std::string& _name,
102  const ExportArgument& _argument1,
103  const ExportArgument& _argument2,
104  const ExportArgument& _argument3,
105  const ExportArgument& _argument4,
106  const ExportArgument& _argument5,
107  const ExportArgument& _argument6,
108  const ExportArgument& _argument7,
109  const ExportArgument& _argument8,
110  const ExportArgument& _argument9
111  )
112 {
113  init( _name,
114  _argument1,_argument2,_argument3,
115  _argument4,_argument5,_argument6,
116  _argument7,_argument8,_argument9 );
117 
118  return *this;
119 }
120 
121 
122 
124  const ExportArgument& _argument2,
125  const ExportArgument& _argument3,
126  const ExportArgument& _argument4,
127  const ExportArgument& _argument5,
128  const ExportArgument& _argument6,
129  const ExportArgument& _argument7,
130  const ExportArgument& _argument8,
131  const ExportArgument& _argument9
132  )
133 {
134  return functionArguments.addArgument( _argument1,_argument2,_argument3,
135  _argument4,_argument5,_argument6,
136  _argument7,_argument8,_argument9 );
137 }
138 
139 
140 
142  bool _returnAsPointer
143  )
144 {
145  retVal = deepcopy( _functionReturnValue );
146  returnAsPointer = _returnAsPointer;
147 
148  return *this;
149 }
150 
151 
152 ExportFunction& ExportFunction::setName( const std::string& _name
153  )
154 {
155  if ( _name.empty() == true )
157 
158  name = _name;
159 
160  return *this;
161 }
162 
163 
164 std::string ExportFunction::getName( ) const
165 {
166  return name;
167 }
168 
169 
170 
172  const std::string& _realString,
173  const std::string& _intString,
174  int _precision
175  ) const
176 {
177  return SUCCESSFUL_RETURN;
178 }
179 
180 
182  const std::string& _realString,
183  const std::string& _intString,
184  int _precision
185  ) const
186 {
187  // do not export undefined (empty) functions
188  if (isDefined() == false)
189  return SUCCESSFUL_RETURN;
190 
191  if (flagPrivate == true)
192  return SUCCESSFUL_RETURN;
193 
194  if (description.empty() == false)
195  {
196  stream << "\n/** " << description;
197 
199  {
200  vector< ExportArgument > args = functionArguments.get();
201 
202  stream << "\n *\n";
203 
204  for (unsigned i = 0; i < args.size(); ++i)
205  {
206  if (args[ i ].isGiven() == true || args[ i ].getDoc().empty() == true)
207  continue;
208 
209  stream << " * \\param " << args[ i ].getName() << " " << args[ i ].getDoc() << endl;
210  }
211  }
212  else
213  {
214  stream << "\n";
215  }
216 
217  if (retVal.getDim())
218  {
219  std::string tmp = retVal.getDoc();
220  if (tmp.empty() == false)
221  stream << " *\n * \\return " << tmp << endl;
222  }
223  stream << " */\n";
224  }
225 
226 
227  if (retVal.getDim())
228  {
229  stream << retVal.getTypeString(_realString, _intString);
230  if ( returnAsPointer == true )
231  stream << "*";
232  }
233  else
234  {
235  stream << "void";
236  }
237 
238  stream << " " << name << "( ";
239  functionArguments.exportCode(stream, _realString, _intString, _precision);
240  stream << " );\n";
241 
242  return SUCCESSFUL_RETURN;
243 }
244 
245 
247  const std::string& _realString,
248  const std::string& _intString,
249  int _precision
250  ) const
251 {
252  //
253  // Do not export undefined (empty) functions
254  //
255  if ( isDefined() == false )
256  return SUCCESSFUL_RETURN;
257 
258  //
259  // Set return value type
260  //
261  if (retVal.getDim())
262  {
263  stream << retVal.getTypeString(_realString, _intString);
264  if ( returnAsPointer == true )
265  stream << "*";
266  }
267  else
268  {
269  stream << "void";
270  }
271 
272  stream << " " << name << "( ";
273  functionArguments.exportCode(stream, _realString, _intString, _precision);
274  stream << " )\n{\n";
275 
277  retVal.exportDataDeclaration(stream, _realString, _intString, _precision);
278 
279  //
280  // Set parent pointers, and run memory allocation
281  //
282  StatementPtrArray::const_iterator it = statements.begin();
283  for(; it != statements.end(); ++it)
284  (*it)->allocate( memAllocator );
285 
286  //
287  // Open a temporary file and export statements to the temporary file
288  //
289  stringstream ss;
290  ExportStatementBlock::exportCode(ss, _realString, _intString, _precision);
291 
292  //
293  // Export local indices (allocated previously)
294  //
295  const std::vector< ExportIndex > indices = memAllocator->getPool();
296  for (unsigned i = 0; i < indices.size(); ++i)
297  indices[ i ].exportDataDeclaration(stream, _realString, _intString, _precision);
298 
299  //
300  // Export local variables -- still done in a very primitive way
301  //
302  for(unsigned i = 0; i < localVariables.size(); ++i)
303  localVariables[ i ].exportDataDeclaration(stream, _realString, _intString, _precision);
304 
305  //
306  // Copy temporary file to main file, and close te temporary file aftwarards
307  //
308  stream << ss.str();
309 
310  //
311  // Finish the export of the function
312  //
313  if (retVal.getDim())
314  stream << "return " << retVal.getFullName() << ";\n";
315  stream << "}\n\n";
316 
317  return SUCCESSFUL_RETURN;
318 }
319 
320 
322 {
323  if (name.empty() == false && (getNumStatements( ) > 0 || retVal.getDim() > 0))
324  return true;
325 
326  return false;
327 }
328 
329 
331 {
333 }
334 
335 
336 //
337 // PROTECTED MEMBER FUNCTIONS:
338 //
339 
341 {
342  returnAsPointer = false;
343 
344  return SUCCESSFUL_RETURN;
345 }
346 
348 {
349  memAllocator->add( _index );
350 
351  return *this;
352 }
353 
355 {
356  memAllocator->acquire( obj );
357 
358  return *this;
359 }
360 
362 {
363  memAllocator->release( obj );
364 
365  return *this;
366 }
367 
369 {
370  localVariables.push_back( _var );
371 
372  return *this;
373 }
374 
375 ExportFunction& ExportFunction::doc(const std::string& _doc)
376 {
377  description = _doc;
378 
379  return *this;
380 }
381 
383 {
384  flagPrivate = _set;
385 
386  return *this;
387 }
388 
390 {
391  return flagPrivate;
392 }
393 
395 
396 // end of file.
virtual returnValue exportCode(std::ostream &stream, const std::string &_realString="real_t", const std::string &_intString="int", int _precision=16) const
returnValue addArgument(const ExportArgument &_argument1, const ExportArgument &_argument2=emptyConstExportArgument, const ExportArgument &_argument3=emptyConstExportArgument, const ExportArgument &_argument4=emptyConstExportArgument, const ExportArgument &_argument5=emptyConstExportArgument, const ExportArgument &_argument6=emptyConstExportArgument, const ExportArgument &_argument7=emptyConstExportArgument, const ExportArgument &_argument8=emptyConstExportArgument, const ExportArgument &_argument9=emptyConstExportArgument)
ExportFunction(const std::string &_name="defaultFunctionName", const ExportArgument &_argument1=emptyConstExportArgument, const ExportArgument &_argument2=emptyConstExportArgument, const ExportArgument &_argument3=emptyConstExportArgument, const ExportArgument &_argument4=emptyConstExportArgument, const ExportArgument &_argument5=emptyConstExportArgument, const ExportArgument &_argument6=emptyConstExportArgument, const ExportArgument &_argument7=emptyConstExportArgument, const ExportArgument &_argument8=emptyConstExportArgument, const ExportArgument &_argument9=emptyConstExportArgument)
unsigned getNumArguments() const
ExportFunction & setName(const std::string &_name)
returnValue init(const std::string &_name="defaultFunctionName", const ExportArgument &_argument1=emptyConstExportArgument, const ExportArgument &_argument2=emptyConstExportArgument, const ExportArgument &_argument3=emptyConstExportArgument, const ExportArgument &_argument4=emptyConstExportArgument, const ExportArgument &_argument5=emptyConstExportArgument, const ExportArgument &_argument6=emptyConstExportArgument, const ExportArgument &_argument7=emptyConstExportArgument, const ExportArgument &_argument8=emptyConstExportArgument, const ExportArgument &_argument9=emptyConstExportArgument)
virtual returnValue exportForwardDeclaration(std::ostream &stream, const std::string &_realString="real_t", const std::string &_intString="int", int _precision=16) const
Allows to pass back messages to the calling function.
virtual ~ExportFunction()
#define CLOSE_NAMESPACE_ACADO
virtual returnValue exportCode(std::ostream &stream, const std::string &_realString="real_t", const std::string &_intString="int", int _precision=16) const
virtual bool isPrivate() const
Defines a scalar-valued index variable to be used for exporting code.
ExportVariable retVal
Defines a matrix-valued variable that can be passed as argument to exported functions.
std::string getTypeString(const std::string &_realString="real_t", const std::string &_intString="int") const
ExportFunction & setup(const std::string &_name="defaultFunctionName", const ExportArgument &_argument1=emptyConstExportArgument, const ExportArgument &_argument2=emptyConstExportArgument, const ExportArgument &_argument3=emptyConstExportArgument, const ExportArgument &_argument4=emptyConstExportArgument, const ExportArgument &_argument5=emptyConstExportArgument, const ExportArgument &_argument6=emptyConstExportArgument, const ExportArgument &_argument7=emptyConstExportArgument, const ExportArgument &_argument8=emptyConstExportArgument, const ExportArgument &_argument9=emptyConstExportArgument)
virtual std::string getDoc() const
virtual ExportFunction & doc(const std::string &_doc)
virtual bool isDefined() const
virtual returnValue exportDataDeclaration(std::ostream &stream, const std::string &_realString="real_t", const std::string &_intString="int", int _precision=16) const
static std::string fcnPrefix
virtual ExportStatement * clone() const
const std::vector< ExportArgument > & get() const
virtual returnValue exportCode(std::ostream &stream, const std::string &_realString="real_t", const std::string &_intString="int", int _precision=16) const
ExportStruct getDataStruct() const
virtual ExportFunction & setPrivate(bool _set=true)
virtual returnValue exportDataDeclaration(std::ostream &stream, const std::string &_realString="real_t", const std::string &_intString="int", int _precision=16) const
Base class for all kind of statements to be exported by the code generation tool. ...
Allows to export code of an arbitrary function.
virtual uint getDim() const
std::string getFullName() const
ExportFunction & setReturnValue(const ExportVariable &_functionReturnValue, bool _returnAsPointer=false)
virtual ExportFunction & acquire(ExportIndex &obj)
ExportFunction & addVariable(const ExportVariable &_var)
std::string getName() const
virtual ExportFunction & release(const ExportIndex &obj)
std::string description
A deepcopy(const A &a)
Make a deep copy of an object (Note: default is a shallow copy!)
#define BEGIN_NAMESPACE_ACADO
returnValue addArgument(const ExportArgument &_argument1, const ExportArgument &_argument2=emptyConstExportArgument, const ExportArgument &_argument3=emptyConstExportArgument, const ExportArgument &_argument4=emptyConstExportArgument, const ExportArgument &_argument5=emptyConstExportArgument, const ExportArgument &_argument6=emptyConstExportArgument, const ExportArgument &_argument7=emptyConstExportArgument, const ExportArgument &_argument8=emptyConstExportArgument, const ExportArgument &_argument9=emptyConstExportArgument)
std::vector< ExportVariable > localVariables
returnValue clear()
std::shared_ptr< MemoryAllocator > MemoryAllocatorPtr
Allows to export code for a block of statements.
ExportFunction & addIndex(const ExportIndex &_index)
ExportArgumentList functionArguments
#define ACADOERROR(retval)
Defines a matrix-valued variable to be used for exporting code.
MemoryAllocatorPtr memAllocator


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:33