export_for_loop.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 
34 
35 #include <sstream>
36 
38 
39 using namespace std;
40 
41 
42 //
43 // PUBLIC MEMBER FUNCTIONS:
44 //
45 
47  const ExportIndex& _startValue,
48  const ExportIndex& _finalValue,
49  const ExportIndex& _increment,
50  bool _doLoopUnrolling
51  ) : ExportStatementBlock( ),
52  loopVariable( _loopVariable ),
53  startValue( _startValue ),
54  finalValue( _finalValue ),
55  increment( _increment ),
56  doLoopUnrolling( _doLoopUnrolling )
57 {
58  sanityCheck();
59 }
60 
61 
64  startValue( arg.startValue ),
65  finalValue( arg.finalValue ),
66  increment( arg.increment ),
68 {
69 }
70 
71 
73 {
74  clear( );
75 }
76 
77 
79 {
80  if ( this != &arg )
81  {
84  }
85 
86  return *this;
87 }
88 
89 
91 {
92  return new ExportForLoop( *this );
93 }
94 
95 
97  const ExportIndex& _startValue,
98  const ExportIndex& _finalValue,
99  const ExportIndex& _increment,
100  bool _doLoopUnrolling
101  )
102 {
103  clear();
104 
105  loopVariable = _loopVariable;
106  startValue = _startValue;
107  finalValue = _finalValue;
108  increment = _increment;
109  doLoopUnrolling = _doLoopUnrolling;
110 
111  return SUCCESSFUL_RETURN;
112 }
113 
114 
115 
116 
118  const std::string& _realString,
119  const std::string& _intString,
120  int _precision
121  ) const
122 {
123  return SUCCESSFUL_RETURN;
124 }
125 
126 
128  const std::string& _realString,
129  const std::string& _intString,
130  int _precision
131  ) const
132 {
133  returnValue status = sanityCheck();
134  if (status != SUCCESSFUL_RETURN)
135  return status;
136 
137  if (startValue.isGiven() == true && finalValue.isGiven() == true)
139  return SUCCESSFUL_RETURN;
140 
141  if ( doLoopUnrolling == false )
142  {
143  stream << "for (" << loopVariable.get() << " = " << startValue.get() << "; ";
144 
145  if (increment.isGiven() == true && increment.getGivenValue() == -1)
146  stream << finalValue.get() << " < " << loopVariable.get() << "; ";
147  else
148  stream << loopVariable.get() << " < " << finalValue.get() << "; ";
149 
150  if (increment.isGiven() == true)
151  {
152  switch ( increment.getGivenValue() )
153  {
154  case 1:
155  stream << "++" << loopVariable.get();
156  break;
157 
158  case -1:
159  stream << "--" << loopVariable.get();
160  break;
161 
162  default:
163  stream << loopVariable.get() << " += " << increment.getGivenValue();
164  break;
165  }
166  }
167  else
168  {
169  stream << loopVariable.get() << " += " << increment.get();
170  }
171 
172  stream << ")" << endl << "{" << endl;
173 
174  ExportStatementBlock::exportCode(stream, _realString, _intString, _precision);
175 
176  stream << "}\n";
177  }
178 
179  return SUCCESSFUL_RETURN;
180 }
181 
182 
183 
185 {
186  doLoopUnrolling = true;
187  return *this;
188 }
189 
190 
192 {
193  doLoopUnrolling = false;
194  return *this;
195 }
196 
198 {
199  //
200  // For loop itself cannot allocate any memory. Thus it just forwards
201  // the pointer, so that later statements can allocate some memory.
202  //
203  StatementPtrArray::const_iterator it = statements.begin();
204  for(; it != statements.end(); ++it)
205  (*it)->allocate( allocator );
206 
207  return *this;
208 }
209 
210 
211 //
212 // PROTECTED MEMBER FUNCTIONS:
213 //
214 
216 {
217  return SUCCESSFUL_RETURN;
218 }
219 
220 //
221 // PRIVATE MEMBER FUNCTIONS:
222 //
224 {
225  if (doLoopUnrolling == true)
226  return ACADOERRORTEXT(RET_NOT_IMPLEMENTED_YET, "Loop unrolling is not yet implemented");
227 
228  if (startValue.isGiven() == true && finalValue.isGiven() == true && increment.isGiven() == true)
229  {
231  return ACADOERRORTEXT(RET_INVALID_ARGUMENTS, "Export for loop arguments are invalid");
232 
234  return ACADOERRORTEXT(RET_INVALID_ARGUMENTS, "Export for loop arguments are invalid");
235  }
236 
237  return SUCCESSFUL_RETURN;
238 }
239 
240 
242 
243 // end of file.
ExportIndex loopVariable
ExportForLoop & allocate(MemoryAllocatorPtr allocator)
Allows to pass back messages to the calling function.
virtual returnValue exportDataDeclaration(std::ostream &stream, const std::string &_realString="real_t", const std::string &_intString="int", int _precision=16) const
virtual ExportStatement * clone() const
Allows to export code of a for-loop.
#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
Defines a scalar-valued index variable to be used for exporting code.
virtual returnValue exportCode(std::ostream &stream, const std::string &_realString="real_t", const std::string &_intString="int", int _precision=16) const
ExportIndex finalValue
returnValue sanityCheck(void) const
virtual ~ExportForLoop()
ExportForLoop(const ExportIndex &_loopVariable=emptyConstExportIndex, const ExportIndex &_startValue=emptyConstExportIndex, const ExportIndex &_finalValue=emptyConstExportIndex, const ExportIndex &_increment=constExportIndexValueOne, bool _doLoopUnrolling=false)
ExportForLoop & unrollLoop()
returnValue clear()
Base class for all kind of statements to be exported by the code generation tool. ...
ExportForLoop & operator=(const ExportForLoop &arg)
const std::string get() const
int getGivenValue() const
bool isGiven() const
ExportIndex startValue
#define BEGIN_NAMESPACE_ACADO
ExportIndex increment
returnValue init(const ExportIndex &_loopVariable, const ExportIndex &_startValue, const ExportIndex &_finalValue, const ExportIndex &_increment, bool _doLoopUnrolling)
ExportForLoop & keepLoop()
ExportStatementBlock & operator=(const ExportStatementBlock &rhs)
std::shared_ptr< MemoryAllocator > MemoryAllocatorPtr
Allows to export code for a block of statements.
#define ACADOERRORTEXT(retval, text)


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