Skip to content

Commit 4ed42c9

Browse files
committed
Replace all new and delete in FGDistributor by unique_ptr. Also constified some methods of FGDistributor and FGCondition.
1 parent 829f040 commit 4ed42c9

File tree

4 files changed

+37
-48
lines changed

4 files changed

+37
-48
lines changed

src/math/FGCondition.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ FGCondition::FGCondition(const string& test, std::shared_ptr<FGPropertyManager>
157157

158158
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159159

160-
bool FGCondition::Evaluate(void )
160+
bool FGCondition::Evaluate(void) const
161161
{
162162
bool pass = false;
163163

@@ -213,7 +213,7 @@ bool FGCondition::Evaluate(void )
213213

214214
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215215

216-
void FGCondition::PrintCondition(string indent)
216+
void FGCondition::PrintCondition(string indent) const
217217
{
218218
string scratch;
219219

src/math/FGCondition.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ class JSBSIM_API FGCondition : public FGJSBBase
6868
FGCondition(const std::string& test, std::shared_ptr<FGPropertyManager> PropertyManager,
6969
Element* el);
7070

71-
bool Evaluate(void);
72-
void PrintCondition(std::string indent=" ");
71+
bool Evaluate(void) const;
72+
void PrintCondition(std::string indent=" ") const;
7373

7474
private:
7575

src/models/flight_control/FGDistributor.cpp

+12-20
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ FGDistributor::FGDistributor(FGFCS* fcs, Element* element)
7171
auto current_case = make_unique<Case>();
7272
Element* test_element = case_element->FindElement("test");
7373
try {
74-
if (test_element) current_case->SetTest(new FGCondition(test_element, PropertyManager));
74+
if (test_element) current_case->SetTest(test_element, PropertyManager);
7575
} catch (BaseException& e) {
7676
FGXMLLogging log(fcs->GetExec()->GetLogger(), test_element, LogLevel::FATAL);
7777
log << LogFormat::RED << e.what() << LogFormat::RESET << "\n\n";
@@ -81,11 +81,11 @@ FGDistributor::FGDistributor(FGFCS* fcs, Element* element)
8181
while (prop_val_element) {
8282
string value_string = prop_val_element->GetAttributeValue("value");
8383
string property_string = prop_val_element->GetDataLine();
84-
current_case->AddPropValPair(new PropValPair(property_string, value_string,
85-
PropertyManager, prop_val_element));
84+
current_case->AddPropValPair(property_string, value_string, PropertyManager,
85+
prop_val_element);
8686
prop_val_element = case_element->FindNextElement("property");
8787
}
88-
Cases.push_back(current_case.release());
88+
Cases.push_back(std::move(current_case));
8989
case_element = element->FindNextElement("case");
9090
}
9191

@@ -94,18 +94,10 @@ FGDistributor::FGDistributor(FGFCS* fcs, Element* element)
9494

9595
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9696

97-
FGDistributor::~FGDistributor()
98-
{
99-
for (auto Case: Cases) delete Case;
100-
Debug(1);
101-
}
102-
103-
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104-
10597
bool FGDistributor::Run(void )
10698
{
10799
bool completed = false;
108-
for (auto Case: Cases) { // Loop through all Cases
100+
for (auto& Case: Cases) { // Loop through all Cases
109101
if (Case->HasTest()) {
110102
if (Case->GetTestResult() && !((Type == eExclusive) && completed)) {
111103
Case->SetPropValPairs();
@@ -146,19 +138,19 @@ void FGDistributor::Debug(int from)
146138
if (from == 0) { // Constructor
147139
FGLogging log(fcs->GetExec()->GetLogger(), LogLevel::DEBUG);
148140
unsigned int ctr=0;
149-
for (auto Case: Cases) {
141+
for (const auto& Case: Cases) {
150142
log << " Case: " << fixed << ctr << "\n";
151143
if (Case->HasTest()) {
152-
Case->GetTest()->PrintCondition(" ");
144+
Case->GetTest().PrintCondition(" ");
153145
} else {
154146
log << " Set these properties by default: \n";
155147
}
156148
log << "\n";
157-
for (auto propVal = Case->IterPropValPairs(); propVal != Case->EndPropValPairs(); ++propVal) {
158-
log << " Set property " << (*propVal)->GetPropName();
159-
if ((*propVal)->GetLateBoundProp()) log << " (late bound)";
160-
log << " to " << (*propVal)->GetValString();
161-
if ((*propVal)->GetLateBoundValue()) log << " (late bound)";
149+
for (const auto& propVal: *Case) {
150+
log << " Set property " << propVal->GetPropName();
151+
if (propVal->GetLateBoundProp()) log << " (late bound)";
152+
log << " to " << propVal->GetValString();
153+
if (propVal->GetLateBoundValue()) log << " (late bound)";
162154
log << "\n";
163155
}
164156
ctr++;

src/models/flight_control/FGDistributor.h

+21-24
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,14 @@ class FGDistributor : public FGFCSComponent
131131
that represents this distributor component */
132132
FGDistributor(FGFCS* fcs, Element* element);
133133

134-
/// Destructor
135-
~FGDistributor();
136-
137134
/** Executes the distributor logic.
138135
@return true - always*/
139136
bool Run(void) override;
140137

141138
private:
142139

143140
enum eType {eInclusive=0, eExclusive} Type;
141+
template<typename T> using vector_of_unique_ptr = std::vector<std::unique_ptr<T>>;
144142

145143
class PropValPair {
146144
public:
@@ -154,15 +152,14 @@ class FGDistributor : public FGFCSComponent
154152
Prop->SetValue(Val->GetValue());
155153
}
156154
catch(...) {
157-
throw(Prop->GetName()+" in distributor component is not known");
155+
throw BaseException(Prop->GetName()+" in distributor component is not known");
158156
}
159157
}
160158

161-
std::string GetPropName() { return Prop->GetName(); }
162-
std::string GetValString() { return Val->GetName(); }
163-
bool GetLateBoundProp() { return Prop->IsLateBound(); }
164-
bool GetLateBoundValue() {return Val->IsLateBound();
165-
}
159+
std::string GetPropName() const { return Prop->GetName(); }
160+
std::string GetValString() const { return Val->GetName(); }
161+
bool GetLateBoundProp() const { return Prop->IsLateBound(); }
162+
bool GetLateBoundValue() const { return Val->IsLateBound(); }
166163
private:
167164
FGPropertyValue_ptr Prop;
168165
FGParameterValue_ptr Val;
@@ -172,30 +169,30 @@ class FGDistributor : public FGFCSComponent
172169
public:
173170
Case() : Test(nullptr) {}
174171

175-
~Case() {
176-
for (auto pair: PropValPairs) delete pair;
177-
delete Test;
172+
void SetTest(Element* test_element, std::shared_ptr<FGPropertyManager> propMan) {
173+
Test = std::make_unique<FGCondition>(test_element, propMan);
174+
}
175+
const FGCondition& GetTest(void) const noexcept { return *Test; }
176+
void AddPropValPair(const std::string& property, const std::string& value,
177+
std::shared_ptr<FGPropertyManager> propManager, Element* prop_val_el) {
178+
PropValPairs.push_back(std::make_unique<PropValPair>(property, value, propManager, prop_val_el));
178179
}
179-
180-
void SetTest(FGCondition* test) {Test = test;}
181-
FGCondition* GetTest(void) {return Test;}
182-
void AddPropValPair(PropValPair* pvPair) {PropValPairs.push_back(pvPair);}
183180
void SetPropValPairs() {
184-
for (auto pair: PropValPairs) pair->SetPropToValue();
181+
for (auto& pair: PropValPairs) pair->SetPropToValue();
185182
}
186-
std::vector<PropValPair*>::const_iterator IterPropValPairs(void) const
183+
vector_of_unique_ptr<PropValPair>::const_iterator begin(void) const
187184
{ return PropValPairs.cbegin(); }
188-
std::vector<PropValPair*>::const_iterator EndPropValPairs(void) const
185+
vector_of_unique_ptr<PropValPair>::const_iterator end(void) const
189186
{ return PropValPairs.cend(); }
190-
bool HasTest() {return Test != nullptr;}
191-
bool GetTestResult() { return Test->Evaluate(); }
187+
bool HasTest() const noexcept { return Test != nullptr; }
188+
bool GetTestResult() const { return Test->Evaluate(); }
192189

193190
private:
194-
FGCondition* Test;
195-
std::vector <PropValPair*> PropValPairs;
191+
std::unique_ptr<FGCondition> Test;
192+
vector_of_unique_ptr<PropValPair> PropValPairs;
196193
};
197194

198-
std::vector <Case*> Cases;
195+
vector_of_unique_ptr<Case> Cases;
199196

200197
void Debug(int from) override;
201198
};

0 commit comments

Comments
 (0)