Skip to content

Commit b37ab73

Browse files
committed
Refactor: Make use of make_unique instead of manual unique ptr initialization
1 parent 463f9d9 commit b37ab73

File tree

8 files changed

+13
-19
lines changed

8 files changed

+13
-19
lines changed

GekkoLib/include/input.h

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ namespace Gekko {
88
struct GameInput {
99
public:
1010
GameInput();
11-
~GameInput();
1211

1312
void Init(GameInput* other);
1413

GekkoLib/include/net.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ namespace Gekko {
124124
static const u64 SYNC_MSG_DELAY = std::chrono::milliseconds(200).count();
125125
static const u64 NET_CHECK_DELAY = std::chrono::milliseconds(1000).count();
126126

127-
Frame last_acked_frame;
128-
u64 last_sent_sync_message;
127+
Frame last_acked_frame = -1;
128+
u64 last_sent_sync_message = 0;
129129
u64 last_received_message = -1;
130130
u64 last_received_frame = 0;
131131

GekkoLib/src/backend.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ void Gekko::MessageSystem::AddPendingInput(bool spectator)
726726
const u32 input_count = (u32)send_list.size();
727727
const u32 total_size = total_input_size * input_count;
728728

729-
std::unique_ptr<u8[]> inputs(new u8[total_size]);
729+
auto inputs = std::make_unique<u8[]>(total_size);
730730

731731
const u32 offset_per_player = total_size / num_players;
732732

GekkoLib/src/gekko.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void Gekko::Session::Init(GekkoConfig* config)
3636
_storage.Init(_config.input_prediction_window, _config.state_size, _config.limited_saving);
3737

3838
// setup disconnected input for disconnected player within the session
39-
_disconnected_input = std::unique_ptr<u8[]>(new u8[_config.input_size]);
39+
_disconnected_input = std::make_unique<u8[]>(_config.input_size);
4040
std::memset(_disconnected_input.get(), 0, _config.input_size);
4141

4242
// we only detect desyncs whenever we are not limited saving for now.

GekkoLib/src/input.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void Gekko::InputBuffer::Init(u8 delay, u8 input_window, u32 input_size)
2929
_incorrent_predicted_input = GameInput::NULL_FRAME;
3030

3131
// init GameInput array
32-
_empty_input = std::unique_ptr<u8[]>(new u8[_input_size]);
32+
_empty_input = std::make_unique<u8[]>(_input_size);
3333
std::memset(_empty_input.get(), 0, _input_size);
3434

3535
for (u32 i = 0; i < BUFF_SIZE; i++) {
@@ -192,7 +192,7 @@ u32 Gekko::InputBuffer::PreviousFrame(Frame frame)
192192

193193
std::unique_ptr<Gekko::GameInput> Gekko::InputBuffer::GetInput(Frame frame, bool prediction)
194194
{
195-
std::unique_ptr<GameInput> inp(new GameInput());
195+
auto inp = std::make_unique<GameInput>();
196196

197197
if (_last_received_input < frame) {
198198
// no input? check if we should predict the input
@@ -264,7 +264,3 @@ Gekko::GameInput::GameInput()
264264
input_len = 0;
265265
input = std::unique_ptr<u8[]>();
266266
}
267-
268-
Gekko::GameInput::~GameInput()
269-
{
270-
}

GekkoLib/src/net.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ u32 Gekko::NetAddress::GetSize()
1212
Gekko::NetAddress::NetAddress(void* data, u32 size)
1313
{
1414
_size = size;
15-
_data = std::unique_ptr<u8[]>(new u8[_size]);
15+
_data = std::make_unique<u8[]>(_size);
1616
// copy address data
1717
std::memcpy(_data.get(), data, _size);
1818
}
@@ -35,7 +35,7 @@ void Gekko::NetAddress::Copy(NetAddress* other)
3535
_data.reset();
3636
}
3737

38-
_data = std::unique_ptr<u8[]>(new u8[_size]);
38+
_data = std::make_unique<u8[]>(_size);
3939
// copy address data
4040
std::memcpy(_data.get(), other->GetAddress(), _size);
4141
}

GekkoLib/src/storage.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void Gekko::StateStorage::Init(u32 num_states, u32 state_size, bool limited)
1313

1414
for (u32 i = 0; i < _max_num_states; i++) {
1515
_states.push_back(std::make_unique<StateEntry>());
16-
_states.back().get()->state = std::unique_ptr<u8[]>(new u8[state_size]);
16+
_states.back().get()->state = std::make_unique<u8[]>(state_size);
1717
_states.back().get()->state_len = state_size;
1818
}
1919
}

GekkoLib/src/sync.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ void Gekko::SyncSystem::Init(u8 num_players, u32 input_size)
1818
_num_players = num_players;
1919
_current_frame = GameInput::NULL_FRAME + 1;
2020

21-
_input_buffers = std::unique_ptr<InputBuffer[]>(new InputBuffer[_num_players]);
22-
21+
_input_buffers = std::make_unique<InputBuffer[]>(num_players);
2322
// on creation setup input buffers
2423
for (int i = 0; i < _num_players; i++) {
2524
_input_buffers[i].Init(0, 0, input_size);
@@ -53,7 +52,7 @@ void Gekko::SyncSystem::IncrementFrame()
5352

5453
bool Gekko::SyncSystem::GetSpectatorInputs(std::unique_ptr<u8[]>& inputs, Frame frame)
5554
{
56-
std::unique_ptr<u8[]> all_input(new u8[_input_size * _num_players]);
55+
auto all_input = std::make_unique<u8[]>(_input_size * _num_players);
5756
for (u8 i = 0; i < _num_players; i++) {
5857
auto inp = _input_buffers[i].GetInput(frame, false);
5958

@@ -69,7 +68,7 @@ bool Gekko::SyncSystem::GetSpectatorInputs(std::unique_ptr<u8[]>& inputs, Frame
6968

7069
bool Gekko::SyncSystem::GetCurrentInputs(std::unique_ptr<u8[]>& inputs, Frame& frame)
7170
{
72-
std::unique_ptr<u8[]> all_input(new u8[_input_size * _num_players]);
71+
auto all_input = std::make_unique<u8[]>(_input_size * _num_players);
7372
for (u8 i = 0; i < _num_players; i++) {
7473
auto inp = _input_buffers[i].GetInput(_current_frame, true);
7574

@@ -87,7 +86,7 @@ bool Gekko::SyncSystem::GetCurrentInputs(std::unique_ptr<u8[]>& inputs, Frame& f
8786
bool Gekko::SyncSystem::GetLocalInputs(std::vector<Handle>& handles, std::unique_ptr<u8[]>& inputs, Frame frame)
8887
{
8988
inputs.reset();
90-
std::unique_ptr<u8[]> all_input(new u8[_input_size * handles.size()]);
89+
auto all_input = std::make_unique<u8[]>(_input_size * handles.size());
9190
for (u8 i = 0; i < handles.size(); i++) {
9291
auto inp = _input_buffers[handles[i]].GetInput(frame, true);
9392

0 commit comments

Comments
 (0)