From b926b07c976d8af8d04748720469c4735bcd2395 Mon Sep 17 00:00:00 2001 From: danij Date: Fri, 28 Sep 2012 03:24:27 +0100 Subject: [PATCH] Refactor: Reimplemented LumpFile in C++ --- doomsday/engine/portable/include/lumpfile.h | 110 +++++++++++++----- doomsday/engine/portable/src/lumpfile.cpp | 118 ++++++++++++++------ 2 files changed, 161 insertions(+), 67 deletions(-) diff --git a/doomsday/engine/portable/include/lumpfile.h b/doomsday/engine/portable/include/lumpfile.h index f847bae738..83ec00970c 100644 --- a/doomsday/engine/portable/include/lumpfile.h +++ b/doomsday/engine/portable/include/lumpfile.h @@ -1,25 +1,28 @@ -/**\file lumpfile.h - *\section License - * License: GPL - * Online License Link: http://www.gnu.org/licenses/gpl.html +/** + * @file lumpfile.h + * Specialization of AbstractFile for working with the lumps of containers + * objects such as WadFile and ZipFile. + * + * @ingroup fs * - *\author Copyright © 2003-2012 Jaakko Keränen - *\author Copyright © 2005-2012 Daniel Swanson + * @see abstractfile.h, AbstractFile * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. + * @author Copyright © 2003-2012 Jaakko Keränen + * @author Copyright © 2006-2012 Daniel Swanson * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * @par License + * GPL: http://www.gnu.org/licenses/gpl.html * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, - * Boston, MA 02110-1301 USA + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. This program is distributed in the hope that it + * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. You should have received a copy of the GNU + * General Public License along with this program; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA */ #ifndef LIBDENG_FILESYS_LUMPFILE_H @@ -28,28 +31,73 @@ #include "lumpinfo.h" #include "abstractfile.h" -struct lumpdirectory_s; +#ifdef __cplusplus +namespace de { /** * LumpFile. Runtime representation of a lump-file for use with LumpDirectory - * - * @ingroup FS */ -typedef struct lumpfile_s { - // Base file. - abstractfile_t _base; -} LumpFile; +class LumpFile +{ +public: + /// Base file instance. + /// @todo Inherit from this instead. + abstractfile_t base; -LumpFile* LumpFile_New(DFile* file, const char* path, const LumpInfo* info); -void LumpFile_Delete(LumpFile* lump); +public: + LumpFile(DFile& file, char const* path, LumpInfo const& info); + ~LumpFile(); + + /// @return Number of lumps (always @c =1). + int lumpCount(); -const LumpInfo* LumpFile_LumpInfo(LumpFile* lump, int lumpIdx); + /** + * Lookup a lump info descriptor for this lump. + * + * @param lumpIdx Ignored. Required argument. + * + * @return Found lump info. + */ + LumpInfo const* lumpInfo(int lumpIdx); + +private: + struct Instance; + Instance* d; +}; + +} // namespace de + +extern "C" { +#endif // __cplusplus /** - * Accessors: + * C wrapper API: */ -/// @return Number of lumps contained within this file. +struct lumpfile_s; // The lumpfile instance (opaque) +typedef struct lumpfile_s LumpFile; + +/** + * Constructs a new LumpFile instance which must be destroyed with LumpFile_Delete() + * once it is no longer needed. + * + * @param file Virtual file handle to the underlying file resource. + * @param path Virtual file system path to associate with the resultant LumpFile. + * @param info File info descriptor for the resultant LumpFile. A copy is made. + */ +LumpFile* LumpFile_New(DFile* file, char const* path, LumpInfo const* info); + +/** + * Destroy LumpFile instance @a lump. + */ +void LumpFile_Delete(LumpFile* lump); + +LumpInfo const* LumpFile_LumpInfo(LumpFile* lump, int lumpIdx); + int LumpFile_LumpCount(LumpFile* lump); -#endif /// LIBDENG_FILESYS_LUMPFILE_H +#ifdef __cplusplus +} // extern "C" +#endif + +#endif /* LIBDENG_FILESYS_LUMPFILE_H */ diff --git a/doomsday/engine/portable/src/lumpfile.cpp b/doomsday/engine/portable/src/lumpfile.cpp index 174b0195f1..9da5b95cc9 100644 --- a/doomsday/engine/portable/src/lumpfile.cpp +++ b/doomsday/engine/portable/src/lumpfile.cpp @@ -1,60 +1,106 @@ -/**\file lumpfile.c - *\section License - * License: GPL - * Online License Link: http://www.gnu.org/licenses/gpl.html +/** + * @file lumpfile.cpp + * Lump (file) accessor abstraction for containers. @ingroup fs * - *\author Copyright © 2003-2012 Jaakko Keränen - *\author Copyright © 2005-2012 Daniel Swanson + * @author Copyright © 2003-2012 Jaakko Keränen + * @author Copyright © 2005-2012 Daniel Swanson * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. + * @par License + * GPL: http://www.gnu.org/licenses/gpl.html * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, - * Boston, MA 02110-1301 USA + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. This program is distributed in the hope that it + * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. You should have received a copy of the GNU + * General Public License along with this program; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA */ #include "de_base.h" -#include "de_console.h" #include "de_filesys.h" -#include "lumpdirectory.h" #include "lumpfile.h" -LumpFile* LumpFile_New(DFile* file, const char* path, const LumpInfo* info) +#include +#include +#include + +de::LumpFile::LumpFile(DFile& file, char const* path, LumpInfo const& info) +{ + AbstractFile_Init(reinterpret_cast(this), FT_LUMPFILE, path, &file, &info); +} + +de::LumpFile::~LumpFile() { - LumpFile* lump = (LumpFile*)malloc(sizeof *lump); - if(!lump) Con_Error("LumpFile::Construct: Failed on allocation of %lu bytes for new LumpFile.", - (unsigned long) sizeof *lump); + F_ReleaseFile(reinterpret_cast(this)); + AbstractFile_Destroy(reinterpret_cast(this)); +} - AbstractFile_Init((abstractfile_t*)lump, FT_LUMPFILE, path, file, info); - return lump; +LumpInfo const* de::LumpFile::lumpInfo(int /*lumpIdx*/) +{ + // Lump files are special cases for this *is* the lump. + return AbstractFile_Info(reinterpret_cast(this)); +} + +int de::LumpFile::lumpCount() +{ + return 1; // Always. +} + +/** + * C Wrapper API: + */ + +#define TOINTERNAL(inst) \ + (inst) != 0? reinterpret_cast(inst) : NULL + +#define TOINTERNAL_CONST(inst) \ + (inst) != 0? reinterpret_cast(inst) : NULL + +#define SELF(inst) \ + DENG2_ASSERT(inst); \ + de::LumpFile* self = TOINTERNAL(inst) + +#define SELF_CONST(inst) \ + DENG2_ASSERT(inst); \ + de::LumpFile const* self = TOINTERNAL_CONST(inst) + +LumpFile* LumpFile_New(DFile* file, char const* path, LumpInfo const* info) +{ + if(!info) LegacyCore_FatalError("LumpFile_New: Received invalid LumpInfo (=NULL)."); + try + { + return reinterpret_cast(new de::LumpFile(*file, path, *info)); + } + catch(de::Error& er) + { + QString msg = QString("LumpFile_New: Failed to instantiate new LumpFile. ") + er.asText(); + LegacyCore_FatalError(msg.toUtf8().constData()); + exit(1); // Unreachable. + } } void LumpFile_Delete(LumpFile* lump) { - assert(lump); - F_ReleaseFile((abstractfile_t*)lump); - AbstractFile_Destroy((abstractfile_t*)lump); - free(lump); + if(lump) + { + SELF(lump); + delete self; + } } -const LumpInfo* LumpFile_LumpInfo(LumpFile* lump, int lumpIdx) +LumpInfo const* LumpFile_LumpInfo(LumpFile* lump, int lumpIdx) { - assert(lump); - /// Lump files are special cases for this *is* the lump. - return AbstractFile_Info((abstractfile_t*)lump); + SELF(lump); + return self->lumpInfo(lumpIdx); } int LumpFile_LumpCount(LumpFile* lump) { - return 1; // Always. + SELF(lump); + return self->lumpCount(); }