|
| 1 | +#include <cstdint> |
| 2 | +#include <iostream> |
| 3 | +#include <string> |
| 4 | +#include <memory> |
| 5 | +#include <utility> |
| 6 | + |
| 7 | +// ------------------- PROPERTY PROXY ------------------- // |
| 8 | +/*! |
| 9 | + * @brief : PROPERTY PROXY |
| 10 | + * Allow you to perform additional actions |
| 11 | + * (For example, log or intercept) on properties |
| 12 | + * access by a client without him seeing any difference. |
| 13 | + */ |
| 14 | + |
| 15 | +template<typename T> |
| 16 | +class PropertyProxy { |
| 17 | +public: |
| 18 | + PropertyProxy(const T& p_val) : m_val(p_val) { } |
| 19 | + ~PropertyProxy() = default; |
| 20 | + |
| 21 | + // Allow conversion to T |
| 22 | + operator T() const { return m_val; } |
| 23 | + |
| 24 | + // Allow conversion from T |
| 25 | + T operator= (const T& p_val) { return m_val = p_val; } |
| 26 | + |
| 27 | +private: |
| 28 | + T m_val; |
| 29 | +}; |
| 30 | + |
| 31 | +class ClassWithProperties { |
| 32 | +public: |
| 33 | + ClassWithProperties ( |
| 34 | + uint32_t p_prop1 = 0, |
| 35 | + uint32_t p_prop2 = 0 ) : |
| 36 | + m_prop1 (p_prop1), |
| 37 | + m_prop2 (p_prop2) {} |
| 38 | + ~ClassWithProperties() = default; |
| 39 | + |
| 40 | +public: |
| 41 | + PropertyProxy<uint32_t> m_prop1; |
| 42 | + PropertyProxy<uint32_t> m_prop2 ; |
| 43 | +}; |
| 44 | + |
| 45 | +// ------------------- VIRTUAL PROXY ------------------- // |
| 46 | +/*! |
| 47 | + * @brief : VIRTUAL PROXY |
| 48 | + * Allow you to perform lazy initializations |
| 49 | + * to not waste memory without the client to |
| 50 | + * even know about it. |
| 51 | + */ |
| 52 | + |
| 53 | +class IPicture { |
| 54 | +public: |
| 55 | + IPicture(std::string p_fileName = ""): m_fileName(p_fileName) {} |
| 56 | + |
| 57 | + virtual void draw(void) = 0; |
| 58 | + std::string getSrcFile(void) const { return m_fileName; } |
| 59 | + |
| 60 | +protected: |
| 61 | + // Assuming images will always be loaded from a file |
| 62 | + std::string m_fileName; |
| 63 | +}; |
| 64 | + |
| 65 | +class JpgPicture : public IPicture { |
| 66 | +public: |
| 67 | + JpgPicture(std::string p_fileName = "") : IPicture(std::move(p_fileName)) |
| 68 | + { |
| 69 | + /* |
| 70 | + * Actually perform the loading of the image eventho |
| 71 | + * we might never use it ! |
| 72 | + */ |
| 73 | + std::cout << "\tLoading the JPEG picture from file " << m_fileName << std::endl; |
| 74 | + // Do the job |
| 75 | + // ... |
| 76 | + } |
| 77 | + |
| 78 | + void draw(void) override |
| 79 | + { |
| 80 | + // All we have to do it to draw |
| 81 | + std::cout << "\tDrawing image loaded from " << m_fileName << std::endl; |
| 82 | + // Draw the image |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +class JpgLazyPicture : public IPicture { |
| 87 | +public: |
| 88 | + JpgLazyPicture(std::string p_fileName = "") : IPicture(std::move(p_fileName)) |
| 89 | + { |
| 90 | + // We do not load the image yet since nobody asked for it yet... |
| 91 | + } |
| 92 | + |
| 93 | + void draw(void) override |
| 94 | + { |
| 95 | + // We load the image now if this is the first request |
| 96 | + if ( !m_jpg ) { m_jpg = std::make_unique<JpgPicture>(m_fileName); } |
| 97 | + |
| 98 | + // And then rely on JpgPicture to draw it |
| 99 | + m_jpg->draw(); |
| 100 | + } |
| 101 | + |
| 102 | +private: |
| 103 | + std::unique_ptr<JpgPicture> m_jpg; |
| 104 | +}; |
| 105 | + |
| 106 | + |
| 107 | +// ------------------- COMMUNICATION PROXY ------------------- // |
| 108 | +/*! |
| 109 | + * @brief : COMMUNICATION PROXY |
| 110 | + * FROM http://www.vishalchovatiya.com/proxy-design-pattern-in-modern-cpp/#Property_Proxy |
| 111 | + */ |
| 112 | + |
| 113 | +template <typename T> |
| 114 | +struct arr2D { |
| 115 | + struct proxy { |
| 116 | + proxy(T *arr) : m_arr_1D(arr) {} |
| 117 | + T &operator[](int32_t idx) { |
| 118 | + return m_arr_1D[idx]; |
| 119 | + } |
| 120 | + |
| 121 | + T *m_arr_1D; |
| 122 | + }; |
| 123 | + |
| 124 | + arr2D::proxy operator[](int32_t idx) { |
| 125 | + return arr2D::proxy(m_arr_2D[idx]); |
| 126 | + } |
| 127 | + |
| 128 | + T m_arr_2D[10][10]; |
| 129 | +}; |
| 130 | + |
| 131 | +/*! |
| 132 | + * @brief CLIENT CODE |
| 133 | + */ |
| 134 | +int main() { |
| 135 | + |
| 136 | + // -------- Property proxy -------- // |
| 137 | + { |
| 138 | + /* The client will use the ClassWithProperties just |
| 139 | + * as if those properties actually were of their |
| 140 | + * base type (i.e. uint32_t) |
| 141 | + */ |
| 142 | + |
| 143 | + ClassWithProperties anObject(10, 5); |
| 144 | + anObject.m_prop1 = 20; |
| 145 | + anObject.m_prop2 = 8; |
| 146 | + |
| 147 | + std::cout << anObject.m_prop1 << std::endl; |
| 148 | + std::cout << anObject.m_prop2 << std::endl; |
| 149 | + } |
| 150 | + |
| 151 | + // -------- Virtual proxy -------- // |
| 152 | + { |
| 153 | + JpgLazyPicture jpgLazy("myJpg.jpg"); |
| 154 | + JpgPicture jpgBase("myJpg.jpg"); |
| 155 | + |
| 156 | + std::cout << "At this point, only jpgBase object has been loaded..." << std::endl; |
| 157 | + |
| 158 | + std::cout << "Now let's ask for jpgLazy..." << std::endl; |
| 159 | + jpgLazy.draw(); |
| 160 | + } |
| 161 | + |
| 162 | + // ---- Communication proxy ---- // |
| 163 | + { |
| 164 | + arr2D<int32_t> arr; |
| 165 | + arr[0][0] = 1; // Uses the proxy object |
| 166 | + } |
| 167 | + |
| 168 | + return 0; |
| 169 | +} |
0 commit comments