00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00034 #include <acado/variables_grid/variable_settings.hpp>
00035
00036
00037
00038 BEGIN_NAMESPACE_ACADO
00039
00040
00041
00042
00043
00044
00045
00046 VariableSettings::VariableSettings( )
00047 {
00048 dim = 0;
00049 type = VT_UNKNOWN;
00050
00051 names = 0;
00052 units = 0;
00053
00054 autoInit = defaultAutoInit;
00055 }
00056
00057
00058 VariableSettings::VariableSettings( uint _dim,
00059 VariableType _type,
00060 const char** const _names,
00061 const char** const _units,
00062 const DVector& _scaling,
00063 const DVector& _lb,
00064 const DVector& _ub,
00065 BooleanType _autoInit
00066 )
00067 {
00068 names = 0;
00069 units = 0;
00070
00071 init( _dim,_type,_names,_units,_scaling,_lb,_ub,_autoInit );
00072 }
00073
00074
00075 VariableSettings::VariableSettings( const VariableSettings& rhs )
00076 {
00077 uint i;
00078
00079 dim = rhs.dim;
00080 type = rhs.type;
00081
00082 if ( rhs.names != 0 )
00083 {
00084 names = (char**) calloc( dim,sizeof(char*) );
00085 for( i=0; i<dim; ++i )
00086 {
00087 names[i] = new char[MAX_LENGTH_NAME+1];
00088 setName( i,rhs.names[i] );
00089 }
00090 }
00091 else
00092 {
00093 names = 0;
00094 }
00095
00096 if ( rhs.units != 0 )
00097 {
00098 units = (char**) calloc( dim,sizeof(char*) );
00099 for( i=0; i<dim; ++i )
00100 {
00101 units[i] = new char[MAX_LENGTH_NAME+1];
00102 setUnit( i,rhs.units[i] );
00103 }
00104 }
00105 else
00106 {
00107 units = 0;
00108 }
00109
00110
00111 if ( rhs.scaling.isEmpty( ) == BT_FALSE )
00112 {
00113 scaling.init( dim );
00114 setScaling( rhs.scaling );
00115 }
00116 else
00117 {
00118 scaling.init( );
00119 }
00120
00121 if ( rhs.lb.isEmpty( ) == BT_FALSE )
00122 {
00123 lb.init( dim );
00124 setLowerBounds( rhs.lb );
00125 }
00126 else
00127 {
00128 lb.init( );
00129 }
00130
00131 if ( rhs.ub.isEmpty( ) == BT_FALSE )
00132 {
00133 ub.init( dim );
00134 setUpperBounds( rhs.ub );
00135 }
00136 else
00137 {
00138 ub.init( );
00139 }
00140
00141 autoInit = rhs.autoInit;
00142 }
00143
00144
00145 VariableSettings::~VariableSettings( )
00146 {
00147 clear( );
00148 }
00149
00150
00151 VariableSettings& VariableSettings::operator=( const VariableSettings& rhs )
00152 {
00153 uint i;
00154
00155 if ( this != &rhs )
00156 {
00157 clear( );
00158
00159 dim = rhs.dim;
00160 type = rhs.type;
00161
00162 if ( rhs.names != 0 )
00163 {
00164 names = (char**) calloc( dim,sizeof(char*) );
00165 for( i=0; i<dim; ++i )
00166 {
00167 names[i] = new char[MAX_LENGTH_NAME+1];
00168 setName( i,rhs.names[i] );
00169 }
00170 }
00171 else
00172 {
00173 names = 0;
00174 }
00175
00176 if ( rhs.units != 0 )
00177 {
00178 units = (char**) calloc( dim,sizeof(char*) );
00179 for( i=0; i<dim; ++i )
00180 {
00181 units[i] = new char[MAX_LENGTH_NAME+1];
00182 setUnit( i,rhs.units[i] );
00183 }
00184 }
00185 else
00186 {
00187 units = 0;
00188 }
00189
00190 if ( rhs.scaling.isEmpty( ) == BT_FALSE )
00191 {
00192 scaling.init( dim );
00193 setScaling( rhs.scaling );
00194 }
00195 else
00196 {
00197 scaling.init( );
00198 }
00199
00200 if ( rhs.lb.isEmpty( ) == BT_FALSE )
00201 {
00202 lb.init( dim );
00203 setLowerBounds( rhs.lb );
00204 }
00205 else
00206 {
00207 lb.init( );
00208 }
00209
00210 if ( rhs.ub.isEmpty( ) == BT_FALSE )
00211 {
00212 ub.init( dim );
00213 setUpperBounds( rhs.ub );
00214 }
00215 else
00216 {
00217 ub.init( );
00218 }
00219
00220 autoInit = rhs.autoInit;
00221 }
00222
00223 return *this;
00224 }
00225
00226
00227
00228 returnValue VariableSettings::init( )
00229 {
00230 clear( );
00231
00232 dim = 0;
00233 type = VT_UNKNOWN;
00234
00235 names = 0;
00236 units = 0;
00237
00238 autoInit = defaultAutoInit;
00239
00240 return SUCCESSFUL_RETURN;
00241 }
00242
00243
00244 returnValue VariableSettings::init( uint _dim,
00245 VariableType _type,
00246 const char** const _names,
00247 const char** const _units,
00248 const DVector& _scaling,
00249 const DVector& _lb,
00250 const DVector& _ub,
00251 BooleanType _autoInit
00252 )
00253 {
00254 init( );
00255
00256 type = _type;
00257 autoInit = _autoInit;
00258
00259 return appendSettings( _dim,_names,_units,_scaling,_lb,_ub );
00260 }
00261
00262
00263
00264 returnValue VariableSettings::appendSettings( const VariableSettings& rhs
00265 )
00266 {
00267 return appendSettings( rhs.dim,(const char**)rhs.names,(const char**)rhs.units,rhs.scaling,rhs.lb,rhs.ub );
00268 }
00269
00270
00271 returnValue VariableSettings::appendSettings( uint _dim,
00272 const char** const _names,
00273 const char** const _units,
00274 const DVector& _scaling,
00275 const DVector& _lb,
00276 const DVector& _ub
00277 )
00278 {
00279 uint i;
00280
00281 uint newDim = dim + _dim;
00282
00283 if ( names != 0 )
00284 {
00285 names = (char**) realloc( names,newDim*sizeof(char*) );
00286
00287 if ( _names != 0 )
00288 {
00289 for( i=0; i<_dim; ++i )
00290 {
00291 names[dim+i] = new char[MAX_LENGTH_NAME+1];
00292 setName( dim+i,_names[i] );
00293 }
00294 }
00295 else
00296 {
00297 for( i=0; i<_dim; ++i )
00298 {
00299 names[dim+i] = new char[MAX_LENGTH_NAME+1];
00300 setName( dim+i,defaultName );
00301 }
00302 }
00303 }
00304 else
00305 {
00306 if ( _names != 0 )
00307 {
00308 names = (char**) realloc( names,newDim*sizeof(char*) );
00309
00310 for( i=0; i<dim; ++i )
00311 {
00312 names[i] = new char[MAX_LENGTH_NAME+1];
00313 setName( i,defaultName );
00314 }
00315
00316 for( i=0; i<_dim; ++i )
00317 {
00318 names[dim+i] = new char[MAX_LENGTH_NAME+1];
00319 setName( dim+i,_names[i] );
00320 }
00321 }
00322 }
00323
00324 if ( units != 0 )
00325 {
00326 units = (char**) realloc( units,newDim*sizeof(char*) );
00327
00328 if ( _units != 0 )
00329 {
00330 for( i=0; i<_dim; ++i )
00331 {
00332 units[dim+i] = new char[MAX_LENGTH_NAME+1];
00333 setUnit( dim+i,_units[i] );
00334 }
00335 }
00336 else
00337 {
00338 for( i=0; i<_dim; ++i )
00339 {
00340 units[dim+i] = new char[MAX_LENGTH_NAME+1];
00341 setUnit( dim+i,defaultUnit );
00342 }
00343 }
00344 }
00345 else
00346 {
00347 if ( _units != 0 )
00348 {
00349 units = (char**) realloc( units,newDim*sizeof(char*) );
00350
00351 for( i=0; i<dim; ++i )
00352 {
00353 units[i] = new char[MAX_LENGTH_NAME+1];
00354 setUnit( i,defaultUnit );
00355 }
00356
00357 for( i=0; i<_dim; ++i )
00358 {
00359 units[dim+i] = new char[MAX_LENGTH_NAME+1];
00360 setUnit( dim+i,_units[i] );
00361 }
00362 }
00363 }
00364
00365 if ( scaling.isEmpty( ) == BT_FALSE )
00366 {
00367 if ( _scaling.isEmpty( ) == BT_FALSE )
00368 {
00369 scaling.append( _scaling );
00370 }
00371 else
00372 {
00373 DVector tmp( _dim );
00374 tmp.setAll( defaultScaling );
00375 scaling.append( tmp );
00376 }
00377 }
00378 else
00379 {
00380 if ( _scaling.isEmpty( ) == BT_FALSE )
00381 {
00382 scaling.init( dim );
00383 scaling.setAll( defaultScaling );
00384 scaling.append( _scaling );
00385 }
00386 }
00387
00388 if ( lb.isEmpty( ) == BT_FALSE )
00389 {
00390 if ( _lb.isEmpty( ) == BT_FALSE )
00391 {
00392 lb.append( _lb );
00393 }
00394 else
00395 {
00396 DVector tmp( _dim );
00397 tmp.setAll( defaultLowerBound );
00398 lb.append( tmp );
00399 }
00400 }
00401 else
00402 {
00403 if ( _lb.isEmpty( ) == BT_FALSE )
00404 {
00405 lb.init( dim );
00406 lb.setAll( defaultLowerBound );
00407 lb.append( _lb );
00408 }
00409 }
00410
00411 if ( ub.isEmpty( ) == BT_FALSE )
00412 {
00413 if ( _ub.isEmpty( ) == BT_FALSE )
00414 {
00415 ub.append( _ub );
00416 }
00417 else
00418 {
00419 DVector tmp( _dim );
00420 tmp.setAll( defaultUpperBound );
00421 ub.append( tmp );
00422 }
00423 }
00424 else
00425 {
00426 if ( _ub.isEmpty( ) == BT_FALSE )
00427 {
00428 ub.init( dim );
00429 ub.setAll( defaultUpperBound );
00430 ub.append( _ub );
00431 }
00432 }
00433
00434 dim = newDim;
00435
00436 return SUCCESSFUL_RETURN;
00437 }
00438
00439
00440
00441 returnValue VariableSettings::getName( uint idx,
00442 char* _name
00443 ) const
00444 {
00445 if ( hasNames( ) != BT_TRUE )
00446 return ACADOERROR( RET_MEMBER_NOT_INITIALISED );
00447
00448 if ( idx >= dim )
00449 return ACADOERROR( RET_INDEX_OUT_OF_BOUNDS );
00450
00451 strncpy( _name,names[idx],MAX_LENGTH_NAME );
00452 _name[MAX_LENGTH_NAME] = '\0';
00453
00454 return SUCCESSFUL_RETURN;
00455 }
00456
00457
00458 returnValue VariableSettings::setName( uint idx,
00459 const char* const _name
00460 )
00461 {
00462 if ( idx >= dim )
00463 return ACADOERROR( RET_INDEX_OUT_OF_BOUNDS );
00464
00465 if ( _name == 0 )
00466 return ACADOERROR( RET_INVALID_ARGUMENTS );
00467
00468 if ( hasNames( ) != BT_TRUE )
00469 {
00470
00471 names = new char*[dim];
00472
00473 for( uint i=0; i<dim; ++i )
00474 {
00475 names[i] = new char[MAX_LENGTH_NAME+1];
00476 strncpy( names[i],defaultName,MAX_LENGTH_NAME );
00477 }
00478 }
00479
00480 strncpy( names[idx],_name,MAX_LENGTH_NAME );
00481 names[MAX_LENGTH_NAME] = NULL;
00482
00483 return SUCCESSFUL_RETURN;
00484 }
00485
00486
00487
00488 returnValue VariableSettings::getUnit( uint idx,
00489 char* _unit
00490 ) const
00491 {
00492 if ( idx >= dim )
00493 return ACADOERROR( RET_INDEX_OUT_OF_BOUNDS );
00494
00495 if ( hasUnits( ) != BT_TRUE )
00496 return ACADOERROR( RET_MEMBER_NOT_INITIALISED );
00497
00498 strncpy( _unit,units[idx],MAX_LENGTH_NAME );
00499 _unit[MAX_LENGTH_NAME] = '\0';
00500
00501 return SUCCESSFUL_RETURN;
00502 }
00503
00504
00505 returnValue VariableSettings::setUnit( uint idx,
00506 const char* const _unit
00507 )
00508 {
00509 if ( idx >= dim )
00510 return ACADOERROR( RET_INDEX_OUT_OF_BOUNDS );
00511
00512 if ( _unit == 0 )
00513 return ACADOERROR( RET_INVALID_ARGUMENTS );
00514
00515 if ( hasUnits( ) != BT_TRUE )
00516 {
00517
00518 units = new char*[dim];
00519
00520 for( uint i=0; i<dim; ++i )
00521 {
00522 units[i] = new char[MAX_LENGTH_NAME+1];
00523 strncpy( units[i],defaultUnit,MAX_LENGTH_NAME );
00524 }
00525 }
00526
00527 strncpy( units[idx],_unit,MAX_LENGTH_NAME );
00528 units[MAX_LENGTH_NAME] = NULL;
00529
00530 return SUCCESSFUL_RETURN;
00531 }
00532
00533
00534
00535
00536
00537
00538
00539 returnValue VariableSettings::clear( )
00540 {
00541 uint i;
00542
00543 if ( names != 0 )
00544 {
00545 for( i=0; i<dim; ++i )
00546 {
00547 if ( names[i] != 0 )
00548 delete[] (names[i]);
00549 }
00550
00551 free( names );
00552 names = 0;
00553 }
00554
00555 if ( units != 0 )
00556 {
00557 for( i=0; i<dim; ++i )
00558 {
00559 if ( units[i] != 0 )
00560 delete[] (units[i]);
00561 }
00562
00563 free( units );
00564 units = 0;
00565 }
00566
00567 return SUCCESSFUL_RETURN;
00568 }
00569
00570
00571
00572 CLOSE_NAMESPACE_ACADO
00573
00574
00575
00576