From 398431416c6df7630e3cb228776bac0bc9013822 Mon Sep 17 00:00:00 2001 From: danij Date: Tue, 12 Feb 2013 13:01:35 +0000 Subject: [PATCH] Refactor: Dumped de::Game's now redundant C wrapper interface --- doomsday/client/include/game.h | 70 ++----------- doomsday/client/src/con_main.cpp | 48 +++------ doomsday/client/src/game.cpp | 172 +++++-------------------------- doomsday/client/src/games.cpp | 8 +- 4 files changed, 48 insertions(+), 250 deletions(-) diff --git a/doomsday/client/include/game.h b/doomsday/client/include/game.h index f65de3e2fa..116d2586a2 100644 --- a/doomsday/client/include/game.h +++ b/doomsday/client/include/game.h @@ -24,10 +24,6 @@ #include "api_plugin.h" #include -#ifdef __cplusplus -extern "C" { -#endif - /** * @defgroup printGameFlags Print Game Flags * @ingroup flags @@ -41,18 +37,12 @@ extern "C" { #define PGF_EVERYTHING (PGF_BANNER|PGF_STATUS|PGF_LIST_STARTUP_RESOURCES|PGF_LIST_OTHER_RESOURCES) ///@} -struct manifest_s; -struct gamedef_s; - -#ifdef __cplusplus -} // extern "C" -#endif - -#ifdef __cplusplus - #include #include +struct manifest_s; +struct gamedef_s; + namespace de { class File1; @@ -81,10 +71,14 @@ class Game char const *title = "Unnamed", char const *author = "Unknown"); virtual ~Game(); - /// @return Collection in which this game exists. + /** + * Returns the owning Games collection. + */ Games &collection() const; - /// @return @c true= @a game is the currently active game. + /** + * Returns @a true if the game is currently active in the owning collection. + */ bool isCurrent() const; /// @return Unique plugin identifier attributed to that which registered this. @@ -223,50 +217,4 @@ inline bool isNullGame(Game const &game) { } // namespace de -extern "C" { -#endif // __cplusplus - -/** - * C wrapper API: - */ - -struct game_s; // The game instance (opaque). -typedef struct game_s Game; - -Game *Game_New(char const *identityKey, char const *configDir, char const *title, char const *author); - -void Game_Delete(Game *game); - -boolean Game_IsNullObject(Game const *game); - -struct game_s *Game_AddManifest(Game *game, struct manifest_s *manifest); - -boolean Game_AllStartupFilesFound(Game const *game); - -Game *Game_SetPluginId(Game *game, pluginid_t pluginId); - -pluginid_t Game_PluginId(Game const *game); - -ddstring_t const *Game_IdentityKey(Game const *game); - -ddstring_t const *Game_Title(Game const *game); - -ddstring_t const *Game_Author(Game const *game); - -ddstring_t const *Game_MainConfig(Game const *game); - -ddstring_t const *Game_BindingConfig(Game const *game); - -Game *Game_FromDef(GameDef const *def); - -void Game_PrintBanner(Game const *game); - -void Game_PrintResources(Game const *game, boolean printStatus, int rflags); - -void Game_Print(Game const *game, int flags); - -#ifdef __cplusplus -} // extern "C" -#endif - #endif /* LIBDENG_GAME_H */ diff --git a/doomsday/client/src/con_main.cpp b/doomsday/client/src/con_main.cpp index cdf6a0d1ca..f308aac762 100644 --- a/doomsday/client/src/con_main.cpp +++ b/doomsday/client/src/con_main.cpp @@ -57,12 +57,13 @@ #include "ui/displaymode.h" #include "ui/busyvisual.h" #include "cbuffer.h" +#include "Game" #ifdef __CLIENT__ # include "updater/downloaddialog.h" #endif -// MACROS ------------------------------------------------------------------ +using namespace de; #define SC_EMPTY_QUOTE -1 @@ -90,8 +91,6 @@ enum { : src == CMDS_CMDLINE? "the command line" \ : src == CMDS_SCRIPT? "an action command" : "???") -// TYPES ------------------------------------------------------------------- - typedef struct execbuff_s { boolean used; // Is this in use? timespan_t when; // System time when to execute the command. @@ -101,10 +100,6 @@ typedef struct execbuff_s { char subCmd[1024]; // A single command w/args. } execbuff_t; -// EXTERNAL FUNCTION PROTOTYPES -------------------------------------------- - -// PUBLIC FUNCTION PROTOTYPES ---------------------------------------------- - D_CMD(AddSub); D_CMD(IncDec); D_CMD(Alias); @@ -126,8 +121,6 @@ D_CMD(InspectMobj); D_CMD(DebugCrash); D_CMD(DebugError); -// PRIVATE FUNCTION PROTOTYPES --------------------------------------------- - static int executeSubCmd(const char *subCmd, byte src, boolean isNetCmd); static void Con_SplitIntoSubCommands(const char *command, timespan_t markerOffset, byte src, @@ -135,10 +128,6 @@ static void Con_SplitIntoSubCommands(const char *command, static void Con_ClearExecBuffer(void); -// EXTERNAL DATA DECLARATIONS ---------------------------------------------- - -// PUBLIC DATA DEFINITIONS ------------------------------------------------- - int CmdReturnValue = 0; byte ConsoleSilent = false; @@ -151,8 +140,6 @@ byte consoleSnapBackOnPrint = false; char* prbuff = NULL; // Print buffer, used by conPrintf. -// PRIVATE DATA DEFINITIONS ------------------------------------------------ - static CBuffer* histBuf = NULL; // The console history buffer (log). static uint bLineOff; // How many lines from the last in the histBuf? @@ -184,8 +171,6 @@ static void (*consolePrintFilter) (char* text); // Maybe alters text. static uint complPos; // Where is the completion cursor? static uint lastCompletion; // The last completed known word match (1-based index). -// CODE -------------------------------------------------------------------- - void Con_Register(void) { C_CMD("add", NULL, AddSub); @@ -1004,11 +989,9 @@ static int executeSubCmd(const char *subCmd, byte src, boolean isNetCmd) break; case CVT_URIPTR: { /// @todo Sanitize and validate against known schemas. - Uri* uri = Uri_NewWithPath2(argptr, RC_NULL); - CVar_SetUri(cvar, uri); - Uri_Delete(uri); - break; - } + de::Uri uri(argptr, RC_NULL); + CVar_SetUri(cvar, reinterpret_cast(&uri)); + break; } default: break; } } @@ -1259,8 +1242,8 @@ static int completeWord(int mode) break; } case WT_GAME: { - Game* game = (Game*)(*match)->data; - foundWord = Str_Text(Game_IdentityKey(game)); + Game *game = (Game *)(*match)->data; + foundWord = Str_Text(game->identityKey()); if(printCompletions) Con_FPrintf(CPF_LIGHT|CPF_BLUE, " %s\n", foundWord); break; @@ -1296,7 +1279,7 @@ static int completeWord(int mode) AutoStr* foundName = CVar_ComposePath((cvar_t*)completeWord->data); str = Str_Text(foundName); break; } - case WT_GAME: str = Str_Text(Game_IdentityKey((Game*)completeWord->data)); break; + case WT_GAME: str = Str_Text(reinterpret_cast(completeWord->data)->identityKey()); break; default: Con_Error("completeWord: Invalid word type %i.", (int)completeWord->type); exit(1); // Unreachable. @@ -2581,9 +2564,8 @@ D_CMD(Font) if(!stricmp(argv[1], "default")) { - Uri* uri = Uri_NewWithPath2(R_ChooseFixedFont(), RC_NULL); - fontid_t newFont = Fonts_ResolveUri(uri); - Uri_Delete(uri); + de::Uri uri(R_ChooseFixedFont(), RC_NULL); + fontid_t newFont = Fonts_ResolveUri(reinterpret_cast(&uri)); if(newFont) { Con_SetFont(newFont); @@ -2596,20 +2578,18 @@ D_CMD(Font) if(!stricmp(argv[1], "name") && argc == 3) { - Uri* uri = Uri_SetUri2(Uri_New(), argv[2], RC_NULL); - fontid_t newFont = Fonts_ResolveUri2(uri, true/*quiet please*/); - Uri_Delete(uri); + de::Uri uri(String(argv[2]), RC_NULL); + fontid_t newFont = Fonts_ResolveUri2(reinterpret_cast(&uri), true/*quiet please*/); if(newFont) { - Uri* uri = Fonts_ComposeUri(newFont); + QScopedPointer uri(reinterpret_cast(Fonts_ComposeUri(newFont))); Con_SetFont(newFont); - if(!Str_CompareIgnoreCase(Uri_Scheme(uri), "Game")) + if(!uri->scheme().compareWithoutCase("Game")) { Con_SetFontScale(1.5f, 2); Con_SetFontLeading(1.25f); Con_SetFontTracking(1); } - Uri_Delete(uri); return true; } Con_Printf("Unknown font '%s'\n", argv[2]); diff --git a/doomsday/client/src/game.cpp b/doomsday/client/src/game.cpp index 23aaaa7915..18f6fedb02 100644 --- a/doomsday/client/src/game.cpp +++ b/doomsday/client/src/game.cpp @@ -22,17 +22,16 @@ #include "de_base.h" #include "de_console.h" #include "de_filesys.h" - #include "filesys/manifest.h" - #include #include +#include #include "game.h" namespace de { -struct Game::Instance +DENG2_PIMPL(Game) { /// Unique identifier of the plugin which registered this game. pluginid_t pluginId; @@ -55,8 +54,8 @@ struct Game::Instance /// Name of the file used for control bindings, set automatically at creation time. ddstring_t bindingConfig; - Instance(char const* _identityKey, char const* configDir) - : pluginId(0), manifests() + Instance(Public &a, char const *_identityKey, char const *configDir) + : Base(a), pluginId(0), manifests() { Str_Set(Str_InitStd(&identityKey), _identityKey); DENG_ASSERT(!Str_IsEmpty(&identityKey)); @@ -79,11 +78,8 @@ struct Game::Instance ~Instance() { - DENG2_FOR_EACH(Game::Manifests, i, manifests) - { - Manifest* manifest = *i; - delete manifest; - } + qDeleteAll(manifests); + manifests.clear(); Str_Free(&identityKey); Str_Free(&mainConfig); @@ -93,10 +89,10 @@ struct Game::Instance } }; -Game::Game(char const* identityKey, char const* configDir, char const* title, - char const* author) +Game::Game(char const *identityKey, char const *configDir, + char const *title, char const *author) + : d(new Instance(*this, identityKey, configDir)) { - d = new Instance(identityKey, configDir); if(title) Str_Set(&d->title, title); if(author) Str_Set(&d->author, author); } @@ -116,7 +112,7 @@ bool Game::isCurrent() const return this == &collection().current(); } -Game& Game::addManifest(Manifest& manifest) +Game &Game::addManifest(Manifest &manifest) { // Ensure we don't add duplicates. Manifests::const_iterator found = d->manifests.find(manifest.resourceClass(), &manifest); @@ -129,10 +125,9 @@ Game& Game::addManifest(Manifest& manifest) bool Game::allStartupFilesFound() const { - DENG2_FOR_EACH_CONST(Manifests, i, d->manifests) + foreach(Manifest *manifest, d->manifests) { - Manifest& manifest = **i; - int const flags = manifest.fileFlags(); + int const flags = manifest->fileFlags(); if((flags & FF_STARTUP) && !(flags & FF_FOUND)) return false; @@ -140,7 +135,7 @@ bool Game::allStartupFilesFound() const return true; } -Game& Game::setPluginId(pluginid_t newId) +Game &Game::setPluginId(pluginid_t newId) { d->pluginId = newId; return *this; @@ -176,26 +171,26 @@ ddstring_t const *Game::author() const return &d->author; } -Game::Manifests const& Game::manifests() const +Game::Manifests const &Game::manifests() const { return d->manifests; } -bool Game::isRequiredFile(File1& file) +bool Game::isRequiredFile(File1 &file) { // If this resource is from a container we must use the path of the // root file container instead. - File1& rootFile = file; + File1 &rootFile = file; while(rootFile.isContained()) { rootFile = rootFile.container(); } - String absolutePath = rootFile.composePath(); + String absolutePath = rootFile.composePath(); bool isRequired = false; for(Manifests::const_iterator i = d->manifests.find(RC_PACKAGE); i != d->manifests.end() && i.key() == RC_PACKAGE; ++i) { - Manifest& manifest = **i; + Manifest &manifest = **i; if(!(manifest.fileFlags() & FF_STARTUP)) continue; if(!manifest.resolvedPath(true/*try locate*/).compare(absolutePath, Qt::CaseInsensitive)) @@ -208,31 +203,31 @@ bool Game::isRequiredFile(File1& file) return isRequired; } -Game* Game::fromDef(GameDef const& def) +Game *Game::fromDef(GameDef const &def) { return new Game(def.identityKey, def.configDir, def.defaultTitle, def.defaultAuthor); } -void Game::printBanner(Game const& game) +void Game::printBanner(Game const &game) { Con_PrintRuler(); Con_FPrintf(CPF_WHITE | CPF_CENTER, "%s\n", Str_Text(game.title())); Con_PrintRuler(); } -void Game::printFiles(Game const& game, int rflags, bool printStatus) +void Game::printFiles(Game const &game, int rflags, bool printStatus) { int numPrinted = 0; // Group output by resource class. - Manifests const& manifests = game.manifests(); + Manifests const &manifests = game.manifests(); for(uint i = 0; i < RESOURCECLASS_COUNT; ++i) { resourceclassid_t const classId = resourceclassid_t(i); for(Manifests::const_iterator i = manifests.find(classId); i != manifests.end() && i.key() == classId; ++i) { - Manifest& manifest = **i; + Manifest &manifest = **i; if(rflags >= 0 && (rflags & manifest.fileFlags())) { Manifest::consolePrint(manifest, printStatus); @@ -247,7 +242,7 @@ void Game::printFiles(Game const& game, int rflags, bool printStatus) } } -void Game::print(Game const& game, int flags) +void Game::print(Game const &game, int flags) { if(isNullGame(game)) flags &= ~PGF_BANNER; @@ -291,122 +286,3 @@ NullGame::NullGame() {} } // namespace de - -/** - * C Wrapper API: - */ - -#define TOINTERNAL(inst) \ - reinterpret_cast(inst) - -#define TOINTERNAL_CONST(inst) \ - reinterpret_cast(inst) - -#define SELF(inst) \ - DENG2_ASSERT(inst); \ - de::Game* self = TOINTERNAL(inst) - -#define SELF_CONST(inst) \ - DENG2_ASSERT(inst); \ - de::Game const* self = TOINTERNAL_CONST(inst) - -struct game_s* Game_New(char const* identityKey, char const* configDir, - char const* title, char const* author) -{ - return reinterpret_cast(new de::Game(identityKey, configDir, title, author)); -} - -void Game_Delete(struct game_s* game) -{ - if(game) - { - SELF(game); - delete self; - } -} - -boolean Game_IsNullObject(Game const* game) -{ - if(!game) return false; - return de::isNullGame(*reinterpret_cast(game)); -} - -struct game_s* Game_AddManifest(struct game_s* game, struct manifest_s* manifest) -{ - SELF(game); - DENG_ASSERT(manifest); - self->addManifest(reinterpret_cast(*manifest)); - return game; -} - -boolean Game_AllStartupFilesFound(struct game_s const* game) -{ - SELF_CONST(game); - return self->allStartupFilesFound(); -} - -struct game_s* Game_SetPluginId(struct game_s* game, pluginid_t pluginId) -{ - SELF(game); - return reinterpret_cast(&self->setPluginId(pluginId)); -} - -pluginid_t Game_PluginId(struct game_s const* game) -{ - SELF_CONST(game); - return self->pluginId(); -} - -ddstring_t const* Game_IdentityKey(struct game_s const* game) -{ - SELF_CONST(game); - return self->identityKey(); -} - -ddstring_t const* Game_Title(struct game_s const* game) -{ - SELF_CONST(game); - return self->title(); -} - -ddstring_t const* Game_Author(struct game_s const* game) -{ - SELF_CONST(game); - return self->author(); -} - -ddstring_t const* Game_MainConfig(struct game_s const* game) -{ - SELF_CONST(game); - return self->mainConfig(); -} - -ddstring_t const* Game_BindingConfig(struct game_s const* game) -{ - SELF_CONST(game); - return self->bindingConfig(); -} - -struct game_s* Game_FromDef(GameDef const* def) -{ - if(!def) return 0; - return reinterpret_cast(de::Game::fromDef(*def)); -} - -void Game_PrintBanner(Game const* game) -{ - if(!game) return; - de::Game::printBanner(*reinterpret_cast(game)); -} - -void Game_PrintResources(Game const* game, boolean printStatus, int rflags) -{ - if(!game) return; - de::Game::printFiles(*reinterpret_cast(game), rflags, CPP_BOOL(printStatus)); -} - -void Game_Print(Game const* game, int flags) -{ - if(!game) return; - de::Game::print(*reinterpret_cast(game), flags); -} diff --git a/doomsday/client/src/games.cpp b/doomsday/client/src/games.cpp index 17f45762d8..91dcca7d30 100644 --- a/doomsday/client/src/games.cpp +++ b/doomsday/client/src/games.cpp @@ -47,13 +47,7 @@ DENG2_PIMPL(Games) ~Instance() { qDeleteAll(games); - - if(nullGame) - { - delete nullGame; - nullGame = 0; - } - currentGame = 0; + if(nullGame) delete nullGame; } };