-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathpatterns.cpp
983 lines (800 loc) · 24.1 KB
/
patterns.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
#include <bits/stdc++.h>
using namespace std;
/**
@file
@brief skeleton C++ examples of OOP and Design patterns
© 2021 Constantine Shulyupin
Patterns help to conform to [SOLID principles](https://en.wikipedia.org/wiki/SOLID):
- [<b>S</b>ingle-responsibility](https://en.wikipedia.org/wiki/Single-responsibility_principle)
- high [cohesion](https://en.wikipedia.org/wiki/Cohesion_(computer_science))
- [<b>O</b>pen-closed](https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle)
- open for extension
- but closed for modification
- [<b>L</b>iskov substitution](https://en.wikipedia.org/wiki/Liskov_substitution_principle)
- [composition over inheritance](https://en.wikipedia.org/wiki/Composition_over_inheritance)
- [<b>I</b>nterface segregation](https://en.wikipedia.org/wiki/Interface_segregation_principle)
- use many specific interfaces
- [<b>D</b>ependency inversion](https://en.wikipedia.org/wiki/Dependency_inversion_principle)
- don't depend on implementations
- depend on interfaces
- [Loose coupling](https://en.wikipedia.org/wiki/Loose_coupling)
Coding style:
Using struct because is it like class with default public members and methods.
Less is more. Skeleton code with minimal optional code and duplications.
Each word "Sample" in an inventer assumes multiple instances like Sample1, Sample2 ... SampleN
Contents
- @ref OOP
- @ref DP
*/
/**
@defgroup OOP C++ OOP patterns
@brief some examples in C++
Four Pillars of
[Object-oriented_programming](https://en.wikipedia.org/wiki/Object-oriented_programming):
- [Abstraction](https://en.wikipedia.org/wiki/Abstraction_(computer_science))
- [Encapsulation](https://en.wikipedia.org/wiki/Encapsulation_(computer_programming))
- [Inheritance](https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming))
- [Polymorphism](https://en.wikipedia.org/wiki/Polymorphism_(computer_science))
[object-oriented-programming-in-cpp](https://www.geeksforgeeks.org/object-oriented-programming-in-cpp)
[class](https://en.cppreference.com/w/cpp/language/class)
[derived_class](https://en.cppreference.com/w/cpp/language/derived_class)
[abstract_class](https://en.cppreference.com/w/cpp/language/abstract_class)
[shared_lock](https://en.cppreference.com/w/cpp/thread/shared_lock)
Three independent interfaces
Setter_interface, Getter_interface and Change_interface demonstrate
[Interface segregation](https://en.wikipedia.org/wiki/Interface_segregation_principle)
[Use multiple inheritance to represent multiple distinct interfaces](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c135-use-multiple-inheritance-to-represent-multiple-distinct-interfaces)
@{
*/
struct Module { };
/// [Object composition](https://en.wikipedia.org/wiki/Object_composition)
struct Composition {
Module m1, m2;
};
/** [Aggregation](https://en.wikipedia.org/wiki/Object_composition#Aggregation)
by reference
*/
struct Reference {
Module &m1, &m2;
Reference() = default;
};
/** [Pointer](https://en.wikipedia.org/wiki/Pointer_(computer_programming)#C_and_C++)
is also can be used for aggregation
*/
struct Pointer {
Module* ptr;
shared_ptr<Module> sm;
unique_ptr<Module> um;
};
/// [Associations](https://en.wikipedia.org/wiki/Association_(object-oriented_programming))
void associations_demo()
{
Module m1, m2;
Reference r { m1, m2 };
Pointer p { &m1 };
// References can't be changed or zeroed like pointers during runtime.
p.ptr = nullptr;
}
/// @brief is a sample of setter abstract interface for Synchronized_encapsulated_value
template <typename ValueType>
struct Setter_interface {
virtual void set(ValueType i) = 0;
virtual ~Setter_interface() noexcept = default;
};
template <typename ValueType>
struct Getter_interface
/// @brief is a sample of getter abstract interface for Synchronized_encapsulated_value
{
virtual ValueType get() const = 0;
virtual ~Getter_interface() noexcept = default;
};
template <typename ValueType>
struct Change_interface
/// @brief is a sample of changer abstract interface for Synchronized_encapsulated_value
{
virtual void change(ValueType c) = 0;
virtual ~Change_interface() noexcept = default;
};
template <typename ValueType>
class Synchronized_encapsulated_value
/**
@brief [encapsulating](https://en.wikipedia.org/wiki/Encapsulation_(computer_programming))
class with only public accessor and [mutator](https://en.wikipedia.org/wiki/Mutator_method) intrfaces
Classes by default are private. This class doesn't contain public members.
Uses [Readers–writer_lock](https://en.wikipedia.org/wiki/Readers–writer_lock).
See also:
- [Boost synchronized values](https://www.boost.org/doc/libs/release/doc/html/thread/sds.html#thread.sds.synchronized_valuesxxx)
- [boost/thread/synchronized_value.hpp](https://github.com/boostorg/thread/blob/HEAD/include/boost/thread/synchronized_value.hpp)
*/
: public Setter_interface<ValueType>,
public Getter_interface<ValueType>,
public Change_interface<ValueType> {
void set(ValueType i) override
{
scoped_lock writer_lock(mtx);
value = i;
}
ValueType get() const override
{
shared_lock reader_lock(mtx); /// [reader writer locks](https://www.modernescpp.com/index.php/reader-writer-locks)
return value;
}
void change(ValueType c) override
{
scoped_lock writer_lock(mtx);
value += c;
}
mutable shared_mutex mtx; ///< [shared_mutex](https://en.cppreference.com/w/cpp/thread/shared_mutex)
ValueType value;
};
void synchronized_encapsulated_value()
/**
Lambda expression 'client' demonstrates [Dependency inversion](https://en.wikipedia.org/wiki/Dependency_inversion_principle) -
it doesn't depends from implementation Synchronized_encapsulated_value but depends only from interfaces.
*/
{
auto client = [](Setter_interface<int>& s, Getter_interface<int>& g) {
s.set(1);
assert(g.get() == 1);
};
Synchronized_encapsulated_value<int> v;
Setter_interface<int>& s = v;
Getter_interface<int>& g = v;
client(s, g);
auto client2 = [](Setter_interface<string>& s, Getter_interface<string>& g) {
s.set("abc");
assert(g.get() == "abc");
};
Synchronized_encapsulated_value<string> v2;
Setter_interface<string>& s2(v2);
Getter_interface<string>& g2(v2);
Change_interface<string>& c2(v2);
client2(s2, g2);
c2.change("de");
assert(g2.get() == "abcde");
}
void oop_demo()
{
associations_demo();
synchronized_encapsulated_value();
}
/// @}
/**
@defgroup DP Design patterns skeleton examples
@brief [Software design patterns](https://en.wikipedia.org/wiki/Software_design_pattern)
https://refactoring.guru/design-patterns
[C++ Programming: Code patterns design](https://en.wikibooks.org/wiki/C++_Programming/Code/Design_Patterns)
Disclaimer:
Example code below for simplicity doesn't utilize synchronization,
privatization and other administrative functions.
Don't forget to add locking, synchronization, encapsulation, privatization,
protection manually where it is required when using examples below.
@{
*/
struct Interface
/// @brief is a common pure virtual interface
{
virtual int method() = 0;
virtual ~Interface() noexcept = default;
};
/**
@defgroup CP Creational
@brief [Creational patterns](https://en.wikipedia.org/wiki/Creational_pattern)
https://refactoring.guru/design-patterns/creational-patterns
@{
*/
/**
The singleton will be automatically safely instantiated on the first call.
Define constructor body after the define */
#define SINGLETON(Singleton) \
public: \
/* Meyers Singleton realization */ \
static Singleton& instance() \
{ \
static Singleton me; \
return me; \
} \
Singleton(const Singleton&) = delete; \
Singleton& operator=(const Singleton&) = delete; \
Singleton(Singleton&&) = delete; \
Singleton& operator=(Singleton&&) = delete; \
\
private: \
Singleton()
struct Singleton_demo {
SINGLETON(Singleton_demo) {};
};
struct Factory_method_demo {
virtual unique_ptr<Interface> factory_method() = 0;
int client()
{
auto p(factory_method());
return p->method();
};
};
struct Sample_product
: Interface {
int data;
int method() override { return data; }
Sample_product(int d = 0)
: data(d)
{
}
};
struct Sample_factory_method_demo
: Factory_method_demo {
unique_ptr<Interface> factory_method() override
{
return make_unique<Sample_product>(123);
}
};
struct Abstract_factory {
virtual unique_ptr<Interface> create() = 0;
};
struct Sample_factory
: Abstract_factory {
virtual unique_ptr<Interface> create()
{
return make_unique<Sample_product>();
}
};
struct Prototype
/// @brief is the factory of himself
: Abstract_factory,
Interface {
int method() override { return 1; }
unique_ptr<Interface> create() override
{
auto clone = new Prototype(*this);
return unique_ptr<Interface>(clone);
}
};
struct Builder {
int data = 0;
Builder& add(int i)
{
data += i;
return *this;
}
Builder& operator<<(int i) { return add(i); }
Interface& create()
{
return *new Sample_product(data);
}
};
void creational_patterns_demo()
{
Singleton_demo& singe = Singleton_demo::instance();
unique_ptr<Abstract_factory> factory(new Sample_factory());
auto product = factory->create();
Prototype p1;
auto p2 = p1.create();
Sample_factory_method_demo C;
assert(C.client() == 123);
Interface& p = (Builder().add(1).add(2) << 3 << 4).create();
assert(p.method() == 10);
// stringstream looks like string builder
delete &p;
}
/// @} CP
/**
@defgroup SP Structural
@brief [Structural patterns](https://en.wikipedia.org/wiki/Structural_pattern)
https://refactoring.guru/design-patterns/structural-patterns
@{
*/
struct Standalone
/** @brief is wrapped by Bridge. AKA adaptee of Adapter
It could be a legacy interface playing adaptee role in Adapter pattern
*/
{
float standalone_method() const
{
return 1.01;
}
};
struct Bridge
/// @brief is a wrapper using different from Standalone interface. AKA Adapter
: public Interface {
Bridge(Standalone& s)
: standalone(s)
{
}
int method() override
{
return this->standalone.standalone_method();
}
private:
Standalone& standalone;
};
struct Proxy
/// @brief is a opaque wrapper with same as wrapped object Interface
: public Interface {
Proxy(Interface& o)
: subject(o)
{
}
int method() override
{
return this->subject.method();
}
private:
Interface& subject;
};
struct Decorator
/// @brief is a partial wrapper of an object with same as wrapped object Interface
: public Interface {
Decorator(Interface& o)
: subject(o)
{
}
int method() override
{
return 100 + this->subject.method();
}
Interface& subject; // decorated object is public
};
struct Composite
: public Interface {
int method() override
{
for (Interface& i : children)
i.method();
return 0;
}
forward_list<reference_wrapper<Interface>> children;
};
void structural_patterns_demo()
{
Standalone sa;
Bridge br(sa);
br.method();
Proxy p(br);
Decorator dec(br);
dec.method();
dec.subject.method();
p.method();
Composite comp;
comp.children.push_front(p);
comp.method();
}
/// @} SP
/**
@defgroup BP Behavioral
@brief [Behavioral patterns](https://en.wikipedia.org/wiki/Behavioral_pattern)
https://refactoring.guru/design-patterns/behavioral-patterns
@{
*/
/**
Credit: [observer](https://cpppatterns.com/patterns/observer.html)
*/
struct Subject;
struct Observer
/// @brief is virtual observer of a Subject
{
/// @brief without arguments
virtual void notify() {};
/// @brief with the only Subject argument
virtual void update(Subject& subject) {};
virtual ~Observer() noexcept = default;
};
struct Subject
/// @brief of Observer
{
void notify_observers()
{
for (Observer& o : observers) {
o.notify();
o.update(*this);
}
}
forward_list<reference_wrapper<Observer>> observers;
};
void observer_demo()
{
Observer o;
Subject s;
s.observers.push_front(o);
s.notify_observers();
}
/**
@defgroup PS Publish–subscribe pattern
@brief [Publish–subscribe pattern](https://en.wikipedia.org/wiki/Publish–subscribe_pattern)
@{
*/
struct Message { };
struct Subscriber {
void message(Message& m) {};
};
struct Publisher {
map<string, forward_list<reference_wrapper<Subscriber>>> topic_subscribers;
void publish(const string& topic, Message& m)
{
for (Subscriber& s : topic_subscribers[topic])
s.message(m);
}
};
void publisher_subscriber_demo()
{
Subscriber sub;
Publisher pub;
pub.topic_subscribers["sample_topic"].push_front(sub);
Message m;
pub.publish("sample_topic", m);
}
/// @} PS
/**
[Mediator_pattern](https://en.wikipedia.org/wiki/Mediator_pattern)
https://refactoring.guru/design-patterns/mediator
*/
struct Mediator;
struct Member {
Mediator* mediator;
void send(Message&);
void receive(Message&) { }
};
struct Mediator {
void register_member(Member& m)
{
m.mediator = this;
members.push_front(m);
}
void dispatch(Message& msg)
{
for (Member& m : members)
m.receive(msg);
}
forward_list<reference_wrapper<Member>> members;
};
void Member::send(Message& m)
{
mediator->dispatch(m);
}
void mediator_demo()
{
Member m1, m2;
Mediator md;
md.register_member(m1);
md.register_member(m2);
Message msg;
m1.send(msg);
}
struct Command
/** @brief encapsulates arguments. AKA Intent, operation.
[Command pattern](https://en.wikipedia.org/wiki/Command_pattern)
https://refactoring.guru/design-patterns/command/cpp/example
*/
{
virtual int execute() { return -1; };
};
/**
@defgroup visitor Visitor
@brief [Visitor pattern](https://en.wikipedia.org/wiki/Visitor_pattern)
https://refactoring.guru/design-patterns/visitor/cpp/example
@{
*/
struct Abstract_visitor;
struct Component
/// @brief accepts a pure virtual Abstract_visitor
{
virtual string component_accept(Abstract_visitor&) const = 0;
virtual ~Component() = default;
};
string client_visit(const forward_list<unique_ptr<Component>>& components,
const forward_list<unique_ptr<Abstract_visitor>>& visitors)
/// @brief knows only virtual visitor and component
{
string res;
for (auto&& v : visitors)
for (auto&& c : components) {
assert(typeid(c) != typeid(Component));
res += string(__func__) + " > " + c->component_accept(*v.get());
}
return res;
}
struct Sample_component;
struct Abstract_visitor
/// @brief is a pure virtual visitor of Sample_component and other specific components
{
/// overloaded function for each component subtype
virtual string visit(const Sample_component&) const = 0;
virtual ~Abstract_visitor() noexcept = default;
};
struct Sample_component
: public Component
/** @brief one of many components
is independent from Sample_visitor and implementations of function visit.
*/
{
string component_accept(Abstract_visitor& visitor) const override
{
assert(typeid(*this) == typeid(Sample_component));
assert(typeid(visitor) != typeid(Abstract_visitor));
return string(__func__) + " > " + visitor.visit(*this);
}
/// @brief is not virtual
string sample_component_method() const
{
return __func__;
}
};
/**
Call hierarchy:
visitor_demo
client_visit
component_accept
visit
sample_component_method
*/
void visitor_demo()
{
/// @brief is one of many specific visitors with custom method visit
/// Per each of the possible pairs of Sample_visitor and Sample_component
struct Sample_visitor
: public Abstract_visitor {
/// overloaded function for each component
string visit(const Sample_component& sc) const override
{
assert(typeid(*this) == typeid(Sample_visitor));
assert(typeid(sc) == typeid(Sample_component));
return string(__func__) + " > " + sc.sample_component_method();
}
};
forward_list<unique_ptr<Component>> components;
components.emplace_front(new Sample_component);
forward_list<unique_ptr<Abstract_visitor>> visitors;
visitors.emplace_front(new Sample_visitor);
assert(client_visit(components, visitors) == "client_visit > component_accept > visit > sample_component_method");
// flat code of expanded client_visit:
for (auto&& v : visitors) {
if (auto sv = dynamic_cast<Sample_visitor*>(v.get()))
for (auto&& c : components) {
if (auto sc = dynamic_cast<Sample_component*>(c.get()))
// inside component_accept:
sv->visit(*sc);
else {
};
}
else {
/* And so on for each pair of visitor and component.
Total number of pairs is multiplication of
number components and number of visitors.
*/
};
}
}
// std::visit demo:
static stringstream output;
struct Visitor_op {
void operator()(int arg) { output << "int "; };
void operator()(long arg) { output << "long "; };
void operator()(double arg) { output << "double "; };
void operator()(string arg) { output << "string "; };
};
void visitor_std_demo()
/// Demonstration of standard template [visit](https://en.cppreference.com/w/cpp/utility/variant/visit)
{
// trivial visit call
visit([](auto&& arg) {}, variant<int> { 1 });
// practical visit usage
using Components = variant<int, long, double, string>;
vector<Components> comps = { 10, 15l, 1.5, "hello" };
Visitor_op visitors;
for (auto& c : comps)
visit(visitors, c);
assert(output.str() == "int long double string ");
}
/// @} visitor
struct Handler
/// @brief is a virtual command handler of Chain_of_responsibility
{
/// Specific handler can process a command and return non-negative
virtual int handle(Command& cmnd) { return cmnd.execute(); };
virtual ~Handler() noexcept = default;
};
struct Chain_of_responsibility
: Handler
/** @brief list based implementation without recursion
[Chain-of-responsibility pattern](https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern)
https://refactoring.guru/design-patterns/chain-of-responsibility
*/
{
void register_handler(Handler&& h, bool front = false)
{
if (front)
handlers.push_front(h);
else
handlers.push_back(h);
}
int handle(Command& cmnd) override
{
int rc = -1;
for (Handler& h : handlers)
if ((rc = h.handle(cmnd)) >= 0)
return rc;
return rc;
}
private:
list<reference_wrapper<Handler>> handlers;
};
void behavioral_patterns_demo()
{
observer_demo();
publisher_subscriber_demo();
mediator_demo();
Chain_of_responsibility chain;
chain.register_handler(Handler());
Command cmnd;
chain.handle(cmnd);
visitor_demo();
visitor_std_demo();
}
/// @} BP
/**
@defgroup AP Architectural
@brief [Architectural patterns](https://en.wikipedia.org/wiki/Architectural_pattern)
@{
*/
struct Model
: Subject
/// @brief is part of MVC with View and Controller
{
void register_observer(Observer& o)
{
observers.push_front(o);
}
int command(Command& cmnd)
{
int rc = cmnd.execute();
notify_observers();
return rc;
}
int command(Command&& cmnd)
{
int rc = cmnd.execute();
notify_observers();
return rc;
}
};
struct View
/// @brief is concrete Observer
: public Observer {
View(Model& m)
: model(m) {};
void notify() override
{
// check model
(void)model;
}
Model& model;
};
struct Controller
/** @brief is part of MVC with Model and View
*/
{
Model& mod; // can be many models
Controller(Model& s)
: mod(s) {};
int command(Command& cmnd)
{
return mod.command(cmnd);
}
int command(Command&& cmnd)
{
return mod.command(cmnd);
}
};
void architectural_patterns_demo()
{
Model mod;
View view(mod);
mod.register_observer(view);
mod.notify_observers();
Controller ctrl(mod);
ctrl.command(Command());
Command cmnd;
ctrl.command(cmnd);
}
/// @} AP
/**
@defgroup CC Concurrency
@brief [Concurrency patterns](https://en.wikipedia.org/wiki/Concurrency_pattern)
@{
*/
template <typename T, class Container = queue<T>>
class Synchronized_queue
/**
See also
- [Boost synchronized queues](https://www.boost.org/doc/libs/release/doc/html/thread/sds.html#thread.sds.synchronized_queues)
- [boost/thread/concurrent_queues/sync_queue.hpp](https://github.com/boostorg/thread/blob/HEAD/include/boost/thread/concurrent_queues/sync_queue.hpp)
- [boost/thread/concurrent_queues/detail/sync_queue_base.hpp](https://github.com/boostorg/thread/blob/HEAD/include/boost/thread/concurrent_queues/detail/sync_queue_base.hpp)
- [Messaging pattern](https://en.wikipedia.org/wiki/Messaging_pattern)
Warning: unbounded synchronized queues can cause [OOM](https://en.wikipedia.org/wiki/Out_of_memory)
*/
: Container {
mutex mtx;
bool stoped = false;
public:
condition_variable cv;
void push(T&& v)
{
lock_guard<mutex> { mtx }, Container::push(v);
cv.notify_one();
};
T& pull()
{
unique_lock<mutex> lk(mtx);
cv.wait(lk, [&] { return !this->empty() || stoped; });
if (stoped)
throw "stopped";
T& ret = Container::front();
this->pop();
return ret;
};
void stop()
{
stoped = true;
cv.notify_all();
}
};
struct Active_object
/** @brief [Active object](https://en.wikipedia.org/wiki/Active_object)
[Revisiting the Active Object Pattern - with C++11 Closures](https://www.codeproject.com/Articles/991641/Revisiting-the-Active-Object-Pattern-with-Cplusplu)
*/
: Interface {
typedef function<void()> Command;
Interface& subject;
Active_object(Interface& s)
: subject(s)
{
th = thread([this] {
try {
while (true)
cmd_q.pull()(); // call Command
} catch (...) {
}
});
}
~Active_object() noexcept
{
cmd_q.stop();
th.join();
}
int method() override
{
promise<int> p;
future f = p.get_future();
cmd_q.push([&p, this] { p.set_value(subject.method()); });
auto status = f.wait_for(1s);
if (status != future_status::ready)
throw status;
return f.get();
}
protected:
Synchronized_queue<Command> cmd_q; // BTW, Circular buffer is more durable because it doesn't cause OOM
thread th;
};
void concurrency_patterns_demo()
{
Sample_product sp(3);
Active_object ao(sp);
assert(ao.method() == 3);
}
/// @} CC
int main()
{
oop_demo();
creational_patterns_demo();
structural_patterns_demo();
behavioral_patterns_demo();
architectural_patterns_demo();
concurrency_patterns_demo();
}
/// @}
/**
@mainpage
@ref OOP
@ref DP
@ref CPP
[Sources](https://github.com/makelinux/examples/tree/main/cpp)
*/