Use make_intrusive/make_shared for smart pointer construction.

This commit is contained in:
Twan van Laarhoven
2020-04-23 23:51:34 +02:00
parent 815df01ba5
commit 708b4389a0
67 changed files with 313 additions and 329 deletions
+2 -2
View File
@@ -21,11 +21,11 @@ template <typename Key, typename Value>
IndexMap<Key,Value>& DelayedIndexMaps<Key,Value>::get(const String& name, const vector<Key>& init_with) {
intrusive_ptr<DelayedIndexMapsData<Key,Value> >& item = data[name];
if (!item) { // no item, make a new one
item = intrusive(new DelayedIndexMapsData<Key,Value>);
item = make_intrusive<DelayedIndexMapsData<Key,Value>>();
item->read_data.init(init_with);
} else if (!item->unread_data.empty()) { // not read, read now
item->read_data.init(init_with);
Reader reader(shared(new wxStringInputStream(item->unread_data)), nullptr, _("delayed data for ") + name);
Reader reader(make_shared<wxStringInputStream>(item->unread_data), nullptr, _("delayed data for ") + name);
reader.handle_greedy(item->read_data);
item->unread_data.clear();
}
+5 -5
View File
@@ -203,13 +203,13 @@ InputStreamP Package::openIn(const String& file) {
InputStreamP stream;
if (it != files.end() && it->second.wasWritten()) {
// written to this file, open the temp file
stream = shared(new BufferedFileInputStream(it->second.tempName));
stream = make_shared<BufferedFileInputStream>(it->second.tempName);
} else if (wxFileExists(filename+_("/")+file)) {
// a file in directory package
stream = shared(new BufferedFileInputStream(filename+_("/")+file));
stream = make_shared<BufferedFileInputStream>(filename+_("/")+file);
} else if (wxFileExists(filename) && it != files.end() && it->second.zipEntry) {
// a file in a zip archive
stream = static_pointer_cast<wxZipInputStream>(shared(new ZipFileInputStream(filename, it->second.zipEntry)));
stream = static_pointer_cast<wxZipInputStream>(make_shared<ZipFileInputStream>(filename, it->second.zipEntry));
} else {
// shouldn't happen, packaged changed by someone else since opening it
throw FileNotFoundError(file, filename);
@@ -222,7 +222,7 @@ InputStreamP Package::openIn(const String& file) {
}
OutputStreamP Package::openOut(const String& file) {
return shared(new wxFileOutputStream(nameOut(file)));
return make_shared<wxFileOutputStream>(nameOut(file));
}
String Package::nameOut(const String& file) {
@@ -296,7 +296,7 @@ InputStreamP Package::openAbsoluteFile(const String& name) {
size_t pos = name.find_first_of(_('\1'));
if (pos == String::npos) {
// temp or dir file
shared_ptr<wxFileInputStream> f = shared(new wxFileInputStream(name));
shared_ptr<wxFileInputStream> f = make_shared<wxFileInputStream>(name);
if (!f->IsOk()) throw FileNotFoundError(_("<unknown>"), name);
return f;
} else {
+10 -10
View File
@@ -65,12 +65,12 @@ PackagedP PackageManager::openAny(const String& name_, bool just_header) {
if (!p) {
// load with the right type, based on extension
wxFileName fn(filename);
if (fn.GetExt() == _("mse-game")) p = intrusive(new Game);
else if (fn.GetExt() == _("mse-style")) p = intrusive(new StyleSheet);
else if (fn.GetExt() == _("mse-locale")) p = intrusive(new Locale);
else if (fn.GetExt() == _("mse-include")) p = intrusive(new IncludePackage);
else if (fn.GetExt() == _("mse-symbol-font")) p = intrusive(new SymbolFont);
else if (fn.GetExt() == _("mse-export-template")) p = intrusive(new ExportTemplate);
if (fn.GetExt() == _("mse-game")) p = make_intrusive<Game>();
else if (fn.GetExt() == _("mse-style")) p = make_intrusive<StyleSheet>();
else if (fn.GetExt() == _("mse-locale")) p = make_intrusive<Locale>();
else if (fn.GetExt() == _("mse-include")) p = make_intrusive<IncludePackage>();
else if (fn.GetExt() == _("mse-symbol-font")) p = make_intrusive<SymbolFont>();
else if (fn.GetExt() == _("mse-export-template")) p = make_intrusive<ExportTemplate>();
else {
throw PackageError(_("Unrecognized package type: '") + fn.GetExt() + _("'\nwhile trying to open: ") + name);
}
@@ -273,7 +273,7 @@ void PackageDirectory::installedPackages(vector<InstallablePackageP>& packages_o
PackageVersionP ver(new PackageVersion(
is_local ? PackageVersion::STATUS_LOCAL : PackageVersion::STATUS_GLOBAL));
ver->check_status(*pack);
packages_out.push_back(intrusive(new InstallablePackage(intrusive(new PackageDescription(*pack)), ver)));
packages_out.push_back(make_intrusive<InstallablePackage>(make_intrusive<PackageDescription>(*pack), ver));
} catch (const Error&) {}
++it2;
} else if ((*it1)->name < *it2) {
@@ -285,7 +285,7 @@ void PackageDirectory::installedPackages(vector<InstallablePackageP>& packages_o
try {
PackagedP pack = package_manager.openAny(*it2, true);
(*it1)->check_status(*pack);
packages_out.push_back(intrusive(new InstallablePackage(intrusive(new PackageDescription(*pack)), *it1)));
packages_out.push_back(make_intrusive<InstallablePackage>(make_intrusive<PackageDescription>(*pack), *it1));
} catch (const Error&) { db_changed = true; }
++it1, ++it2;
}
@@ -339,7 +339,7 @@ void PackageDirectory::loadDatabase() {
String filename = databaseFile();
if (wxFileExists(filename)) {
// packages file not existing is not an error
shared_ptr<wxFileInputStream> file = shared(new wxFileInputStream(filename));
shared_ptr<wxFileInputStream> file = make_shared<wxFileInputStream>(filename);
if (!file->Ok()) return; // failure is not an error
Reader reader(file, nullptr, filename);
reader.handle_greedy(*this);
@@ -348,7 +348,7 @@ void PackageDirectory::loadDatabase() {
}
void PackageDirectory::saveDatabase() {
Writer writer(shared(new wxFileOutputStream(databaseFile())), app_version);
Writer writer(make_shared<wxFileOutputStream>(databaseFile()), app_version);
writer.handle(*this);
}
String PackageDirectory::databaseFile() {
+1 -1
View File
@@ -200,7 +200,7 @@ class Reader {
*/
template <typename T>
intrusive_ptr<T> read_new(Reader& reader) {
return intrusive(new T());
return make_intrusive<T>();
}
/// Update the 'index' member of a value for use by IndexMap
+7 -18
View File
@@ -33,29 +33,18 @@ template <typename T> using scoped_ptr = unique_ptr<T>;
class Type; \
typedef shared_ptr<Type> Type##P;
// ----------------------------------------------------------------------------- : Creating
/// Wrap a newly allocated pointer in an shared_ptr
/** Usage:
* return shared(new T(stuff)));
*/
template <typename T>
//[[deprecated("use make_shared")]]
inline shared_ptr<T> shared(T* ptr) {
return shared_ptr<T>(ptr);
}
template <typename T>
//[[deprecated("use make_shared")]]
inline shared_ptr<T> intrusive(T* ptr) {
return shared_ptr<T>(ptr);
}
// ----------------------------------------------------------------------------- : Intrusive pointers
#define DECLARE_POINTER_TYPE DECLARE_SHARED_POINTER_TYPE
#define intrusive_ptr shared_ptr
template <typename T> class IntrusivePtrBase {};
template <typename T> using intrusive_ptr = shared_ptr<T>;
/// Allocate an object of type T and store it in a new intrusive_ptr, similar to std::make_shared
template <typename T, class... Args>
inline intrusive_ptr<T> make_intrusive(Args&&... args) {
return std::make_shared<T>(std::forward<Args>(args)...);
}
/// IntrusivePtrBase with a virtual destructor
class IntrusivePtrVirtualBase : public IntrusivePtrBase<IntrusivePtrVirtualBase> {
+2 -2
View File
@@ -17,8 +17,8 @@ VCSP read_new<VCS>(Reader& reader) {
// there must be a type specified
String type;
reader.handle(_("type"), type);
if (type == _("none")) return intrusive(new VCS);
else if (type == _("subversion")) return intrusive(new SubversionVCS);
if (type == _("none")) return make_intrusive<VCS>();
else if (type == _("subversion")) return make_intrusive<SubversionVCS>();
else if (type.empty()) {
reader.warning(_ERROR_1_("expected key", _("version control system")));
throw ParseError(_ERROR_("aborting parsing"));