@@ -427,16 +427,13 @@ struct Command
427
427
428
428
/* *
429
429
@defgroup visitor Visitor
430
+ @brief [Visitor pattern](https://en.wikipedia.org/wiki/Visitor_pattern)
431
+
432
+ https://refactoring.guru/design-patterns/visitor/cpp/example
430
433
@{
431
434
*/
432
435
433
- struct Visitor
434
- // / @brief is a pure virtual visitor of Sample_component and other specific components
435
- {
436
- struct Sample_component ;
437
- virtual string visit (const Sample_component&) const = 0;
438
- virtual ~Visitor () = default ;
439
- };
436
+ struct Visitor ;
440
437
441
438
struct Component
442
439
// / @brief accepts a pure virtual Visitor
@@ -445,22 +442,20 @@ struct Component
445
442
virtual ~Component () = default ;
446
443
};
447
444
448
- string client_visit ( const forward_list<unique_ptr<Component>>& components,
449
- Visitor& visitor)
450
- // / @brief knows only virtual visitor and component
445
+ struct Sample_component ;
446
+ struct Visitor
447
+ // / @brief is a pure virtual visitor of Sample_component and other specific components
451
448
{
452
- string res;
453
- for (auto && comp : components) {
454
- res += string (__func__) + " > " + comp->component_accept (visitor);
455
- // assert(rc);
456
- }
457
- // assert(rc);
458
- return res;
459
- }
449
+ // / overloaded function for each component
450
+ virtual string visit (const Sample_component&) const = 0;
451
+ virtual ~Visitor () = default ;
452
+ };
460
453
461
- struct Visitor :: Sample_component
454
+ struct Sample_component
462
455
: public Component
463
- // / @brief one of many components
456
+ /* * @brief one of many components
457
+ is independent from Sample_visitor and implementations of function visit.
458
+ */
464
459
{
465
460
string component_accept (Visitor& visitor) const override {
466
461
return string (__func__) + " > " + visitor.visit (*this );
@@ -471,6 +466,18 @@ struct Visitor::Sample_component
471
466
}
472
467
};
473
468
469
+ string client_visit (const forward_list<unique_ptr<Component>>& components,
470
+ const forward_list<unique_ptr<Visitor>>& visitors)
471
+ // / @brief knows only virtual visitor and component
472
+ {
473
+ string res;
474
+ for (auto && comp : components)
475
+ for (auto && vis : visitors) {
476
+ res += string (__func__) + " > " + comp->component_accept (*vis.get ());
477
+ }
478
+ return res;
479
+ }
480
+
474
481
/* *
475
482
Call hierarchy:
476
483
@@ -487,16 +494,19 @@ void visitor_demo()
487
494
// / Per each of the possible pairs of Sample_visitor and Sample_component
488
495
struct Sample_visitor
489
496
: public Visitor {
490
- string visit (const Visitor::Sample_component& c) const override {
497
+ // / overloaded function for each component
498
+ string visit (const Sample_component& c) const override {
491
499
return string (__func__) + " > " + c.component_method ();
492
500
}
493
501
};
494
502
495
503
forward_list<unique_ptr<Component>> components;
496
- components.emplace_front (new Visitor:: Sample_component);
504
+ components.emplace_front (new Sample_component);
497
505
Sample_visitor v;
498
- assert (client_visit (components, v) ==
499
- " client_visit > component_accept > visit > component_method" );
506
+ forward_list<unique_ptr<Visitor>> visitors;
507
+ visitors.emplace_front (new Sample_visitor);
508
+ assert (client_visit (components, visitors) ==
509
+ " client_visit > component_accept > visit > component_method" );
500
510
}
501
511
502
512
// / @} visitor
0 commit comments