Browse code
stdlib ifc
Greg Wiley authored on 03/05/2017 22:46:18
Showing 2 changed files
Showing 2 changed files
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,19 @@ |
1 |
+#include <cstdlib> |
|
2 |
+#include <memory> |
|
3 |
+ |
|
4 |
+#include "sys_stdlib.h" |
|
5 |
+ |
|
6 |
+namespace { |
|
7 |
+ class impl : public stdlib_ifc { |
|
8 |
+ public: |
|
9 |
+ int rand() const override { |
|
10 |
+ return ::rand(); |
|
11 |
+ } |
|
12 |
+ |
|
13 |
+ }; |
|
14 |
+} |
|
15 |
+ |
|
16 |
+stdlib const &stdlib::get() { |
|
17 |
+ static stdlib singleton {std::make_shared<impl>()}; |
|
18 |
+ return singleton; |
|
19 |
+} |
0 | 20 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,23 @@ |
1 |
+#ifndef SYS_CSTDLIB_H_ |
|
2 |
+#define SYS_CSTDLIB_H_ |
|
3 |
+ |
|
4 |
+#include <memory> |
|
5 |
+ |
|
6 |
+class stdlib_ifc { |
|
7 |
+ public: |
|
8 |
+ virtual int rand() const {return 0;} |
|
9 |
+}; |
|
10 |
+ |
|
11 |
+class stdlib { |
|
12 |
+ public: |
|
13 |
+ using delegate = std::shared_ptr<stdlib_ifc>; |
|
14 |
+ private: |
|
15 |
+ delegate delegate_; |
|
16 |
+ public: |
|
17 |
+ stdlib(delegate delegate = std::make_shared<stdlib_ifc>()) : delegate_(delegate) {} |
|
18 |
+ int rand() const { return delegate_->rand(); } |
|
19 |
+ static const stdlib& get(); |
|
20 |
+}; |
|
21 |
+ |
|
22 |
+ |
|
23 |
+#endif |