Skip to content

Commit 29f9fd0

Browse files
committed
Switched from using std::optional to std::shared_ptr for easier state handling
1 parent 03de526 commit 29f9fd0

File tree

4 files changed

+64
-66
lines changed

4 files changed

+64
-66
lines changed

source/async_postgres.hpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <chrono>
99
#include <exception>
1010
#include <memory>
11-
#include <optional>
1211
#include <queue>
1312
#include <string_view>
1413
#include <variant>
@@ -99,6 +98,8 @@ namespace async_postgres {
9998
CreatePreparedCommand, PreparedCommand,
10099
DescribePreparedCommand, DescribePortalCommand>;
101100

101+
Query(CommandVariant&& command) : command(std::move(command)) {}
102+
102103
CommandVariant command;
103104
GLua::AutoReference callback;
104105
bool sent = false;
@@ -112,8 +113,8 @@ namespace async_postgres {
112113

113114
struct Connection {
114115
pg::conn conn;
115-
std::optional<Query> query;
116-
std::optional<ResetEvent> reset_event;
116+
std::shared_ptr<Query> query;
117+
std::shared_ptr<ResetEvent> reset_event;
117118
GLua::AutoReference on_notify;
118119

119120
Connection(GLua::ILuaInterface* lua, pg::conn&& conn);

source/connection.cpp

+7-9
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void async_postgres::reset(GLua::ILuaInterface* lua, Connection* state,
105105
throw std::runtime_error(PQerrorMessage(state->conn.get()));
106106
}
107107

108-
state->reset_event = ResetEvent();
108+
state->reset_event = std::make_shared<ResetEvent>();
109109
}
110110

111111
if (callback) {
@@ -119,26 +119,24 @@ void async_postgres::process_reset(GLua::ILuaInterface* lua,
119119
return;
120120
}
121121

122-
auto& event = state->reset_event.value();
122+
auto event = state->reset_event;
123123
if (!socket_is_ready(state->conn.get(), state->reset_event->status)) {
124124
return;
125125
}
126126

127-
event.status = PQresetPoll(state->conn.get());
128-
if (event.status == PGRES_POLLING_OK) {
129-
auto callbacks = std::move(event.callbacks);
127+
event->status = PQresetPoll(state->conn.get());
128+
if (event->status == PGRES_POLLING_OK) {
130129
state->reset_event.reset();
131130

132-
for (auto& callback : callbacks) {
131+
for (auto& callback : event->callbacks) {
133132
callback.Push();
134133
lua->PushBool(true);
135134
pcall(lua, 1, 0);
136135
}
137-
} else if (event.status == PGRES_POLLING_FAILED) {
138-
auto callbacks = std::move(event.callbacks);
136+
} else if (event->status == PGRES_POLLING_FAILED) {
139137
state->reset_event.reset();
140138

141-
for (auto& callback : callbacks) {
139+
for (auto& callback : event->callbacks) {
142140
callback.Push();
143141
lua->PushBool(false);
144142
lua->PushString(PQerrorMessage(state->conn.get()));

source/main.cpp

+40-40
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@ namespace async_postgres::lua {
5050
throw std::runtime_error("query already in progress");
5151
}
5252

53-
async_postgres::SimpleCommand command = {lua->GetString(2)};
54-
async_postgres::Query query = {std::move(command)};
53+
state->query = std::make_shared<async_postgres::Query>(
54+
async_postgres::SimpleCommand{
55+
lua->GetString(2),
56+
});
5557

5658
if (lua->IsType(3, GLua::Type::Function)) {
57-
query.callback = GLua::AutoReference(lua, 3);
59+
state->query->callback = GLua::AutoReference(lua, 3);
5860
}
5961

60-
state->query = std::move(query);
6162
return 0;
6263
}
6364

@@ -71,17 +72,16 @@ namespace async_postgres::lua {
7172
throw std::runtime_error("query already in progress");
7273
}
7374

74-
async_postgres::ParameterizedCommand command = {
75-
lua->GetString(2),
76-
async_postgres::array_to_params(lua, 3),
77-
};
78-
async_postgres::Query query = {std::move(command)};
75+
state->query = std::make_shared<async_postgres::Query>(
76+
async_postgres::ParameterizedCommand{
77+
lua->GetString(2),
78+
async_postgres::array_to_params(lua, 3),
79+
});
7980

8081
if (lua->IsType(4, GLua::Type::Function)) {
81-
query.callback = GLua::AutoReference(lua, 4);
82+
state->query->callback = GLua::AutoReference(lua, 4);
8283
}
8384

84-
state->query = std::move(query);
8585
return 0;
8686
}
8787

@@ -95,15 +95,16 @@ namespace async_postgres::lua {
9595
throw std::runtime_error("query already in progress");
9696
}
9797

98-
async_postgres::CreatePreparedCommand command = {lua->GetString(2),
99-
lua->GetString(3)};
100-
async_postgres::Query query = {std::move(command)};
98+
state->query = std::make_shared<async_postgres::Query>(
99+
async_postgres::CreatePreparedCommand{
100+
lua->GetString(2),
101+
lua->GetString(3),
102+
});
101103

102104
if (lua->IsType(4, GLua::Type::Function)) {
103-
query.callback = GLua::AutoReference(lua, 4);
105+
state->query->callback = GLua::AutoReference(lua, 4);
104106
}
105107

106-
state->query = std::move(query);
107108
return 0;
108109
}
109110

@@ -117,17 +118,16 @@ namespace async_postgres::lua {
117118
throw std::runtime_error("query already in progress");
118119
}
119120

120-
async_postgres::PreparedCommand command = {
121-
lua->GetString(2),
122-
async_postgres::array_to_params(lua, 3),
123-
};
124-
async_postgres::Query query = {std::move(command)};
121+
state->query = std::make_shared<async_postgres::Query>(
122+
async_postgres::PreparedCommand{
123+
lua->GetString(2),
124+
async_postgres::array_to_params(lua, 3),
125+
});
125126

126127
if (lua->IsType(4, GLua::Type::Function)) {
127-
query.callback = GLua::AutoReference(lua, 4);
128+
state->query->callback = GLua::AutoReference(lua, 4);
128129
}
129130

130-
state->query = std::move(query);
131131
return 0;
132132
}
133133

@@ -140,14 +140,15 @@ namespace async_postgres::lua {
140140
throw std::runtime_error("query already in progress");
141141
}
142142

143-
async_postgres::DescribePreparedCommand command = {lua->GetString(2)};
144-
async_postgres::Query query = {std::move(command)};
143+
state->query = std::make_shared<async_postgres::Query>(
144+
async_postgres::DescribePreparedCommand{
145+
lua->GetString(2),
146+
});
145147

146148
if (lua->IsType(3, GLua::Type::Function)) {
147-
query.callback = GLua::AutoReference(lua, 3);
149+
state->query->callback = GLua::AutoReference(lua, 3);
148150
}
149151

150-
state->query = std::move(query);
151152
return 0;
152153
}
153154

@@ -160,14 +161,15 @@ namespace async_postgres::lua {
160161
throw std::runtime_error("query already in progress");
161162
}
162163

163-
async_postgres::DescribePortalCommand command = {lua->GetString(2)};
164-
async_postgres::Query query = {std::move(command)};
164+
state->query = std::make_shared<async_postgres::Query>(
165+
async_postgres::DescribePortalCommand{
166+
lua->GetString(2),
167+
});
165168

166169
if (lua->IsType(3, GLua::Type::Function)) {
167-
query.callback = GLua::AutoReference(lua, 3);
170+
state->query->callback = GLua::AutoReference(lua, 3);
168171
}
169172

170-
state->query = std::move(query);
171173
return 0;
172174
}
173175

@@ -201,9 +203,8 @@ namespace async_postgres::lua {
201203

202204
auto state = lua_connection_state();
203205
if (state->reset_event) {
204-
auto& event = state->reset_event.value();
205-
while (state->reset_event.has_value() &&
206-
&event == &state->reset_event.value()) {
206+
auto event = state->reset_event;
207+
while (event == state->reset_event) {
207208
bool write =
208209
state->reset_event->status == PGRES_POLLING_WRITING;
209210
bool read = state->reset_event->status == PGRES_POLLING_READING;
@@ -220,16 +221,15 @@ namespace async_postgres::lua {
220221
}
221222

222223
if (state->query) {
223-
auto& query = state->query.value();
224+
auto query = state->query;
224225

225226
// if query wasn't sent, send in through process_query
226-
if (!query.sent) {
227+
if (!query->sent) {
227228
async_postgres::process_query(lua, state);
228229
}
229230

230231
// while query is the same and it's not done
231-
while (state->query.has_value() &&
232-
&query == &state->query.value()) {
232+
while (query == state->query) {
233233
async_postgres::process_result(lua, state,
234234
pg::getResult(state->conn));
235235
}
@@ -253,14 +253,14 @@ namespace async_postgres::lua {
253253
lua_protected_fn(querying) {
254254
lua->CheckType(1, async_postgres::connection_meta);
255255
auto state = lua_connection_state();
256-
lua->PushBool(state->query.has_value());
256+
lua->PushBool(!!state->query);
257257
return 1;
258258
}
259259

260260
lua_protected_fn(resetting) {
261261
lua->CheckType(1, async_postgres::connection_meta);
262262
auto state = lua_connection_state();
263-
lua->PushBool(state->reset_event.has_value());
263+
lua->PushBool(!!state->reset_event);
264264
return 1;
265265
}
266266
} // namespace async_postgres::lua

source/query.cpp

+13-14
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
using namespace async_postgres;
44

55
#define get_if_command(type) \
6-
const auto* command = std::get_if<type>(&query.command)
6+
const auto* command = std::get_if<type>(&query->command)
77

88
// returns true if query was sent
99
// returns false on error
10-
inline bool send_query(PGconn* conn, Query& query) {
10+
inline bool send_query(PGconn* conn, Query* query) {
1111
if (get_if_command(SimpleCommand)) {
1212
return PQsendQuery(conn, command->command.c_str()) == 1;
1313
} else if (get_if_command(ParameterizedCommand)) {
@@ -39,11 +39,10 @@ void query_failed(GLua::ILuaInterface* lua, Connection* state) {
3939
return;
4040
}
4141

42-
auto query = std::move(*state->query);
42+
auto query = state->query;
4343
state->query.reset();
4444

45-
if (query.callback) {
46-
query.callback.Push();
45+
if (query->callback.Push()) {
4746
lua->PushBool(false);
4847
lua->PushString(PQerrorMessage(state->conn.get()));
4948
pcall(lua, 2, 0);
@@ -112,15 +111,15 @@ void query_result(GLua::ILuaInterface* lua, pg::result&& result,
112111

113112
// returns true if poll was successful
114113
// returns false if there was an error
115-
inline bool poll_query(PGconn* conn, Query& query) {
114+
inline bool poll_query(PGconn* conn, Query* query) {
116115
auto socket = check_socket_status(conn);
117116
if (socket.read_ready || socket.write_ready) {
118117
if (socket.read_ready && PQconsumeInput(conn) == 0) {
119118
return false;
120119
}
121120

122-
if (!query.flushed) {
123-
query.flushed = PQflush(conn) == 0;
121+
if (!query->flushed) {
122+
query->flushed = PQflush(conn) == 0;
124123
}
125124
}
126125
return true;
@@ -142,10 +141,10 @@ void async_postgres::process_result(GLua::ILuaInterface* lua, Connection* state,
142141
auto next_result = pg::getResult(state->conn);
143142
if (!next_result) {
144143
// query is done, we need to remove query from the state
145-
Query query = std::move(*state->query);
144+
auto query = state->query;
146145
state->query.reset();
147146

148-
query_result(lua, std::move(result), query.callback);
147+
query_result(lua, std::move(result), query->callback);
149148

150149
// callback might added another query, process it rightaway
151150
process_query(lua, state);
@@ -169,15 +168,15 @@ void async_postgres::process_query(GLua::ILuaInterface* lua,
169168
return;
170169
}
171170

172-
auto& query = state->query.value();
173-
if (!query.sent) {
171+
auto* query = state->query.get();
172+
if (!query->sent) {
174173
if (!send_query(state->conn.get(), query)) {
175174
query_failed(lua, state);
176175
return process_query(lua, state);
177176
}
178177

179-
query.sent = true;
180-
query.flushed = PQflush(state->conn.get()) == 0;
178+
query->sent = true;
179+
query->flushed = PQflush(state->conn.get()) == 0;
181180
}
182181

183182
// if (!poll_query(state->conn.get(), query)) {

0 commit comments

Comments
 (0)