File: Option.h

package info (click to toggle)
qtop 2.3.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,976 kB
  • sloc: cpp: 40,477; makefile: 7
file content (281 lines) | stat: -rw-r--r-- 6,696 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#ifndef Option_h
#define Option_h

/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* Any WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program.  If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/

#include "Counter.h"
#include "Debug.h"

#include <QDataStream>
#include <QString>
#include <QTextStream>

//* stream boolean
QTextStream& operator >> ( QTextStream& in, bool& value );

/**
\class   Option
\brief   Option objects for string, int, bool and double options
*/
class Option:private Base::Counter<Option>
{

    public:

    //* flags
    enum Flag
    {
        None = 0,
        Current = 1 << 0,
        Recordable = 1<<1
    };

    Q_DECLARE_FLAGS(Flags, Flag)

    //* default constructor
    explicit Option();

    //* constructor
    explicit Option( const char*, Flags = Flag::Recordable );

    //* constructor
    explicit Option( const QByteArray&, Flags = Flag::Recordable );

    //* constructor
    explicit Option( const QString&, Flags = Flag::Recordable );

    //* destructor
    virtual ~Option() = default;

    //*@name accessors
    //@{

    //* flags
    Flags flags() const
    { return flags_; }

    //* flags
    bool hasFlag( const Flag& flag ) const
    { return flags_ & flag; }

    //* current
    bool isCurrent() const
    { return hasFlag( Flag::Current ); }

    //* default
    bool isDefault() const
    { return defaultValue_ == value_ && defaultFlags_ == flags_; }

    //* recordable
    bool isRecordable() const
    { return hasFlag( Flag::Recordable ); }

    //* raw accessor
    const QByteArray& raw() const
    { return value_; }

    //* default value
    const QByteArray& defaultValue() const
    { return defaultValue_; }

    //* generic accessor
    template < typename T >
        T get() const
    {

        // check if option is set
        Q_ASSERT( !value_.isEmpty() );

        // cast value
        // the const-cast here is because the string should not be affected
        // (hence the ReadOnly) but Qt does not allow to pass a const pointer
        QTextStream s( const_cast<QByteArray*>(&value_), QIODevice::ReadOnly );
        T out;
        s >> out;
        Q_ASSERT( s.status() == QTextStream::Ok );
        return out;
    }

    //* check status
    bool isSet() const
    {return !value_.isEmpty();}

    //@}

    //*@name modifiers
    //@{

    //* flags
    Option& setFlags( Flags value )
    {
        flags_ = value;
        return *this;
    }

    //* flags
    Option& setFlag( Flag flag, bool value = true )
    {
        if( value ) { flags_ |= flag; }
        else { flags_ &= ~static_cast<Flags>(flag); }
        return *this;
    }

    //* current
    Option& setCurrent( bool value )
    { return setFlag( Flag::Current, value ); }

    //* default
    Option& setDefault()
    {
        defaultValue_ = value_;
        defaultFlags_ = flags_;
        return *this;
    }

    //* raw modifier
    Option& setRaw( const QByteArray& value )
    {
        value_ = value;
        return *this;
    }

    //* raw modifier
    Option& setRaw( const QString& value )
    {
        value_ = value.toUtf8();
        return *this;
    }

    //* generic modifier
    template < typename T >
        Option& set( const T& value )
    {

        value_.clear();
        QTextStream s( &value_, QIODevice::WriteOnly );
        s << value;
        return *this;

    }

    //* restore default value
    Option& restoreDefault()
    {
        value_ = defaultValue_;
        flags_ = defaultFlags_;
        return *this;
    }

    //@}

    //* used to retrieve file records that match a given flag
    class HasFlagFTor
    {

        public:

        //* constructor
        explicit HasFlagFTor( Option::Flags flags ):
            flags_( flags )
        {}

        //* predicate
        bool operator() ( const Option& option ) const
        { return option.flags() & flags_; }

        //* sorting predicate
        /** it is used to ensure that options that have a given flag appear first in a list */
        bool operator() (const Option& first, const Option& second ) const
        { return ( (first.flags() & flags_) && !(second.flags()&flags_) ); }

        private:

        // predicted flag
        Option::Flags flags_;

    };

    //* used to get options with matching values
    class SameValueFTor
    {
        public:

        //* constructor
        explicit SameValueFTor( const Option& option ):
            value_( option.value_ )
            {}

        //* predicate
        bool operator() (const Option& other ) const
        { return value_ == other.value_; }

        private:

        //* prediction
        QByteArray value_;

    };

    private:

    //* option value
    QByteArray value_;

    //* option default value
    QByteArray defaultValue_;

    //* flags
    Flags flags_ = Flag::Recordable;

    //* default flags
    Flags defaultFlags_ = Flag::Recordable;

    //*@name serializer
    //@{

    friend QDataStream &operator << (QDataStream&, const Option& option );
    friend QDataStream &operator >> (QDataStream&, Option& option );


    //@}

    //*@name text streamer
    //@{
    friend QTextStream &operator << ( QTextStream &out, const Option& option );
    friend QTextStream &operator << ( QTextStream &out, const QList<Option>& options );
    //@}

    //* less than operator
    friend bool operator < (const Option& first, const Option& second )
    {
        if( first.value_ != second.value_ ) return first.value_ < second.value_;
        else if( first.flags_ != second.flags_ ) return first.flags_ < second.flags_;
        else if( first.defaultValue_ != second.defaultValue_ ) return first.defaultValue_ < second.defaultValue_;
        else return first.defaultFlags_ < second.defaultFlags_;
    }

    //* equal to operator
    friend bool operator == (const Option& first, const Option& second )
    { return first.value_ == second.value_ && first.flags_ == second.flags_; }
};

Q_DECLARE_OPERATORS_FOR_FLAGS( Option::Flags )

#endif