Design Pattern Examples
Overview of object-oriented design patterns
Flyweight_Classes.cpp
Go to the documentation of this file.
1
7
8#include "Flyweight_Classes.h"
9
11{
12
15
17 {
18 _resource = resource;
19 _context = context;
20 }
21
23 {
24 }
25
27 {
28 return _context;
29 }
30
32 {
33 _context = context;
34 }
35
36
38 {
39 if (_resource != nullptr)
40 {
41 return _context.ImageWidth;
42 }
43 return 0;
44 }
45
46
48 {
49 if (_resource != nullptr)
50 {
51 return _context.ImageHeight;
52 }
53 return 0;
54 }
55
56 void Flyweight_Class::Render(std::vector<std::vector<char>>& display, int offset_x, int image_width, int image_height, int position_x, int position_y)
57 {
58 if (_resource != nullptr)
59 {
60 // Let the big resource handle the rendering at the given position.
61 _resource->Render(display, offset_x, image_width, image_height, position_x, position_y);
62 }
63 }
64
65
68
69
70 void BigResource::Render(std::vector<std::vector<char>>& display, int offset_x, int image_width, int image_height, int position_x, int position_y)
71 {
72 size_t display_width = display[0].size();
73 size_t display_height = display.size();
74 int starting_position_x = position_x;
75 int starting_position_y = position_y;
76
77 // Size of image to render (can be smaller than actual image if image
78 // lies partially of right or bottom of display).
79 size_t image_render_width = image_width;
80 size_t image_render_height = image_height;
81
82 // Position into image to start rendering from (non-zero if
83 // image is off the left or top edge of display).
84 int starting_row_in_image = 0;
85 int starting_col_in_image = offset_x;
86
87 // Clip the image to the display.
88 if (starting_position_x < 0)
89 {
90 starting_col_in_image = -starting_position_x;
91 image_render_width += starting_position_x;
92 starting_position_x = 0;
93 }
94 else if (starting_position_x + image_render_width > display_width)
95 {
96 image_render_width = display_width - starting_position_x;
97 }
98 if (starting_position_y < 0)
99 {
100 starting_row_in_image = -starting_position_y;
101 image_render_height += starting_position_y;
102 starting_position_y = 0;
103 }
104 else if (starting_position_y + image_render_height > display_height)
105 {
106 image_render_height = display_height - starting_position_y;
107 }
108
109 // If the image is even partially visible, render it
110 if (image_render_width > 0 && image_render_height > 0)
111 {
112 int current_display_row = starting_position_y;
113 int current_image_row = starting_row_in_image;
114 for (size_t row = 0; row < image_render_height; ++row)
115 {
116 for (size_t col = 0; col < image_render_width; ++col)
117 {
118 display[current_display_row][starting_position_x + col] = _resource[current_image_row][starting_col_in_image + col];
119 }
120 current_display_row += 1;
121 current_image_row += 1;
122 }
123 }
124 }
125
127 {
128 return _resourceId;
129 }
130
132 {
133 if (_resource.size() > 0)
134 {
135 return static_cast<int>(_resource[0].size());
136 }
137 return 0;
138 }
139
141 {
142 return static_cast<int>(_resource.size());
143 }
144
146 {
147 return std::make_unique<Flyweight_Class>(this, context);
148 }
149
150
153
154
156
158
159
161 {
162 int nextId = _nextResourceId;
164 return nextId;
165 }
166
168 {
169 BigResource* foundResource = nullptr;
170
171 for (BigResourceList::iterator resourceIter = std::begin(_resources);
172 resourceIter != std::end(_resources);
173 resourceIter++)
174 {
175 if ((*resourceIter)->ResourceId() == resourceId)
176 {
177 foundResource = (*resourceIter).get();
178 break;
179 }
180 }
181
182 return foundResource;
183 }
184
185 int BigResourceManager::AddResource(std::vector<std::string> rawResource)
186 {
187 int newResourceId = GetNextResourceId();
188 _resources.push_back(std::make_unique<BigResource>(rawResource, newResourceId));
189
190 return newResourceId;
191 }
192
194 {
195 Flyweight_Class::unique_ptr_t flyweightClass;
196 BigResource* bigResource = FindResource(bigResourceId);
197 if (bigResource != nullptr)
198 {
199 flyweightClass = bigResource->CreateFlyweight(context);
200 }
201 return flyweightClass;
202 }
203
204} // end namespace
Declaration of the Flyweight_Context struct, and the Flyweight_Class, BigResource,...
Represents some big resource. In this case, a text "image" rendered as a list of strings....
int ResourceId()
Retrieve the resource ID for this resource.
void Render(std::vector< std::vector< char > > &display, int offset_x, int image_width, int image_height, int position_x, int position_y)
Render the big resource into the given display at the given position.
Flyweight_Class::unique_ptr_t CreateFlyweight(Flyweight_Context context)
Generate a Flyweight class that will represent this big resource in some context-dependent way.
std::vector< std::string > _resource
int ImageHeight()
Retrieve the "image" height of the resource.
int ImageWidth()
Retrieve the "image" width of the resource.
static int _nextResourceId
The next ID to assign a raw resource for management.
std::vector< std::unique_ptr< BigResource > > BigResourceList
static int GetNextResourceId()
Retrieve the next resource ID that can be used.
static BigResourceList _resources
A list of all big resources managed by this class.
static BigResource * FindResource(int resourceId)
Retrieve the BigResource corresponding to the specified ID.
static int AddResource(std::vector< std::string > rawResource)
Add a new big resource and return the ID of the resource.
static Flyweight_Class::unique_ptr_t CreateFlyweight(int bigResourceId, Flyweight_Context context)
Create a new instance of the Flyweight_Class associated with the given big resource and a context,...
void Render(std::vector< std::vector< char > > &display, int offset_x, int image_width, int image_height, int position_x, int position_y)
Render the image associated with this flyweight instance into the given display at the given position...
Flyweight_Context _context
The context associated with this class. The calling entity uses this context to manipulate the flywei...
BigResource * _resource
The big resource being referenced by this flyweight class. In C#, a class is always a reference to an...
Flyweight_Context Context()
Retrieve the context for this class instance.
void SetContext(Flyweight_Context context)
Set the context for this class instance.
std::unique_ptr< Flyweight_Class > unique_ptr_t
int ImageHeight()
Retrieve the "image" height from underlying big resource.
int ImageWidth()
Retrieve the "image" width from underlying big resource.
The namespace containing all Design Pattern Examples implemented in C++.
Represents the context for an instance of the Flyweight_Class. In this case, the context includes pos...