From a0d235213de256a6fd2107a7704955248512a632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Neun=C3=BCller?= Date: Thu, 25 Jul 2013 15:44:48 +0200 Subject: [PATCH] Replace FileSystem singleton with free functions. --- TODO.txt | 5 +++-- src/CMakeLists.txt | 1 - src/luaexport/FileSystemMeta.cpp | 10 ---------- src/main.cpp | 17 +++++++---------- src/svc/FileSystem.cpp | 31 +++++++++++-------------------- src/svc/FileSystem.hpp | 18 +++--------------- 6 files changed, 24 insertions(+), 58 deletions(-) delete mode 100644 src/luaexport/FileSystemMeta.cpp diff --git a/TODO.txt b/TODO.txt index 5d1bbe8..8df05c5 100644 --- a/TODO.txt +++ b/TODO.txt @@ -9,11 +9,12 @@ Refactoring * Put (at least) core components in namespace jd to allow ADL: avoids ugly forward declarations of operator<< before #include and is good style anyway. -* Replace the FileSystem singleton with free functions in a namespace. * StateManager / State needs to be completely rewritten: It's way to complicated without providing any benefits and, worse, lowers flexibility. * Maybe replace jd.connect with a Lua function <-> luabind::object() converter so that jd.connect() is not necessary anymore and fooComponent:connect_bar(f) can be used instead. * Get rid of LuaComponent::bindLuaPart(): Maybe luabind::adopt() is now - capable of replacing it. \ No newline at end of file + capable of replacing it. +* Exceptions: Use boost::exception/error_info system or at least a proper + hierarchy. \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 65cb98f..2facff8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -78,7 +78,6 @@ set(LUAEXPORT_SOURCES luaexport/ServiceLocator.cpp luaexport/Collisions.cpp luaexport/Geometry.cpp - luaexport/FileSystemMeta.cpp luaexport/DrawServiceMeta.cpp luaexport/MainloopMeta.cpp luaexport/StateManagerMeta.cpp diff --git a/src/luaexport/FileSystemMeta.cpp b/src/luaexport/FileSystemMeta.cpp deleted file mode 100644 index f0973a4..0000000 --- a/src/luaexport/FileSystemMeta.cpp +++ /dev/null @@ -1,10 +0,0 @@ -// Part of the Jade Engine -- Copyright (c) Christian Neumüller 2012--2013 -// This file is subject to the terms of the BSD 2-Clause License. -// See LICENSE.txt or http://opensource.org/licenses/BSD-2-Clause - -#include "svc/FileSystem.hpp" - -#include "compsys/BasicMetaComponent.hpp" - - -JD_BASIC_COMPONENT_IMPL(FileSystem) diff --git a/src/main.cpp b/src/main.cpp index d9eaa8a..aade274 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -280,10 +280,7 @@ int main(int argc, char* argv[]) auto const regSvc = ServiceLocator::registerService; LOG_D("Initializing virtual filesystem..."); - FileSystem::Init fsinit; - - FileSystem& fs = FileSystem::get(); - regSvc(fs); + vfs::Init fsinit; fs::path programpath = PHYSFS_getBaseDir(); std::vector baselibpaths; @@ -294,17 +291,17 @@ int main(int argc, char* argv[]) baselibpaths.push_back((programpath / "../share/base.jd").string()); baselibpaths.push_back(basepath + "/base.jd"); baselibpaths.push_back("./base.jd"); - fs.mountFirstWorking(baselibpaths, "/", FileSystem::logWarnings|FileSystem::mountOptional); + vfs::mountFirstWorking(baselibpaths, "/", vfs::logWarnings|vfs::mountOptional); - if (!fs.mount(game, "/", FileSystem::logWarnings|(gameSpecified ? - FileSystem::prependPath : FileSystem::mountOptional)) + if (!vfs::mount(game, "/", vfs::logWarnings|(gameSpecified ? + vfs::prependPath : vfs::mountOptional)) ) { LOG_W("Mounting working directory instead."); - fs.mount("."); + vfs::mount("."); } - fs.mount(basepath + "data/", "/", - FileSystem::writeDirectory|FileSystem::prependPath); + vfs::mount(basepath + "data/", "/", + vfs::writeDirectory|vfs::prependPath); LOG_D("Finished initializing virtual filesystem."); diff --git a/src/svc/FileSystem.cpp b/src/svc/FileSystem.cpp index 78e5a61..9bcfce6 100644 --- a/src/svc/FileSystem.cpp +++ b/src/svc/FileSystem.cpp @@ -19,7 +19,7 @@ static std::string lastPhysFsError() } -FileSystem::Error::Error(std::string const& msg, bool getLastError): +vfs::Error::Error(std::string const& msg, bool getLastError): std::runtime_error(msg + ( getLastError ? std::string(": ") + lastPhysFsError() : std::string())) { } @@ -27,7 +27,7 @@ FileSystem::Error::Error(std::string const& msg, bool getLastError): #define CALL_PHYSFS(fn, ...) \ if (!fn(__VA_ARGS__)) \ - throw ::FileSystem::Error(#fn " failed"); \ + throw ::vfs::Error(#fn " failed"); \ (void)0 VFile::VFile(): @@ -36,7 +36,7 @@ VFile::VFile(): } VFile::VFile(std::string const& filename, OpenMode mode): - m_f(FileSystem::get().openRaw(filename, mode)) + m_f(vfs::openRaw(filename, mode)) { } @@ -58,7 +58,7 @@ VFile::VFile(VFile&& rhs) void VFile::open(std::string const& filename, OpenMode mode) { close(); - m_f = FileSystem::get().openRaw(filename, mode); + m_f = vfs::openRaw(filename, mode); m_err.clear(); } @@ -77,6 +77,7 @@ VFile::~VFile() try { close(); } catch(std::exception const& e) { + LOG_E("Error closing VFile in destructor:"); LOG_EX(e); } } @@ -140,7 +141,7 @@ sf::Int64 VFile::check(sf::Int64 r, sf::Int64 rq, std::string const& op) void VFile::throwError() const { if (!m_err.empty()) - throw FileSystem::Error(m_err, false); + throw vfs::Error(m_err, false); } @@ -200,13 +201,13 @@ void VFileDevice::throwErr() ////////////////////////////////////////////////////////// -FileSystem::Init::Init() +vfs::Init::Init() { // Use original encoding here. CALL_PHYSFS(PHYSFS_init, argv()[0]); } -FileSystem::Init::~Init() +vfs::Init::~Init() { if (!PHYSFS_isInit()) { LOG_W("PhysFS already shut down."); @@ -219,17 +220,7 @@ FileSystem::Init::~Init() ////////////////////////////////////////////////////////// -FileSystem::FileSystem() -{ - if (!PHYSFS_isInit()) - throw Error("FileSystem not initialized", false); -} - -FileSystem::~FileSystem() -{ -} - -PHYSFS_File* FileSystem::openRaw(std::string const& name, VFile::OpenMode mode) +PHYSFS_File* vfs::openRaw(std::string const& name, VFile::OpenMode mode) { PHYSFS_File* f; switch (mode) { @@ -243,7 +234,7 @@ PHYSFS_File* FileSystem::openRaw(std::string const& name, VFile::OpenMode mode) return f; } -bool FileSystem::mount( +bool vfs::mount( std::string const& path, std::string const& mountPoint, int flags) @@ -268,7 +259,7 @@ bool FileSystem::mount( } -bool FileSystem::mountFirstWorking( +bool vfs::mountFirstWorking( std::vector const& paths, std::string const& mountPoint, int flags) diff --git a/src/svc/FileSystem.hpp b/src/svc/FileSystem.hpp index 4fe478f..c31864b 100644 --- a/src/svc/FileSystem.hpp +++ b/src/svc/FileSystem.hpp @@ -79,23 +79,14 @@ class VFileDevice VFile& m_f; }; -class FileSystem: public Component { - JD_COMPONENT -public: +namespace vfs { // Virtual File System struct Init { Init(); ~Init(); }; + class Error: public std::runtime_error { public: Error(std::string const& msg, bool getLastError = true); }; - virtual ~FileSystem(); - - static FileSystem& get() - { - static FileSystem instance; - return instance; - } - PHYSFS_File* openRaw(std::string const& name, VFile::OpenMode mode); enum MountFlags { @@ -113,9 +104,6 @@ class FileSystem: public Component { std::vector const& paths, std::string const& mountPoint = std::string(), int flags = prependPath); - -private: - FileSystem(); -}; +} // namespace vfs #endif