export_function.cpp
Go to the documentation of this file.
00001 /*
00002  *    This file is part of ACADO Toolkit.
00003  *
00004  *    ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
00005  *    Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
00006  *    Milan Vukov, Rien Quirynen, KU Leuven.
00007  *    Developed within the Optimization in Engineering Center (OPTEC)
00008  *    under supervision of Moritz Diehl. All rights reserved.
00009  *
00010  *    ACADO Toolkit is free software; you can redistribute it and/or
00011  *    modify it under the terms of the GNU Lesser General Public
00012  *    License as published by the Free Software Foundation; either
00013  *    version 3 of the License, or (at your option) any later version.
00014  *
00015  *    ACADO Toolkit is distributed in the hope that it will be useful,
00016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  *    Lesser General Public License for more details.
00019  *
00020  *    You should have received a copy of the GNU Lesser General Public
00021  *    License along with ACADO Toolkit; if not, write to the Free Software
00022  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00023  *
00024  */
00025 
00026 
00027 
00034 #include <acado/code_generation/export_function.hpp>
00035 #include <acado/code_generation/export_function_call.hpp>
00036 
00037 using namespace std;
00038 
00039 BEGIN_NAMESPACE_ACADO
00040 
00041 
00042 //
00043 // PUBLIC MEMBER FUNCTIONS:
00044 //
00045 
00046 ExportFunction::ExportFunction( const std::string& _name,
00047                                                                 const ExportArgument& _argument1,
00048                                                                 const ExportArgument& _argument2,
00049                                                                 const ExportArgument& _argument3,
00050                                                                 const ExportArgument& _argument4,
00051                                                                 const ExportArgument& _argument5,
00052                                                                 const ExportArgument& _argument6,
00053                                                                 const ExportArgument& _argument7,
00054                                                                 const ExportArgument& _argument8,
00055                                                                 const ExportArgument& _argument9
00056                                                                 ) : ExportStatementBlock( )
00057 {
00058         returnAsPointer = false;
00059         flagPrivate = false;
00060 
00061         memAllocator = MemoryAllocatorPtr( new MemoryAllocator );
00062 
00063         init( _name,_argument1,_argument2,_argument3,
00064                                 _argument4,_argument5,_argument6,
00065                                 _argument7,_argument8,_argument9 );
00066 }
00067 
00068 ExportFunction::~ExportFunction( )
00069 {}
00070 
00071 ExportStatement* ExportFunction::clone( ) const
00072 {
00073         return new ExportFunction(*this);
00074 }
00075 
00076 returnValue ExportFunction::init(       const std::string& _name,
00077                                                                         const ExportArgument& _argument1,
00078                                                                         const ExportArgument& _argument2,
00079                                                                         const ExportArgument& _argument3,
00080                                                                         const ExportArgument& _argument4,
00081                                                                         const ExportArgument& _argument5,
00082                                                                         const ExportArgument& _argument6,
00083                                                                         const ExportArgument& _argument7,
00084                                                                         const ExportArgument& _argument8,
00085                                                                         const ExportArgument& _argument9
00086                                                                         )
00087 {
00088         ExportStatementBlock::clear( );
00089         clear( );
00090 
00091         setName( _name );
00092 
00093         addArgument(    _argument1,_argument2,_argument3,
00094                                         _argument4,_argument5,_argument6,
00095                                         _argument7,_argument8,_argument9 );
00096 
00097         return SUCCESSFUL_RETURN;
00098 }
00099 
00100 
00101 ExportFunction& ExportFunction::setup(  const std::string& _name,
00102                                                                                 const ExportArgument& _argument1,
00103                                                                                 const ExportArgument& _argument2,
00104                                                                                 const ExportArgument& _argument3,
00105                                                                                 const ExportArgument& _argument4,
00106                                                                                 const ExportArgument& _argument5,
00107                                                                                 const ExportArgument& _argument6,
00108                                                                                 const ExportArgument& _argument7,
00109                                                                                 const ExportArgument& _argument8,
00110                                                                                 const ExportArgument& _argument9
00111                                                                                 )
00112 {
00113         init(   _name,
00114                         _argument1,_argument2,_argument3,
00115                         _argument4,_argument5,_argument6,
00116                         _argument7,_argument8,_argument9 );
00117 
00118         return *this;
00119 }
00120 
00121 
00122 
00123 returnValue ExportFunction::addArgument(        const ExportArgument& _argument1,
00124                                                                                         const ExportArgument& _argument2,
00125                                                                                         const ExportArgument& _argument3,
00126                                                                                         const ExportArgument& _argument4,
00127                                                                                         const ExportArgument& _argument5,
00128                                                                                         const ExportArgument& _argument6,
00129                                                                                         const ExportArgument& _argument7,
00130                                                                                         const ExportArgument& _argument8,
00131                                                                                         const ExportArgument& _argument9
00132                                                                                         )
00133 {
00134         return functionArguments.addArgument(   _argument1,_argument2,_argument3,
00135                                                                                         _argument4,_argument5,_argument6,
00136                                                                                         _argument7,_argument8,_argument9 );
00137 }
00138 
00139 
00140 
00141 ExportFunction& ExportFunction::setReturnValue( const ExportVariable& _functionReturnValue,
00142                                                                                                 bool _returnAsPointer
00143                                                                                                 )
00144 {
00145         retVal = deepcopy( _functionReturnValue );
00146         returnAsPointer = _returnAsPointer;
00147 
00148         return *this;
00149 }
00150 
00151 
00152 ExportFunction& ExportFunction::setName(        const std::string& _name
00153                                                                                         )
00154 {
00155         if ( _name.empty() == true )
00156                 ACADOERROR( RET_INVALID_ARGUMENTS );
00157 
00158         name = _name;
00159         
00160         return *this;
00161 }
00162 
00163 
00164 std::string ExportFunction::getName( ) const
00165 {
00166         return name;
00167 }
00168 
00169 
00170 
00171 returnValue ExportFunction::exportDataDeclaration(      std::ostream& stream,
00172                                                                                                         const std::string& _realString,
00173                                                                                                         const std::string& _intString,
00174                                                                                                         int _precision
00175                                                                                                         ) const
00176 {
00177         return SUCCESSFUL_RETURN;
00178 }
00179 
00180 
00181 returnValue ExportFunction::exportForwardDeclaration(   std::ostream& stream,
00182                                                                                                                 const std::string& _realString,
00183                                                                                                                 const std::string& _intString,
00184                                                                                                                 int _precision
00185                                                                                                                 ) const
00186 {
00187         // do not export undefined (empty) functions
00188         if (isDefined() == false)
00189                 return SUCCESSFUL_RETURN;
00190 
00191         if (flagPrivate == true)
00192                 return SUCCESSFUL_RETURN;
00193 
00194         if (description.empty() == false)
00195         {
00196                 stream <<  "\n/** " << description;
00197 
00198                 if (functionArguments.getNumArguments() > 0)
00199                 {
00200                         vector< ExportArgument > args = functionArguments.get();
00201 
00202                         stream << "\n *\n";
00203 
00204                         for (unsigned i = 0; i < args.size(); ++i)
00205                         {
00206                                 if (args[ i ].isGiven() == true || args[ i ].getDoc().empty() == true)
00207                                         continue;
00208 
00209                                 stream << " *  \\param " << args[ i ].getName() << " " << args[ i ].getDoc() << endl;
00210                         }
00211                 }
00212                 else
00213                 {
00214                         stream << "\n";
00215                 }
00216 
00217                 if (retVal.getDim())
00218                 {
00219                         std::string tmp = retVal.getDoc();
00220                         if (tmp.empty() == false)
00221                                 stream << " *\n *  \\return " << tmp << endl;
00222                 }
00223                 stream << " */\n";
00224         }
00225 
00226 
00227         if (retVal.getDim())
00228         {
00229                 stream << retVal.getTypeString(_realString, _intString);
00230                 if ( returnAsPointer == true )
00231                         stream << "*";
00232         }
00233         else
00234         {
00235                 stream << "void";
00236         }
00237 
00238         stream << " " << name << "( ";
00239         functionArguments.exportCode(stream, _realString, _intString, _precision);
00240         stream << " );\n";
00241 
00242         return SUCCESSFUL_RETURN;
00243 }
00244 
00245 
00246 returnValue ExportFunction::exportCode( std::ostream& stream,
00247                                                                                 const std::string& _realString,
00248                                                                                 const std::string& _intString,
00249                                                                                 int _precision
00250                                                                                 ) const
00251 {
00252         //
00253         // Do not export undefined (empty) functions
00254         //
00255         if ( isDefined() == false )
00256                 return SUCCESSFUL_RETURN;
00257 
00258         //
00259         // Set return value type
00260         //
00261         if (retVal.getDim())
00262         {
00263                 stream << retVal.getTypeString(_realString, _intString);
00264                 if ( returnAsPointer == true )
00265                         stream << "*";
00266         }
00267         else
00268         {
00269                 stream << "void";
00270         }
00271         
00272         stream << " " << name << "( ";
00273         functionArguments.exportCode(stream, _realString, _intString, _precision);
00274         stream << " )\n{\n";
00275 
00276         if (retVal.getDataStruct() == ACADO_LOCAL)
00277                 retVal.exportDataDeclaration(stream, _realString, _intString, _precision);
00278 
00279         //
00280         // Set parent pointers, and run memory allocation
00281         //
00282         StatementPtrArray::const_iterator it = statements.begin();
00283         for(; it != statements.end(); ++it)
00284                 (*it)->allocate( memAllocator );
00285 
00286         //
00287         // Open a temporary file and export statements to the temporary file
00288         //
00289         stringstream ss;
00290         ExportStatementBlock::exportCode(ss, _realString, _intString, _precision);
00291 
00292         //
00293         // Export local indices (allocated previously)
00294         //
00295         const std::vector< ExportIndex > indices = memAllocator->getPool();
00296         for (unsigned i = 0; i < indices.size(); ++i)
00297                 indices[ i ].exportDataDeclaration(stream, _realString, _intString, _precision);
00298 
00299         //
00300         // Export local variables -- still done in a very primitive way
00301         //
00302         for(unsigned i = 0; i < localVariables.size(); ++i)
00303                 localVariables[ i ].exportDataDeclaration(stream, _realString, _intString, _precision);
00304 
00305         //
00306         // Copy temporary file to main file, and close te temporary file aftwarards
00307         //
00308         stream << ss.str();
00309 
00310         //
00311         // Finish the export of the function
00312         //
00313         if (retVal.getDim())
00314                 stream << "return " <<  retVal.getFullName() << ";\n";
00315         stream << "}\n\n";
00316 
00317         return SUCCESSFUL_RETURN;
00318 }
00319 
00320 
00321 bool ExportFunction::isDefined( ) const
00322 {
00323         if (name.empty() == false && (getNumStatements( ) > 0 || retVal.getDim() > 0))
00324                 return true;
00325 
00326         return false;
00327 }
00328 
00329 
00330 unsigned ExportFunction::getNumArguments( ) const
00331 {
00332         return functionArguments.getNumArguments( );
00333 }
00334 
00335 
00336 //
00337 // PROTECTED MEMBER FUNCTIONS:
00338 //
00339 
00340 returnValue ExportFunction::clear( )
00341 {
00342         returnAsPointer = false;
00343 
00344         return SUCCESSFUL_RETURN;
00345 }
00346 
00347 ExportFunction& ExportFunction::addIndex(const ExportIndex& _index)
00348 {
00349         memAllocator->add( _index );
00350 
00351         return *this;
00352 }
00353 
00354 ExportFunction& ExportFunction::acquire(ExportIndex& obj)
00355 {
00356         memAllocator->acquire( obj );
00357 
00358         return *this;
00359 }
00360 
00361 ExportFunction& ExportFunction::release(const ExportIndex& obj)
00362 {
00363         memAllocator->release( obj );
00364 
00365         return *this;
00366 }
00367 
00368 ExportFunction& ExportFunction::addVariable(const ExportVariable& _var)
00369 {
00370         localVariables.push_back( _var );
00371 
00372         return *this;
00373 }
00374 
00375 ExportFunction& ExportFunction::doc(const std::string& _doc)
00376 {
00377         description = _doc;
00378 
00379         return *this;
00380 }
00381 
00382 ExportFunction& ExportFunction::setPrivate(bool _set)
00383 {
00384         flagPrivate = _set;
00385 
00386         return *this;
00387 }
00388 
00389 bool ExportFunction::isPrivate() const
00390 {
00391         return flagPrivate;
00392 }
00393 
00394 CLOSE_NAMESPACE_ACADO
00395 
00396 // end of file.


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Thu Aug 27 2015 11:58:09