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,7 +21,7 @@ DECLARE_TYPEOF_COLLECTION(KeywordModeP);
AddKeywordAction::AddKeywordAction(Set& set)
: KeywordListAction(set)
, action(ADD, intrusive(new Keyword()), set.keywords)
, action(ADD, make_intrusive<Keyword>(), set.keywords)
{
Keyword& keyword = *action.steps.front().item;
// find default mode
@@ -185,7 +185,7 @@ bool KeywordReminderTextValue::checkScript(const ScriptP& script) {
const KeywordParam& kwp = *keyword.parameters[i];
String param_name = String(_("param")) << (int)(i+1);
String param_value = _("<atom-kwpph>") + (kwp.placeholder.empty() ? kwp.name : kwp.placeholder) + _("</atom-kwpph>");
ctx.setVariable(param_name, intrusive(new KeywordParamValue(kwp.name, _(""), _(""), param_value)));
ctx.setVariable(param_name, make_intrusive<KeywordParamValue>(kwp.name, _(""), _(""), param_value));
}
script->eval(ctx);
errors.clear();
+1 -1
View File
@@ -23,7 +23,7 @@ DECLARE_TYPEOF_COLLECTION(int);
AddCardAction::AddCardAction(Set& set)
: CardListAction(set)
, action(ADD, intrusive(new Card(*set.game)), set.cards)
, action(ADD, make_intrusive<Card>(*set.game), set.cards)
{}
AddCardAction::AddCardAction(AddingOrRemoving ar, Set& set, const CardP& card)
+2 -2
View File
@@ -398,7 +398,7 @@ ControlPointRemoveAction::ControlPointRemoveAction(const SymbolShapeP& shape, co
FOR_EACH(point, shape->points) {
if (to_delete.find(point) != to_delete.end()) {
// remove this point
removals.push_back(intrusive(new SinglePointRemoveAction(shape, index)));
removals.push_back(make_intrusive<SinglePointRemoveAction>(shape, index));
}
++index;
}
@@ -422,7 +422,7 @@ void ControlPointRemoveAction::perform(bool to_undo) {
Action* control_point_remove_action(const SymbolShapeP& shape, const set<ControlPointP>& to_delete) {
if (shape->points.size() - to_delete.size() < 2) {
// TODO : remove part?
//intrusive(new ControlPointRemoveAllAction(part));
//make_intrusive<ControlPointRemoveAllAction>(part);
return 0; // no action
} else {
return new ControlPointRemoveAction(shape, to_delete);
+1 -1
View File
@@ -34,7 +34,7 @@ void AddCardsScript::perform(Set& set, vector<CardP>& out) {
// is this a new card?
if (contains(set.cards,card) || contains(out,card)) {
// make copy
card = intrusive(new Card(*card));
card = make_intrusive<Card>(*card);
}
out.push_back(card);
}
+9 -9
View File
@@ -76,15 +76,15 @@ intrusive_ptr<Field> read_new<Field>(Reader& reader) {
// there must be a type specified
String type;
reader.handle(_("type"), type);
if (type == _("text")) return intrusive(new TextField());
else if (type == _("choice")) return intrusive(new ChoiceField());
else if (type == _("multiple choice")) return intrusive(new MultipleChoiceField());
else if (type == _("boolean")) return intrusive(new BooleanField());
else if (type == _("image")) return intrusive(new ImageField());
else if (type == _("symbol")) return intrusive(new SymbolField());
else if (type == _("color")) return intrusive(new ColorField());
else if (type == _("info")) return intrusive(new InfoField());
else if (type == _("package choice")) return intrusive(new PackageChoiceField());
if (type == _("text")) return make_intrusive<TextField>();
else if (type == _("choice")) return make_intrusive<ChoiceField>();
else if (type == _("multiple choice")) return make_intrusive<MultipleChoiceField>();
else if (type == _("boolean")) return make_intrusive<BooleanField>();
else if (type == _("image")) return make_intrusive<ImageField>();
else if (type == _("symbol")) return make_intrusive<SymbolField>();
else if (type == _("color")) return make_intrusive<ColorField>();
else if (type == _("info")) return make_intrusive<InfoField>();
else if (type == _("package choice")) return make_intrusive<PackageChoiceField>();
else if (type.empty()) {
reader.warning(_ERROR_1_("expected key", _("type")));
throw ParseError(_ERROR_("aborting parsing"));
+17 -17
View File
@@ -265,23 +265,23 @@ inline String type_name(const Value&) {
virtual String typeName() const
// implement newStyle and newValue
#define IMPLEMENT_FIELD_TYPE(Type, NAME) \
StyleP Type ## Field::newStyle(const FieldP& thisP) const { \
assert(thisP.get() == this); \
return intrusive(new Type ## Style(static_pointer_cast<Type ## Field>(thisP))); \
} \
ValueP Type ## Field::newValue(const FieldP& thisP) const { \
assert(thisP.get() == this); \
return intrusive(new Type ## Value(static_pointer_cast<Type ## Field>(thisP))); \
} \
StyleP Type ## Style::clone() const { \
return intrusive(new Type ## Style(*this)); \
} \
ValueP Type ## Value::clone() const { \
return intrusive(new Type ## Value(*this)); \
} \
String Type ## Field::typeName() const { \
return _(NAME); \
#define IMPLEMENT_FIELD_TYPE(Type, NAME) \
StyleP Type ## Field::newStyle(const FieldP& thisP) const { \
assert(thisP.get() == this); \
return make_intrusive<Type ## Style>(static_pointer_cast<Type ## Field>(thisP)); \
} \
ValueP Type ## Field::newValue(const FieldP& thisP) const { \
assert(thisP.get() == this); \
return make_intrusive<Type ## Value>(static_pointer_cast<Type ## Field>(thisP)); \
} \
StyleP Type ## Style::clone() const { \
return make_intrusive<Type ## Style>(*this); \
} \
ValueP Type ## Value::clone() const { \
return make_intrusive<Type ## Value>(*this); \
} \
String Type ## Field::typeName() const { \
return _(NAME); \
}
#define DECLARE_STYLE_TYPE(Type) \
+4 -6
View File
@@ -12,8 +12,8 @@
// ----------------------------------------------------------------------------- : BooleanField
BooleanField::BooleanField() {
choices->choices.push_back(intrusive(new Choice(_("yes"))));
choices->choices.push_back(intrusive(new Choice(_("no"))));
choices->choices.push_back(make_intrusive<Choice>(_("yes")));
choices->choices.push_back(make_intrusive<Choice>(_("no")));
choices->initIds();
}
@@ -32,10 +32,8 @@ BooleanStyle::BooleanStyle(const ChoiceFieldP& field)
: ChoiceStyle(field)
{
render_style = RENDER_BOTH;
//choice_images[_("yes")] = ScriptableImage(_("buildin_image(\"bool_yes\")"));
//choice_images[_("no")] = ScriptableImage(_("buildin_image(\"bool_no\")"));
choice_images[_("yes")] = ScriptableImage(intrusive(new BuiltInImage(_("bool_yes"))));
choice_images[_("no")] = ScriptableImage(intrusive(new BuiltInImage(_("bool_no"))));
choice_images[_("yes")] = ScriptableImage(make_intrusive<BuiltInImage>(_("bool_yes")));
choice_images[_("no")] = ScriptableImage(make_intrusive<BuiltInImage>(_("bool_no")));
}
IMPLEMENT_REFLECTION(BooleanStyle) {
+1 -1
View File
@@ -48,5 +48,5 @@ void ImageValue::reflect(Writer& tag) {
void ImageValue::reflect(GetMember& tag) {}
void ImageValue::reflect(GetDefaultMember& tag) {
// convert to ScriptImageP for scripting
tag.handle( (ScriptValueP)intrusive(new ImageValueToImage(filename, last_update)) );
tag.handle( (ScriptValueP)make_intrusive<ImageValueToImage>(filename, last_update) );
}
+2 -2
View File
@@ -538,7 +538,7 @@ void ApprCardDatabase::doRead(wxInputStream& in) {
progress_target->onProgress(0.4f * float(i) / cards.size(),
String(_("reading card ")) << i << _(" of ") << (int)cards.size());
}
card = intrusive(new ApprCardRecord());
card = make_intrusive<ApprCardRecord>();
card->readHead(data);
head_pos = in.TellI();
in.SeekI(card->data_pos);
@@ -753,7 +753,7 @@ bool ApprenticeExportWindow::exportSet() {
cardlist.removeSet(set->apprentice_code);
// add cards from set
FOR_EACH(card, set->cards) {
ApprCardRecordP rec = intrusive(new ApprCardRecord(*card, set->apprentice_code));
ApprCardRecordP rec = make_intrusive<ApprCardRecord>(*card, set->apprentice_code);
cardlist.cards.push_back(rec);
}
cardlist.write();
+3 -6
View File
@@ -10,9 +10,6 @@
#include <data/format/formats.hpp>
#include <data/set.hpp>
DECLARE_POINTER_TYPE(FileFormat);
DECLARE_TYPEOF_COLLECTION(FileFormatP);
// ----------------------------------------------------------------------------- : Formats
// All supported file formats
@@ -49,11 +46,11 @@ String export_formats(const Game& game) {
}
void export_set(Set& set, const String& filename, size_t format_index, bool is_copy) {
FileFormatP format = file_formats.at(format_index);
if (!format->canExport(*set.game)) {
FileFormat& format = *file_formats.at(format_index);
if (!format.canExport(*set.game)) {
throw InternalError(_("File format doesn't apply to set"));
}
format->exportSet(set, filename, is_copy);
format.exportSet(set, filename, is_copy);
}
SetP import_set(String name) {
+2 -1
View File
@@ -16,7 +16,6 @@
class Game;
DECLARE_POINTER_TYPE(Set);
DECLARE_POINTER_TYPE(Card);
DECLARE_POINTER_TYPE(FileFormat);
// ----------------------------------------------------------------------------- : FileFormat
@@ -47,6 +46,8 @@ class FileFormat : public IntrusivePtrVirtualBase {
}
};
using FileFormatP = unique_ptr<FileFormat>;
// ----------------------------------------------------------------------------- : Formats
/// Initialize the list of file formats
+2 -2
View File
@@ -187,10 +187,10 @@ SymbolShapeP read_symbol_shape(const ImageData& data) {
}
// add to shape and place a mark
shape->points.push_back(intrusive(new ControlPoint(
shape->points.push_back(make_intrusive<ControlPoint>(
double(x) / data.width,
double(y) / data.height
)));
));
if (x > old_x) data(old_x, y) |= MARKED; // mark when moving right -> only mark the top of the shape
last_move = (x + y) - (old_x + old_y);
old_x = x;
+1 -1
View File
@@ -30,7 +30,7 @@ class MSE1FileFormat : public FileFormat {
};
FileFormatP mse1_file_format() {
return intrusive(new MSE1FileFormat());
return make_unique<MSE1FileFormat>();
}
// ----------------------------------------------------------------------------- : Importing
+1 -1
View File
@@ -45,5 +45,5 @@ class MSE2FileFormat : public FileFormat {
};
FileFormatP mse2_file_format() {
return intrusive(new MSE2FileFormat());
return make_unique<MSE2FileFormat>();
}
+3 -3
View File
@@ -44,7 +44,7 @@ class MtgEditorFileFormat : public FileFormat {
};
FileFormatP mtg_editor_file_format() {
return intrusive(new MtgEditorFileFormat());
return make_unique<MtgEditorFileFormat>();
}
// ----------------------------------------------------------------------------- : Importing
@@ -69,7 +69,7 @@ SetP MtgEditorFileFormat::importSet(const String& filename) {
// read file
while (!f.Eof()) {
// read a line
if (!current_card) current_card = intrusive(new Card(*set->game));
if (!current_card) current_card = make_intrusive<Card>(*set->game);
String line = file.ReadLine();
if (line == _("#SET###########")) { // set.title
target = &set->value<TextValue>(_("title")).value;
@@ -104,7 +104,7 @@ SetP MtgEditorFileFormat::importSet(const String& filename) {
set->cards.push_back(current_card);
}
first = false;
current_card = intrusive(new Card(*set->game));
current_card = make_intrusive<Card>(*set->game);
target = &current_card->value<TextValue>(_("name")).value;
} else if (line == _("#DATE##########")) { // date
// remember date for generation of illustration filename
+2 -2
View File
@@ -73,7 +73,7 @@ void Game::validate(Version v) {
vector<StatsDimensionP> dims;
FOR_EACH(f, card_fields) {
if (f->show_statistics) {
dims.push_back(intrusive(new StatsDimension(*f)));
dims.push_back(make_intrusive<StatsDimension>(*f));
}
}
statistics_dimensions.insert(statistics_dimensions.begin(), dims.begin(), dims.end()); // push front
@@ -82,7 +82,7 @@ void Game::validate(Version v) {
{
vector<StatsCategoryP> cats;
FOR_EACH(dim, statistics_dimensions) {
cats.push_back(intrusive(new StatsCategory(dim)));
cats.push_back(make_intrusive<StatsCategory>(dim));
}
statistics_categories.insert(statistics_categories.begin(), cats.begin(), cats.end()); // push front
}
+9 -9
View File
@@ -105,12 +105,12 @@ void Installer::install(bool local, bool check_dependencies) {
}
PackagedP pack;
wxString fn(wxFileName(p).GetExt());
if (fn == _("mse-game")) pack = intrusive(new Game());
else if (fn == _("mse-style")) pack = intrusive(new StyleSheet());
else if (fn == _("mse-locale")) pack = intrusive(new Locale());
else if (fn == _("mse-include")) pack = intrusive(new IncludePackage());
else if (fn == _("mse-symbol-font")) pack = intrusive(new SymbolFont());
else if (fn == _("mse-export-template")) pack = intrusive(new ExportTemplate());
if (fn == _("mse-game")) pack = make_intrusive<Game>();
else if (fn == _("mse-style")) pack = make_intrusive<StyleSheet>();
else if (fn == _("mse-locale")) pack = make_intrusive<Locale>();
else if (fn == _("mse-include")) pack = make_intrusive<IncludePackage>();
else if (fn == _("mse-symbol-font")) pack = make_intrusive<SymbolFont>();
else if (fn == _("mse-export-template")) pack = make_intrusive<ExportTemplate>();
else {
throw PackageError(_("Unrecognized package type: '") + fn + _("'\nwhile trying to install: ") + p);
}
@@ -186,7 +186,7 @@ void Installer::addPackage(Packaged& package) {
return; // already added
}
}
packages.push_back(intrusive(new PackageDescription(package)));
packages.push_back(make_intrusive<PackageDescription>(package));
// use this as a filename?
if (prefered_filename.empty()) {
prefered_filename = package.name() + _(".mse-installer");
@@ -387,7 +387,7 @@ void merge(InstallablePackages& list1, const InstallablePackages& list2) {
void merge(InstallablePackages& installed, const DownloadableInstallerP& installer) {
InstallablePackages ips;
FOR_EACH(p, installer->packages) {
ips.push_back(intrusive(new InstallablePackage(p,installer)));
ips.push_back(make_intrusive<InstallablePackage>(p,installer));
}
sort(ips);
merge(installed, ips);
@@ -625,5 +625,5 @@ InstallablePackageP mse_installable_package() {
mse_description->position_hint = -100;
mse_description->icon = load_resource_image(_("installer_program"));
//mse_description->description = _LABEL_("magic set editor package");
return intrusive(new InstallablePackage(mse_description, mse_version));
return make_intrusive<InstallablePackage>(mse_description, mse_version);
}
+1 -1
View File
@@ -77,7 +77,7 @@ SubLocaleP find_wildcard(map<String,SubLocaleP>& items, const String& name) {
FOR_EACH_CONST(i, items) {
if (i.second && match_wildcard(i.first, name)) return i.second;
}
return intrusive(new SubLocale()); // so we don't search again
return make_intrusive<SubLocale>(); // so we don't search again
}
SubLocaleP find_wildcard_and_set(map<String,SubLocaleP>& items, const String& name) {
return items[name] = find_wildcard(items, name);
+6 -6
View File
@@ -29,13 +29,13 @@ DECLARE_TYPEOF_NO_REV(IndexMap<FieldP COMMA ValueP>);
// ----------------------------------------------------------------------------- : Set
Set::Set()
: vcs (intrusive(new VCS()))
: vcs (make_intrusive<VCS>())
, script_manager(new SetScriptManager(*this))
{}
Set::Set(const GameP& game)
: game(game)
, vcs (intrusive(new VCS()))
, vcs (make_intrusive<VCS>())
, script_manager(new SetScriptManager(*this))
{
data.init(game->set_fields);
@@ -44,7 +44,7 @@ Set::Set(const GameP& game)
Set::Set(const StyleSheetP& stylesheet)
: game(stylesheet->game)
, stylesheet(stylesheet)
, vcs (intrusive(new VCS()))
, vcs (make_intrusive<VCS>())
, script_manager(new SetScriptManager(*this))
{
data.init(game->set_fields);
@@ -164,7 +164,7 @@ void Set::validate(Version file_app_version) {
}
*/ }
// we want at least one card
if (cards.empty()) cards.push_back(intrusive(new Card(*game)));
if (cards.empty()) cards.push_back(make_intrusive<Card>(*game));
// update scripts
script_manager->updateAll();
}
@@ -234,7 +234,7 @@ void Set::reflect_cards<Writer> (Writer& tag) {
// ----------------------------------------------------------------------------- : Script utilities
ScriptValueP make_iterator(const Set& set) {
return intrusive(new ScriptCollectionIterator<vector<CardP> >(&set.cards));
return make_intrusive<ScriptCollectionIterator<vector<CardP>>>(&set.cards);
}
void mark_dependency_member(const Set& set, const String& name, const Dependency& dep) {
@@ -279,7 +279,7 @@ int Set::positionOfCard(const CardP& card, const ScriptValueP& order_by, const S
Profiler prof(t, order_by.get(), _("init order cache"));
#endif
// 3. initialize order cache
order = intrusive(new OrderCache<CardP>(cards, values, filter ? &keep : nullptr));
order = make_intrusive<OrderCache<CardP>>(cards, values, filter ? &keep : nullptr);
}
return order->find(card);
}
+4 -4
View File
@@ -203,7 +203,7 @@ void Settings::addRecentFile(const String& filename) {
GameSettings& Settings::gameSettingsFor(const Game& game) {
GameSettingsP& gs = game_settings[game.name()];
if (!gs) gs = intrusive(new GameSettings);
if (!gs) gs = make_intrusive<GameSettings>();
gs->initDefaults(game);
return *gs;
}
@@ -222,7 +222,7 @@ ColumnSettings& Settings::columnSettingsFor(const Game& game, const Field& field
}
StyleSheetSettings& Settings::stylesheetSettingsFor(const StyleSheet& stylesheet) {
StyleSheetSettingsP& ss = stylesheet_settings[stylesheet.name()];
if (!ss) ss = intrusive(new StyleSheetSettings);
if (!ss) ss = make_intrusive<StyleSheetSettings>();
ss->useDefault(default_stylesheet_settings); // update default settings
return *ss;
}
@@ -295,7 +295,7 @@ void Settings::read() {
String filename = settingsFile();
if (wxFileExists(filename)) {
// settings 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);
@@ -303,6 +303,6 @@ void Settings::read() {
}
void Settings::write() {
Writer writer(shared(new wxFileOutputStream(settingsFile())), app_version);
Writer writer(make_shared<wxFileOutputStream>(settingsFile()), app_version);
writer.handle(*this);
}
+10 -10
View File
@@ -139,9 +139,9 @@ SymbolPartP read_new<SymbolPart>(Reader& reader) {
// there must be a type specified
String type;
reader.handle(_("type"), type);
if (type == _("shape") || type.empty()) return intrusive(new SymbolShape);
else if (type == _("symmetry")) return intrusive(new SymbolSymmetry);
else if (type == _("group")) return intrusive(new SymbolGroup);
if (type == _("shape") || type.empty()) return make_intrusive<SymbolShape>();
else if (type == _("symmetry")) return make_intrusive<SymbolSymmetry>();
else if (type == _("group")) return make_intrusive<SymbolGroup>();
else {
throw ParseError(_("Unsupported symbol part type: '") + type + _("'"));
}
@@ -200,7 +200,7 @@ SymbolPartP SymbolShape::clone() const {
SymbolShapeP part(new SymbolShape(*this));
// also clone the control points
FOR_EACH(p, part->points) {
p = intrusive(new ControlPoint(*p));
p = make_intrusive<ControlPoint>(*p);
}
return part;
}
@@ -357,18 +357,18 @@ double Symbol::aspectRatio() const {
// A default symbol part, a square, moved by d
SymbolShapeP default_symbol_part(double d) {
SymbolShapeP part = intrusive(new SymbolShape);
part->points.push_back(intrusive(new ControlPoint(d + .2, d + .2)));
part->points.push_back(intrusive(new ControlPoint(d + .2, d + .8)));
part->points.push_back(intrusive(new ControlPoint(d + .8, d + .8)));
part->points.push_back(intrusive(new ControlPoint(d + .8, d + .2)));
SymbolShapeP part = make_intrusive<SymbolShape>();
part->points.push_back(make_intrusive<ControlPoint>(d + .2, d + .2));
part->points.push_back(make_intrusive<ControlPoint>(d + .2, d + .8));
part->points.push_back(make_intrusive<ControlPoint>(d + .8, d + .8));
part->points.push_back(make_intrusive<ControlPoint>(d + .8, d + .2));
part->name = _("Square");
return part;
}
// A default symbol, a square
SymbolP default_symbol() {
SymbolP symbol = intrusive(new Symbol);
auto symbol = make_intrusive<Symbol>();
symbol->parts.push_back(default_symbol_part(0));
return symbol;
}
+1 -1
View File
@@ -54,7 +54,7 @@ class AutoReplace : public IntrusivePtrVirtualBase {
String match;
String replace;
inline AutoReplaceP clone() const { return intrusive(new AutoReplace(*this)); }
inline AutoReplaceP clone() const { return make_intrusive<AutoReplace>(*this); }
DECLARE_REFLECTION();
};