|
| 1 | +/*************************************************************************** |
| 2 | + qgsoptional.h - QgsOptional |
| 3 | +
|
| 4 | + --------------------- |
| 5 | + begin : 7.9.2016 |
| 6 | + copyright : (C) 2016 by Matthias Kuhn |
| 7 | + email : matthias@opengis.ch |
| 8 | + *************************************************************************** |
| 9 | + * * |
| 10 | + * This program is free software; you can redistribute it and/or modify * |
| 11 | + * it under the terms of the GNU General Public License as published by * |
| 12 | + * the Free Software Foundation; either version 2 of the License, or * |
| 13 | + * (at your option) any later version. * |
| 14 | + * * |
| 15 | + ***************************************************************************/ |
| 16 | +#ifndef QGSOPTIONAL_H |
| 17 | +#define QGSOPTIONAL_H |
| 18 | + |
| 19 | +/** |
| 20 | + * \ingroup core |
| 21 | + * |
| 22 | + * \brief |
| 23 | + * QgsOptional is a container for other classes and adds an additional enabled/disabled flag. |
| 24 | + * |
| 25 | + * Often it is used for configuration options which can be enabled or disabled but also have |
| 26 | + * more internal configuration information that should not be lost when disabling and re-enabling. |
| 27 | + * |
| 28 | + * @note Added in QGIS 3.0 |
| 29 | + * @note For python you need to use implementations for specific template classes |
| 30 | + */ |
| 31 | +template<class T> |
| 32 | +class CORE_EXPORT QgsOptional |
| 33 | +{ |
| 34 | + public: |
| 35 | + /** |
| 36 | + * A QgsOptional is disabled by default if default constructed. |
| 37 | + */ |
| 38 | + QgsOptional() |
| 39 | + : mEnabled( false ) |
| 40 | + { |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * A QgsOptional is enabled by default if constructed with payload. |
| 45 | + */ |
| 46 | + QgsOptional( const T& data ) |
| 47 | + : mEnabled( true ) |
| 48 | + , mData( data ) |
| 49 | + { |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * A QgsOptional constructed with enabled status and data |
| 54 | + */ |
| 55 | + QgsOptional( const T& data, bool enabled ) |
| 56 | + : mEnabled( enabled ) |
| 57 | + , mData( data ) |
| 58 | + { |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Compare this QgsOptional to another one. |
| 63 | + * |
| 64 | + * This will compare the enabled flag and call the == operator |
| 65 | + * of the contained class. |
| 66 | + * |
| 67 | + * @note Added in QGIS 3.0 |
| 68 | + */ |
| 69 | + bool operator== ( const QgsOptional<T>& other ) const |
| 70 | + { |
| 71 | + return mEnabled == other.mEnabled && mData == other.mData; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Boolean operator. Will return true if this optional is enabled. |
| 76 | + */ |
| 77 | + operator bool() const |
| 78 | + { |
| 79 | + return mEnabled; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Check if this optional is enabled |
| 84 | + * |
| 85 | + * @note Added in QGIS 3.0 |
| 86 | + */ |
| 87 | + bool enabled() const |
| 88 | + { |
| 89 | + return mEnabled; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Set if this optional is enabled |
| 94 | + * |
| 95 | + * @note Added in QGIS 3.0 |
| 96 | + */ |
| 97 | + void setEnabled( bool enabled ) |
| 98 | + { |
| 99 | + mEnabled = enabled; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Access the payload data |
| 104 | + * |
| 105 | + * @note Added in QGIS 3.0 |
| 106 | + */ |
| 107 | + const T* operator->() const |
| 108 | + { |
| 109 | + return &mData; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Access the payload data |
| 114 | + * |
| 115 | + * @note Added in QGIS 3.0 |
| 116 | + */ |
| 117 | + T data() const |
| 118 | + { |
| 119 | + return mData; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Set the payload data |
| 124 | + * |
| 125 | + * @note Added in QGIS 3.0 |
| 126 | + */ |
| 127 | + void setData( const T& data ) |
| 128 | + { |
| 129 | + mData = data; |
| 130 | + } |
| 131 | + |
| 132 | + private: |
| 133 | + T mData; |
| 134 | + bool mEnabled; |
| 135 | +}; |
| 136 | + |
| 137 | +#endif // QGSOPTIONAL_H |
0 commit comments