/thirdparty/qxt/qxtweb-standalone/qxtweb/qxtnullable.h

http://github.com/tomahawk-player/tomahawk · C Header · 146 lines · 61 code · 18 blank · 67 comment · 1 complexity · ec6ea087de71698e89c9d50b2092c823 MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) Qxt Foundation. Some rights reserved.
  4. **
  5. ** This file is part of the QxtCore module of the Qxt library.
  6. **
  7. ** This library is free software; you can redistribute it and/or modify it
  8. ** under the terms of the Common Public License, version 1.0, as published
  9. ** by IBM, and/or under the terms of the GNU Lesser General Public License,
  10. ** version 2.1, as published by the Free Software Foundation.
  11. **
  12. ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
  13. ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
  14. ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
  15. ** FITNESS FOR A PARTICULAR PURPOSE.
  16. **
  17. ** You should have received a copy of the CPL and the LGPL along with this
  18. ** file. See the LICENSE file and the cpl1.0.txt/lgpl-2.1.txt files
  19. ** included with the source distribution for more information.
  20. ** If you did not receive a copy of the licenses, contact the Qxt Foundation.
  21. **
  22. ** <http://libqxt.org> <foundation@libqxt.org>
  23. **
  24. ****************************************************************************/
  25. /*!
  26. \class QxtNullable QxtNullable
  27. \inmodule QxtCore
  28. \brief distinct null value compatible with any data type.
  29. in general it's a templated abstraction to allow any data type to be
  30. expressed with a null value distinct from any real value. An example
  31. of such a use is for optional arguments.
  32. \n
  33. prepare a function for argument skipping:
  34. \code
  35. void somefunction( qxtNull(int,a) , qxtNull(int,b) )
  36. {
  37. if (!a.isNull())
  38. {
  39. int i = a.value();
  40. //do something with i
  41. }
  42. if (!b.isNull())
  43. {
  44. int x = b.value();
  45. //do something with x
  46. }
  47. }
  48. \endcode
  49. usage:
  50. \code
  51. somefunction(SKIP,1,2);
  52. somefunction(3,4);
  53. somefunction(3,SKIP,6);
  54. somefunction(1);
  55. \endcode
  56. */
  57. #ifndef QXTNULLABLE_H
  58. #define QXTNULLABLE_H
  59. #include <qxtglobal.h>
  60. /*! \relates QxtNullable
  61. * defines a skipable argument with type \a t and variable name \a n
  62. */
  63. #define qxtNull(t,n) QxtNullable<t> n = QxtNullable<t>()
  64. #include <qxtnull.h>
  65. template<typename T>
  66. class /*QXT_CORE_EXPORT*/ QxtNullable
  67. {
  68. public:
  69. QxtNullable(QxtNull);
  70. QxtNullable(const T& p);
  71. QxtNullable();
  72. ///determinates if the Value is set to something meaningfull
  73. bool isNull() const;
  74. ///delete Value
  75. void nullify();
  76. T& value() const;
  77. operator T() const;
  78. void operator=(const T& p);
  79. private:
  80. T* val;
  81. };
  82. template<typename T>
  83. QxtNullable<T>::QxtNullable(QxtNull)
  84. {
  85. val = 0;
  86. }
  87. template<typename T>
  88. QxtNullable<T>::QxtNullable(const T& p)
  89. {
  90. val = const_cast<T*>(&p);
  91. }
  92. template<typename T>
  93. QxtNullable<T>::QxtNullable()
  94. {
  95. val = 0;
  96. }
  97. template<typename T>
  98. QxtNullable<T>::operator T() const
  99. {
  100. return *val;
  101. }
  102. template<typename T>
  103. T& QxtNullable<T>::value() const
  104. {
  105. return *val;
  106. }
  107. template<typename T>
  108. bool QxtNullable<T>::isNull() const
  109. {
  110. return (val == 0);
  111. }
  112. template<typename T>
  113. void QxtNullable<T>::nullify()
  114. {
  115. val = 0;
  116. }
  117. template<typename T>
  118. void QxtNullable<T>::operator=(const T & p)
  119. {
  120. val = const_cast<T*>(&p);
  121. }
  122. #endif