Skip to content

Commit

Permalink
Refactor|libdoomsday|FS1: Implement pimpl in FS1 (class) with DENG2_P…
Browse files Browse the repository at this point in the history
…RIVATE, cleanup
  • Loading branch information
danij-deng committed Jun 17, 2014
1 parent fdc8566 commit b7552c9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 79 deletions.
Expand Up @@ -33,9 +33,6 @@ namespace de {
class LIBDOOMSDAY_PUBLIC FileHandleBuilder
{
public:
static void init();
static void shutdown();

/**
* Create a new handle on the File @a file.
*
Expand Down
5 changes: 1 addition & 4 deletions doomsday/libdoomsday/include/doomsday/filesys/fs_main.h
Expand Up @@ -283,8 +283,6 @@ class LIBDOOMSDAY_PUBLIC FS1
*/
FS1();

virtual ~FS1();

/// Register the console commands, variables, etc..., of this module.
static void consoleRegister();

Expand Down Expand Up @@ -569,8 +567,7 @@ class LIBDOOMSDAY_PUBLIC FS1
int unloadAllNonStartupFiles();

private:
struct Instance;
Instance *d;
DENG2_PRIVATE(d)
};

Q_DECLARE_OPERATORS_FOR_FLAGS(FS1::Scheme::Flags)
Expand Down
12 changes: 1 addition & 11 deletions doomsday/libdoomsday/src/filesys/filehandle.cpp
Expand Up @@ -71,17 +71,7 @@ static void errorIfNotValid(FileHandle const &file, char const * /*callerName*/)
if(!file.isValid()) exit(1);
}

void FileHandleBuilder::init()
{
// Nothing to do.
}

void FileHandleBuilder::shutdown()
{
// Nothing to do.
}

FileHandle* FileHandleBuilder::fromLump(File1 &lump, bool dontBuffer)
FileHandle *FileHandleBuilder::fromLump(File1 &lump, bool dontBuffer)
{
LOG_AS("FileHandle::fromLump");

Expand Down
97 changes: 36 additions & 61 deletions doomsday/libdoomsday/src/filesys/fs_main.cpp
Expand Up @@ -126,41 +126,25 @@ static bool matchFileName(String const &string, String const &pattern)
return st->isNull();
}

struct FS1::Instance
DENG2_PIMPL(FS1)
{
FS1& self;

/// @c true= Flag newly opened files as "startup".
bool loadingForStartup;

/// List of currently opened files.
FileList openFiles;

/// List of all loaded files present in the system.
FileList loadedFiles;
bool loadingForStartup; ///< @c true= Flag newly opened files as "startup".

FileList openFiles; ///< List of currently opened files.
FileList loadedFiles; ///< List of all loaded files present in the system.
uint loadedFilesCRC;
FileIds fileIds; ///< Database of unique identifiers for all loaded/opened files.

/// Database of unique identifiers for all loaded/opened files.
FileIds fileIds;

/// Primary index of all files in the system.
LumpIndex primaryIndex;

/// Type-specific index for ZipFiles.
LumpIndex zipFileIndex;

/// Virtual (file) path => Lump name mapping.
LumpMappings lumpMappings;
LumpIndex primaryIndex; ///< Primary index of all files in the system.
LumpIndex zipFileIndex; ///< Type-specific index for ZipFiles.

/// Virtual file-directory mapping.
PathMappings pathMappings;
LumpMappings lumpMappings; ///< Virtual (file) path => Lump name mapping.
PathMappings pathMappings; ///< Virtual file-directory mapping.

/// System subspace schemes containing subsets of the total files.
Schemes schemes;
Schemes schemes; ///< File subsets.

Instance(FS1 *d)
: self(*d)
Instance(Public *i)
: Base(i)
, loadingForStartup(true)
, loadedFilesCRC (0)
, zipFileIndex (true/*paths are unique*/)
Expand Down Expand Up @@ -206,14 +190,14 @@ struct FS1::Instance
return false;
}

void clearLoadedFiles(de::LumpIndex* index = 0)
void clearLoadedFiles(de::LumpIndex *index = 0)
{
loadedFilesCRC = 0;

// Unload in reverse load order.
for(int i = loadedFiles.size() - 1; i >= 0; i--)
{
File1& file = loadedFiles[i]->file();
File1 &file = loadedFiles[i]->file();
if(!index || index->catalogues(file))
{
self.deindex(file);
Expand All @@ -234,12 +218,12 @@ struct FS1::Instance
zipFileIndex.clear();
}

String findPath(de::Uri const& search)
String findPath(de::Uri const &search)
{
// Within a subspace scheme?
try
{
FS1::Scheme& scheme = self.scheme(search.scheme());
FS1::Scheme &scheme = self.scheme(search.scheme());
LOG_RES_XVERBOSE("Using scheme '%s'...") << scheme.name();

// Ensure the scheme's index is up to date.
Expand All @@ -255,7 +239,7 @@ struct FS1::Instance
// At least one node name was matched (perhaps partially).
DENG2_FOR_EACH_CONST(FS1::Scheme::FoundNodes, i, foundNodes)
{
PathTree::Node& node = **i;
PathTree::Node &node = **i;
if(!node.comparePath(search.path(), PathTree::NoBranch))
{
// This is the file we are looking for.
Expand All @@ -267,11 +251,11 @@ struct FS1::Instance
/// @todo Should return not-found here but some searches are still dependent
/// on falling back to a wider search. -ds
}
catch(FS1::UnknownSchemeError const&)
catch(FS1::UnknownSchemeError const &)
{} // Ignore this error.

// Try a wider search of the whole virtual file system.
File1* file = openFile(search.path(), "rb", 0, true /* allow duplicates */);
File1 *file = openFile(search.path(), "rb", 0, true /* allow duplicates */);
if(file)
{
String found = file->composePath();
Expand All @@ -282,7 +266,7 @@ struct FS1::Instance
return ""; // Not found.
}

File1* findLump(String path, String const& /*mode*/)
File1 *findLump(String path, String const & /*mode*/)
{
if(path.isEmpty()) return 0;

Expand All @@ -304,7 +288,7 @@ struct FS1::Instance
{
DENG2_FOR_EACH_CONST(LumpMappings, i, lumpMappings)
{
LumpMapping const& mapping = *i;
LumpMapping const &mapping = *i;
if(mapping.first.compare(path)) continue;

lumpnum_t lumpNum = self.lumpNumForName(mapping.second);
Expand All @@ -317,9 +301,9 @@ struct FS1::Instance
return 0;
}

FILE* findAndOpenNativeFile(String path, String const& mymode, String& foundPath)
FILE *findAndOpenNativeFile(String path, String const &mymode, String &foundPath)
{
DENG_ASSERT(!path.isEmpty());
DENG2_ASSERT(!path.isEmpty());

// We must have an absolute path - prepend the CWD if necessary.
path = NativePath::workPath().withSeparators('/') / path;
Expand All @@ -333,7 +317,7 @@ struct FS1::Instance

// First try a real native file at this absolute path.
NativePath nativePath = NativePath(path);
FILE* nativeFile = fopen(nativePath.toUtf8().constData(), mode);
FILE *nativeFile = fopen(nativePath.toUtf8().constData(), mode);
if(nativeFile)
{
foundPath = nativePath.expand().withSeparators('/');
Expand All @@ -344,7 +328,7 @@ struct FS1::Instance
if(!pathMappings.empty())
{
QByteArray pathUtf8 = path.toUtf8();
AutoStr* mapped = AutoStr_NewStd();
AutoStr *mapped = AutoStr_NewStd();
DENG2_FOR_EACH_CONST(PathMappings, i, pathMappings)
{
Str_Set(mapped, pathUtf8.constData());
Expand All @@ -364,8 +348,8 @@ struct FS1::Instance
return 0;
}

File1* openFile(String path, String const& mode, size_t baseOffset,
bool allowDuplicate)
File1 *openFile(String path, String const& mode, size_t baseOffset,
bool allowDuplicate)
{
if(path.isEmpty()) return 0;

Expand All @@ -378,13 +362,13 @@ struct FS1::Instance

bool const reqNativeFile = mode.contains('f');

FileHandle* hndl = 0;
FileHandle *hndl = 0;
FileInfo info; // The temporary info descriptor.

// First check for lumps?
if(!reqNativeFile)
{
if(File1* found = findLump(path, mode))
if(File1 *found = findLump(path, mode))
{
// Do not read files twice.
if(!allowDuplicate && !self.checkFileId(found->composeUri())) return 0;
Expand All @@ -403,7 +387,7 @@ struct FS1::Instance
if(!hndl)
{
String foundPath;
if(FILE* found = findAndOpenNativeFile(path, mode, foundPath))
if(FILE *found = findAndOpenNativeFile(path, mode, foundPath))
{
// Do not read files twice.
if(!allowDuplicate && !self.checkFileId(de::Uri(foundPath, RC_NULL)))
Expand All @@ -427,7 +411,7 @@ struct FS1::Instance
// been mapped to another location. We want the file to be attributed with
// the path it is to be known by throughout the virtual file system.

File1& file = self.interpret(*hndl, path, info);
File1 &file = self.interpret(*hndl, path, info);

if(loadingForStartup)
{
Expand All @@ -438,27 +422,18 @@ struct FS1::Instance
}
};

FS1::FS1()
{
d = new Instance(this);
FileHandleBuilder::init();
}

FS1::~FS1()
{
delete d;
FileHandleBuilder::shutdown();
}
FS1::FS1() : d(new Instance(this))
{}

FS1::Scheme& FS1::createScheme(String name, Scheme::Flags flags)
FS1::Scheme &FS1::createScheme(String name, Scheme::Flags flags)
{
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);

// Create a new scheme.
Scheme* newScheme = new Scheme(name, flags);
Scheme *newScheme = new Scheme(name, flags);
d->schemes.insert(name.toLower(), newScheme);
return *newScheme;
}
Expand Down

0 comments on commit b7552c9

Please sign in to comment.