Skip to content

Commit e2358a8

Browse files
committed
use smart pointers
also fixes AP_NO_DEFAULT_DATA_PACKAGE_STORE code path
1 parent 6866205 commit e2358a8

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

apclient.hpp

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,18 @@ class APClient {
157157
}
158158
}
159159

160-
#ifndef AP_NO_DEFAULT_DATA_PACKAGE_STORE
161160
if (!_dataPackageStore) {
162-
_dataPackageStore = new DefaultDataPackageStore();
163-
_dataPackageStoreAllocated = true;
164-
}
161+
#ifndef AP_NO_DEFAULT_DATA_PACKAGE_STORE
162+
_autoDataPackageStore.reset(new DefaultDataPackageStore());
163+
_dataPackageStore =_autoDataPackageStore.get();
165164
#else
166-
const char* msg = "dataPackageStore is required if compiled with AP_NO_DEFAULT_DATA_PACKAGE_STORE";
167-
fprintf(stderr, "APClient: %s!\n", msg);
168-
#ifdef __cpp_exceptions
169-
throw std::runtime_error(msg);
170-
#endif
165+
const char* msg = "dataPackageStore is required if compiled with AP_NO_DEFAULT_DATA_PACKAGE_STORE";
166+
fprintf(stderr, "APClient: %s!\n", msg);
167+
#ifdef __cpp_exceptions
168+
throw std::runtime_error(msg);
169+
#endif
171170
#endif
171+
}
172172

173173
_uuid = uuid;
174174
_game = game;
@@ -185,12 +185,6 @@ class APClient {
185185

186186
virtual ~APClient()
187187
{
188-
#ifndef AP_NO_DEFAULT_DATA_PACKAGE_STORE
189-
if (_dataPackageStoreAllocated)
190-
delete _dataPackageStore;
191-
#endif
192-
delete _ws;
193-
_ws = nullptr;
194188
}
195189

196190
enum class State {
@@ -1103,11 +1097,10 @@ class APClient {
11031097
*/
11041098
void poll()
11051099
{
1106-
if (_ws && _state == State::DISCONNECTED) {
1107-
delete _ws;
1108-
_ws = nullptr;
1109-
}
1110-
if (_ws) _ws->poll();
1100+
if (_ws && _state == State::DISCONNECTED)
1101+
_ws.reset();
1102+
if (_ws)
1103+
_ws->poll();
11111104
if (_state < State::SOCKET_CONNECTED) {
11121105
auto t = now();
11131106
if (t - _lastSocketConnect > _socketReconnectInterval || _reconnectNow) {
@@ -1134,8 +1127,7 @@ class APClient {
11341127
_hintCostPercent = 0;
11351128
_hintPoints = 0;
11361129
_players.clear();
1137-
delete _ws;
1138-
_ws = nullptr;
1130+
_ws.reset();
11391131
_state = State::DISCONNECTED;
11401132
_hasPassword = false;
11411133
}
@@ -1499,7 +1491,7 @@ class APClient {
14991491
void connect_socket()
15001492
{
15011493
_reconnectNow = false;
1502-
delete _ws;
1494+
_ws.reset();
15031495
if (_uri.empty()) {
15041496
_ws = nullptr;
15051497
_state = State::DISCONNECTED;
@@ -1508,7 +1500,7 @@ class APClient {
15081500
_state = State::SOCKET_CONNECTING;
15091501

15101502
try {
1511-
_ws = new WS(_uri,
1503+
_ws.reset(new WS(_uri,
15121504
[this]() { onopen(); },
15131505
[this]() { onclose(); },
15141506
[this](const std::string& s) { onmessage(s); },
@@ -1520,7 +1512,7 @@ class APClient {
15201512
#if WSWRAP_VERSION >= 10100
15211513
, _certStore
15221514
#endif
1523-
);
1515+
));
15241516
} catch (const std::exception& ex) {
15251517
_ws = nullptr;
15261518
if (_tryWSS && _uri.rfind("ws://", 0) == 0) {
@@ -1599,7 +1591,7 @@ class APClient {
15991591
std::string _game;
16001592
std::string _uuid;
16011593
std::string _certStore;
1602-
WS* _ws = nullptr;
1594+
std::unique_ptr<WS> _ws;
16031595
State _state = State::DISCONNECTED;
16041596
bool _tryWSS = false;
16051597

@@ -1648,7 +1640,7 @@ class APClient {
16481640
std::set<int64_t> _missingLocations;
16491641
APDataPackageStore* _dataPackageStore;
16501642
#ifndef AP_NO_DEFAULT_DATA_PACKAGE_STORE
1651-
bool _dataPackageStoreAllocated = false;
1643+
std::unique_ptr<APDataPackageStore> _autoDataPackageStore;
16521644
#endif
16531645
std::map<int, NetworkSlot> _slotInfo;
16541646

0 commit comments

Comments
 (0)