File: size.h

package info (click to toggle)
yapet 2.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,920 kB
  • sloc: cpp: 32,397; sh: 5,032; makefile: 880; ansic: 36; sed: 16
file content (238 lines) | stat: -rw-r--r-- 5,813 bytes parent folder | download | duplicates (4)
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
// -*- mode: c++ -*-
//
// This file is part of libyacurs.
// Copyright (C) 2013  Rafael Ostertag
//
// This program 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 3 of the
// License, or (at your option) any later version.
//
// This program 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/>.
//
//
// $Id$

#ifndef SIZE_H
#define SIZE_H 1

// Can't use #ifdef HAVE_STDINT_H since that would require pulling in
// config.h or libyacurscfg.h which might cause undesired side
// effects.
#include <stdint.h>

#include <cassert>
#include <stdexcept>
#include <string>

namespace YACURS {
/**
 * Describes size in terms of lines and columns.
 *
 * Neither lines nor columns may be less than 0. When subtracting two
 * sizes object, and either component would be smaller than 0, the
 * value in question is silently set to 0.
 *
 */
class Size {
   private:
    /**
     * Object representing zero.
     */
    static Size _zero;
    int16_t _rows, _cols;

   public:
    /**
     * Constructor.
     *
     * When no arguments are provided, it initializes the object
     * to zero, i.e. rows=, lines=0.
     *
     * @param rows amount of rows
     * @param cols amount of columns.
     */
    Size(int16_t rows = 0, int16_t cols = 0);

    /**
     * Get the colums.
     *
     * @return the number of columns.
     */
    int16_t cols() const;

    /**
     * Get the rows.
     *
     * @return the number of rows.
     */
    int16_t rows() const;

    /**
     * Set the number of columns.
     *
     * If number of columns is tried to set to a negative value, an
     * out_of_range exception is thrown.
     *
     * @param cols number of columns
     */
    void cols(int16_t cols);

    /**
     * Set the number of rows.
     *
     * If number of rows is tried to set to a negative value, an
     * out_of_range exception is thrown.
     *
     * @param rows number of rows
     */
    void rows(int16_t rows);

    /**
     * Add Size to this object.
     *
     * Addition will be performed component wise, i.e. rows +
     * rows, columns + columns.
     *
     * @param rhs Size to add.
     *
     * @returns reference to this.
     */
    const Size& operator+=(const Size& rhs);

    /**
     * Subtract Size from this object.
     *
     * Subtraction will be performed component wise, i.e. rows -
     * rows, columns - columns. If either resulting component
     * would be less than 0, it will be silently set to 0.
     *
     * @param rhs Size to subtract.
     *
     * @returns reference to this.
     */
    const Size& operator-=(const Size& rhs);

    /**
     * Test two Size objects for equality.
     *
     * @param rhs right hand side
     *
     * @return @c true if lines and rows are equal, @c false
     * otherwise.
     */
    bool operator==(const Size& rhs) const;

    /**
     * Return the object representing zero.
     *
     * @return Size object representing zero.
     */
    static const Size& zero();
};

/**
 * Add Size to this object.
 *
 * Addition will be performed component wise, i.e. rows +
 * rows, columns + columns.
 *
 * @param rhs Size to add.
 *
 * @returns size object holding sum
 */
Size operator+(const Size& lhs, const Size& rhs);

/**
 * Subtract this from Size object
 *
 * Subtraction will be performed component wise, i.e. rows -
 * rows, columns - columns. If either resulting component
 * would be less than 0, it will be silently set to 0.
 *
 * @param rhs Size object
 *
 * @returns size object holding difference
 */
Size operator-(const Size& lhs, const Size& rhs);

/**
 * Test two Size objects for inequality.
 *
 * @param rhs right hand side.
 *
 * @return @c true if lines or rows are not equal, @c false
 * otherwise.
 */
bool operator!=(const Size& lhs, const Size& rhs);

inline Size::Size(int16_t rows, int16_t cols) : _rows(rows), _cols(cols) {
    // Please note: The two strings below will not be translated,
    // since that would require including "gettext.h" which is not
    // supposed to be installed.
    if (_rows < 0) throw std::out_of_range("Rows cannot be <0");
    if (_cols < 0) throw std::out_of_range("Columns cannot be <0");
}

inline int16_t Size::cols() const { return _cols; }

inline int16_t Size::rows() const { return _rows; }

inline void Size::cols(int16_t cols) {
    if (cols < 0) throw std::out_of_range("Columns cannot be <0");
    _cols = cols;
}

inline void Size::rows(int16_t rows) {
    if (rows < 0) throw std::out_of_range("Rows cannot be <0");
    _rows = rows;
}

inline const Size& Size::operator+=(const Size& rhs) {
    assert(rhs._cols >= 0);
    assert(rhs._rows >= 0);
    _cols += rhs._cols;
    _rows += rhs._rows;
    return *this;
}

inline const Size& Size::operator-=(const Size& rhs) {
    _cols -= rhs._cols;
    if (_cols < 0) _cols = 0;

    _rows -= rhs._rows;
    if (_rows < 0) _rows = 0;
    return *this;
}

inline bool Size::operator==(const Size& rhs) const {
    return _cols == rhs._cols && _rows == rhs._rows;
}

inline const Size& Size::zero() { return _zero; }

inline Size operator+(const Size& lhs, const Size& rhs) {
    Size tmp = lhs;

    return tmp += rhs;
}

inline Size operator-(const Size& lhs, const Size& rhs) {
    Size tmp = lhs;

    return tmp -= rhs;
}

inline bool operator!=(const Size& lhs, const Size& rhs) {
    return !(lhs == rhs);
}
}  // namespace YACURS

#endif  // SIZE_H