Skip to content

Commit

Permalink
Refactor|AbstractFile: Return const references to lump infos and dire…
Browse files Browse the repository at this point in the history
…ctory nodes
  • Loading branch information
danij-deng committed Oct 7, 2012
1 parent d271dd7 commit 53ebf9f
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 161 deletions.
49 changes: 42 additions & 7 deletions doomsday/engine/portable/include/abstractfile.h
Expand Up @@ -89,7 +89,7 @@ class AbstractFile
/**
* @return Immutable copy of the info descriptor for this resource.
*/
LumpInfo const* info() const;
LumpInfo const& info() const;

/// @return @c true= this file is contained within another.
bool isContained() const;
Expand Down Expand Up @@ -128,19 +128,43 @@ class AbstractFile
* @todo Extract these into one or more interface classes/subcomponents.
*/

/**
* @return @c true= @a lumpIdx is a valid logical index for a lump in this file.
*
* @attention This default implementation assumes there is only one lump in
* the file. Subclasses with multiple lumps should override this function
* accordingly.
*/
virtual bool isValidIndex(int lumpIdx) { return lumpIdx == 0; }

/**
* @return Logical index of the last lump in this file's directory or @c -1 if empty.
*
* @attention This default implementation assumes there is only one lump in
* the file. Subclasses with multiple lumps should override this function
* accordingly.
*/
virtual int lastIndex() { return 0; }

/**
* @return Number of "lumps" contained within this resource.
*
* @attention This default implementation assumes there is only one lump in
* the file. Subclasses with multiple lumps should override this function
* accordingly.
*/
virtual int lumpCount() { return 1; }

/**
* Lookup a directory node for a lump contained by this file.
* Retrieve the directory node for a lump contained by this file.
*
* @param lumpIdx Logical index for the lump in this file's directory.
*
* @return Found directory node else @c NULL if @a lumpIdx is not valid.
* @return Directory node for this lump.
*
* @throws de::Error If @a lumpIdx is not valid.
*/
virtual PathDirectoryNode* lumpDirectoryNode(int lumpIdx) = 0;
virtual PathDirectoryNode const& lumpDirectoryNode(int lumpIdx) = 0;

/**
* Compose the absolute VFS path to a lump contained by this file.
Expand All @@ -156,13 +180,19 @@ class AbstractFile
virtual AutoStr* composeLumpPath(int lumpIdx, char delimiter = '/') = 0;

/**
* Lookup a lump info descriptor for a lump contained by this file.
* Retrieve the LumpInfo descriptor for a lump contained by this file.
*
* @param lumpIdx Logical index for the lump in this file's directory.
*
* @return Found lump info.
* @return Lump info descriptor for the lump.
*
* @throws de::Error If @a lumpIdx is not valid.
*
* @attention This default implementation assumes there is only one lump in
* the file and therefore its decriptor is that of the file itself. Subclasses
* with multiple lumps should override this function accordingly.
*/
virtual LumpInfo const* lumpInfo(int /*lumpIdx*/) { return info(); }
virtual LumpInfo const& lumpInfo(int /*lumpIdx*/) { return info(); }

/**
* Lookup the uncompressed size of lump contained by this file.
Expand All @@ -173,6 +203,11 @@ class AbstractFile
*
* @note This method is intended mainly for convenience. @see lumpInfo() for
* a better method of looking up multiple @ref LumpInfo properties.
*
* @attention This default implementation assumes there is only one lump in
* the file and therefore its decriptor is that of the file itself. Subclasses
* with multiple lumps should override this function accordingly.
*
*/
virtual size_t lumpSize(int lumpIdx) = 0;

Expand Down
17 changes: 5 additions & 12 deletions doomsday/engine/portable/include/genericfile.h
Expand Up @@ -47,13 +47,15 @@ class GenericFile : public AbstractFile
~GenericFile();

/**
* Lookup a directory node for a lump contained by this file.
* Retrieve the directory node for a lump contained by this file.
*
* @param lumpIdx Logical index for the lump in this file's directory.
*
* @return Found directory node else @c NULL if @a lumpIdx is not valid.
* @return Directory node for this lump.
*
* @throws de::Error If @a lumpIdx is not valid.
*/
PathDirectoryNode* lumpDirectoryNode(int lumpIdx);
PathDirectoryNode const& lumpDirectoryNode(int lumpIdx);

/**
* Compose the absolute VFS path to a lump contained by this file.
Expand All @@ -68,15 +70,6 @@ class GenericFile : public AbstractFile
*/
AutoStr* composeLumpPath(int lumpIdx, char delimiter = '/');

/**
* Lookup the lump info descriptor for this lump.
*
* @param lumpIdx Ignored. Required argument.
*
* @return Found lump info.
*/
LumpInfo const* lumpInfo(int lumpIdx);

/**
* Lookup the uncompressed size of lump contained by this file.
*
Expand Down
19 changes: 5 additions & 14 deletions doomsday/engine/portable/include/lumpfile.h
Expand Up @@ -53,13 +53,13 @@ class LumpFile : public AbstractFile
~LumpFile();

/**
* Lookup a directory node for a lump contained by this file.
* Retrieve the directory node for a lump contained by this file.
*
* @param lumpIdx Logical index for the lump in this file's directory.
* @param lumpIdx Ignored. Required argument.
*
* @return Found directory node else @c NULL if @a lumpIdx is not valid.
* @return Directory node for this lump.
*/
PathDirectoryNode* lumpDirectoryNode(int lumpIdx);
PathDirectoryNode const& lumpDirectoryNode(int lumpIdx);

/**
* Compose the absolute VFS path to a lump contained by this file.
Expand All @@ -74,15 +74,6 @@ class LumpFile : public AbstractFile
*/
AutoStr* composeLumpPath(int lumpIdx, char delimiter = '/');

/**
* Lookup the lump info descriptor for this lump.
*
* @param lumpIdx Ignored. Required argument.
*
* @return Found lump info.
*/
LumpInfo const* lumpInfo(int lumpIdx);

/**
* Lookup the uncompressed size of lump contained by this file.
*
Expand Down Expand Up @@ -167,4 +158,4 @@ struct lumpfile_s; // The lumpfile instance (opaque)
} // extern "C"
#endif

#endif /* LIBDENG_FILESYS_LUMPFILE_H */
#endif /* LIBDENG_FILESYS_LUMPFILE_H */
4 changes: 2 additions & 2 deletions doomsday/engine/portable/include/pathdirectory.h
Expand Up @@ -118,10 +118,10 @@ class PathDirectoryNode
* another PathDirectoryNode (possibly from another PathDirectory instance). This
* would allow for further optimizations in the file system (among others) -ds
*/
int matchDirectory(int flags, PathMap* candidatePath);
int matchDirectory(int flags, PathMap* candidatePath) const;

/// @return The path fragment which this node represents.
const ddstring_t* pathFragment() const;
ddstring_t const* pathFragment() const;

/**
* Composes and/or calculates the composed-length of the path for this node.
Expand Down
14 changes: 8 additions & 6 deletions doomsday/engine/portable/include/wadfile.h
Expand Up @@ -59,13 +59,15 @@ class WadFile : public AbstractFile
bool empty();

/**
* Lookup a directory node for a lump contained by this file.
* Retrieve the directory node for a lump contained by this file.
*
* @param lumpIdx Logical index for the lump in this file's directory.
*
* @return Found directory node else @c NULL if @a lumpIdx is not valid.
* @return Directory node for this lump.
*
* @throws de::Error If @a lumpIdx is not valid.
*/
PathDirectoryNode* lumpDirectoryNode(int lumpIdx);
PathDirectoryNode& lumpDirectoryNode(int lumpIdx);

/**
* Compose the absolute VFS path to a lump contained by this file.
Expand All @@ -81,15 +83,15 @@ class WadFile : public AbstractFile
AutoStr* composeLumpPath(int lumpIdx, char delimiter = '/');

/**
* Lookup a lump info descriptor for a lump contained by this file.
* Retrieve the LumpInfo descriptor for a lump contained by this file.
*
* @param lumpIdx Logical index for the lump in this file's directory.
*
* @return Found lump info.
* @return Lump info descriptor for the lump.
*
* @throws de::Error If @a lumpIdx is not valid.
*/
LumpInfo const* lumpInfo(int lumpIdx);
LumpInfo const& lumpInfo(int lumpIdx);

/**
* Lookup the uncompressed size of lump contained by this file.
Expand Down
14 changes: 8 additions & 6 deletions doomsday/engine/portable/include/zipfile.h
Expand Up @@ -61,13 +61,15 @@ class ZipFile : public AbstractFile
bool empty();

/**
* Lookup a directory node for a lump contained by this file.
* Retrieve the directory node for a lump contained by this file.
*
* @param lumpIdx Logical index for the lump in this file's directory.
*
* @return Found directory node else @c NULL if @a lumpIdx is not valid.
* @return Directory node for this lump.
*
* @throws de::Error If @a lumpIdx is not valid.
*/
PathDirectoryNode* lumpDirectoryNode(int lumpIdx);
PathDirectoryNode& lumpDirectoryNode(int lumpIdx);

/**
* Compose the absolute VFS path to a lump contained by this file.
Expand All @@ -83,15 +85,15 @@ class ZipFile : public AbstractFile
AutoStr* composeLumpPath(int lumpIdx, char delimiter = '/');

/**
* Lookup a lump info descriptor for a lump contained by this file.
* Retrieve the LumpInfo descriptor for a lump contained by this file.
*
* @param lumpIdx Logical index for the lump in this file's directory.
*
* @return Found lump info.
* @return Lump info descriptor for the lump.
*
* @throws de::Error If @a lumpIdx is not valid.
*/
LumpInfo const* lumpInfo(int lumpIdx);
LumpInfo const& lumpInfo(int lumpIdx);

/**
* Lookup the uncompressed size of lump contained by this file.
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/src/abstractfile.cpp
Expand Up @@ -61,9 +61,9 @@ filetype_t AbstractFile::type() const
return type_;
}

LumpInfo const* AbstractFile::info() const
LumpInfo const& AbstractFile::info() const
{
return &info_;
return info_;
}

bool AbstractFile::isContained() const
Expand Down
8 changes: 4 additions & 4 deletions doomsday/engine/portable/src/dfile.cpp
Expand Up @@ -126,15 +126,15 @@ void DFileBuilder::shutdown(void)

DFile* DFileBuilder::fromFileLump(AbstractFile& container, int lumpIdx, bool dontBuffer)
{
LumpInfo const* info = container.lumpInfo(lumpIdx);
if(!info) return NULL;
if(!container.isValidIndex(lumpIdx)) return 0;

LumpInfo const& info = container.lumpInfo(lumpIdx);
DFile* file = new DFile();
// Init and load in the lump data.
file->d->flags.open = true;
if(!dontBuffer)
{
file->d->size = info->size;
file->d->size = info.size;
file->d->pos = file->d->data = (uint8_t*) M_Malloc(file->d->size);
if(!file->d->data)
Con_Error("DFileBuilder::fromFileLump: Failed on allocation of %lu bytes for data buffer.",
Expand All @@ -147,7 +147,7 @@ DFile* DFileBuilder::fromFileLump(AbstractFile& container, int lumpIdx, bool don
F_PrettyPath(Str_Text(path)));
)
#endif
container.readLump(lumpIdx, (uint8_t*)file->d->data, 0, info->size);
container.readLump(lumpIdx, (uint8_t*)file->d->data, 0, info.size);
}
return file;
}
Expand Down
23 changes: 10 additions & 13 deletions doomsday/engine/portable/src/fs_main.cpp
Expand Up @@ -769,8 +769,8 @@ char const* FS::lumpName(lumpnum_t absoluteLumpNum)
if(info)
{
AbstractFile* container = reinterpret_cast<AbstractFile*>(info->container);
PathDirectoryNode* node = container->lumpDirectoryNode(info->lumpIdx);
return Str_Text(node->pathFragment());
PathDirectoryNode const& node = container->lumpDirectoryNode(info->lumpIdx);
return Str_Text(node.pathFragment());
}
return "";
}
Expand Down Expand Up @@ -915,10 +915,7 @@ bool FS::dumpLump(lumpnum_t absoluteLumpNum, char const* path)
{
int lumpIdx;
AbstractFile* file = lumpFile(absoluteLumpNum, &lumpIdx);
if(!file) return false;

LumpInfo const* info = file->lumpInfo(lumpIdx);
if(!info) return false;
if(!file || !file->isValidIndex(lumpIdx)) return false;

char const* lumpName = FS::lumpName(absoluteLumpNum);
char const* fname;
Expand All @@ -931,7 +928,7 @@ bool FS::dumpLump(lumpnum_t absoluteLumpNum, char const* path)
fname = lumpName;
}

bool dumpedOk = dump(file->cacheLump(lumpIdx), info->size, fname);
bool dumpedOk = dump(file->cacheLump(lumpIdx), file->lumpInfo(lumpIdx).size, fname);
file->unlockLump(lumpIdx);
if(!dumpedOk) return false;

Expand Down Expand Up @@ -1261,8 +1258,7 @@ int FS::allResourcePaths(char const* rawSearchPattern, int flags,
LumpInfo const* lumpInfo = *i;
AbstractFile* container = reinterpret_cast<AbstractFile*>(lumpInfo->container);
DENG_ASSERT(container);
PathDirectoryNode* node = container->lumpDirectoryNode(lumpInfo->lumpIdx);
DENG_ASSERT(node);
PathDirectoryNode const& node = container->lumpDirectoryNode(lumpInfo->lumpIdx);

AutoStr* filePath = 0;
bool patternMatched;
Expand All @@ -1273,7 +1269,7 @@ int FS::allResourcePaths(char const* rawSearchPattern, int flags,
}
else
{
patternMatched = node->matchDirectory(PCF_MATCH_FULL, &patternMap);
patternMatched = node.matchDirectory(PCF_MATCH_FULL, &patternMap);
if(patternMatched)
{
filePath = container->composeLumpPath(lumpInfo->lumpIdx);
Expand Down Expand Up @@ -1477,7 +1473,7 @@ static DFile* openAsLumpFile(AbstractFile* container, int lumpIdx,

// Prepare the temporary info descriptor.
LumpInfo info; F_InitLumpInfo(&info);
F_CopyLumpInfo(&info, container->lumpInfo(lumpIdx));
F_CopyLumpInfo(&info, &container->lumpInfo(lumpIdx));

// Try to open the referenced file as a specialised file type.
DFile* file = tryOpenFile3(hndl, Str_Text(&absPath), &info);
Expand Down Expand Up @@ -1803,7 +1799,7 @@ DFile* FS::openLump(lumpnum_t absoluteLumpNum)

LumpFile* lump = new LumpFile(*DFileBuilder::fromFileLump(*container, lumpIdx, false),
Str_Text(container->composeLumpPath(lumpIdx)),
*container->lumpInfo(lumpIdx));
container->lumpInfo(lumpIdx));
if(!lump) return 0;

DFile* openFileHndl = DFileBuilder::fromFile(*lump);
Expand Down Expand Up @@ -2521,7 +2517,8 @@ LumpInfo const* F_LumpInfo(struct abstractfile_s* _file, int lumpIdx)
{
if(!_file) return 0;
AbstractFile* file = reinterpret_cast<AbstractFile*>(_file);
return file->lumpInfo(lumpIdx);
if(!file->isValidIndex(lumpIdx)) return 0;
return &file->lumpInfo(lumpIdx);
}

size_t F_ReadLump(struct abstractfile_s* _file, int lumpIdx, uint8_t* buffer)
Expand Down

0 comments on commit 53ebf9f

Please sign in to comment.