Design Pattern Examples
Overview of object-oriented design patterns
Decorator_Classes.h
Go to the documentation of this file.
1
7
8#pragma once
9#ifndef __DECORATOR_CLASSES_H__
10#define __DECORATOR_CLASSES_H__
11
12#include <string>
13#include <memory>
14
16#include "helpers/formatstring.h"
17
19{
20
21 //########################################################################
22 //########################################################################
23
24
30 {
34 using shared_ptr_t = std::shared_ptr<IRenderElement>;
35
36 virtual ~IRenderElement() {}
37
42 virtual std::string Render() = 0;
43 };
44
45
46 //########################################################################
47 //########################################################################
48
49
55 {
56 private:
58
59 public:
66 : _wrappedElement(element)
67 {
68 if (!_wrappedElement)
69 {
70 throw Helpers::argumentnull_error("element",
71 "The element being decorated cannot be null.");
72 }
73 }
74
75
80 std::string Render()
81 {
82 return _wrappedElement->Render();
83 }
84 };
85
86 //########################################################################
87 //########################################################################
88
89
95 {
96 public:
97
106 : Decorator(element)
107 {
108 }
109
110 // IRenderElement Members
111
116 std::string Render()
117 {
118 return Helpers::formatstring("\x1b[47m%s\x1b[49m", Decorator::Render().c_str());
119 }
120 };
121
122
123 //########################################################################
124 //########################################################################
125
126
132 {
133 public:
134
143 : Decorator(element)
144 {
145 }
146
147 // IRenderElement Members
148
153 std::string Render()
154 {
155 return Helpers::formatstring("\x1b[4m%s\x1b[24m", Decorator::Render().c_str());
156 }
157 };
158
159
160 //########################################################################
161 //########################################################################
162
163
169 {
170 public:
171
180 : Decorator(element)
181 {
182 }
183
184 // IRenderElement Members
185
190 std::string Render()
191 {
192 return Helpers::formatstring("\x1b[31m%s\x1b[39m", Decorator::Render().c_str());
193 }
194 };
195
196
197 //########################################################################
198 //########################################################################
199
200
211 {
212 private:
216 std::string _elementText;
217
218 public:
219
224 TextElement(std::string element)
225 {
226 _elementText = element;
227 }
228
229 // IRenderElement Members
230
235 std::string Render()
236 {
237 return _elementText;
238 }
239 };
240
241} // end namespace
242
243#endif // __DECORATOR_CLASSES_H__
Implementation of the argumentnull_error exception.
Represents the base class of all decorators and is responsible for handling the wrapped element being...
std::string Render()
Render the wrapped element.
IRenderElement::shared_ptr_t _wrappedElement
Decorator(IRenderElement::shared_ptr_t element)
Constructor.
Represents the RedForeground decorator, which renders the wrapped content as red text.
RedForegroundDecorator(IRenderElement::shared_ptr_t element)
Constructor that wraps the specified element.
std::string Render()
Render the wrapped element with decorations.
Represents the core element that can be decorated. Note that this class implements the IRenderElement...
std::string _elementText
The raw text at the center of all decorators.
TextElement(std::string element)
Constructor.
std::string Render()
Render this element as a string.
Represents the Underline decorator, which underlines the wrapped content.
UnderlineDecorator(IRenderElement::shared_ptr_t element)
Constructor that wraps the specified element.
std::string Render()
Render the wrapped element with decorations.
Represents the WhiteBackground decorator, which changes the background color of the wrapped element t...
std::string Render()
Render the wrapped element with decorations.
WhiteBackgroundDecorator(IRenderElement::shared_ptr_t element)
Constructor that wraps the specified element.
Exception for arguments that are null.
The namespace containing all Design Pattern Examples implemented in C++.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....
Represents an element that can be rendered in text. All decorators and the core element class impleme...
std::shared_ptr< IRenderElement > shared_ptr_t
An alias to simplify syntax for this shared pointer.
virtual std::string Render()=0
Render this element as a string.