Instead of the new_intrusive<T>() functions, use intrusive(new T)

This means we no longer need 8 different functions for different numbers of arguments, and non-const references can now also be passed to constructors without problems.

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1443 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2010-07-21 14:32:28 +00:00
parent 8800500d86
commit 51dfed69b4
66 changed files with 304 additions and 353 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 = new_intrusive<DelayedIndexMapsData<Key,Value> >();
item = intrusive(new 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(new_shared1<wxStringInputStream>(item->unread_data), nullptr, _("delayed data for ") + name);
Reader reader(shared(new wxStringInputStream(item->unread_data)), nullptr, _("delayed data for ") + name);
reader.handle_greedy(item->read_data);
item->unread_data.clear();
}
+6 -6
View File
@@ -212,16 +212,16 @@ InputStreamP Package::openIn(const String& file) {
InputStreamP stream;
if (it->second.wasWritten()) {
// written to this file, open the temp file
stream = new_shared1<BufferedFileInputStream>(it->second.tempName);
stream = shared(new BufferedFileInputStream(it->second.tempName));
} else if (wxFileExists(filename+_("/")+file)) {
// a file in directory package
stream = new_shared1<BufferedFileInputStream>(filename+_("/")+file);
stream = shared(new BufferedFileInputStream(filename+_("/")+file));
} else if (wxFileExists(filename) && it->second.zipEntry) {
// a file in a zip archive
// somebody in wx thought seeking was no longer needed, it now only works with the 'compatability constructor'
stream = new_shared2<wxZipInputStream>(filename, it->second.zipEntry->GetInternalName());
stream = shared(new wxZipInputStream(filename, it->second.zipEntry->GetInternalName()));
//stream = static_pointer_cast<wxZipInputStream>(
// new_shared2<ZipFileInputStream>(filename, it->second.zipEntry));
// shared(new ZipFileInputStream(filename, it->second.zipEntry)));
} else {
// shouldn't happen, packaged changed by someone else since opening it
throw FileNotFoundError(file, filename);
@@ -234,7 +234,7 @@ InputStreamP Package::openIn(const String& file) {
}
OutputStreamP Package::openOut(const String& file) {
return new_shared1<wxFileOutputStream>(nameOut(file));
return shared(new wxFileOutputStream(nameOut(file)));
}
String Package::nameOut(const String& file) {
@@ -308,7 +308,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 = new_shared1<wxFileInputStream>(name);
shared_ptr<wxFileInputStream> f = shared(new wxFileInputStream(name));
if (!f->IsOk()) throw FileNotFoundError(_("<unknown>"), name);
return f;
} else {
+1 -1
View File
@@ -138,7 +138,7 @@ class Package : public IntrusivePtrVirtualBase {
protected:
// TODO: I dislike putting this here very much. There ought to be a better way.
virtual VCSP getVCS() { return new_intrusive<VCS>(); }
virtual VCSP getVCS() { return intrusive(new VCS()); }
/// true if this is a zip file, false if a directory (updated on open/save)
bool isZipfile() { return zipfile; }
+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 = new_intrusive<Game>();
else if (fn.GetExt() == _("mse-style")) p = new_intrusive<StyleSheet>();
else if (fn.GetExt() == _("mse-locale")) p = new_intrusive<Locale>();
else if (fn.GetExt() == _("mse-include")) p = new_intrusive<IncludePackage>();
else if (fn.GetExt() == _("mse-symbol-font")) p = new_intrusive<SymbolFont>();
else if (fn.GetExt() == _("mse-export-template")) p = new_intrusive<ExportTemplate>();
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);
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(new_intrusive2<InstallablePackage>(new_intrusive1<PackageDescription>(*pack), ver));
packages_out.push_back(intrusive(new InstallablePackage(intrusive(new 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(new_intrusive2<InstallablePackage>(new_intrusive1<PackageDescription>(*pack), *it1));
packages_out.push_back(intrusive(new InstallablePackage(intrusive(new 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 = new_shared1<wxFileInputStream>(filename);
shared_ptr<wxFileInputStream> file = shared(new 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(new_shared1<wxFileOutputStream>(databaseFile()), app_version);
Writer writer(shared(new 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 new_intrusive<T>();
return intrusive(new T());
}
/// Update the 'index' member of a value for use by IndexMap
+16 -60
View File
@@ -38,6 +38,15 @@ using namespace boost;
// ----------------------------------------------------------------------------- : Creating
/// Wrap a newly allocated pointer in an shared_ptr
/** Usage:
* return shared(new T(stuff)));
*/
template <typename T>
inline shared_ptr<T> shared(T* ptr) {
return shared_ptr<T>(ptr);
}
/// Allocate a new shared-pointed object
template <typename T>
inline shared_ptr<T> new_shared() {
@@ -98,58 +107,15 @@ inline shared_ptr<T> new_shared9(const A0& a0, const A1& a1, const A2& a2, const
class Type; \
typedef intrusive_ptr<Type> Type##P;
/// Allocate a new intrusive-pointed object
/// Wrap a newly allocated pointer in an intrusive_ptr
/** Usage:
* return intrusive(new T(stuff)));
*/
template <typename T>
inline intrusive_ptr<T> new_intrusive() {
return intrusive_ptr<T>(new T());
inline intrusive_ptr<T> intrusive(T* ptr) {
return intrusive_ptr<T>(ptr);
}
/// Allocate a new intrusive-pointed object, given one argument to pass to the ctor of T
template <typename T, typename A0>
inline intrusive_ptr<T> new_intrusive1(const A0& a0) {
return intrusive_ptr<T>(new T(a0));
}
/// Allocate a new intrusive-pointed object, given two arguments to pass to the ctor of T
template <typename T, typename A0, typename A1>
inline intrusive_ptr<T> new_intrusive2(const A0& a0, const A1& a1) {
return intrusive_ptr<T>(new T(a0, a1));
}
/// Allocate a new intrusive-pointed object, given three arguments to pass to the ctor of T
template <typename T, typename A0, typename A1, typename A2>
inline intrusive_ptr<T> new_intrusive3(const A0& a0, const A1& a1, const A2& a2) {
return intrusive_ptr<T>(new T(a0, a1, a2));
}
/// Allocate a new intrusive-pointed object, given four arguments to pass to the ctor of T
template <typename T, typename A0, typename A1, typename A2, typename A3>
inline intrusive_ptr<T> new_intrusive4(const A0& a0, const A1& a1, const A2& a2, const A3& a3) {
return intrusive_ptr<T>(new T(a0, a1, a2, a3));
}
/// Allocate a new intrusive-pointed object, given five arguments to pass to the ctor of T
template <typename T, typename A0, typename A1, typename A2, typename A3, typename A4>
inline intrusive_ptr<T> new_intrusive5(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4) {
return intrusive_ptr<T>(new T(a0, a1, a2, a3, a4));
}
/// Allocate a new intrusive-pointed object, given six arguments to pass to the ctor of T
template <typename T, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5>
inline intrusive_ptr<T> new_intrusive6(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5) {
return intrusive_ptr<T>(new T(a0, a1, a2, a3, a4, a5));
}
/// Allocate a new intrusive-pointed object, given seven arguments to pass to the ctor of T
template <typename T, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
inline intrusive_ptr<T> new_intrusive7(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6) {
return intrusive_ptr<T>(new T(a0, a1, a2, a3, a4, a5, a6));
}
/// Allocate a new intrusive-pointed object, given eight arguments to pass to the ctor of T
template <typename T, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
inline intrusive_ptr<T> new_intrusive8(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7) {
return intrusive_ptr<T>(new T(a0, a1, a2, a3, a4, a5, a6, a7));
}
/// Allocate a new intrusive-pointed object, given nine arguments to pass to the ctor of T
template <typename T, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
inline intrusive_ptr<T> new_intrusive9(const A0& a0, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8) {
return intrusive_ptr<T>(new T(a0, a1, a2, a3, a4, a5, a6, a7, a8));
}
// ----------------------------------------------------------------------------- : Intrusive pointer base
template <typename T> class IntrusivePtrBase;
@@ -215,16 +181,6 @@ inline shared_ptr<T> new_shared9(const A0& a0, const A1& a1, const A2& a2, const
#else
#define DECLARE_POINTER_TYPE DECLARE_SHARED_POINTER_TYPE
#define intrusive_ptr shared_ptr
#define new_intrusive new_shared
#define new_intrusive1 new_shared1
#define new_intrusive2 new_shared2
#define new_intrusive3 new_shared3
#define new_intrusive4 new_shared4
#define new_intrusive5 new_shared5
#define new_intrusive6 new_shared6
#define new_intrusive7 new_shared7
#define new_intrusive8 new_shared8
#define new_intrusive9 new_shared9
template <typename T> class IntrusivePtrBase {};
+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 new_intrusive<VCS>();
else if (type == _("subversion")) return new_intrusive<SubversionVCS>();
if (type == _("none")) return intrusive(new VCS);
else if (type == _("subversion")) return intrusive(new SubversionVCS);
else if (type.empty()) {
reader.warning(_ERROR_1_("expected key", _("version control system")));
throw ParseError(_ERROR_("aborting parsing"));
+25 -45
View File
@@ -11,25 +11,29 @@
// ----------------------------------------------------------------------------- : SVN File Manipulation
bool run_svn(const Char** arguments) {
switch (wxExecute(const_cast<Char**>(arguments), wxEXEC_SYNC)) { // Yuck, const_cast
// Success
case 0:
return true;
// Couldn't run SVN
case -1:
handle_error(String(_("Can't run SVN.")));
return false;
// SVN error
default:
handle_error(String(_("SVN encountered an error")));
return false;
}
}
void SubversionVCS::addFile(const wxFileName& filename)
{
String name = filename.GetFullPath();
const Char* name_c[] = {_("svn"), _("add"), name.c_str(), nullptr};
switch (wxExecute(const_cast<Char**>(name_c), wxEXEC_SYNC)) // Yuck, const_cast
{
// Success
case 0:
return;
// Couldn't run SVN
case -1:
handle_error(String(_("Can't run SVN.")));
VCS::addFile(filename);
return;
// SVN error
default:
handle_error(String(_("SVN encountered an error")));
VCS::addFile(filename);
return;
if (!run_svn(name_c)) {
VCS::addFile(filename);
}
}
@@ -37,21 +41,8 @@ void SubversionVCS::moveFile(const wxFileName& source, const wxFileName& dest)
{
String source_name = source.GetFullPath(), dest_name = dest.GetFullPath();
const Char* name_c[] = {_("svn"), _("mv"), source_name.c_str(), dest_name.c_str(), nullptr};
switch (wxExecute(const_cast<Char**>(name_c), wxEXEC_SYNC)) // Once again, yuck
{
// Success
case 0:
return;
// Couldn't run SVN
case -1:
handle_error(String(_("Can't run SVN.")));
VCS::moveFile(source, dest);
return;
// SVN error
default:
handle_error(String(_("SVN encountered an error")));
VCS::moveFile(source, dest);
return;
if (!run_svn(name_c)) {
VCS::moveFile(source, dest);
}
}
@@ -60,22 +51,10 @@ void SubversionVCS::removeFile(const wxFileName& filename)
String name = filename.GetFullPath();
const Char* name_c[] = {_("svn"), _("rm"), name.c_str(), nullptr};
handle_warning(String(name_c[0]) + name_c[1] + name_c[2]);
// TODO: do we really need to remove the file before calling "svn remove"?
VCS::removeFile(filename);
switch (wxExecute(const_cast<Char**>(name_c), wxEXEC_SYNC)) // Once again, yuck
{
// Success
case 0:
return;
// Couldn't run SVN
case -1:
handle_error(String(_("Can't run SVN.")));
VCS::removeFile(filename);
return;
// SVN error
default:
handle_error(String(_("SVN encountered an error")));
VCS::removeFile(filename);
return;
if (!run_svn(name_c)) {
VCS::removeFile(filename);
}
}
@@ -85,4 +64,5 @@ IMPLEMENT_REFLECTION(SubversionVCS) {
REFLECT(type);
}
}
// ----------------------------------------------------------------------------- : EOF
+1 -2
View File
@@ -14,8 +14,7 @@
// ----------------------------------------------------------------------------- : SubversionVCS
class SubversionVCS : public VCS
{
class SubversionVCS : public VCS {
public:
virtual void addFile (const wxFileName& filename);
virtual void moveFile (const wxFileName& source, const wxFileName& destination);