Skip to content

Commit 1da1d7b

Browse files
authored
Add move assignment operators to Statement and Attachment (asfernandes#36)
1 parent 8be6ce5 commit 1da1d7b

10 files changed

Lines changed: 159 additions & 42 deletions

File tree

src/fb-cpp/Attachment.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ using namespace fbcpp::impl;
3131

3232

3333
Attachment::Attachment(Client& client, const std::string& uri, const AttachmentOptions& options)
34-
: client{client}
34+
: client{&client}
3535
{
3636
const auto master = client.getMaster();
3737

@@ -68,8 +68,8 @@ void Attachment::disconnectOrDrop(bool drop)
6868
{
6969
assert(isValid());
7070

71-
const auto status = client.newStatus();
72-
StatusWrapper statusWrapper{client, status.get()};
71+
const auto status = client->newStatus();
72+
StatusWrapper statusWrapper{*client, status.get()};
7373

7474
if (drop)
7575
handle->dropDatabase(&statusWrapper);

src/fb-cpp/Attachment.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,22 @@ namespace fbcpp
192192
{
193193
}
194194

195-
Attachment& operator=(Attachment&&) = delete;
195+
///
196+
/// @brief Transfers ownership of another Attachment into this one.
197+
///
198+
/// The old handle is released via `FbRef::operator=(FbRef&&)`.
199+
/// After the assignment, `this` is valid (with `o`'s handle) and `o` is invalid.
200+
///
201+
Attachment& operator=(Attachment&& o) noexcept
202+
{
203+
if (this != &o)
204+
{
205+
client = o.client;
206+
handle = std::move(o.handle);
207+
}
208+
209+
return *this;
210+
}
196211

197212
Attachment(const Attachment&) = delete;
198213
Attachment& operator=(const Attachment&) = delete;
@@ -229,7 +244,7 @@ namespace fbcpp
229244
///
230245
Client& getClient() noexcept
231246
{
232-
return client;
247+
return *client;
233248
}
234249

235250
///
@@ -254,7 +269,7 @@ namespace fbcpp
254269
void disconnectOrDrop(bool drop);
255270

256271
private:
257-
Client& client;
272+
Client* client;
258273
FbRef<fb::IAttachment> handle;
259274
};
260275
} // namespace fbcpp

src/fb-cpp/CalendarConverter.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace fbcpp::impl
4949
{
5050
public:
5151
explicit CalendarConverter(Client& client, StatusWrapper* statusWrapper)
52-
: client{client},
52+
: client{&client},
5353
statusWrapper{statusWrapper}
5454
{
5555
}
@@ -67,7 +67,7 @@ namespace fbcpp::impl
6767
if (yearValue <= 0)
6868
throwInvalidDateValue();
6969

70-
return OpaqueDate{client.getUtil()->encodeDate(static_cast<unsigned>(yearValue), monthValue, dayValue)};
70+
return OpaqueDate{client->getUtil()->encodeDate(static_cast<unsigned>(yearValue), monthValue, dayValue)};
7171
}
7272

7373
Date opaqueDateToDate(OpaqueDate date)
@@ -76,7 +76,7 @@ namespace fbcpp::impl
7676
unsigned month;
7777
unsigned day;
7878

79-
client.getUtil()->decodeDate(date.value, &year, &month, &day);
79+
client->getUtil()->decodeDate(date.value, &year, &month, &day);
8080

8181
return Date{std::chrono::year{static_cast<int>(year)}, std::chrono::month{month}, std::chrono::day{day}};
8282
}
@@ -140,7 +140,7 @@ namespace fbcpp::impl
140140
const auto subseconds = static_cast<unsigned>(time.subseconds().count() / 100);
141141

142142
OpaqueTime opaqueTime;
143-
opaqueTime.value = client.getUtil()->encodeTime(hours, minutes, seconds, subseconds);
143+
opaqueTime.value = client->getUtil()->encodeTime(hours, minutes, seconds, subseconds);
144144

145145
return opaqueTime;
146146
}
@@ -152,7 +152,7 @@ namespace fbcpp::impl
152152
unsigned seconds;
153153
unsigned subseconds;
154154

155-
const auto util = client.getUtil();
155+
const auto util = client->getUtil();
156156
util->decodeTime(time.value, &hours, &minutes, &seconds, &subseconds);
157157

158158
const auto timeOfDay = std::chrono::hours{hours} + std::chrono::minutes{minutes} +
@@ -252,7 +252,7 @@ namespace fbcpp::impl
252252

253253
OpaqueTimeTz opaque{};
254254

255-
client.getUtil()->encodeTimeTz(statusWrapper, &opaque.value, 0u, 0u, 0u, 0u, timeTz.zone.c_str());
255+
client->getUtil()->encodeTimeTz(statusWrapper, &opaque.value, 0u, 0u, 0u, 0u, timeTz.zone.c_str());
256256

257257
opaque.value.utc_time = static_cast<ISC_TIME>(duration.count() / 100);
258258

@@ -269,7 +269,7 @@ namespace fbcpp::impl
269269
unsigned fractions;
270270
std::array<char, 128> timeZoneBuffer;
271271

272-
client.getUtil()->decodeTimeTz(statusWrapper, &opaqueTime.value, &hours, &minutes, &seconds, &fractions,
272+
client->getUtil()->decodeTimeTz(statusWrapper, &opaqueTime.value, &hours, &minutes, &seconds, &fractions,
273273
static_cast<unsigned>(timeZoneBuffer.size()), timeZoneBuffer.data());
274274

275275
TimeTz timeTz;
@@ -295,7 +295,7 @@ namespace fbcpp::impl
295295
unsigned fractions;
296296
std::array<char, 128> timeZoneBuffer;
297297

298-
client.getUtil()->decodeTimeTz(statusWrapper, &time.value, &hours, &minutes, &seconds, &fractions,
298+
client->getUtil()->decodeTimeTz(statusWrapper, &time.value, &hours, &minutes, &seconds, &fractions,
299299
static_cast<unsigned>(timeZoneBuffer.size()), timeZoneBuffer.data());
300300

301301
return std::format("{:02}:{:02}:{:02}.{:04} {}", hours, minutes, seconds, fractions, timeZoneBuffer.data());
@@ -360,7 +360,7 @@ namespace fbcpp::impl
360360

361361
OpaqueTimeTz encoded;
362362
const std::string timeZoneString{makeComponentView(5)};
363-
client.getUtil()->encodeTimeTz(
363+
client->getUtil()->encodeTimeTz(
364364
statusWrapper, &encoded.value, hours, minutes, seconds, fractions, timeZoneString.c_str());
365365

366366
return opaqueTimeTzToTimeTz(encoded);
@@ -389,7 +389,7 @@ namespace fbcpp::impl
389389
OpaqueTimestamp opaqueTimestamp;
390390
opaqueTimestamp.value.timestamp_date = opaqueDate.value;
391391
opaqueTimestamp.value.timestamp_time =
392-
client.getUtil()->encodeTime(static_cast<unsigned>(timestamp.time.hours().count()),
392+
client->getUtil()->encodeTime(static_cast<unsigned>(timestamp.time.hours().count()),
393393
static_cast<unsigned>(timestamp.time.minutes().count()),
394394
static_cast<unsigned>(timestamp.time.seconds().count()), static_cast<unsigned>(subseconds / 100));
395395

@@ -406,7 +406,7 @@ namespace fbcpp::impl
406406
unsigned seconds;
407407
unsigned subseconds;
408408

409-
const auto util = client.getUtil();
409+
const auto util = client->getUtil();
410410
util->decodeDate(timestamp.value.timestamp_date, &year, &month, &day);
411411
util->decodeTime(timestamp.value.timestamp_time, &hours, &minutes, &seconds, &subseconds);
412412

@@ -513,7 +513,7 @@ namespace fbcpp::impl
513513
{
514514
OpaqueTimestampTz opaque;
515515

516-
client.getUtil()->encodeTimeStampTz(
516+
client->getUtil()->encodeTimeStampTz(
517517
statusWrapper, &opaque.value, 1u, 1u, 1u, 0u, 0u, 0u, 0u, timestampTz.zone.c_str());
518518

519519
const auto utcOpaque = timestampToOpaqueTimestamp(timestampTz.utcTimestamp);
@@ -539,7 +539,7 @@ namespace fbcpp::impl
539539
unsigned subseconds;
540540
std::array<char, 128> timeZoneBuffer;
541541

542-
client.getUtil()->decodeTimeStampTz(statusWrapper, &opaqueTimestamp.value, &year, &month, &day, &hours,
542+
client->getUtil()->decodeTimeStampTz(statusWrapper, &opaqueTimestamp.value, &year, &month, &day, &hours,
543543
&minutes, &seconds, &subseconds, static_cast<unsigned>(timeZoneBuffer.size()), timeZoneBuffer.data());
544544

545545
TimestampTz timestampTz;
@@ -569,7 +569,7 @@ namespace fbcpp::impl
569569
unsigned subseconds;
570570
std::array<char, 128> timeZoneBuffer;
571571

572-
client.getUtil()->decodeTimeStampTz(statusWrapper, &timestamp.value, &year, &month, &day, &hours, &minutes,
572+
client->getUtil()->decodeTimeStampTz(statusWrapper, &timestamp.value, &year, &month, &day, &hours, &minutes,
573573
&seconds, &subseconds, static_cast<unsigned>(timeZoneBuffer.size()), timeZoneBuffer.data());
574574

575575
return std::format("{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:04} {}", year, month, day, hours, minutes,
@@ -648,7 +648,7 @@ namespace fbcpp::impl
648648

649649
OpaqueTimestampTz encoded;
650650
const std::string timeZoneString{makeComponentView(8)};
651-
client.getUtil()->encodeTimeStampTz(statusWrapper, &encoded.value,
651+
client->getUtil()->encodeTimeStampTz(statusWrapper, &encoded.value,
652652
static_cast<unsigned>(static_cast<int>(date.year())), monthValue, dayValue, hours, minutes, seconds,
653653
fractions, timeZoneString.c_str());
654654

@@ -674,7 +674,7 @@ namespace fbcpp::impl
674674
isc_arg_end,
675675
};
676676

677-
throw DatabaseException(client, STATUS_CONVERSION_ERROR_FROM_STRING);
677+
throw DatabaseException(*client, STATUS_CONVERSION_ERROR_FROM_STRING);
678678
}
679679

680680
[[noreturn]] void throwInvalidDateValue()
@@ -684,7 +684,7 @@ namespace fbcpp::impl
684684
isc_arg_end,
685685
};
686686

687-
throw DatabaseException(client, STATUS_INVALID_DATE_VALUE);
687+
throw DatabaseException(*client, STATUS_INVALID_DATE_VALUE);
688688
}
689689

690690
[[noreturn]] void throwInvalidTimeValue()
@@ -694,7 +694,7 @@ namespace fbcpp::impl
694694
isc_arg_end,
695695
};
696696

697-
throw DatabaseException(client, STATUS_INVALID_TIME_VALUE);
697+
throw DatabaseException(*client, STATUS_INVALID_TIME_VALUE);
698698
}
699699

700700
[[noreturn]] void throwInvalidTimestampValue()
@@ -704,15 +704,15 @@ namespace fbcpp::impl
704704
isc_arg_end,
705705
};
706706

707-
throw DatabaseException(client, STATUS_INVALID_TIMESTAMP_VALUE);
707+
throw DatabaseException(*client, STATUS_INVALID_TIMESTAMP_VALUE);
708708
}
709709

710710
private:
711711
static constexpr auto TICKS_PER_DAY = std::int64_t{24} * 60 * 60 * 10000;
712712
static constexpr auto BASE_EPOCH = std::chrono::local_days{
713713
std::chrono::year{1858} / std::chrono::November / 17,
714714
};
715-
Client& client;
715+
Client* client;
716716
StatusWrapper* statusWrapper;
717717
};
718718
} // namespace fbcpp::impl

src/fb-cpp/Exception.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ using namespace fbcpp::impl;
3535
void StatusWrapper::checkException(StatusWrapper* status)
3636
{
3737
if (status->dirty && (status->getState() & fb::IStatus::STATE_ERRORS))
38-
throw DatabaseException{status->client, status->getErrors()};
38+
throw DatabaseException{*status->client, status->getErrors()};
3939
}
4040

4141
void StatusWrapper::catchException(fb::IStatus* status) noexcept

src/fb-cpp/Exception.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace fbcpp::impl
4646
{
4747
public:
4848
explicit StatusWrapper(Client& client, IStatus* status)
49-
: client{client},
49+
: client{&client},
5050
status{status}
5151
{
5252
}
@@ -159,7 +159,7 @@ namespace fbcpp::impl
159159
}
160160

161161
protected:
162-
Client& client;
162+
Client* client;
163163
IStatus* status;
164164
bool dirty = false;
165165

src/fb-cpp/NumericConverter.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ namespace fbcpp::impl
148148
{
149149
public:
150150
explicit NumericConverter(Client& client, StatusWrapper* statusWrapper)
151-
: client{client},
151+
: client{&client},
152152
statusWrapper{statusWrapper}
153153
{
154154
}
@@ -161,7 +161,7 @@ namespace fbcpp::impl
161161
isc_arg_end,
162162
};
163163

164-
throw DatabaseException(client, STATUS_NUMERIC_OUT_OF_RANGE);
164+
throw DatabaseException(*client, STATUS_NUMERIC_OUT_OF_RANGE);
165165
}
166166

167167
[[noreturn]] void throwConversionErrorFromString(const std::string& str)
@@ -172,7 +172,7 @@ namespace fbcpp::impl
172172
isc_arg_end,
173173
};
174174

175-
throw DatabaseException(client, STATUS_CONVERSION_ERROR_FROM_STRING);
175+
throw DatabaseException(*client, STATUS_CONVERSION_ERROR_FROM_STRING);
176176
}
177177

178178
public:
@@ -356,23 +356,23 @@ namespace fbcpp::impl
356356

357357
std::string opaqueInt128ToString(const OpaqueInt128& opaqueInt128, int scale)
358358
{
359-
const auto int128Util = client.getUtil()->getInt128(statusWrapper);
359+
const auto int128Util = client->getUtil()->getInt128(statusWrapper);
360360
char buffer[fb::IInt128::STRING_SIZE + 1];
361361
int128Util->toString(statusWrapper, &opaqueInt128, scale, static_cast<unsigned>(sizeof(buffer)), buffer);
362362
return buffer;
363363
}
364364

365365
std::string opaqueDecFloat16ToString(const OpaqueDecFloat16& opaqueDecFloat16)
366366
{
367-
const auto decFloat16Util = client.getDecFloat16Util(statusWrapper);
367+
const auto decFloat16Util = client->getDecFloat16Util(statusWrapper);
368368
char buffer[fb::IDecFloat16::STRING_SIZE + 1];
369369
decFloat16Util->toString(statusWrapper, &opaqueDecFloat16, static_cast<unsigned>(sizeof(buffer)), buffer);
370370
return buffer;
371371
}
372372

373373
std::string opaqueDecFloat34ToString(const OpaqueDecFloat34& opaqueDecFloat34)
374374
{
375-
const auto decFloat34Util = client.getDecFloat34Util(statusWrapper);
375+
const auto decFloat34Util = client->getDecFloat34Util(statusWrapper);
376376
char buffer[fb::IDecFloat34::STRING_SIZE + 1];
377377
decFloat34Util->toString(statusWrapper, &opaqueDecFloat34, static_cast<unsigned>(sizeof(buffer)), buffer);
378378
return buffer;
@@ -401,7 +401,7 @@ namespace fbcpp::impl
401401

402402
OpaqueDecFloat16 boostDecFloat16ToOpaqueDecFloat16(const BoostDecFloat16& boostDecFloat16)
403403
{
404-
const auto decFloat16Util = client.getDecFloat16Util(statusWrapper);
404+
const auto decFloat16Util = client->getDecFloat16Util(statusWrapper);
405405
OpaqueDecFloat16 opaqueDecFloat16;
406406
decFloat16Util->fromString(statusWrapper, boostDecFloat16.str().c_str(), &opaqueDecFloat16);
407407
return opaqueDecFloat16;
@@ -414,7 +414,7 @@ namespace fbcpp::impl
414414

415415
OpaqueDecFloat34 boostDecFloat34ToOpaqueDecFloat34(const BoostDecFloat34& boostDecFloat34)
416416
{
417-
const auto decFloat34Util = client.getDecFloat34Util(statusWrapper);
417+
const auto decFloat34Util = client->getDecFloat34Util(statusWrapper);
418418
OpaqueDecFloat34 opaqueDecFloat34;
419419
decFloat34Util->fromString(statusWrapper, boostDecFloat34.str().c_str(), &opaqueDecFloat34);
420420
return opaqueDecFloat34;
@@ -573,7 +573,7 @@ namespace fbcpp::impl
573573
}
574574

575575
private:
576-
Client& client;
576+
Client* client;
577577
StatusWrapper* statusWrapper;
578578
};
579579
} // namespace fbcpp::impl

src/fb-cpp/Statement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ using namespace fbcpp::impl;
3333

3434
Statement::Statement(
3535
Attachment& attachment, Transaction& transaction, std::string_view sql, const StatementOptions& options)
36-
: attachment{attachment},
36+
: attachment{&attachment},
3737
status{attachment.getClient().newStatus()},
3838
statusWrapper{attachment.getClient(), status.get()},
3939
calendarConverter{attachment.getClient(), &statusWrapper},

0 commit comments

Comments
 (0)