Ocean
platform/win/Utilities.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_PLATFORM_WIN_UTILITIES_H
9 #define META_OCEAN_PLATFORM_WIN_UTILITIES_H
10 
11 #include "ocean/platform/win/Win.h"
13 
14 #include "ocean/base/Frame.h"
15 
17 
18 #ifdef OCEAN_COMPILER_MSC
19  #pragma managed(push, off)
20 #endif
21 
22 #include "ocean/base/String.h"
23 
24 #include <string>
25 
26 #ifdef OCEAN_COMPILER_MSC
27  #pragma managed(pop)
28 #endif
29 
30 namespace Ocean
31 {
32 
33 namespace Platform
34 {
35 
36 namespace Win
37 {
38 
39 /**
40  * This class implements utilities and helper functions.
41  * @ingroup platformwin
42  */
43 class OCEAN_PLATFORM_WIN_EXPORT Utilities
44 {
45  public:
46 
47  /**
48  * Prints a text on the given device context.
49  * @param dc Device context receiving the text
50  * @param x Horizontal output position
51  * @param y Vertical output position
52  * @param text Text to be printed
53  */
54  static void textOutput(HDC dc, const int x, const int y, const std::string& text);
55 
56  /**
57  * Prints a text on the desktop.
58  * @param x Horizontal output position
59  * @param y Vertical output position
60  * @param text Text to be printed
61  */
62  static void desktopTextOutput(const int x, const int y, const std::string& text);
63 
64  /**
65  * Prints a frame on the given device context.
66  * @param dc Device context receiving the frame
67  * @param x Horizontal output position
68  * @param y Vertical output position
69  * @param frame The frame to be printed
70  */
71  static void frameOutput(HDC dc, const int x, const int y, const Frame& frame);
72 
73  /**
74  * Prints a frame on the given device context.
75  * @param dc Device context receiving the text
76  * @param x Horizontal output position
77  * @param y Vertical output position
78  * @param width Output width in pixel
79  * @param height Output height in pixel
80  * @param frame The frame to be printed
81  */
82  static void frameOutput(HDC dc, const int x, const int y, const unsigned int width, const unsigned int height, const Frame& frame);
83 
84  /**
85  * Prints a given frame on the Windows' main desktop at a specified location.
86  * You do not need to have a GUI application to use this function.<br>
87  * Call this function from any application and from any thread.<br>
88  * The painted frame will disappear immediately when Windows receives any repaint event.<br>
89  * This function is intended for debugging purposes only.
90  * @param x Horizontal output position, with range (-infinity, infinity)
91  * @param y Vertical output position, with range (-infinity, infinity)
92  * @param frame The frame to be printed
93  */
94  static void desktopFrameOutput(const int x, const int y, const Frame& frame);
95 
96  /**
97  * Prints a given frame on the Windows' main desktop at a specified location.
98  * You do not need to have a GUI application to use this function.<br>
99  * Call this function from any application and from any thread.<br>
100  * The painted frame will disappear immediately when Windows receives any repaint event.<br>
101  * This function is intended for debugging purposes only.
102  * @param x Horizontal output position, with range (-infinity, infinity)
103  * @param y Vertical output position, with range (-infinity, infinity)
104  * @param scale The scale that is applied to the frame, a factor of 2 will increase the painted frame by two, with range [1, infinity)
105  * @param frame The frame to be printed
106  */
107  static inline void desktopFrameOutput(const int x, const int y, const unsigned int scale, const Frame& frame);
108 
109  /**
110  * Prints a given frame on the Windows' main desktop at a specified location.
111  * You do not need to have a GUI application to use this function.<br>
112  * Call this function from any application and from any thread.<br>
113  * The painted frame will disappear immediately when Windows receives any repaint event.<br>
114  * This function is intended for debugging purposes only.
115  * @param x Horizontal output position, with range (-infinity, infinity)
116  * @param y Vertical output position, with range (-infinity, infinity)
117  * @param width The width of the painted frame in the domain of the desktop, in pixel, with range [1, infinity)
118  * @param height The height of the painted frame in the domain of the desktop, in pixel, with range [1, infinity)
119  * @param frame The frame to be printed
120  */
121  static void desktopFrameOutput(const int x, const int y, const unsigned int width, const unsigned int height, const Frame& frame);
122 
123  /**
124  * Prints a bitmap on the given device context.
125  * @param dc Device context receiving the bitmap
126  * @param x Horizontal output position
127  * @param y Vertical output position
128  * @param bitmap bitmap to be printed
129  */
130  static void bitmapOutput(HDC dc, const int x, const int y, const Bitmap& bitmap);
131 
132  /**
133  * Prints a bitmap on the desktop.
134  * @param x Horizontal output position
135  * @param y Vertical output position
136  * @param scale Scale of the output bitmap
137  * @param bitmap Bitmap to be printed
138  */
139  static inline void desktopBitmapOutput(const int x, const int y, const unsigned int scale, const Bitmap& bitmap);
140 
141  /**
142  * Prints a bitmap on the given device context.
143  * @param dc Device context receiving the text
144  * @param x Horizontal output position
145  * @param y Vertical output position
146  * @param width Output width in pixel
147  * @param height Output height in pixel
148  * @param bitmap bitmap to be printed
149  */
150  static void bitmapOutput(HDC dc, const int x, const int y, const unsigned int width, const unsigned int height, const Bitmap& bitmap);
151 
152  /**
153  * Prints a bitmap on the desktop.
154  * @param x Horizontal output position
155  * @param y Vertical output position
156  * @param bitmap bitmap to be printed
157  */
158  static void desktopBitmapOutput(const int x, const int y, const Bitmap& bitmap);
159 
160  /**
161  * Prints a bitmap on the desktop.
162  * @param x Horizontal output position
163  * @param y Vertical output position
164  * @param width Output width in pixel
165  * @param height Output height in pixel
166  * @param bitmap bitmap to be printed
167  */
168  static void desktopBitmapOutput(const int x, const int y, const unsigned int width, const unsigned int height, const Bitmap& bitmap);
169 
170  /**
171  * Determines the bounding box of a given string with specified font and font size.
172  * @param value The string for which the bounding box will be determined
173  * @param font The name of the font which will be applied
174  * @param size The size of the font
175  * @return The bounding box of the given string
176  */
177  static CV::PixelBoundingBox textBoundingBox(const std::string& value, const std::string& font = std::string(), const unsigned int size = 0u);
178 
179  /**
180  * Determines the bounding box of a given string with specified font and font size.
181  * @param value The string for which the bounding box will be determined
182  * @param font The name of the font which will be applied
183  * @param size The size of the font
184  * @return The bounding box of the given string
185  */
186  static CV::PixelBoundingBox textBoundingBox(const std::wstring& value, const std::wstring& font = std::wstring(), const unsigned int size = 0u);
187 };
188 
189 /**
190  * This class implements a nested scoped object which disables a window object until the scope of all nested elements ends (or until all nested object are released explicitly).
191  * The creation of the first nested object disables the window.<br>
192  * Following nested objects only increase the internal counter while do not have any further consequence.<br>
193  * @ingroup platformwin
194  */
196 {
197  protected:
198 
199  /**
200  * This class implements a simple counter for nested disable window objects.
201  */
202  class DisableWindowCounter : public Singleton<DisableWindowCounter>
203  {
204  friend class Singleton<DisableWindowCounter>;
205  friend class ScopedDisableWindow;
206 
207  protected:
208 
209  /**
210  * Definition of a map mapping window handles to counters.
211  */
212  typedef std::unordered_map<HWND, unsigned int> CounterMap;
213 
214  protected:
215 
216  /**
217  * Creates a new DisableWindowCounter object.
218  */
219  inline DisableWindowCounter();
220 
221  /**
222  * Destructs a DisableWindowCounter object.
223  */
224  inline ~DisableWindowCounter();
225 
226  /**
227  * Increases the counter of this object.
228  * @return True, if the counter was zero before the counter has been increased
229  */
230  inline bool increase(const HWND windowHandle);
231 
232  /**
233  * Decreases the counter of this object.
234  * @return True, if the counter is zero after it has been decreased
235  */
236  inline bool decrease(const HWND windowHandle);
237 
238  /**
239  * Returns whether the counter is zero.
240  * @return True, if so
241  */
242  inline bool isZero(const HWND windowHandle);
243 
244  /**
245  * Returns the lock object.
246  * @return Lock object
247  */
248  inline Lock& lock();
249 
250  protected:
251 
252  /// Disable window counter.
254 
255  /// Disable window lock.
257  };
258 
259  public:
260 
261  /**
262  * Creates a new scoped object and disables the associated window.
263  * @param windowHandle Handle of the window to disable, this window must not be disposed before this scoped object is disposed
264  */
265  explicit ScopedDisableWindow(const HWND windowHandle);
266 
267  /**
268  * Destructs this scoped object and enables the associated window.
269  */
270  inline ~ScopedDisableWindow();
271 
272  /**
273  * Explicitly releases the scoped object and enables the associated window (already before the scope ends).
274  */
275  void release();
276 
277  protected:
278 
279  /**
280  * Disabled copy constructor.
281  * @param window The window that would be copied
282  */
283  ScopedDisableWindow(const ScopedDisableWindow& window) = delete;
284 
285  /**
286  * Disabled assign operator.
287  * @param window The window that would be assigned
288  */
290 
291  protected:
292 
293  /// The associated window, nullptr if the object has been released already.
294  HWND handle_ = nullptr;
295 };
296 
297 #if defined(OCEAN_COMPILER_MSC) && defined(_MANAGED)
298 
299 inline System::String^ toString(const std::string& value)
300 {
301  return gcnew System::String(value.c_str());
302 }
303 
304 inline System::String^ toString(const char* value)
305 {
306  return gcnew System::String(value);
307 }
308 
309 inline System::String^ toString(const std::wstring& value)
310 {
311  return gcnew System::String(value.c_str());
312 }
313 
314 inline System::String^ toString(const wchar_t* value)
315 {
316  return gcnew System::String(value);
317 }
318 
319 inline std::string toAString(System::String^ value)
320 {
321  char* str = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(value);
322  std::string result(str);
323 
324  System::Runtime::InteropServices::Marshal::FreeHGlobal(System::IntPtr(str));
325 
326  return result;
327 }
328 
329 inline std::wstring toWString(System::String^ value)
330 {
331  char* str = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(value);
332  std::string result(str);
333 
334  System::Runtime::InteropServices::Marshal::FreeHGlobal(System::IntPtr(str));
335 
336  return Ocean::String::toWString(result);
337 }
338 
339 #endif // OCEAN_COMPILER_MSC && _MANAGED
340 
341 inline void Utilities::desktopFrameOutput(const int x, const int y, const unsigned int scale, const Frame& frame)
342 {
343  desktopFrameOutput(x, y, frame.width() * scale, frame.height() * scale, frame);
344 }
345 
346 inline void Utilities::desktopBitmapOutput(const int x, const int y, const unsigned int scale, const Bitmap& bitmap)
347 {
348  desktopBitmapOutput(x, y, bitmap.width() * scale, bitmap.height() * scale, bitmap);
349 }
350 
352 {
353  // nothing to do here
354 }
355 
357 {
358 #ifdef OCEAN_DEBUG
359 
360  for (CounterMap::const_iterator it = counterMap_.begin(); it != counterMap_.end(); ++it)
361  ocean_assert(it->second == 0u);
362 
363 #endif
364 }
365 
366 inline bool ScopedDisableWindow::DisableWindowCounter::increase(const HWND windowHandle)
367 {
368  counterMap_[windowHandle]++;
369 
370  return counterMap_[windowHandle] == 1u;
371 }
372 
373 inline bool ScopedDisableWindow::DisableWindowCounter::decrease(const HWND windowHandle)
374 {
375  ocean_assert(counterMap_.find(windowHandle) != counterMap_.end());
376  ocean_assert(counterMap_[windowHandle] >= 1u);
377 
378  counterMap_[windowHandle]--;
379 
380  return counterMap_[windowHandle] == 0u;
381 }
382 
383 inline bool ScopedDisableWindow::DisableWindowCounter::isZero(const HWND windowHandle)
384 {
385  return counterMap_[windowHandle] == 0u;
386 }
387 
389 {
390  return windowLock_;
391 }
392 
394 {
395  release();
396 }
397 
398 }
399 
400 }
401 
402 }
403 
404 #endif // META_OCEAN_PLATFORM_WIN_UTILITIES_H
This class implements Ocean's image class.
Definition: Frame.h:1792
unsigned int width() const
Returns the width of the frame format in pixel.
Definition: Frame.h:3143
unsigned int height() const
Returns the height of the frame in pixel.
Definition: Frame.h:3148
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements a Windows device independent bitmap.
Definition: win/Bitmap.h:30
unsigned int width() const
Returns the width of the bitmap.
Definition: win/Bitmap.h:267
unsigned int height() const
Returns the height of the bitmap.
Definition: win/Bitmap.h:272
This class implements a simple counter for nested disable window objects.
Definition: platform/win/Utilities.h:203
CounterMap counterMap_
Disable window counter.
Definition: platform/win/Utilities.h:253
std::unordered_map< HWND, unsigned int > CounterMap
Definition of a map mapping window handles to counters.
Definition: platform/win/Utilities.h:212
~DisableWindowCounter()
Destructs a DisableWindowCounter object.
Definition: platform/win/Utilities.h:356
Lock windowLock_
Disable window lock.
Definition: platform/win/Utilities.h:256
bool isZero(const HWND windowHandle)
Returns whether the counter is zero.
Definition: platform/win/Utilities.h:383
DisableWindowCounter()
Creates a new DisableWindowCounter object.
Definition: platform/win/Utilities.h:351
Lock & lock()
Returns the lock object.
Definition: platform/win/Utilities.h:388
bool increase(const HWND windowHandle)
Increases the counter of this object.
Definition: platform/win/Utilities.h:366
bool decrease(const HWND windowHandle)
Decreases the counter of this object.
Definition: platform/win/Utilities.h:373
This class implements a nested scoped object which disables a window object until the scope of all ne...
Definition: platform/win/Utilities.h:196
ScopedDisableWindow(const ScopedDisableWindow &window)=delete
Disabled copy constructor.
HWND handle_
The associated window, nullptr if the object has been released already.
Definition: platform/win/Utilities.h:294
~ScopedDisableWindow()
Destructs this scoped object and enables the associated window.
Definition: platform/win/Utilities.h:393
ScopedDisableWindow(const HWND windowHandle)
Creates a new scoped object and disables the associated window.
void release()
Explicitly releases the scoped object and enables the associated window (already before the scope end...
ScopedDisableWindow & operator=(const ScopedDisableWindow &window)=delete
Disabled assign operator.
This class implements utilities and helper functions.
Definition: platform/win/Utilities.h:44
static void bitmapOutput(HDC dc, const int x, const int y, const unsigned int width, const unsigned int height, const Bitmap &bitmap)
Prints a bitmap on the given device context.
static void frameOutput(HDC dc, const int x, const int y, const Frame &frame)
Prints a frame on the given device context.
static void frameOutput(HDC dc, const int x, const int y, const unsigned int width, const unsigned int height, const Frame &frame)
Prints a frame on the given device context.
static void desktopFrameOutput(const int x, const int y, const Frame &frame)
Prints a given frame on the Windows' main desktop at a specified location.
static void textOutput(HDC dc, const int x, const int y, const std::string &text)
Prints a text on the given device context.
static CV::PixelBoundingBox textBoundingBox(const std::string &value, const std::string &font=std::string(), const unsigned int size=0u)
Determines the bounding box of a given string with specified font and font size.
static void desktopTextOutput(const int x, const int y, const std::string &text)
Prints a text on the desktop.
static CV::PixelBoundingBox textBoundingBox(const std::wstring &value, const std::wstring &font=std::wstring(), const unsigned int size=0u)
Determines the bounding box of a given string with specified font and font size.
static void desktopBitmapOutput(const int x, const int y, const unsigned int width, const unsigned int height, const Bitmap &bitmap)
Prints a bitmap on the desktop.
static void bitmapOutput(HDC dc, const int x, const int y, const Bitmap &bitmap)
Prints a bitmap on the given device context.
static void desktopFrameOutput(const int x, const int y, const unsigned int width, const unsigned int height, const Frame &frame)
Prints a given frame on the Windows' main desktop at a specified location.
static void desktopBitmapOutput(const int x, const int y, const Bitmap &bitmap)
Prints a bitmap on the desktop.
static void desktopBitmapOutput(const int x, const int y, const unsigned int scale, const Bitmap &bitmap)
Prints a bitmap on the desktop.
Definition: platform/win/Utilities.h:346
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
static std::wstring toWString(const char value)
Converts a value to a wstring.
std::string toAString(System::String^ value)
Definition: platform/win/Utilities.h:319
std::wstring toWString(System::String^ value)
Definition: platform/win/Utilities.h:329
System::String toString(const std::string &value)
Definition: platform/win/Utilities.h:299
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15