Skip to content

Commit 9ba345f

Browse files
slayoozdaq12
andauthored
introducing GimmickGuard (to avoid manual memory freeing) (#263)
Co-authored-by: Zach D'Aquino <[email protected]>
1 parent 82e320c commit 9ba345f

File tree

9 files changed

+44
-19
lines changed

9 files changed

+44
-19
lines changed

src/aero_data.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ struct AeroData {
3434
{
3535
if (!InputGimmick::unique_keys(json))
3636
throw std::runtime_error("Species names must be unique");
37-
gimmick_ptr() = std::make_unique<InputGimmick>(json);
37+
38+
GimmickGuard<InputGimmick> guard(json);
3839
f_aero_data_from_json(this->ptr.f_arg());
39-
gimmick_ptr().reset(); // TODO #117: guard
4040
}
4141

4242
static auto spec_by_name(const AeroData &self, const std::string &name) {

src/aero_dist.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ struct AeroDist {
5151
{
5252
if (!InputGimmick::unique_keys(json))
5353
throw std::runtime_error("Mode names must be unique");
54-
gimmick_ptr() = std::make_unique<InputGimmick>(json, "", "mode_name", 1);
54+
55+
GimmickGuard<InputGimmick> guard(json, "", "mode_name", 1);
5556
f_aero_dist_from_json(ptr.f_arg_non_const(), aero_data->ptr.f_arg_non_const());
56-
gimmick_ptr().reset();
5757
}
5858

5959
AeroDist() :

src/aero_mode.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ struct AeroMode {
130130
{
131131
if (json.size() != 1)
132132
throw std::runtime_error("Single element expected");
133-
gimmick_ptr() = std::make_unique<InputGimmick>(json, "", "mode_name");
133+
134+
GimmickGuard<InputGimmick> guard(json, "", "mode_name");
134135
f_aero_mode_from_json(ptr.f_arg_non_const(), aero_data.ptr.f_arg_non_const());
135-
gimmick_ptr().reset();
136136
}
137137

138138
static auto get_num_conc(const AeroMode &self){

src/env_state.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ struct EnvState {
2929
EnvState(const nlohmann::json &json) :
3030
ptr(f_env_state_ctor, f_env_state_dtor)
3131
{
32-
gimmick_ptr() = std::make_unique<InputGimmick>(json); // TODO #117: guard
32+
GimmickGuard<InputGimmick> guard(json);
3333
f_env_state_from_json(this->ptr.f_arg());
34-
gimmick_ptr().reset();
3534
}
3635

3736
static void set_temperature(const EnvState &self, double &temperature) {

src/gas_data.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ struct GasData {
3434
nlohmann::json::array()
3535
}}));
3636

37-
gimmick_ptr() = std::make_unique<InputGimmick>(json_array);
37+
GimmickGuard<InputGimmick> guard(json_array);
3838
f_gas_data_from_json(this->ptr.f_arg());
39-
gimmick_ptr().reset(); // TODO #117: guard
4039
}
4140

4241
static auto __str__(const GasData &self) {

src/gas_state.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,11 @@ struct GasState {
5959
}
6060

6161
static auto __str__(const GasState &self) {
62-
gimmick_ptr() = std::make_unique<OutputGimmick>(); // TODO #117: guard
62+
GimmickGuard<OutputGimmick> guard;
6363

6464
f_gas_state_to_json(self.ptr.f_arg());
6565
auto str = gimmick_ptr()->str();
6666

67-
gimmick_ptr().reset(); // TODO #117: guard
6867
return str;
6968
}
7069

@@ -118,10 +117,8 @@ struct GasState {
118117
}
119118

120119
static void set_mix_rats(const GasState &self, const nlohmann::json &json) {
121-
122-
gimmick_ptr() = std::make_unique<InputGimmick>(json);
120+
GimmickGuard<InputGimmick> guard(json);
123121
f_gas_state_from_json(self.ptr.f_arg(),
124122
self.gas_data->ptr.f_arg());
125-
gimmick_ptr().reset(); // TODO #117: guard
126123
}
127124
};

src/gimmicks.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,35 @@ struct OutputGimmick: Gimmick {
308308

309309
std::unique_ptr<Gimmick> &gimmick_ptr();
310310

311+
template <typename T>
312+
struct GimmickGuard {
313+
GimmickGuard() {
314+
gimmick_ptr() = std::make_unique<T>();
315+
}
316+
317+
GimmickGuard(const nlohmann::json & json) {
318+
gimmick_ptr() = std::make_unique<T>(json);
319+
}
320+
321+
GimmickGuard(
322+
const nlohmann::json & json,
323+
const std::string key_cond,
324+
const std::string key_name
325+
) {
326+
gimmick_ptr() = std::make_unique<T>(json, key_cond, key_name);
327+
}
328+
329+
GimmickGuard(
330+
const nlohmann::json & json,
331+
const std::string key_cond,
332+
const std::string key_name,
333+
const int max_zoom_level
334+
) {
335+
gimmick_ptr() = std::make_unique<T>(json, key_cond, key_name, max_zoom_level);
336+
}
337+
338+
~GimmickGuard() {
339+
gimmick_ptr().reset();
340+
}
341+
};
342+

src/run_part_opt.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ struct RunPartOpt {
4646
if (json_copy.find(key) == json_copy.end())
4747
json_copy[key] = 0;
4848

49-
gimmick_ptr() = std::make_unique<InputGimmick>(json_copy); // TODO #117: guard
49+
GimmickGuard<InputGimmick> guard(json_copy);
5050
f_run_part_opt_from_json(this->ptr.f_arg());
51-
gimmick_ptr().reset();
5251
}
5352

5453
static auto t_max(const RunPartOpt &self){

src/scenario.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,12 @@ struct Scenario {
9595
ptr(f_scenario_ctor, f_scenario_dtor),
9696
json(json)
9797
{
98-
gimmick_ptr() = std::make_unique<InputGimmick>(json, "dist", "mode_name"); // TODO #117: guard
98+
GimmickGuard<InputGimmick> guard(json, "dist", "mode_name");
9999
f_scenario_from_json(
100100
gas_data.ptr.f_arg(),
101101
aero_data.ptr.f_arg(),
102102
this->ptr.f_arg()
103103
);
104-
gimmick_ptr().reset();
105104
}
106105

107106
static auto __str__(const Scenario &self) {

0 commit comments

Comments
 (0)