Skip to content

Commit

Permalink
Refactor|Resources: Hide internal resource collection mechanics at AP…
Browse files Browse the repository at this point in the history
…I level
  • Loading branch information
danij-deng committed Nov 17, 2013
1 parent 67b00b7 commit 20a5b2a
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 205 deletions.
19 changes: 1 addition & 18 deletions doomsday/client/include/resource/fonts.h
Expand Up @@ -26,7 +26,6 @@
#include "FontScheme"
#include "uri.hh"
#include <de/Error>
#include <de/Observers>
#include <de/String>
#include <QList>
#include <QMap>
Expand All @@ -46,10 +45,7 @@ namespace de {
*
* @ingroup resource
*/
class Fonts :
DENG2_OBSERVES(FontScheme, ManifestDefined),
DENG2_OBSERVES(FontManifest, Deletion),
DENG2_OBSERVES(AbstractFont, Deletion)
class Fonts
{
/// Internal typedefs for brevity/cleanliness.
typedef class FontManifest Manifest;
Expand Down Expand Up @@ -194,19 +190,6 @@ DENG2_OBSERVES(AbstractFont, Deletion)
*/
All const &all() const;

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

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

// Observes Manifest FontDerived.
//void manifestFontDerived(Manifest &manifest, AbstractFont &font);

// Observes AbstractFont Deletion.
void fontBeingDeleted(AbstractFont const &font);

private:
DENG2_PRIVATE(d)
};
Expand Down
20 changes: 1 addition & 19 deletions doomsday/client/include/resource/materials.h
Expand Up @@ -29,7 +29,6 @@
#endif
#include "uri.hh"
#include <de/Error>
#include <de/Observers>
#include <QList>
#include <QMap>
#include <QSet>
Expand Down Expand Up @@ -60,11 +59,7 @@ namespace de {
*
* @ingroup resource
*/
class Materials :
DENG2_OBSERVES(MaterialScheme, ManifestDefined),
DENG2_OBSERVES(MaterialManifest, MaterialDerived),
DENG2_OBSERVES(MaterialManifest, Deletion),
DENG2_OBSERVES(Material, Deletion)
class Materials
{
/// Internal typedefs for brevity/cleanliness.
typedef class MaterialScheme Scheme;
Expand Down Expand Up @@ -297,19 +292,6 @@ DENG2_OBSERVES(Material, Deletion)

#endif // __CLIENT__

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

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

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

// Observes Material Deletion.
void materialBeingDeleted(Material const &material);

private:
DENG2_PRIVATE(d)
};
Expand Down
24 changes: 5 additions & 19 deletions doomsday/client/include/resource/textures.h
@@ -1,4 +1,4 @@
/** @file textures.h Texture Resource Collection.
/** @file textures.h Texture Resource Collection.
*
* @authors Copyright © 2010-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2010-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
Expand All @@ -18,15 +18,14 @@
* 02110-1301 USA</small>
*/

#ifndef LIBDENG_RESOURCE_TEXTURES_H
#define LIBDENG_RESOURCE_TEXTURES_H
#ifndef DENG_RESOURCE_TEXTURES_H
#define DENG_RESOURCE_TEXTURES_H

#include "Texture"
#include "TextureManifest"
#include "TextureScheme"
#include "uri.hh"
#include <de/Error>
#include <de/Observers>
#include <de/Vector>
#include <QList>
#include <QMap>
Expand All @@ -43,10 +42,7 @@ namespace de {
*
* @ingroup resource
*/
class Textures :
DENG2_OBSERVES(TextureScheme, ManifestDefined),
DENG2_OBSERVES(TextureManifest, TextureDerived),
DENG2_OBSERVES(Texture, Deletion)
class Textures
{
/// Internal typedefs for brevity/cleanliness.
typedef class TextureManifest Manifest;
Expand Down Expand Up @@ -169,20 +165,10 @@ DENG2_OBSERVES(Texture, Deletion)
*/
All const &all() const;

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

// Observes Manifest TextureDerived.
void manifestTextureDerived(Manifest &manifest, Texture &texture);

// Observes Texture Deletion.
void textureBeingDeleted(Texture const &texture);

private:
DENG2_PRIVATE(d)
};

} // namespace de

#endif /* LIBDENG_RESOURCE_TEXTURES_H */
#endif // DENG_RESOURCE_TEXTURES_H
124 changes: 63 additions & 61 deletions doomsday/client/src/resource/fonts.cpp
Expand Up @@ -24,13 +24,17 @@
#include "de_console.h"
#include "dd_main.h" // App_Fonts(), verbose
#include <de/Log>
#include <de/Observers>
#include <de/memory.h>
#include <QList>
#include <QtAlgorithms>

namespace de {

DENG2_PIMPL_NOREF(Fonts)
DENG2_PIMPL(Fonts),
DENG2_OBSERVES(FontScheme, ManifestDefined),
DENG2_OBSERVES(FontManifest, Deletion),
DENG2_OBSERVES(AbstractFont, Deletion)
{
/// System subspace schemes containing the fonts.
Schemes schemes;
Expand All @@ -42,24 +46,19 @@ DENG2_PIMPL_NOREF(Fonts)
uint manifestIdMapSize;
FontManifest **manifestIdMap; ///< Index with fontid_t-1

Instance()
: manifestCount(0)
Instance(Public *i)
: Base(i)
, manifestCount(0)
, manifestIdMapSize(0)
, manifestIdMap(0)
{}

~Instance()
{
clearFonts();
self.clearAllSchemes();
clearManifests();
}

void clearFonts()
{
qDeleteAll(fonts);
fonts.clear();
}

void clearManifests()
{
qDeleteAll(schemes);
Expand All @@ -74,9 +73,59 @@ DENG2_PIMPL_NOREF(Fonts)
}
manifestCount = 0;
}

/// Observes Scheme ManifestDefined.
void schemeManifestDefined(Scheme & /*scheme*/, Manifest &manifest)
{
// We want notification when the manifest is derived to produce a resource.
//manifest.audienceForFontDerived += this;

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

// Acquire a new unique identifier for the manifest.
fontid_t const id = ++manifestCount; // 1-based.
manifest.setUniqueId(id);

// Add the new manifest to the id index/map.
if(manifestCount > manifestIdMapSize)
{
// Allocate more memory.
manifestIdMapSize += 32;
manifestIdMap = (Manifest **) M_Realloc(manifestIdMap, sizeof(*manifestIdMap) * manifestIdMapSize);
}
manifestIdMap[manifestCount - 1] = &manifest;
}

#if 0
/// Observes Manifest FontDerived.
void manifestFontDerived(Manifest & /*manifest*/, AbstractFont &font)
{
// Include this new font in the scheme-agnostic list of instances.
fonts.push_back(&font);

// We want notification when the font is about to be deleted.
font.audienceForDeletion += this;
}
#endif

/// Observes Manifest Deletion.
void manifestBeingDeleted(Manifest const &manifest)
{
manifestIdMap[manifest.uniqueId() - 1 /*1-based*/] = 0;

// There will soon be one fewer manifest in the system.
manifestCount -= 1;
}

/// Observes AbstractFont Deletion.
void fontBeingDeleted(AbstractFont const &font)
{
fonts.removeOne(const_cast<AbstractFont *>(&font));
}
};

Fonts::Fonts() : d(new Instance)
Fonts::Fonts() : d(new Instance(this))
{}

Fonts::Scheme &Fonts::scheme(String name) const
Expand All @@ -93,7 +142,7 @@ Fonts::Scheme &Fonts::scheme(String name) const

Fonts::Scheme &Fonts::createScheme(String name)
{
DENG_ASSERT(name.length() >= Scheme::min_name_length);
DENG2_ASSERT(name.length() >= Scheme::min_name_length);

// Ensure this is a unique name.
if(knownScheme(name)) return scheme(name);
Expand All @@ -104,7 +153,7 @@ Fonts::Scheme &Fonts::createScheme(String name)
d->schemeCreationOrder.push_back(newScheme);

// We want notification when a new manifest is defined in this scheme.
newScheme->audienceForManifestDefined += this;
newScheme->audienceForManifestDefined += d;

return *newScheme;
}
Expand All @@ -113,8 +162,7 @@ bool Fonts::knownScheme(String name) const
{
if(!name.isEmpty())
{
Schemes::iterator found = d->schemes.find(name.toLower());
if(found != d->schemes.end()) return true;
return d->schemes.contains(name.toLower());
}
return false;
}
Expand Down Expand Up @@ -211,52 +259,6 @@ Fonts::Manifest &Fonts::toManifest(fontid_t id) const
throw UnknownIdError("Fonts::toManifest", QString("Invalid font ID %1, valid range [1..%2)").arg(id).arg(d->manifestCount + 1));
}

void Fonts::schemeManifestDefined(Scheme & /*scheme*/, Manifest &manifest)
{
// We want notification when the manifest is derived to produce a resource.
//manifest.audienceForFontDerived += this;

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

// Acquire a new unique identifier for the manifest.
fontid_t const id = ++d->manifestCount; // 1-based.
manifest.setUniqueId(id);

// Add the new manifest to the id index/map.
if(d->manifestCount > d->manifestIdMapSize)
{
// Allocate more memory.
d->manifestIdMapSize += 32;
d->manifestIdMap = (Manifest **) M_Realloc(d->manifestIdMap, sizeof(*d->manifestIdMap) * d->manifestIdMapSize);
}
d->manifestIdMap[d->manifestCount - 1] = &manifest;
}

#if 0
void Fonts::manifestFontDerived(Manifest & /*manifest*/, AbstractFont &font)
{
// Include this new font in the scheme-agnostic list of instances.
d->fonts.push_back(&font);

// We want notification when the font is about to be deleted.
font.audienceForDeletion += this;
}
#endif

void Fonts::manifestBeingDeleted(Manifest const &manifest)
{
d->manifestIdMap[manifest.uniqueId() - 1 /*1-based*/] = 0;

// There will soon be one fewer manifest in the system.
d->manifestCount -= 1;
}

void Fonts::fontBeingDeleted(AbstractFont const &font)
{
d->fonts.removeOne(const_cast<AbstractFont *>(&font));
}

Fonts::All const &Fonts::all() const
{
return d->fonts;
Expand Down

0 comments on commit 20a5b2a

Please sign in to comment.