Skip to content

Commit b3178d3

Browse files
authored
Inflection 134 Use std::unique_ptr to avoid delete (#162)
* Use std::unique_ptr to avoid delete * Remove change to warm up code * Make destructor public instead of adding friend
1 parent 6a1f6c5 commit b3178d3

8 files changed

+20
-23
lines changed

inflection/src/inflection/dialog/CommonConceptFactoryImpl.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,9 @@ SpeakableString* CommonConceptFactoryImpl::quantify(const NumberConcept& number,
210210
{
211211
::std::unique_ptr<SpeakableString> formattedNumber;
212212
if (semanticFeatureForNumberConcept != nullptr) {
213-
auto speakableFeatureValue = npc(semanticConcept)->getFeatureValue(*npc(semanticFeatureForNumberConcept));
214-
if (speakableFeatureValue != nullptr) {
215-
auto rulename = semanticFeatureToNumberConceptName.find(npc(speakableFeatureValue)->getPrint());
216-
delete speakableFeatureValue;
213+
::std::unique_ptr<SpeakableString> speakableFeatureValue(npc(semanticConcept)->getFeatureValue(*npc(semanticFeatureForNumberConcept)));
214+
if (speakableFeatureValue.get() != nullptr) {
215+
auto rulename = semanticFeatureToNumberConceptName.find(speakableFeatureValue->getPrint());
217216
if (rulename != semanticFeatureToNumberConceptName.end()) {
218217
formattedNumber.reset(new SpeakableString(number.asSpokenWords(rulename->second)));
219218
}

inflection/src/inflection/dialog/DefinitenessDisplayFunction.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ int32_t DefinitenessDisplayFunction::getArticlePrefixLength(DisplayValue* origin
7979
DisplayValue*
8080
DefinitenessDisplayFunction::replaceDisplayValue(DisplayValue* originalDisplayValue, const SpeakableString& string) const
8181
{
82-
auto displayValueConstraints(npc(originalDisplayValue)->getConstraintMap());
82+
::std::unique_ptr<DisplayValue> value(npc(originalDisplayValue));
83+
auto displayValueConstraints(value->getConstraintMap());
8384
if (!string.speakEqualsPrint()) {
8485
displayValueConstraints[*npc(speakFeature)] = string.getSpeak();
8586
}
86-
auto newValue = new DisplayValue(string.getPrint(), displayValueConstraints);
87-
delete originalDisplayValue;
88-
return newValue;
87+
value.reset(new DisplayValue(string.getPrint(), displayValueConstraints));
88+
return value.release();
8989
}
9090

9191
DisplayValue*

inflection/src/inflection/dialog/SemanticConceptList.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,11 @@ std::u16string SemanticConceptList::toString() const
126126
if (previousConcept != nullptr) {
127127
displayValue += comma;
128128
}
129-
auto conceptResult = npc(conceptObj)->toSpeakableString();
130-
if (conceptResult != nullptr) {
131-
displayValue += *npc(conceptResult);
129+
::std::unique_ptr<::inflection::dialog::SpeakableString> conceptResult(npc(conceptObj)->toSpeakableString());
130+
if (conceptResult.get() != nullptr) {
131+
displayValue += *conceptResult;
132132
}
133133
previousConcept = conceptObj;
134-
delete conceptResult;
135134
}
136135
return u"[" + displayValue.getPrint() + u"]";
137136
}

inflection/src/inflection/dictionary/DictionaryMetaData_MMappedDictionary.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ DictionaryMetaData_MMappedDictionary::DictionaryMetaData_MMappedDictionary(::inf
4545

4646
DictionaryMetaData_MMappedDictionary::~DictionaryMetaData_MMappedDictionary()
4747
{
48-
delete inflector;
49-
delete memoryMappedRegion;
5048
}
5149

5250
const ::inflection::util::ULocale& DictionaryMetaData_MMappedDictionary::getLocale() const
@@ -189,11 +187,11 @@ bool DictionaryMetaData_MMappedDictionary::getWordPropertyValues(::std::vector<:
189187
return true;
190188
}
191189

192-
const bool useInflectionTrieForKeys = (inflector != nullptr) && (propertyNameIdentifier == inflectionKeyIdentifier);
190+
const bool useInflectionTrieForKeys = (inflector.get() != nullptr) && (propertyNameIdentifier == inflectionKeyIdentifier);
193191

194192
for (const auto propertyIdentifier: propertyIdentifiers) {
195193
npc(result)->emplace_back(useInflectionTrieForKeys
196-
? npc(inflector)->mmappedDictionary.identifierToInflectionPatternTrie.getKey(propertyIdentifier)
194+
? inflector->mmappedDictionary.identifierToInflectionPatternTrie.getKey(propertyIdentifier)
197195
: propertyValuesStringContainer.getString(propertyIdentifier));
198196
}
199197

inflection/src/inflection/dictionary/DictionaryMetaData_MMappedDictionary.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ class INFLECTION_INTERNAL_API inflection::dictionary::DictionaryMetaData_MMapped
7070
::inflection::dictionary::metadata::StringArrayContainer propertyNameToKeyId { };
7171
::inflection::dictionary::metadata::StringContainer propertyValuesStringContainer { };
7272
::inflection::dictionary::metadata::CompressedArray<int32_t> propertyValueMaps {::std::vector<int32_t>()};
73-
::inflection::dictionary::Inflector* inflector { };
74-
::inflection::util::MemoryMappedFile* memoryMappedRegion { };
73+
::std::unique_ptr<::inflection::dictionary::Inflector> inflector { };
74+
::std::unique_ptr<::inflection::util::MemoryMappedFile> memoryMappedRegion { };
7575
int32_t inflectionKeyIdentifier { -1 };
7676
int32_t bitsPropertyValueMapKeyMask { };
7777
int32_t bitsPropertyValueMapKeySize { };

inflection/src/inflection/dictionary/Inflector.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
namespace inflection::dictionary {
1414

1515
const Inflector& Inflector::getInflector(const ::inflection::util::ULocale &locale) {
16-
const Inflector* result = npc(DictionaryMetaData::createDictionary(locale))->dictionary->inflector;
16+
const Inflector* result = npc(DictionaryMetaData::createDictionary(locale))->dictionary->inflector.get();
1717
if (result == nullptr) {
1818
throw ::inflection::exception::NullPointerException(inflection::util::StringViewUtils::to_u16string("Inflector not found for " + locale.getName()));
1919
}

inflection/src/inflection/dictionary/Inflector.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class inflection::dictionary::Inflector
3333
private:
3434
explicit Inflector(inflection::util::MemoryMappedFile& memoryMappedFile, const ::std::u16string& sourcePath, const ::inflection::dictionary::DictionaryMetaData_MMappedDictionary &dictionary);
3535

36+
public:
3637
~Inflector() override;
3738

3839
private:

inflection/src/inflection/lang/StringFilterUtil.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,18 @@ static ::std::map<const ::icu4cxx::UnicodeSet*, const ::icu4cxx::UnicodeSet*>* S
117117
return setSingletons_;
118118
}
119119

120-
const icu4cxx::UnicodeSet* StringFilterUtil::getSetSingleton(::icu4cxx::UnicodeSet* set)
120+
const icu4cxx::UnicodeSet* StringFilterUtil::getSetSingleton(::icu4cxx::UnicodeSet* input)
121121
{
122122
// TODO: this code probably doesn't work that well anymore. We should check the caching again.
123123
auto setSingletons = SET_SINGLETONS();
124124
const icu4cxx::UnicodeSet* singleton;
125-
auto singletonCache = npc(setSingletons)->find(set);
125+
::std::unique_ptr<::icu4cxx::UnicodeSet> set(input);
126+
auto singletonCache = npc(setSingletons)->find(set.get());
126127
if (singletonCache == npc(setSingletons)->end()) {
127-
singleton = ::inflection::util::UnicodeSetUtils::freeze(set);
128+
singleton = ::inflection::util::UnicodeSetUtils::freeze(set.release());
128129
npc(setSingletons)->emplace(singleton, singleton);
129130
}
130131
else {
131-
delete set;
132132
singleton = singletonCache->second;
133133
}
134134
return singleton;

0 commit comments

Comments
 (0)