| ... | ... |
@@ -316,166 +316,166 @@ |
| 316 | 316 |
void first(Item& item) const {
|
| 317 | 317 |
container->first(item); |
| 318 | 318 |
} |
| 319 | 319 |
|
| 320 | 320 |
/// \brief Next item in the container. |
| 321 | 321 |
/// |
| 322 | 322 |
/// Returns the next item in the container. It is |
| 323 | 323 |
/// for iterate on the container. |
| 324 | 324 |
void next(Item& item) const {
|
| 325 | 325 |
container->next(item); |
| 326 | 326 |
} |
| 327 | 327 |
|
| 328 | 328 |
/// \brief Returns the id of the item. |
| 329 | 329 |
/// |
| 330 | 330 |
/// Returns the id of the item provided by the container. |
| 331 | 331 |
int id(const Item& item) const {
|
| 332 | 332 |
return container->id(item); |
| 333 | 333 |
} |
| 334 | 334 |
|
| 335 | 335 |
/// \brief Returns the maximum id of the container. |
| 336 | 336 |
/// |
| 337 | 337 |
/// Returns the maximum id of the container. |
| 338 | 338 |
int maxId() const {
|
| 339 | 339 |
return container->maxId(Item()); |
| 340 | 340 |
} |
| 341 | 341 |
|
| 342 | 342 |
protected: |
| 343 | 343 |
|
| 344 | 344 |
void attach(ObserverBase& observer) {
|
| 345 | 345 |
observer._index = _observers.insert(_observers.begin(), &observer); |
| 346 | 346 |
observer._notifier = this; |
| 347 | 347 |
} |
| 348 | 348 |
|
| 349 | 349 |
void detach(ObserverBase& observer) {
|
| 350 | 350 |
_observers.erase(observer._index); |
| 351 | 351 |
observer._index = _observers.end(); |
| 352 | 352 |
observer._notifier = 0; |
| 353 | 353 |
} |
| 354 | 354 |
|
| 355 | 355 |
public: |
| 356 | 356 |
|
| 357 | 357 |
/// \brief Notifies all the registed observers about an item added to |
| 358 | 358 |
/// the container. |
| 359 | 359 |
/// |
| 360 | 360 |
/// It notifies all the registed observers about an item added to |
| 361 | 361 |
/// the container. |
| 362 | 362 |
/// |
| 363 | 363 |
void add(const Item& item) {
|
| 364 | 364 |
typename Observers::reverse_iterator it; |
| 365 | 365 |
try {
|
| 366 | 366 |
for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
|
| 367 | 367 |
(*it)->add(item); |
| 368 | 368 |
} |
| 369 | 369 |
} catch (...) {
|
| 370 | 370 |
typename Observers::iterator jt; |
| 371 | 371 |
for (jt = it.base(); jt != _observers.end(); ++jt) {
|
| 372 | 372 |
(*jt)->erase(item); |
| 373 | 373 |
} |
| 374 | 374 |
throw; |
| 375 | 375 |
} |
| 376 | 376 |
} |
| 377 | 377 |
|
| 378 | 378 |
/// \brief Notifies all the registed observers about more item added to |
| 379 | 379 |
/// the container. |
| 380 | 380 |
/// |
| 381 | 381 |
/// It notifies all the registed observers about more item added to |
| 382 | 382 |
/// the container. |
| 383 | 383 |
/// |
| 384 | 384 |
void add(const std::vector<Item>& items) {
|
| 385 | 385 |
typename Observers::reverse_iterator it; |
| 386 | 386 |
try {
|
| 387 | 387 |
for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
|
| 388 | 388 |
(*it)->add(items); |
| 389 | 389 |
} |
| 390 | 390 |
} catch (...) {
|
| 391 | 391 |
typename Observers::iterator jt; |
| 392 | 392 |
for (jt = it.base(); jt != _observers.end(); ++jt) {
|
| 393 | 393 |
(*jt)->erase(items); |
| 394 | 394 |
} |
| 395 | 395 |
throw; |
| 396 | 396 |
} |
| 397 | 397 |
} |
| 398 | 398 |
|
| 399 | 399 |
/// \brief Notifies all the registed observers about an item erased from |
| 400 | 400 |
/// the container. |
| 401 | 401 |
/// |
| 402 | 402 |
/// It notifies all the registed observers about an item erased from |
| 403 | 403 |
/// the container. |
| 404 | 404 |
/// |
| 405 | 405 |
void erase(const Item& item) throw() {
|
| 406 | 406 |
typename Observers::iterator it = _observers.begin(); |
| 407 | 407 |
while (it != _observers.end()) {
|
| 408 | 408 |
try {
|
| 409 | 409 |
(*it)->erase(item); |
| 410 | 410 |
++it; |
| 411 | 411 |
} catch (const ImmediateDetach&) {
|
| 412 |
it = _observers.erase(it); |
|
| 413 | 412 |
(*it)->_index = _observers.end(); |
| 414 | 413 |
(*it)->_notifier = 0; |
| 414 |
it = _observers.erase(it); |
|
| 415 | 415 |
} |
| 416 | 416 |
} |
| 417 | 417 |
} |
| 418 | 418 |
|
| 419 | 419 |
/// \brief Notifies all the registed observers about more item erased |
| 420 | 420 |
/// from the container. |
| 421 | 421 |
/// |
| 422 | 422 |
/// It notifies all the registed observers about more item erased from |
| 423 | 423 |
/// the container. |
| 424 | 424 |
/// |
| 425 | 425 |
void erase(const std::vector<Item>& items) {
|
| 426 | 426 |
typename Observers::iterator it = _observers.begin(); |
| 427 | 427 |
while (it != _observers.end()) {
|
| 428 | 428 |
try {
|
| 429 | 429 |
(*it)->erase(items); |
| 430 | 430 |
++it; |
| 431 | 431 |
} catch (const ImmediateDetach&) {
|
| 432 |
it = _observers.erase(it); |
|
| 433 | 432 |
(*it)->_index = _observers.end(); |
| 434 | 433 |
(*it)->_notifier = 0; |
| 434 |
it = _observers.erase(it); |
|
| 435 | 435 |
} |
| 436 | 436 |
} |
| 437 | 437 |
} |
| 438 | 438 |
|
| 439 | 439 |
/// \brief Notifies all the registed observers about the container is |
| 440 | 440 |
/// built. |
| 441 | 441 |
/// |
| 442 | 442 |
/// Notifies all the registed observers about the container is built |
| 443 | 443 |
/// from an empty container. |
| 444 | 444 |
void build() {
|
| 445 | 445 |
typename Observers::reverse_iterator it; |
| 446 | 446 |
try {
|
| 447 | 447 |
for (it = _observers.rbegin(); it != _observers.rend(); ++it) {
|
| 448 | 448 |
(*it)->build(); |
| 449 | 449 |
} |
| 450 | 450 |
} catch (...) {
|
| 451 | 451 |
typename Observers::iterator jt; |
| 452 | 452 |
for (jt = it.base(); jt != _observers.end(); ++jt) {
|
| 453 | 453 |
(*jt)->clear(); |
| 454 | 454 |
} |
| 455 | 455 |
throw; |
| 456 | 456 |
} |
| 457 | 457 |
} |
| 458 | 458 |
|
| 459 | 459 |
/// \brief Notifies all the registed observers about all items are |
| 460 | 460 |
/// erased. |
| 461 | 461 |
/// |
| 462 | 462 |
/// Notifies all the registed observers about all items are erased |
| 463 | 463 |
/// from the container. |
| 464 | 464 |
void clear() {
|
| 465 | 465 |
typename Observers::iterator it = _observers.begin(); |
| 466 | 466 |
while (it != _observers.end()) {
|
| 467 | 467 |
try {
|
| 468 | 468 |
(*it)->clear(); |
| 469 | 469 |
++it; |
| 470 | 470 |
} catch (const ImmediateDetach&) {
|
| 471 |
it = _observers.erase(it); |
|
| 472 | 471 |
(*it)->_index = _observers.end(); |
| 473 | 472 |
(*it)->_notifier = 0; |
| 473 |
it = _observers.erase(it); |
|
| 474 | 474 |
} |
| 475 | 475 |
} |
| 476 | 476 |
} |
| 477 | 477 |
}; |
| 478 | 478 |
|
| 479 | 479 |
} |
| 480 | 480 |
|
| 481 | 481 |
#endif |
0 comments (0 inline)