Skip to content

Commit 8353f49

Browse files
committed
constexpr + noexcept SafeArg
1 parent 18db2e3 commit 8353f49

File tree

2 files changed

+51
-52
lines changed

2 files changed

+51
-52
lines changed

src/common/classes/SafeArg.cpp

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,18 @@
2929
// Localized messages type-safe printing facility.
3030

3131
#include "firebird.h"
32+
#include <algorithm>
3233
#include "SafeArg.h"
3334

3435
namespace MsgFormat
3536
{
3637

3738
// This is just a silly convenience in case all arguments are of type int.
38-
SafeArg::SafeArg(const int val[], FB_SIZE_T v_size)
39+
SafeArg::SafeArg(const int val[], FB_SIZE_T v_size) noexcept
3940
: m_extras(0)
4041
{
41-
if (v_size > SAFEARG_MAX_ARG)
42-
v_size = SAFEARG_MAX_ARG; // Simply truncate.
43-
44-
m_count = v_size;
42+
// Simply truncate
43+
m_count = std::min(v_size, SAFEARG_MAX_ARG);
4544

4645
for (FB_SIZE_T a_count = 0; a_count < m_count; ++a_count)
4746
{
@@ -51,7 +50,7 @@ SafeArg::SafeArg(const int val[], FB_SIZE_T v_size)
5150
}
5251

5352
// Here follows the list of overloads to insert the basic data types.
54-
SafeArg& SafeArg::operator<<(char c)
53+
SafeArg& SafeArg::operator<<(char c) noexcept
5554
{
5655
if (m_count < SAFEARG_MAX_ARG)
5756
{
@@ -62,7 +61,7 @@ SafeArg& SafeArg::operator<<(char c)
6261
return *this;
6362
}
6463

65-
SafeArg& SafeArg::operator<<(unsigned char c)
64+
SafeArg& SafeArg::operator<<(unsigned char c) noexcept
6665
{
6766
if (m_count < SAFEARG_MAX_ARG)
6867
{
@@ -74,7 +73,7 @@ SafeArg& SafeArg::operator<<(unsigned char c)
7473
}
7574

7675
/*
77-
SafeArg& SafeArg::operator<<(_int16 c)
76+
SafeArg& SafeArg::operator<<(_int16 c) noexcept
7877
{
7978
if (m_count < SAFEARG_MAX_ARG)
8079
{
@@ -86,7 +85,7 @@ SafeArg& SafeArg::operator<<(_int16 c)
8685
}
8786
*/
8887

89-
SafeArg& SafeArg::operator<<(short c)
88+
SafeArg& SafeArg::operator<<(short c) noexcept
9089
{
9190
if (m_count < SAFEARG_MAX_ARG)
9291
{
@@ -97,7 +96,7 @@ SafeArg& SafeArg::operator<<(short c)
9796
return *this;
9897
}
9998

100-
SafeArg& SafeArg::operator<<(unsigned short c)
99+
SafeArg& SafeArg::operator<<(unsigned short c) noexcept
101100
{
102101
if (m_count < SAFEARG_MAX_ARG)
103102
{
@@ -109,7 +108,7 @@ SafeArg& SafeArg::operator<<(unsigned short c)
109108
}
110109

111110
/*
112-
SafeArg& SafeArg::operator<<(_int32 c)
111+
SafeArg& SafeArg::operator<<(_int32 c) noexcept
113112
{
114113
if (m_count < SAFEARG_MAX_ARG)
115114
{
@@ -121,7 +120,7 @@ SafeArg& SafeArg::operator<<(_int32 c)
121120
}
122121
*/
123122

124-
SafeArg& SafeArg::operator<<(int c)
123+
SafeArg& SafeArg::operator<<(int c) noexcept
125124
{
126125
if (m_count < SAFEARG_MAX_ARG)
127126
{
@@ -132,7 +131,7 @@ SafeArg& SafeArg::operator<<(int c)
132131
return *this;
133132
}
134133

135-
SafeArg& SafeArg::operator<<(unsigned int c)
134+
SafeArg& SafeArg::operator<<(unsigned int c) noexcept
136135
{
137136
if (m_count < SAFEARG_MAX_ARG)
138137
{
@@ -143,7 +142,7 @@ SafeArg& SafeArg::operator<<(unsigned int c)
143142
return *this;
144143
}
145144

146-
SafeArg& SafeArg::operator<<(long int c)
145+
SafeArg& SafeArg::operator<<(long int c) noexcept
147146
{
148147
if (m_count < SAFEARG_MAX_ARG)
149148
{
@@ -154,7 +153,7 @@ SafeArg& SafeArg::operator<<(long int c)
154153
return *this;
155154
}
156155

157-
SafeArg& SafeArg::operator<<(unsigned long int c)
156+
SafeArg& SafeArg::operator<<(unsigned long int c) noexcept
158157
{
159158
if (m_count < SAFEARG_MAX_ARG)
160159
{
@@ -165,7 +164,7 @@ SafeArg& SafeArg::operator<<(unsigned long int c)
165164
return *this;
166165
}
167166

168-
SafeArg& SafeArg::operator<<(SINT64 c)
167+
SafeArg& SafeArg::operator<<(SINT64 c) noexcept
169168
{
170169
if (m_count < SAFEARG_MAX_ARG)
171170
{
@@ -176,7 +175,7 @@ SafeArg& SafeArg::operator<<(SINT64 c)
176175
return *this;
177176
}
178177

179-
SafeArg& SafeArg::operator<<(FB_UINT64 c)
178+
SafeArg& SafeArg::operator<<(FB_UINT64 c) noexcept
180179
{
181180
if (m_count < SAFEARG_MAX_ARG)
182181
{
@@ -189,7 +188,7 @@ SafeArg& SafeArg::operator<<(FB_UINT64 c)
189188

190189

191190
/*
192-
SafeArg& SafeArg::operator<<(long c)
191+
SafeArg& SafeArg::operator<<(long c) noexcept
193192
{
194193
if (m_count < SAFEARG_MAX_ARG)
195194
{
@@ -201,7 +200,7 @@ SafeArg& SafeArg::operator<<(long c)
201200
}
202201
*/
203202

204-
SafeArg& SafeArg::operator<<(SINT128 c)
203+
SafeArg& SafeArg::operator<<(SINT128 c) noexcept
205204
{
206205
if (m_count < SAFEARG_MAX_ARG)
207206
{
@@ -212,7 +211,7 @@ SafeArg& SafeArg::operator<<(SINT128 c)
212211
return *this;
213212
}
214213

215-
SafeArg& SafeArg::operator<<(double c)
214+
SafeArg& SafeArg::operator<<(double c) noexcept
216215
{
217216
if (m_count < SAFEARG_MAX_ARG)
218217
{
@@ -223,7 +222,7 @@ SafeArg& SafeArg::operator<<(double c)
223222
return *this;
224223
}
225224

226-
SafeArg& SafeArg::operator<<(const char* c)
225+
SafeArg& SafeArg::operator<<(const char* c) noexcept
227226
{
228227
if (m_count < SAFEARG_MAX_ARG)
229228
{
@@ -234,7 +233,7 @@ SafeArg& SafeArg::operator<<(const char* c)
234233
return *this;
235234
}
236235

237-
SafeArg& SafeArg::operator<<(const unsigned char* c)
236+
SafeArg& SafeArg::operator<<(const unsigned char* c) noexcept
238237
{
239238
if (m_count < SAFEARG_MAX_ARG)
240239
{
@@ -245,7 +244,7 @@ SafeArg& SafeArg::operator<<(const unsigned char* c)
245244
return *this;
246245
}
247246

248-
SafeArg& SafeArg::operator<<(void* c)
247+
SafeArg& SafeArg::operator<<(void* c) noexcept
249248
{
250249
if (m_count < SAFEARG_MAX_ARG)
251250
{
@@ -262,7 +261,7 @@ SafeArg& SafeArg::operator<<(void* c)
262261
// converted to null pointer and void* to TEXT*. Supposedly, the caller has
263262
// information on the real types of the values. This can be done with a loop
264263
// using getCount() and getCell() and looking at the safe_cell's type data member.
265-
void SafeArg::dump(const TEXT* target[], FB_SIZE_T v_size) const
264+
void SafeArg::dump(const TEXT* target[], FB_SIZE_T v_size) const noexcept
266265
{
267266
for (FB_SIZE_T i = 0; i < v_size; ++i)
268267
{
@@ -307,9 +306,9 @@ void SafeArg::dump(const TEXT* target[], FB_SIZE_T v_size) const
307306

308307
// Get one specific cell. If out of range, a cell with invalid type (at_none)
309308
// is returned.
310-
const safe_cell& SafeArg::getCell(FB_SIZE_T index) const
309+
const safe_cell& SafeArg::getCell(FB_SIZE_T index) const noexcept
311310
{
312-
static safe_cell aux_cell = {safe_cell::at_none, {0}};
311+
static constexpr safe_cell aux_cell = {safe_cell::at_none, {0}};
313312

314313
if (index < m_count)
315314
return m_arguments[index];

src/common/classes/SafeArg.h

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace MsgFormat
6565
{
6666

6767
// For now we allow 9 parameters; @1..@9 in MsgPrint.
68-
const FB_SIZE_T SAFEARG_MAX_ARG = 9;
68+
inline constexpr FB_SIZE_T SAFEARG_MAX_ARG = 9;
6969

7070
// This is the unit that represents one parameter in the format routines.
7171
// The user of the routines rarely needs to be concerned with it.
@@ -142,29 +142,29 @@ class BaseStream;
142142
class SafeArg
143143
{
144144
public:
145-
SafeArg();
146-
SafeArg(const int val[], FB_SIZE_T v_size);
147-
SafeArg& clear();
148-
SafeArg& operator<<(char c);
149-
SafeArg& operator<<(unsigned char c);
150-
//SafeArg& operator<<(int16_t c);
151-
SafeArg& operator<<(short c);
152-
SafeArg& operator<<(unsigned short c);
153-
//SafeArg& operator<<(int32_t c);
154-
SafeArg& operator<<(int c);
155-
SafeArg& operator<<(unsigned int c);
156-
SafeArg& operator<<(long int c);
157-
SafeArg& operator<<(unsigned long int c);
158-
SafeArg& operator<<(SINT64 c);
159-
SafeArg& operator<<(FB_UINT64 c);
160-
//SafeArg& operator<<(long c);
161-
SafeArg& operator<<(SINT128 c);
162-
SafeArg& operator<<(double c);
163-
SafeArg& operator<<(const char* c);
164-
SafeArg& operator<<(const unsigned char* c);
165-
SafeArg& operator<<(void* c);
166-
void dump(const TEXT* target[], FB_SIZE_T v_size) const;
167-
const safe_cell& getCell(FB_SIZE_T index) const;
145+
SafeArg() noexcept;
146+
SafeArg(const int val[], FB_SIZE_T v_size) noexcept;
147+
SafeArg& clear() noexcept;
148+
SafeArg& operator<<(char c) noexcept;
149+
SafeArg& operator<<(unsigned char c) noexcept;
150+
//SafeArg& operator<<(int16_t c) noexcept;
151+
SafeArg& operator<<(short c) noexcept;
152+
SafeArg& operator<<(unsigned short c) noexcept;
153+
//SafeArg& operator<<(int32_t c) noexcept;
154+
SafeArg& operator<<(int c) noexcept;
155+
SafeArg& operator<<(unsigned int c) noexcept;
156+
SafeArg& operator<<(long int c) noexcept;
157+
SafeArg& operator<<(unsigned long int c) noexcept;
158+
SafeArg& operator<<(SINT64 c) noexcept;
159+
SafeArg& operator<<(FB_UINT64 c) noexcept;
160+
//SafeArg& operator<<(long c) noexcept;
161+
SafeArg& operator<<(SINT128 c) noexcept;
162+
SafeArg& operator<<(double c) noexcept;
163+
SafeArg& operator<<(const char* c) noexcept;
164+
SafeArg& operator<<(const unsigned char* c) noexcept;
165+
SafeArg& operator<<(void* c) noexcept;
166+
void dump(const TEXT* target[], FB_SIZE_T v_size) const noexcept;
167+
const safe_cell& getCell(FB_SIZE_T index) const noexcept;
168168
FB_SIZE_T getCount() const noexcept;
169169

170170
private:
@@ -176,12 +176,12 @@ class SafeArg
176176
bool userFormatting);
177177
};
178178

179-
inline SafeArg::SafeArg()
179+
inline SafeArg::SafeArg() noexcept
180180
: m_count(0), m_extras(0)
181181
{
182182
}
183183

184-
inline SafeArg& SafeArg::clear()
184+
inline SafeArg& SafeArg::clear() noexcept
185185
{
186186
m_count = 0;
187187
m_extras = 0;

0 commit comments

Comments
 (0)