Skip to content

Commit

Permalink
libdeng2: Renamed DependAssets to AssetGroup, dropped SuspendTime policy
Browse files Browse the repository at this point in the history
AssetGroup is a more descriptive and simpler name.

The SuspendTime policy was not abstract/generic enough -- the correct
way to do this is to have an AssetGroup in de::Clock for determining
when clock time can advance: if a Required asset is not ready, time
will not advance.
  • Loading branch information
skyjake committed Apr 18, 2013
1 parent f9cdb0c commit e2b5dc6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 42 deletions.
22 changes: 9 additions & 13 deletions doomsday/libdeng2/include/de/core/asset.h
Expand Up @@ -76,27 +76,26 @@ class DENG2_PUBLIC Asset
* pools of dependencies, and quickly check whether all the required
* dependencies are currently available.
*
* DependAssets is derived from Asset so it is possible to group assets
* AssetGroup is derived from Asset so it is possible to group assets
* together and depend on the groups as a whole.
*
* @todo Any better name for this class?
*/
class DENG2_PUBLIC DependAssets : public Asset,
DENG2_OBSERVES(Asset, Deletion),
DENG2_OBSERVES(Asset, StateChange)
class DENG2_PUBLIC AssetGroup : public Asset,
DENG2_OBSERVES(Asset, Deletion),
DENG2_OBSERVES(Asset, StateChange)
{
public:
enum Policy {
Ignore, ///< State of the asset should be ignored.
Required, ///< Dependents cannot operate without the asset.
SuspendTime ///< Time cannot advance without the asset.
Required ///< Dependents cannot operate without the asset.
};

typedef std::map<Asset const *, Policy> Dependencies;
typedef std::map<Asset const *, Policy> Members;

public:
DependAssets();
virtual ~DependAssets();
AssetGroup();
virtual ~AssetGroup();

int size() const;

Expand All @@ -106,7 +105,7 @@ class DENG2_PUBLIC DependAssets : public Asset,

void insert(Asset const &dep, Policy policy = Required);

DependAssets &operator += (Asset const &dep) {
AssetGroup &operator += (Asset const &dep) {
insert(dep, Required);
return *this;
}
Expand All @@ -119,9 +118,6 @@ class DENG2_PUBLIC DependAssets : public Asset,

Dependencies const &all() const;

/// Determines if any of the time-suspending assets are not ready.
bool mustSuspendTime() const;

// Observes contained Assets.
void assetDeleted(Asset &);
void assetStateChanged(Asset &);
Expand Down
45 changes: 16 additions & 29 deletions doomsday/libdeng2/src/core/asset.cpp
Expand Up @@ -50,21 +50,20 @@ bool Asset::isReady() const

//----------------------------------------------------------------------------

DENG2_PIMPL_NOREF(DependAssets)
DENG2_PIMPL_NOREF(AssetGroup)
{
Dependencies deps;
Members deps;

/**
* Determines if all the assets in the group are ready.
*/
bool allReady() const
{
DENG2_FOR_EACH_CONST(Dependencies, i, deps)
DENG2_FOR_EACH_CONST(Members, i, deps)
{
switch(i->second)
{
case Required:
case SuspendTime:
if(!i->first->isReady()) return false;
break;

Expand All @@ -76,34 +75,34 @@ DENG2_PIMPL_NOREF(DependAssets)
return true;
}

void update(DependAssets &self)
void update(AssetGroup &self)
{
self.setState(allReady()? Ready : NotReady);
}
};

DependAssets::DependAssets() : d(new Instance)
AssetGroup::AssetGroup() : d(new Instance)
{
// An empty set of dependencies means the group is Ready.
// An empty set of members means the group is Ready.
setState(Ready);
}

DependAssets::~DependAssets()
AssetGroup::~AssetGroup()
{
// We are about to be deleted.
audienceForStateChange.clear();

clear();
}

int DependAssets::size() const
int AssetGroup::size() const
{
return d->deps.size();
}

void DependAssets::clear()
void AssetGroup::clear()
{
DENG2_FOR_EACH(Dependencies, i, d->deps)
DENG2_FOR_EACH(Members, i, d->deps)
{
i->first->audienceForDeletion -= this;
}
Expand All @@ -112,51 +111,39 @@ void DependAssets::clear()
d->update(*this);
}

void DependAssets::insert(Asset const &asset, Policy policy)
void AssetGroup::insert(Asset const &asset, Policy policy)
{
d->deps[&asset] = policy;
asset.audienceForDeletion += this;
d->update(*this);
}

void DependAssets::remove(Asset const &asset)
void AssetGroup::remove(Asset const &asset)
{
asset.audienceForDeletion -= this;
d->deps.erase(&asset);
d->update(*this);
}

bool DependAssets::has(Asset const &asset) const
bool AssetGroup::has(Asset const &asset) const
{
return d->deps.find(&asset) != d->deps.end();
}

void DependAssets::setPolicy(Asset const &asset, Policy policy)
void AssetGroup::setPolicy(Asset const &asset, Policy policy)
{
DENG2_ASSERT(d->deps.find(&asset) != d->deps.end());

d->deps[&asset] = policy;
d->update(*this);
}

bool DependAssets::mustSuspendTime() const
{
DENG2_FOR_EACH_CONST(Dependencies, i, d->deps)
{
if(i->second == SuspendTime && !i->first->isReady())
{
return true;
}
}
return false;
}

void DependAssets::assetDeleted(Asset &asset)
void AssetGroup::assetDeleted(Asset &asset)
{
remove(asset);
}

void DependAssets::assetStateChanged(Asset &)
void AssetGroup::assetStateChanged(Asset &)
{
d->update(*this);
}
Expand Down

0 comments on commit e2b5dc6

Please sign in to comment.