Skip to content

Commit

Permalink
Materials: Observe MaterialManifest deletion
Browse files Browse the repository at this point in the history
To ensure the manifest groups are always valid.
  • Loading branch information
danij-deng committed Mar 3, 2013
1 parent bce1f5a commit bf3305e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doomsday/client/include/resource/materialmanifest.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class MaterialManifest : public PathTree::Node,
/// Required material instance is missing. @ingroup errors
DENG2_ERROR(MissingMaterialError);

DENG2_DEFINE_AUDIENCE(Deletion, void manifestBeingDeleted(MaterialManifest const &manifest))

DENG2_DEFINE_AUDIENCE(MaterialDerived, void manifestMaterialDerived(MaterialManifest &manifest, Material &material))

enum Flag
Expand Down
11 changes: 8 additions & 3 deletions doomsday/client/include/resource/materials.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace de {
*/
class Materials : DENG2_OBSERVES(MaterialScheme, ManifestDefined),
DENG2_OBSERVES(MaterialManifest, MaterialDerived),
DENG2_OBSERVES(MaterialManifest, Deletion),
DENG2_OBSERVES(Material, Deletion)
{
/// Internal typedefs for brevity/cleanliness.
Expand Down Expand Up @@ -165,7 +166,8 @@ class Materials : DENG2_OBSERVES(MaterialScheme, ManifestDefined),
inline int schemeCount() const { return allSchemes().count(); }

/**
* Clear all materials in all schemes.
* Clear all manifests and materials in all schemes.
*
* @see allSchemes(), Scheme::clear().
*/
inline void clearAllSchemes()
Expand Down Expand Up @@ -287,10 +289,13 @@ class Materials : DENG2_OBSERVES(MaterialScheme, ManifestDefined),

protected:
// Observes Scheme ManifestDefined.
void schemeManifestDefined(MaterialScheme &scheme, MaterialManifest &manifest);
void schemeManifestDefined(Scheme &scheme, Manifest &manifest);

// Observes Manifest Deletion.
void manifestBeingDeleted(Manifest const &manifest);

// Observes Manifest MaterialDerived.
void manifestMaterialDerived(MaterialManifest &manifest, Material &material);
void manifestMaterialDerived(Manifest &manifest, Material &material);

// Observes Material Deletion.
void materialBeingDeleted(Material const &material);
Expand Down
2 changes: 2 additions & 0 deletions doomsday/client/src/resource/materialmanifest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ MaterialManifest::MaterialManifest(PathTree::NodeArgs const &args)

MaterialManifest::~MaterialManifest()
{
DENG2_FOR_AUDIENCE(Deletion, i) i->manifestBeingDeleted(*this);

delete d;
}

Expand Down
11 changes: 11 additions & 0 deletions doomsday/client/src/resource/materials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ void Materials::schemeManifestDefined(MaterialScheme &scheme, MaterialManifest &
// We want notification when the manifest is derived to produce a material.
manifest.audienceForMaterialDerived += this;

// We want notification when the manifest is about to be deleted.
manifest.audienceForDeletion += this;

// Acquire a new unique identifier for the manifest.
materialid_t const id = ++d->manifestCount; // 1-based.
manifest.setId(id);
Expand All @@ -352,6 +355,14 @@ void Materials::manifestMaterialDerived(MaterialManifest &manifest, Material &ma
material.audienceForDeletion += this;
}

void Materials::manifestBeingDeleted(MaterialManifest const &manifest)
{
foreach(ManifestGroup *group, d->groups)
{
group->remove(const_cast<Manifest *>(&manifest));
}
}

void Materials::materialBeingDeleted(Material const &material)
{
d->materials.removeOne(const_cast<Material *>(&material));
Expand Down

0 comments on commit bf3305e

Please sign in to comment.