Skip to content

Commit

Permalink
Fixed values returned from methods to have CppOwnership
Browse files Browse the repository at this point in the history
In this case, the returned objects default to JavaScript ownership,
which we wanted to avoid to work around the garbage collection related
issue.
  • Loading branch information
bjorn committed Nov 15, 2023
1 parent 7d10168 commit cf20924
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 15 deletions.
19 changes: 13 additions & 6 deletions src/tiled/editablelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,30 @@ EditableLayer *EditableLayer::get(EditableMap *map, Layer *layer)
if (!layer)
return nullptr;

if (auto editable = find(layer))
auto editable = find(layer);
if (editable)
return editable;

Q_ASSERT(!map || layer->map() == map->map());

switch (layer->layerType()) {
case Layer::TileLayerType:
return new EditableTileLayer(map, static_cast<TileLayer*>(layer));
editable = new EditableTileLayer(map, static_cast<TileLayer*>(layer));
break;
case Layer::ObjectGroupType:
return new EditableObjectGroup(map, static_cast<ObjectGroup*>(layer));
editable = new EditableObjectGroup(map, static_cast<ObjectGroup*>(layer));
break;
case Layer::ImageLayerType:
return new EditableImageLayer(map, static_cast<ImageLayer*>(layer));
editable = new EditableImageLayer(map, static_cast<ImageLayer*>(layer));
break;
case Layer::GroupLayerType:
return new EditableGroupLayer(map, static_cast<GroupLayer*>(layer));
editable = new EditableGroupLayer(map, static_cast<GroupLayer*>(layer));
break;
}

return nullptr;
editable->moveOwnershipToCpp();

return editable;
}

void EditableLayer::release(Layer *layer)
Expand Down
7 changes: 5 additions & 2 deletions src/tiled/editablemapobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,15 @@ EditableMapObject *EditableMapObject::get(EditableAsset *asset, MapObject *mapOb
if (!mapObject)
return nullptr;

if (auto editable = EditableMapObject::find(mapObject))
auto editable = EditableMapObject::find(mapObject);
if (editable)
return editable;

Q_ASSERT(mapObject->objectGroup());

return new EditableMapObject(asset, mapObject);
editable = new EditableMapObject(asset, mapObject);
editable->moveOwnershipToCpp();
return editable;
}

void EditableMapObject::release(MapObject *mapObject)
Expand Down
4 changes: 3 additions & 1 deletion src/tiled/editableobjectgroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ EditableObjectGroup *EditableObjectGroup::get(EditableAsset *asset, ObjectGroup
if (auto editable = EditableLayer::find(objectGroup))
return static_cast<EditableObjectGroup*>(editable);

return new EditableObjectGroup(asset, objectGroup);
auto editable = new EditableObjectGroup(asset, objectGroup);
editable->moveOwnershipToCpp();
return editable;
}

void EditableObjectGroup::setColor(const QColor &color)
Expand Down
7 changes: 5 additions & 2 deletions src/tiled/editabletile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,13 @@ EditableTile *EditableTile::get(EditableTileset *tileset, Tile *tile)
Q_ASSERT(tile);
Q_ASSERT(tile->tileset() == tileset->tileset());

if (auto editable = EditableTile::find(tile))
auto editable = EditableTile::find(tile);
if (editable)
return editable;

return new EditableTile(tileset, tile);
editable = new EditableTile(tileset, tile);
editable->moveOwnershipToCpp();
return editable;
}

void EditableTile::setImageFileName(const QString &fileName)
Expand Down
7 changes: 5 additions & 2 deletions src/tiled/editabletileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,13 @@ EditableTileset *EditableTileset::get(Tileset *tileset)
if (auto document = TilesetDocument::findDocumentForTileset(tileset->sharedFromThis()))
return document->editable();

if (auto editable = EditableTileset::find(tileset))
auto editable = EditableTileset::find(tileset);
if (editable)
return editable;

return new EditableTileset(tileset);
editable = new EditableTileset(tileset);
editable->moveOwnershipToCpp();
return editable;
}

void EditableTileset::setName(const QString &name)
Expand Down
7 changes: 5 additions & 2 deletions src/tiled/editablewangset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,13 @@ EditableWangSet *EditableWangSet::get(EditableTileset *tileset, WangSet *wangSet
Q_ASSERT(wangSet);
Q_ASSERT(wangSet->tileset() == tileset->tileset());

if (auto editable = EditableWangSet::find(wangSet))
auto editable = EditableWangSet::find(wangSet);
if (editable)
return editable;

return new EditableWangSet(tileset, wangSet);
editable = new EditableWangSet(tileset, wangSet);
editable->moveOwnershipToCpp();
return editable;
}

/**
Expand Down

0 comments on commit cf20924

Please sign in to comment.