PageRenderTime 193ms CodeModel.GetById 91ms app.highlight 4ms RepoModel.GetById 96ms app.codeStats 0ms

/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/*!
 27\class QxtNullable QxtNullable
 28\inmodule QxtCore
 29\brief distinct null value compatible with any data type.
 30
 31in general it's a templated abstraction to allow any data type to be
 32expressed with a null value distinct from any real value. An example
 33of such a use is for optional arguments.
 34\n
 35prepare a function for argument skipping:
 36
 37\code
 38void  somefunction( qxtNull(int,a) , qxtNull(int,b) )
 39{
 40
 41if (!a.isNull())
 42 {
 43 int i = a.value();
 44 //do something with i
 45 }
 46 if (!b.isNull())
 47 {
 48 int x = b.value();
 49 //do something with x
 50 }
 51}
 52\endcode
 53
 54usage:
 55\code
 56
 57somefunction(SKIP,1,2);
 58somefunction(3,4);
 59somefunction(3,SKIP,6);
 60somefunction(1);
 61\endcode
 62
 63*/
 64
 65#ifndef QXTNULLABLE_H
 66#define QXTNULLABLE_H
 67#include <qxtglobal.h>
 68
 69/*! \relates QxtNullable
 70 * defines a skipable argument with type \a t and variable name \a n
 71 */
 72#define qxtNull(t,n)   QxtNullable<t> n = QxtNullable<t>()
 73
 74#include <qxtnull.h>
 75
 76template<typename T>
 77class /*QXT_CORE_EXPORT*/ QxtNullable
 78{
 79public:
 80    QxtNullable(QxtNull);
 81    QxtNullable(const T& p);
 82    QxtNullable();
 83
 84    ///determinates if the Value is set to something meaningfull
 85    bool isNull() const;
 86
 87    ///delete Value
 88    void nullify();
 89
 90    T& value() const;
 91    operator T() const;
 92    void operator=(const T& p);
 93
 94private:
 95    T* val;
 96};
 97
 98template<typename T>
 99QxtNullable<T>::QxtNullable(QxtNull)
100{
101    val = 0;
102}
103
104template<typename T>
105QxtNullable<T>::QxtNullable(const T& p)
106{
107    val = const_cast<T*>(&p);
108}
109
110template<typename T>
111QxtNullable<T>::QxtNullable()
112{
113    val = 0;
114}
115
116template<typename T>
117QxtNullable<T>::operator T() const
118{
119    return *val;
120}
121
122template<typename T>
123T& QxtNullable<T>::value() const
124{
125    return *val;
126}
127
128template<typename T>
129bool QxtNullable<T>::isNull() const
130{
131    return (val == 0);
132}
133
134template<typename T>
135void QxtNullable<T>::nullify()
136{
137    val = 0;
138}
139
140template<typename T>
141void QxtNullable<T>::operator=(const T & p)
142{
143    val = const_cast<T*>(&p);
144}
145
146#endif