Skip to content

Commit 933de75

Browse files
author
Karl Herbig
committed
make pointer to Private Device implementation a unique_ptr
1 parent 61b6e07 commit 933de75

File tree

19 files changed

+53
-69
lines changed

19 files changed

+53
-69
lines changed

include/modbuspp/device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,8 @@ namespace Modbus {
555555

556556
protected:
557557
class Private;
558-
Device (Private &dd);
559-
std::unique_ptr<Private> d_ptr;
558+
Device (std::unique_ptr<Device::Private> &&dd);
559+
std::unique_ptr<Device::Private> d_ptr;
560560

561561
private:
562562
PIMP_DECLARE_PRIVATE (Device)

include/modbuspp/master.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ namespace Modbus {
259259

260260
protected:
261261
class Private;
262-
Master (Private &dd);
262+
Master (std::unique_ptr<Private> &&dd);
263263

264264
private:
265265
PIMP_DECLARE_PRIVATE (Master)

include/modbuspp/router.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ namespace Modbus {
276276

277277
protected:
278278
class Private;
279-
Router (Private &dd);
279+
Router (std::unique_ptr<Private> &&dd);
280280

281281
private:
282282
PIMP_DECLARE_PRIVATE (Router)

include/modbuspp/server.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ namespace Modbus {
170170
/**
171171
* @overload
172172
*/
173-
virtual void close();
173+
virtual void close() override;
174174

175175
/**
176176
* @brief Performs all server operations
@@ -307,7 +307,7 @@ namespace Modbus {
307307

308308
protected:
309309
class Private;
310-
Server (Private &dd);
310+
Server (std::unique_ptr<Private> &&dd);
311311

312312
private:
313313
PIMP_DECLARE_PRIVATE (Server)

src/device.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace Modbus {
3939
// ---------------------------------------------------------------------------
4040

4141
// ---------------------------------------------------------------------------
42-
Device::Device (Device::Private &dd) : d_ptr (&dd) {}
42+
Device::Device (std::unique_ptr<Device::Private> &&dd) : d_ptr (std::move(dd)) {}
4343

4444
// ---------------------------------------------------------------------------
4545
Device::Device () : d_ptr (new Private (this)) {}
@@ -198,7 +198,7 @@ namespace Modbus {
198198
if (net() == Rtu) {
199199
PIMP_D (Device);
200200

201-
return * reinterpret_cast<RtuLayer *> (d->backend);
201+
return * reinterpret_cast<RtuLayer *> (d->backend.get());
202202
}
203203
throw std::domain_error ("Unable to return RTU layer !");
204204
}
@@ -209,7 +209,7 @@ namespace Modbus {
209209
if (net() == Ascii) {
210210
PIMP_D (Device);
211211

212-
return * reinterpret_cast<AsciiLayer *> (d->backend);
212+
return * reinterpret_cast<AsciiLayer *> (d->backend.get());
213213
}
214214
throw std::domain_error ("Unable to return ASCII layer !");
215215
}
@@ -220,7 +220,7 @@ namespace Modbus {
220220
if (net() == Tcp) {
221221
PIMP_D (Device);
222222

223-
return * reinterpret_cast<TcpLayer *> (d->backend);
223+
return * reinterpret_cast<TcpLayer *> (d->backend.get());
224224
}
225225
throw std::domain_error ("Unable to return TCP layer !");
226226
}
@@ -419,7 +419,7 @@ namespace Modbus {
419419
}
420420
while (d->recoveryLink && rc == -1 && !msg->isResponse());
421421

422-
if (rc > 0 && rc != msg->size()) {
422+
if (rc > 0 && static_cast<size_t>(rc) != msg->aduSize()) {
423423

424424
errno = EMBBADDATA;
425425
return -1;
@@ -451,15 +451,9 @@ namespace Modbus {
451451

452452
// ---------------------------------------------------------------------------
453453
Device::Private::Private (Device * q) :
454-
q_ptr (q), isOpen (false), backend (0), recoveryLink (false),
454+
q_ptr (q), isOpen (false), backend (nullptr), recoveryLink (false),
455455
debug (false) {}
456456

457-
// ---------------------------------------------------------------------------
458-
Device::Private::~Private() {
459-
460-
delete backend;
461-
}
462-
463457
// ---------------------------------------------------------------------------
464458
void Device::Private::setConfigFromFile (const std::string & jsonfile,
465459
const std::string & key) {
@@ -513,15 +507,15 @@ namespace Modbus {
513507
switch (net) {
514508

515509
case Tcp:
516-
backend = new TcpLayer (connection, settings);
510+
backend = std::unique_ptr<TcpLayer>{new TcpLayer (connection, settings)};
517511
break;
518512

519513
case Rtu:
520-
backend = new RtuLayer (connection, settings);
514+
backend = std::unique_ptr<RtuLayer>{new RtuLayer (connection, settings)};
521515
break;
522516

523517
case Ascii:
524-
backend = new AsciiLayer (connection, settings);
518+
backend = std::unique_ptr<AsciiLayer>{new AsciiLayer (connection, settings)};
525519
break;
526520

527521
default:
@@ -555,7 +549,7 @@ namespace Modbus {
555549
// ---------------------------------------------------------------------------
556550
int Device::Private::defaultSlave (int addr) const {
557551

558-
if (addr < 0 && backend != 0) {
552+
if (addr < 0 && backend != nullptr) {
559553
Net n = backend->net();
560554
return (n == Rtu || n == Ascii) ? Broadcast : TcpSlave;
561555
}

src/device_p.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ namespace Modbus {
2828

2929
public:
3030
Private (Device * q);
31-
virtual ~Private();
3231
virtual void setBackend (Net net, const std::string & connection,
3332
const std::string & settings);
3433
virtual void setConfig (const nlohmann::json & config);
@@ -39,18 +38,26 @@ namespace Modbus {
3938
virtual bool open();
4039
virtual void close();
4140
inline modbus_t * ctx() {
42-
return backend->context();
41+
if ( backend != nullptr ) {
42+
return backend->context();
43+
} else {
44+
return nullptr;
45+
}
4346
}
4447
inline modbus_t * ctx() const {
45-
return backend->context();
48+
if ( backend != nullptr ) {
49+
return backend->context();
50+
} else {
51+
return nullptr;
52+
}
4653
}
4754
int defaultSlave (int addr) const;
4855
bool isConnected () const;
4956
void printError (const char * what = nullptr) const;
5057

51-
Device * const q_ptr;
58+
Device * const q_ptr = nullptr;
5259
bool isOpen;
53-
NetLayer * backend;
60+
std::unique_ptr<NetLayer> backend = nullptr;
5461
bool recoveryLink;
5562
bool debug;
5663

src/master.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ namespace Modbus {
2828
// ---------------------------------------------------------------------------
2929

3030
// ---------------------------------------------------------------------------
31-
Master::Master (Master::Private &dd) : Device (dd) {}
31+
Master::Master (std::unique_ptr<Master::Private> &&dd) : Device (std::move(dd)) {}
3232

3333
// ---------------------------------------------------------------------------
34-
Master::Master () : Device (*new Private (this)) {}
34+
Master::Master () : Device (std::unique_ptr<Private>(new Private (this))) {}
3535

3636
// ---------------------------------------------------------------------------
3737
Master::Master (Net net, const std::string & connection,
@@ -185,9 +185,6 @@ namespace Modbus {
185185
// ---------------------------------------------------------------------------
186186
Master::Private::Private (Master * q) : Device::Private (q) {}
187187

188-
// ---------------------------------------------------------------------------
189-
Master::Private::~Private() = default;
190-
191188
// ---------------------------------------------------------------------------
192189
// virtual
193190
void Master::Private::setBackend (Net net, const std::string & connection,

src/master_p.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ namespace Modbus {
2626

2727
public:
2828
Private (Master * q);
29-
virtual ~Private();
30-
virtual void setBackend (Net net, const std::string & connection,
31-
const std::string & settings);
32-
virtual void setConfig (const nlohmann::json & config);
3329

34-
virtual Slave * addSlave (int slaveAddr);
30+
void setBackend (Net net, const std::string & connection,
31+
const std::string & settings) override;
32+
void setConfig (const nlohmann::json & config) override;
33+
34+
Slave * addSlave (int slaveAddr);
3535

3636
std::map <int, std::shared_ptr<Slave>> slave;
3737
PIMP_DECLARE_PUBLIC (Master)
3838
};
3939
}
4040

4141
/* ========================================================================== */
42+

src/message.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -512,19 +512,22 @@ namespace Modbus {
512512

513513
// ---------------------------------------------------------------------------
514514
Message::Private::Private (Message * q, Net n) :
515-
q_ptr (q), net (n), aduSize (0), isResponse (false), backend (0),
515+
q_ptr (q), net (n), aduSize (0), isResponse (false), backend (nullptr),
516516
transactionId (1) {
517-
NetLayer * b;
517+
std::unique_ptr<NetLayer> b = nullptr;
518518

519519
switch (net) {
520520

521521
case Tcp:
522-
b = new TcpLayer ("*", "1502");
522+
b = std::unique_ptr<TcpLayer> (new TcpLayer{"*", "1502"} );
523523
break;
524524

525525
case Rtu:
526+
b = std::unique_ptr<RtuLayer> (new RtuLayer{"*", "1502"} );
527+
break;
528+
526529
case Ascii:
527-
b = new RtuLayer ("COM1", "9600E1");
530+
b = std::unique_ptr<AsciiLayer> (new AsciiLayer{"*", "1502"} );
528531
break;
529532

530533
default:
@@ -535,7 +538,6 @@ namespace Modbus {
535538

536539
pduBegin = modbus_get_header_length (b->context());
537540
maxAduLength = b->maxAduLength();
538-
delete b;
539541
adu.resize (maxAduLength, 0);
540542
if (net == Tcp) {
541543
adu[6] = MODBUS_TCP_SLAVE;
@@ -556,9 +558,6 @@ namespace Modbus {
556558

557559
adu[pduBegin] = static_cast<uint8_t> (func);
558560
}
559-
560-
// ---------------------------------------------------------------------------
561-
Message::Private::~Private() = default;
562561
}
563562

564563
/* ========================================================================== */

src/message_p.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ namespace Modbus {
2828
Private (Message * q, Net n);
2929
Private (Message * q, Net n, const std::vector<uint8_t> & m);
3030
Private (Message * q, Net n, Function f);
31-
virtual ~Private();
31+
virtual ~Private() = default;
3232

33-
Message * const q_ptr;
33+
Message * const q_ptr = nullptr;
3434
Net net;
3535
int pduBegin;
3636
size_t aduSize;
3737
uint16_t maxAduLength;
3838
bool isResponse;
39-
NetLayer * backend;
39+
NetLayer * backend = nullptr;
4040
uint16_t transactionId;
4141
std::vector<uint8_t> adu;
4242
};

0 commit comments

Comments
 (0)