Commit ac1b7fa3d4fbf08c74e7bffee5f720c07fec32cf
1 parent
175afd1b
Methods set(), append() and insert() now return a pointer to the appended model.
Showing
3 changed files
with
105 additions
and
91 deletions
include/GModels.hpp
| ... | ... | @@ -82,11 +82,11 @@ public: |
| 82 | 82 | GModels* clone(void) const; |
| 83 | 83 | int size(void) const { return m_models.size(); } |
| 84 | 84 | bool isempty(void) const { return m_models.empty(); } |
| 85 | - void set(const int& index, const GModel& model); | |
| 86 | - void set(const std::string& name, const GModel& model); | |
| 87 | - void append(const GModel& model); | |
| 88 | - void insert(const int& index, const GModel& model); | |
| 89 | - void insert(const std::string& name, const GModel& model); | |
| 85 | + GModel* set(const int& index, const GModel& model); | |
| 86 | + GModel* set(const std::string& name, const GModel& model); | |
| 87 | + GModel* append(const GModel& model); | |
| 88 | + GModel* insert(const int& index, const GModel& model); | |
| 89 | + GModel* insert(const std::string& name, const GModel& model); | |
| 90 | 90 | void remove(const int& index); |
| 91 | 91 | void remove(const std::string& name); |
| 92 | 92 | void reserve(const int& num) { return m_models.reserve(num); } | ... | ... |
pyext/GModels.i
| ... | ... | @@ -63,11 +63,11 @@ public: |
| 63 | 63 | GModels* clone(void) const; |
| 64 | 64 | int size(void) const; |
| 65 | 65 | bool isempty(void) const; |
| 66 | - void set(const int& index, const GModel& model); | |
| 67 | - void set(const std::string& name, const GModel& model); | |
| 68 | - void append(const GModel& model); | |
| 69 | - void insert(const int& index, const GModel& model); | |
| 70 | - void insert(const std::string& name, const GModel& model); | |
| 66 | + GModel* set(const int& index, const GModel& model); | |
| 67 | + GModel* set(const std::string& name, const GModel& model); | |
| 68 | + GModel* append(const GModel& model); | |
| 69 | + GModel* insert(const int& index, const GModel& model); | |
| 70 | + GModel* insert(const std::string& name, const GModel& model); | |
| 71 | 71 | void remove(const int& index); |
| 72 | 72 | void remove(const std::string& name); |
| 73 | 73 | void reserve(const int& num); | ... | ... |
src/model/GModels.cpp
| ... | ... | @@ -299,23 +299,98 @@ GModels* GModels::clone(void) const |
| 299 | 299 | |
| 300 | 300 | |
| 301 | 301 | /***********************************************************************//** |
| 302 | + * @brief Set model in container | |
| 303 | + * | |
| 304 | + * @param[in] index Model index [0,...,size()-1]. | |
| 305 | + * @param[in] model Model. | |
| 306 | + * @return Pointer to deep copy of model. | |
| 307 | + * | |
| 308 | + * @exception GException::out_of_range | |
| 309 | + * Model index is out of range. | |
| 310 | + * | |
| 311 | + * Set model in the container. A deep copy of the model will be made. | |
| 312 | + ***************************************************************************/ | |
| 313 | +GModel* GModels::set(const int& index, const GModel& model) | |
| 314 | +{ | |
| 315 | + // Compile option: raise exception if index is out of range | |
| 316 | + #if defined(G_RANGE_CHECK) | |
| 317 | + if (index < 0 || index >= size()) { | |
| 318 | + throw GException::out_of_range(G_SET1, index, 0, size()-1); | |
| 319 | + } | |
| 320 | + #endif | |
| 321 | + | |
| 322 | + // Delete any existing model | |
| 323 | + if (m_models[index] != NULL) delete m_models[index]; | |
| 324 | + | |
| 325 | + // Assign new model by cloning | |
| 326 | + m_models[index] = model.clone(); | |
| 327 | + | |
| 328 | + // Set parameter pointers | |
| 329 | + set_pointers(); | |
| 330 | + | |
| 331 | + // Return pointer to model | |
| 332 | + return m_models[index]; | |
| 333 | +} | |
| 334 | + | |
| 335 | + | |
| 336 | +/***********************************************************************//** | |
| 337 | + * @brief Set model in container | |
| 338 | + * | |
| 339 | + * @param[in] name Model name. | |
| 340 | + * @param[in] model Model pointer. | |
| 341 | + * @return Pointer to deep copy of model. | |
| 342 | + * | |
| 343 | + * @exception GException::model_not_found | |
| 344 | + * Model with specified name not found in container. | |
| 345 | + * | |
| 346 | + * Set model in the container. A deep copy of the model will be made. | |
| 347 | + ***************************************************************************/ | |
| 348 | +GModel* GModels::set(const std::string& name, const GModel& model) | |
| 349 | +{ | |
| 350 | + // Get parameter index | |
| 351 | + int index = get_index(name); | |
| 352 | + | |
| 353 | + // Throw exception if parameter name was not found | |
| 354 | + if (index >= size()) { | |
| 355 | + throw GException::model_not_found(G_SET2, name); | |
| 356 | + } | |
| 357 | + | |
| 358 | + // Delete any existing model | |
| 359 | + if (m_models[index] != NULL) delete m_models[index]; | |
| 360 | + | |
| 361 | + // Assign new model by cloning | |
| 362 | + m_models[index] = model.clone(); | |
| 363 | + | |
| 364 | + // Set parameter pointers | |
| 365 | + set_pointers(); | |
| 366 | + | |
| 367 | + // Return pointer to model | |
| 368 | + return m_models[index]; | |
| 369 | +} | |
| 370 | + | |
| 371 | + | |
| 372 | +/***********************************************************************//** | |
| 302 | 373 | * @brief Append model to container |
| 303 | 374 | * |
| 304 | 375 | * @param[in] model Model. |
| 376 | + * @return Pointer to deep copy of model. | |
| 305 | 377 | * |
| 306 | 378 | * Appends model to the container by making a deep copy of the model and |
| 307 | 379 | * storing its pointer. |
| 308 | 380 | ***************************************************************************/ |
| 309 | -void GModels::append(const GModel& model) | |
| 381 | +GModel* GModels::append(const GModel& model) | |
| 310 | 382 | { |
| 383 | + // Create deep copy of model | |
| 384 | + GModel* ptr = model.clone(); | |
| 385 | + | |
| 311 | 386 | // Append deep copy of model |
| 312 | - m_models.push_back(model.clone()); | |
| 387 | + m_models.push_back(ptr); | |
| 313 | 388 | |
| 314 | 389 | // Set parameter pointers |
| 315 | 390 | set_pointers(); |
| 316 | 391 | |
| 317 | - // Return | |
| 318 | - return; | |
| 392 | + // Return pointer to model | |
| 393 | + return ptr; | |
| 319 | 394 | } |
| 320 | 395 | |
| 321 | 396 | |
| ... | ... | @@ -324,6 +399,7 @@ void GModels::append(const GModel& model) |
| 324 | 399 | * |
| 325 | 400 | * @param[in] index Model index [0,...,size()-1]. |
| 326 | 401 | * @param[in] model Model. |
| 402 | + * @return Pointer to deep copy of model. | |
| 327 | 403 | * |
| 328 | 404 | * @exception GException::out_of_range |
| 329 | 405 | * Model index is out of range. |
| ... | ... | @@ -331,7 +407,7 @@ void GModels::append(const GModel& model) |
| 331 | 407 | * Inserts a @p model into the container before the model with the specified |
| 332 | 408 | * @p index. |
| 333 | 409 | ***************************************************************************/ |
| 334 | -void GModels::insert(const int& index, const GModel& model) | |
| 410 | +GModel* GModels::insert(const int& index, const GModel& model) | |
| 335 | 411 | { |
| 336 | 412 | // Compile option: raise exception if index is out of range |
| 337 | 413 | #if defined(G_RANGE_CHECK) |
| ... | ... | @@ -347,14 +423,17 @@ void GModels::insert(const int& index, const GModel& model) |
| 347 | 423 | } |
| 348 | 424 | #endif |
| 349 | 425 | |
| 426 | + // Create deep copy of model | |
| 427 | + GModel* ptr = model.clone(); | |
| 428 | + | |
| 350 | 429 | // Inserts deep copy of model |
| 351 | - m_models.insert(m_models.begin()+index, model.clone()); | |
| 430 | + m_models.insert(m_models.begin()+index, ptr); | |
| 352 | 431 | |
| 353 | 432 | // Set parameter pointers |
| 354 | 433 | set_pointers(); |
| 355 | 434 | |
| 356 | - // Return | |
| 357 | - return; | |
| 435 | + // Return pointer to model | |
| 436 | + return ptr; | |
| 358 | 437 | } |
| 359 | 438 | |
| 360 | 439 | |
| ... | ... | @@ -363,6 +442,7 @@ void GModels::insert(const int& index, const GModel& model) |
| 363 | 442 | * |
| 364 | 443 | * @param[in] name Model name. |
| 365 | 444 | * @param[in] model Model. |
| 445 | + * @return Pointer to deep copy of model. | |
| 366 | 446 | * |
| 367 | 447 | * @exception GException::model_not_found |
| 368 | 448 | * Model with specified name not found in container. |
| ... | ... | @@ -370,7 +450,7 @@ void GModels::insert(const int& index, const GModel& model) |
| 370 | 450 | * Inserts a @p model into the container before the model with the specified |
| 371 | 451 | * @p name. |
| 372 | 452 | ***************************************************************************/ |
| 373 | -void GModels::insert(const std::string& name, const GModel& model) | |
| 453 | +GModel* GModels::insert(const std::string& name, const GModel& model) | |
| 374 | 454 | { |
| 375 | 455 | // Get parameter index |
| 376 | 456 | int index = get_index(name); |
| ... | ... | @@ -380,14 +460,17 @@ void GModels::insert(const std::string& name, const GModel& model) |
| 380 | 460 | throw GException::model_not_found(G_INSERT2, name); |
| 381 | 461 | } |
| 382 | 462 | |
| 463 | + // Create deep copy of model | |
| 464 | + GModel* ptr = model.clone(); | |
| 465 | + | |
| 383 | 466 | // Inserts deep copy of model |
| 384 | - m_models.insert(m_models.begin()+index, model.clone()); | |
| 467 | + m_models.insert(m_models.begin()+index, ptr); | |
| 385 | 468 | |
| 386 | 469 | // Set parameter pointers |
| 387 | 470 | set_pointers(); |
| 388 | 471 | |
| 389 | - // Return | |
| 390 | - return; | |
| 472 | + // Return pointer to model | |
| 473 | + return ptr; | |
| 391 | 474 | } |
| 392 | 475 | |
| 393 | 476 | |
| ... | ... | @@ -488,75 +571,6 @@ void GModels::extend(const GModels& models) |
| 488 | 571 | |
| 489 | 572 | |
| 490 | 573 | /***********************************************************************//** |
| 491 | - * @brief Set model in container | |
| 492 | - * | |
| 493 | - * @param[in] index Model index [0,...,size()-1]. | |
| 494 | - * @param[in] model Model. | |
| 495 | - * | |
| 496 | - * @exception GException::out_of_range | |
| 497 | - * Model index is out of range. | |
| 498 | - * | |
| 499 | - * Set model in the container. A deep copy of the model will be made. | |
| 500 | - ***************************************************************************/ | |
| 501 | -void GModels::set(const int& index, const GModel& model) | |
| 502 | -{ | |
| 503 | - // Compile option: raise exception if index is out of range | |
| 504 | - #if defined(G_RANGE_CHECK) | |
| 505 | - if (index < 0 || index >= size()) { | |
| 506 | - throw GException::out_of_range(G_SET1, index, 0, size()-1); | |
| 507 | - } | |
| 508 | - #endif | |
| 509 | - | |
| 510 | - // Delete any existing model | |
| 511 | - if (m_models[index] != NULL) delete m_models[index]; | |
| 512 | - | |
| 513 | - // Assign new model by cloning | |
| 514 | - m_models[index] = model.clone(); | |
| 515 | - | |
| 516 | - // Set parameter pointers | |
| 517 | - set_pointers(); | |
| 518 | - | |
| 519 | - // Return | |
| 520 | - return; | |
| 521 | -} | |
| 522 | - | |
| 523 | - | |
| 524 | -/***********************************************************************//** | |
| 525 | - * @brief Set model in container | |
| 526 | - * | |
| 527 | - * @param[in] name Model name. | |
| 528 | - * @param[in] model Model pointer. | |
| 529 | - * | |
| 530 | - * @exception GException::model_not_found | |
| 531 | - * Model with specified name not found in container. | |
| 532 | - * | |
| 533 | - * Set model in the container. A deep copy of the model will be made. | |
| 534 | - ***************************************************************************/ | |
| 535 | -void GModels::set(const std::string& name, const GModel& model) | |
| 536 | -{ | |
| 537 | - // Get parameter index | |
| 538 | - int index = get_index(name); | |
| 539 | - | |
| 540 | - // Throw exception if parameter name was not found | |
| 541 | - if (index >= size()) { | |
| 542 | - throw GException::model_not_found(G_SET2, name); | |
| 543 | - } | |
| 544 | - | |
| 545 | - // Delete any existing model | |
| 546 | - if (m_models[index] != NULL) delete m_models[index]; | |
| 547 | - | |
| 548 | - // Assign new model by cloning | |
| 549 | - m_models[index] = model.clone(); | |
| 550 | - | |
| 551 | - // Set parameter pointers | |
| 552 | - set_pointers(); | |
| 553 | - | |
| 554 | - // Return | |
| 555 | - return; | |
| 556 | -} | |
| 557 | - | |
| 558 | - | |
| 559 | -/***********************************************************************//** | |
| 560 | 574 | * @brief Load models from XML file |
| 561 | 575 | * |
| 562 | 576 | * @param[in] filename XML filename. | ... | ... |