ASPiK SDK
vstguibase.h
1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4 
5 #ifndef __vstguibase__
6 #define __vstguibase__
7 
8 #include <cstdlib>
9 #include <cstdio>
10 #include <cstring>
11 #include <string>
12 
13 //-----------------------------------------------------------------------------
14 // VSTGUI Version
15 //-----------------------------------------------------------------------------
16 #define VSTGUI_VERSION_MAJOR 4
17 #define VSTGUI_VERSION_MINOR 6
18 
19 //-----------------------------------------------------------------------------
20 // Platform definitions
21 //-----------------------------------------------------------------------------
22 #if __APPLE_CC__
23  #ifndef __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES
24  #define __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES 0
25  #endif
26  #include <stdint.h>
27  #include <AvailabilityMacros.h>
28  #include <TargetConditionals.h>
29  #if TARGET_OS_IPHONE
30  #ifndef MAC
31  #define MAC 1
32  #endif
33  #ifndef MAC_COCOA
34  #define MAC_COCOA 1
35  #endif
36  #define VSTGUI_OPENGL_SUPPORT 0 // there's an implementation, but not yet tested, so this is zero
37  #define VSTGUI_TOUCH_EVENT_HANDLING 1
38  #else
39  #ifndef MAC_OS_X_VERSION_10_9
40  #error you need at least OSX SDK 10.9 to build vstgui
41  #endif
42  #ifndef MAC_COCOA
43  #define MAC_COCOA 1
44  #endif
45  #ifndef MAC
46  #define MAC 1
47  #endif
48  #if !__LP64__ && !defined (MAC_CARBON)
49  #define MAC_CARBON 1
50  #ifndef TARGET_API_MAC_CARBON
51  #define TARGET_API_MAC_CARBON 1
52  #endif
53  #ifndef __CF_USE_FRAMEWORK_INCLUDES__
54  #define __CF_USE_FRAMEWORK_INCLUDES__ 1
55  #endif
56  #endif
57  #endif
58 
59  #ifndef __has_feature
60  #error compiler not supported
61  #endif
62  #if __has_feature (cxx_rvalue_references) == 0
63  #error need cxx_rvalue_references support from compiler
64  #endif
65  #if __has_feature (cxx_range_for) == 0
66  #error need cxx_range_for support from compiler
67  #endif
68  #include <type_traits>
69 
70  #if defined (__clang__) && __clang_major__ > 4
71  #if defined (VSTGUI_WARN_EVERYTHING) && VSTGUI_WARN_EVERYTHING == 1
72  #pragma clang diagnostic warning "-Weverything"
73  #pragma clang diagnostic warning "-Wconversion"
74  #pragma clang diagnostic ignored "-Wreorder"
75  #else
76  #pragma clang diagnostic warning "-Wunreachable-code"
77  #endif
78  #endif
79 
80 #elif WIN32 || WINDOWS || defined(_WIN32)
81  #ifndef NOMINMAX
82  #define NOMINMAX
83  #endif
84  #include <sdkddkver.h>
85  #if _WIN32_WINNT < 0x601
86  #error unsupported Platform SDK you need at least the Windows 7 Platform SDK to compile VSTGUI
87  #endif
88  #ifdef __GNUC__
89  #if __cplusplus < 201103L
90  #error compiler not supported
91  #endif
92  #elif _MSC_VER < 1800
93  #error Visual Studio 2013 or newer needed
94  #endif
95  #include <type_traits>
96  #include <stdint.h>
97  #ifndef WINDOWS
98  #define WINDOWS 1
99  #endif
100  #if !defined(__GNUC__) && _MSC_VER == 1800
101  #define noexcept // only supported since VS 2015
102  #endif
103  #define VSTGUI_DIRECT2D_SUPPORT 1
104  #define DEPRECATED_ATTRIBUTE __declspec(deprecated)
105  #ifdef _MSC_VER
106  #pragma warning(3 : 4189) // local variable is initialized but not referenced
107  #pragma warning(3 : 4702) // unreachable code
108  #pragma warning(3 : 4995) // deprecated
109  #pragma warning(3 : 4431) // missing type specifier - int assumed. Note: C no longer supports default-int
110  #pragma warning(3 : 4254) // conversion from 'type1' to 'type2', possible loss of data
111  #pragma warning(3 : 4388) // signed/unsigned mismatch
112  #endif
113 
114  #if defined (__clang__) && __clang__
115  #if defined (VSTGUI_WARN_EVERYTHING) && VSTGUI_WARN_EVERYTHING == 1
116  #pragma clang diagnostic warning "-Wconversion"
117  #pragma clang diagnostic ignored "-Wreorder"
118  #else
119  #pragma clang diagnostic warning "-Wunreachable-code"
120  #endif
121  #endif
122 
123  #include <algorithm>
124  using std::min;
125  using std::max;
126 
127 #elif defined(__linux__)
128  #include <cstdint>
129  #include <type_traits>
130  #include <algorithm>
131  #include <climits>
132  using std::min;
133  using std::max;
134  #ifndef LINUX
135  #define LINUX 1
136  #endif
137 
138 #else
139  #error unsupported compiler
140 #endif
141 
142 #include <atomic>
143 
144 #ifdef UNICODE
145  #undef UNICODE
146 #endif
147 #define UNICODE 1
148 
149 //----------------------------------------------------
150 // Deprecation setting
151 //----------------------------------------------------
152 #ifndef VSTGUI_ENABLE_DEPRECATED_METHODS
153  #define VSTGUI_ENABLE_DEPRECATED_METHODS 0
154 #endif
155 
156 #ifndef DEPRECATED_ATTRIBUTE
157  #define DEPRECATED_ATTRIBUTE
158 #endif
159 
160 #if VSTGUI_ENABLE_DEPRECATED_METHODS
161  #define VSTGUI_DEPRECATED(x) DEPRECATED_ATTRIBUTE x
162 #else
163  #define VSTGUI_DEPRECATED(x)
164 #endif
165 
166 //----------------------------------------------------
167 // Feature setting
168 //----------------------------------------------------
169 #ifndef VSTGUI_OPENGL_SUPPORT
170  #define VSTGUI_OPENGL_SUPPORT 1
171 #endif
172 
173 #ifndef VSTGUI_TOUCH_EVENT_HANDLING
174  #define VSTGUI_TOUCH_EVENT_HANDLING 0
175 #endif
176 
177 #if VSTGUI_ENABLE_DEPRECATED_METHODS
178  #define VSTGUI_OVERRIDE_VMETHOD override
179  #define VSTGUI_FINAL_VMETHOD final
180 #else
181  #define VSTGUI_OVERRIDE_VMETHOD static_assert (false, "VSTGUI_OVERRIDE_VMETHOD is deprecated, just use override!");
182  #define VSTGUI_FINAL_VMETHOD static_assert (false, "VSTGUI_FINAL_VMETHOD is deprecated, just use final!");
183 #endif
184 
185 //----------------------------------------------------
186 // Helper makros
187 //----------------------------------------------------
188 #define VSTGUI_MAKE_STRING_PRIVATE_DONT_USE(x) # x
189 #define VSTGUI_MAKE_STRING(x) VSTGUI_MAKE_STRING_PRIVATE_DONT_USE(x)
190 
191 //----------------------------------------------------
192 #if DEVELOPMENT
193  #ifndef DEBUG
194  #define DEBUG 1
195  #endif
196 #else
197  #if !defined(NDEBUG) && !defined(DEBUG)
198  #define NDEBUG 1
199  #endif
200 #endif
201 
202 //----------------------------------------------------
203 #define CLASS_METHODS(name, parent) CBaseObject* newCopy () const override { return new name (*this); }
204 #define CLASS_METHODS_NOCOPY(name, parent) CBaseObject* newCopy () const override { return 0; }
205 #define CLASS_METHODS_VIRTUAL(name, parent) CBaseObject* newCopy () const override = 0;
206 
207 //----------------------------------------------------
208 namespace VSTGUI {
209 
210 using CCoord = double;
211 using IdStringPtr = const char*;
212 using UTF8StringPtr = const char*;
213 using UTF8StringBuffer = char*;
214 
215 //-----------------------------------------------------------------------------
216 // @brief Byte Order
217 //-----------------------------------------------------------------------------
218 enum ByteOrder {
219  kBigEndianByteOrder = 0,
220  kLittleEndianByteOrder,
221 #if WINDOWS || defined (__LITTLE_ENDIAN__)
222  kNativeByteOrder = kLittleEndianByteOrder
223 #else
224  kNativeByteOrder = kBigEndianByteOrder
225 #endif
226 };
227 
228 //-----------------------------------------------------------------------------
229 // @brief Message Results
230 //-----------------------------------------------------------------------------
231 enum CMessageResult
232 {
233  kMessageUnknown = 0,
234  kMessageNotified = 1
235 };
236 
237 //-----------------------------------------------------------------------------
239 {
240 public:
241  virtual void forget () = 0;
242  virtual void remember () = 0;
243 };
244 
245 //-----------------------------------------------------------------------------
246 template <typename T>
248 {
249 public:
250  ReferenceCounted () = default;
251  virtual ~ReferenceCounted () noexcept = default;
252 
254  ReferenceCounted& operator= (const ReferenceCounted&) { return *this; }
255 
256  //-----------------------------------------------------------------------------
258  //-----------------------------------------------------------------------------
260  void forget () override { if (--nbReference == 0) { beforeDelete (); delete this; } }
261  void remember () override { nbReference++; }
262  virtual int32_t getNbReference () const { return nbReference; }
263  virtual void beforeDelete () {}
265 private:
266  T nbReference {1};
267 };
268 
269 using AtomicReferenceCounted = ReferenceCounted<std::atomic<int32_t>>;
270 using NonAtomicReferenceCounted = ReferenceCounted<int32_t>;
271 
272 //-----------------------------------------------------------------------------
273 // CBaseObject Declaration
275 //-----------------------------------------------------------------------------
277 {
278 public:
279  CBaseObject () = default;
280  ~CBaseObject () noexcept override = default;
281 
282  CBaseObject (const CBaseObject& o) {};
283  CBaseObject& operator= (const CBaseObject& obj) { return *this; }
284 
285  //-----------------------------------------------------------------------------
287  //-----------------------------------------------------------------------------
289  virtual CMessageResult notify (CBaseObject* sender, IdStringPtr message) { return kMessageUnknown; }
291 
293  virtual CBaseObject* newCopy () const { return 0; }
295 };
296 
297 //------------------------------------------------------------------------
298 template <class I>
300 {
301 public:
302 //------------------------------------------------------------------------
303  inline SharedPointer (I* ptr, bool remember = true) noexcept;
304  inline SharedPointer (const SharedPointer&) noexcept;
305  inline SharedPointer () noexcept;
306  inline ~SharedPointer () noexcept;
307 
308  inline I* operator=(I* ptr) noexcept;
309  inline SharedPointer<I>& operator=(const SharedPointer<I>& ) noexcept;
310 
311  inline operator I* () const noexcept { return ptr; } // act as I*
312  inline I* operator->() const noexcept { return ptr; } // act as I*
313 
314  inline I* get () const noexcept { return ptr; }
315 
316  template<class T> T* cast () const { return dynamic_cast<T*> (ptr); }
317 
318  inline SharedPointer (SharedPointer<I>&& mp) noexcept;
319  inline SharedPointer<I>& operator=(SharedPointer<I>&& mp) noexcept;
320 
321  template<typename T>
322  inline SharedPointer (const SharedPointer<T>& op) noexcept
323  {
324  *this = static_cast<I*> (op.get ());
325  }
326 
327  template<typename T>
328  inline SharedPointer& operator= (const SharedPointer<T>& op) noexcept
329  {
330  *this = static_cast<I*> (op.get ());
331  return *this;
332  }
333 
334  template<typename T>
335  inline SharedPointer (SharedPointer<T>&& op) noexcept
336  {
337  *this = std::move (op);
338  }
339 
340  template<typename T>
341  inline SharedPointer& operator= (SharedPointer<T>&& op) noexcept
342  {
343  if (ptr)
344  ptr->forget ();
345  ptr = op.ptr;
346  op.ptr = nullptr;
347  return *this;
348  }
349 
350 //------------------------------------------------------------------------
351 protected:
352  template<typename T>
353  friend class SharedPointer;
354 
355  I* ptr {nullptr};
356 };
357 
358 //-----------------------------------------------------------------------------
360 {
361 public:
362  explicit CBaseObjectGuard (CBaseObject* _obj) : obj (_obj) {}
363 protected:
365 };
366 
367 
368 //------------------------------------------------------------------------
369 template <class I>
370 inline SharedPointer<I>::SharedPointer (I* _ptr, bool remember) noexcept
371 : ptr (_ptr)
372 {
373  if (ptr && remember)
374  ptr->remember ();
375 }
376 
377 //------------------------------------------------------------------------
378 template <class I>
379 inline SharedPointer<I>::SharedPointer (const SharedPointer<I>& other) noexcept
380 : ptr (other.ptr)
381 {
382  if (ptr)
383  ptr->remember ();
384 }
385 
386 //------------------------------------------------------------------------
387 template <class I>
388 inline SharedPointer<I>::SharedPointer () noexcept
389 : ptr (0)
390 {}
391 
392 //------------------------------------------------------------------------
393 template <class I>
394 inline SharedPointer<I>::~SharedPointer () noexcept
395 {
396  if (ptr)
397  ptr->forget ();
398 }
399 
400 //------------------------------------------------------------------------
401 template <class I>
402 inline SharedPointer<I>::SharedPointer (SharedPointer<I>&& mp) noexcept
403 : ptr (nullptr)
404 {
405  *this = std::move (mp);
406 }
407 
408 //------------------------------------------------------------------------
409 template <class I>
410 inline SharedPointer<I>& SharedPointer<I>::operator=(SharedPointer<I>&& mp) noexcept
411 {
412  if (ptr)
413  ptr->forget ();
414  ptr = mp.ptr;
415  mp.ptr = nullptr;
416  return *this;
417 }
418 
419 //------------------------------------------------------------------------
420 template <class I>
421 inline I* SharedPointer<I>::operator=(I* _ptr) noexcept
422 {
423  if (_ptr != ptr)
424  {
425  if (ptr)
426  ptr->forget ();
427  ptr = _ptr;
428  if (ptr)
429  ptr->remember ();
430  }
431  return ptr;
432 }
433 
434 //------------------------------------------------------------------------
435 template <class I>
436 inline SharedPointer<I>& SharedPointer<I>::operator=(const SharedPointer<I>& _ptr) noexcept
437 {
438  operator= (_ptr.ptr);
439  return *this;
440 }
441 
442 //------------------------------------------------------------------------
443 template <class I>
444 inline SharedPointer<I> owned (I* p) noexcept { return SharedPointer<I> (p, false); }
445 
446 //------------------------------------------------------------------------
447 template <class I>
448 inline SharedPointer<I> shared (I* p) noexcept { return SharedPointer<I> (p, true); }
449 
450 //------------------------------------------------------------------------
451 template <class I, typename ...Args>
452 inline SharedPointer<I> makeOwned (Args&& ...args)
453 {
454  return SharedPointer<I> (new I (std::forward<Args>(args)...), false);
455 }
456 
457 #if VSTGUI_ENABLE_DEPRECATED_METHODS
458 //------------------------------------------------------------------------
466 template <class I>
467 class OwningPointer : public SharedPointer<I>
468 {
469 public:
470 //------------------------------------------------------------------------
471  inline OwningPointer (I* p) : SharedPointer<I> (p, false) {}
472  inline OwningPointer (const SharedPointer<I>& p) : SharedPointer<I> (p) {}
473  inline OwningPointer (const OwningPointer<I>& p) : SharedPointer<I> (p) {}
474  inline OwningPointer () : SharedPointer<I> () {}
475  inline I* operator=(I* _ptr)
476  {
477  if (_ptr != this->ptr)
478  {
479  if (this->ptr)
480  this->ptr->forget ();
481  this->ptr = _ptr;
482  }
483  return this->ptr;
484  }
485 };
486 #endif
487 
488 //------------------------------------------------------------------------
489 template <typename T, typename B>
490 inline void setBit (T& storage, B bit, bool state)
491 {
492  static_assert (std::is_integral<T>::value, "only works for integral types");
493  static_assert (sizeof (T) >= sizeof (B), "bit type is too big");
494  if (state)
495  storage |= static_cast<T> (bit);
496  else
497  storage &= ~(static_cast<T> (bit));
498 }
499 
500 //------------------------------------------------------------------------
501 template <typename T, typename B>
502 inline constexpr bool hasBit (T storage, B bit)
503 {
504  static_assert (std::is_integral<T>::value, "only works for integral types");
505  static_assert (sizeof (T) >= sizeof (B), "bit type is too big");
506  return (storage & static_cast<T> (bit)) ? true : false;
507 }
508 
509 } // namespace
510 
511 //-----------------------------------------------------------------------------
512 #include "vstguidebug.h"
513 
514 #endif
Definition: vstguibase.h:299
Base Object with reference counter.
Definition: vstguibase.h:276
void remember() override
increase refcount
Definition: vstguibase.h:261
Definition: customcontrols.cpp:8
Definition: vstguibase.h:359
Definition: vstguibase.h:247
void forget() override
decrease refcount and delete object if refcount == 0
Definition: vstguibase.h:260
Definition: vstguibase.h:238
virtual int32_t getNbReference() const
get refcount
Definition: vstguibase.h:262