From 10b1b28610dedb876187e65836d083c15f3ea6ad Mon Sep 17 00:00:00 2001 From: Sven Hesse Date: Thu, 21 May 2009 17:12:33 +0000 Subject: Splitting a few helper functions out of gob.h svn-id: r40765 --- engines/gob/dataio.cpp | 1 + engines/gob/demos/demoplayer.cpp | 1 + engines/gob/detection.cpp | 1 + engines/gob/draw_v1.cpp | 1 + engines/gob/draw_v2.cpp | 1 + engines/gob/game.cpp | 1 + engines/gob/game_v1.cpp | 1 + engines/gob/game_v2.cpp | 1 + engines/gob/game_v6.cpp | 1 + engines/gob/gob.h | 72 -------------------------- engines/gob/goblin.cpp | 1 + engines/gob/helper.h | 106 +++++++++++++++++++++++++++++++++++++++ engines/gob/inter_v1.cpp | 1 + engines/gob/inter_v2.cpp | 1 + engines/gob/inter_v4.cpp | 1 + engines/gob/inter_v6.cpp | 1 + engines/gob/sound/cdrom.cpp | 1 + engines/gob/variables.cpp | 1 + engines/gob/video.h | 1 + engines/gob/videoplayer.cpp | 1 + 20 files changed, 124 insertions(+), 72 deletions(-) create mode 100644 engines/gob/helper.h diff --git a/engines/gob/dataio.cpp b/engines/gob/dataio.cpp index dbb1e66eb2..d9a217e84c 100644 --- a/engines/gob/dataio.cpp +++ b/engines/gob/dataio.cpp @@ -27,6 +27,7 @@ #include "gob/gob.h" #include "gob/dataio.h" +#include "gob/helper.h" #include "gob/global.h" #include "gob/util.h" diff --git a/engines/gob/demos/demoplayer.cpp b/engines/gob/demos/demoplayer.cpp index 423ca1073b..d9d6b1e949 100644 --- a/engines/gob/demos/demoplayer.cpp +++ b/engines/gob/demos/demoplayer.cpp @@ -28,6 +28,7 @@ #include "gob/gob.h" #include "gob/demos/demoplayer.h" +#include "gob/helper.h" #include "gob/global.h" #include "gob/util.h" #include "gob/draw.h" diff --git a/engines/gob/detection.cpp b/engines/gob/detection.cpp index bab103c42a..1982cff27f 100644 --- a/engines/gob/detection.cpp +++ b/engines/gob/detection.cpp @@ -27,6 +27,7 @@ #include "engines/advancedDetector.h" #include "gob/gob.h" +#include "gob/helper.h" namespace Gob { diff --git a/engines/gob/draw_v1.cpp b/engines/gob/draw_v1.cpp index b856be1310..1943c327d8 100644 --- a/engines/gob/draw_v1.cpp +++ b/engines/gob/draw_v1.cpp @@ -28,6 +28,7 @@ #include "gob/gob.h" #include "gob/draw.h" +#include "gob/helper.h" #include "gob/global.h" #include "gob/util.h" #include "gob/game.h" diff --git a/engines/gob/draw_v2.cpp b/engines/gob/draw_v2.cpp index 6e03e24531..5e307d760e 100644 --- a/engines/gob/draw_v2.cpp +++ b/engines/gob/draw_v2.cpp @@ -28,6 +28,7 @@ #include "gob/gob.h" #include "gob/draw.h" +#include "gob/helper.h" #include "gob/global.h" #include "gob/util.h" #include "gob/game.h" diff --git a/engines/gob/game.cpp b/engines/gob/game.cpp index 9f64099e73..1a3734ca88 100644 --- a/engines/gob/game.cpp +++ b/engines/gob/game.cpp @@ -27,6 +27,7 @@ #include "gob/gob.h" #include "gob/game.h" +#include "gob/helper.h" #include "gob/global.h" #include "gob/util.h" #include "gob/dataio.h" diff --git a/engines/gob/game_v1.cpp b/engines/gob/game_v1.cpp index 4d2361e78f..f972bd3d0b 100644 --- a/engines/gob/game_v1.cpp +++ b/engines/gob/game_v1.cpp @@ -28,6 +28,7 @@ #include "gob/gob.h" #include "gob/game.h" +#include "gob/helper.h" #include "gob/global.h" #include "gob/util.h" #include "gob/dataio.h" diff --git a/engines/gob/game_v2.cpp b/engines/gob/game_v2.cpp index 91996c15ab..93540757b0 100644 --- a/engines/gob/game_v2.cpp +++ b/engines/gob/game_v2.cpp @@ -28,6 +28,7 @@ #include "gob/gob.h" #include "gob/game.h" +#include "gob/helper.h" #include "gob/global.h" #include "gob/util.h" #include "gob/dataio.h" diff --git a/engines/gob/game_v6.cpp b/engines/gob/game_v6.cpp index 40722676d1..575e07798e 100644 --- a/engines/gob/game_v6.cpp +++ b/engines/gob/game_v6.cpp @@ -29,6 +29,7 @@ #include "gob/gob.h" #include "gob/game.h" +#include "gob/helper.h" #include "gob/global.h" #include "gob/inter.h" #include "gob/draw.h" diff --git a/engines/gob/gob.h b/engines/gob/gob.h index d5c704e910..ecd9e19025 100644 --- a/engines/gob/gob.h +++ b/engines/gob/gob.h @@ -26,7 +26,6 @@ #ifndef GOB_GOB_H #define GOB_GOB_H - #include "common/system.h" #include "common/savefile.h" @@ -129,77 +128,6 @@ enum { kDebugDemo = 1 << 11 }; -inline char *strncpy0(char *dest, const char *src, size_t n) { - strncpy(dest, src, n); - dest[n] = 0; - return dest; -} - -inline char *strdupcpy(const char *str) { - if (!str) - return 0; - - size_t len = strlen(str) + 1; - - char *nstr = new char[len]; - - memcpy(nstr, str, len); - - return nstr; -} - -// A "smart" reference counting templated class -template -class ReferenceCounter { -public: - class Ptr { - public: - bool operator==(const Ptr &p) const { return _p == p._p; } - bool operator==(const ReferenceCounter *p) const { return _p == p; } - - T *operator-> () { return _p; } - T &operator* () { return *_p; } - operator T*() { return _p; } - - Ptr(T *p) : _p(p) { ++_p->_references; } - Ptr() : _p(0) { } - - ~Ptr() { - if (_p && (--_p->_references == 0)) - delete _p; - } - - Ptr(const Ptr &p) : _p(p._p) { ++_p->_references; } - - Ptr &operator= (const Ptr &p) { - ++p._p->_references; - if (_p && (--_p->_references == 0)) - delete _p; - _p = p._p; - return *this; - } - Ptr *operator= (const Ptr *p) { - if (p) - ++p->_p->_references; - if (_p && (--_p->_references == 0)) - delete _p; - - _p = p ? p->_p : 0; - return this; - } - - private: - T *_p; - }; - - ReferenceCounter() : _references(0) { } - virtual ~ReferenceCounter() {} - -private: - unsigned _references; - friend class Ptr; -}; - struct GOBGameDescription; class GobEngine : public Engine { diff --git a/engines/gob/goblin.cpp b/engines/gob/goblin.cpp index 81671b8247..90303e46a7 100644 --- a/engines/gob/goblin.cpp +++ b/engines/gob/goblin.cpp @@ -25,6 +25,7 @@ #include "gob/gob.h" #include "gob/goblin.h" +#include "gob/helper.h" #include "gob/global.h" #include "gob/util.h" #include "gob/draw.h" diff --git a/engines/gob/helper.h b/engines/gob/helper.h new file mode 100644 index 0000000000..3e4e3387bc --- /dev/null +++ b/engines/gob/helper.h @@ -0,0 +1,106 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * 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 Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#ifndef GOB_HELPER_H +#define GOB_HELPER_H + +namespace Gob { + +/** A strncpy that forces the final \0. */ +inline char *strncpy0(char *dest, const char *src, size_t n) { + strncpy(dest, src, n); + dest[n] = 0; + return dest; +} + +/** A strdup that new[]s the buffer. */ +inline char *strdupcpy(const char *str) { + if (!str) + return 0; + + size_t len = strlen(str) + 1; + + char *nstr = new char[len]; + + memcpy(nstr, str, len); + + return nstr; +} + +/** A "smart" reference counting templated class. */ +template +class ReferenceCounter { +public: + class Ptr { + public: + bool operator==(const Ptr &p) const { return _p == p._p; } + bool operator==(const ReferenceCounter *p) const { return _p == p; } + + T *operator-> () { return _p; } + T &operator* () { return *_p; } + operator T*() { return _p; } + + Ptr(T *p) : _p(p) { ++_p->_references; } + Ptr() : _p(0) { } + + ~Ptr() { + if (_p && (--_p->_references == 0)) + delete _p; + } + + Ptr(const Ptr &p) : _p(p._p) { ++_p->_references; } + + Ptr &operator= (const Ptr &p) { + ++p._p->_references; + if (_p && (--_p->_references == 0)) + delete _p; + _p = p._p; + return *this; + } + Ptr *operator= (const Ptr *p) { + if (p) + ++p->_p->_references; + if (_p && (--_p->_references == 0)) + delete _p; + + _p = p ? p->_p : 0; + return this; + } + + private: + T *_p; + }; + + ReferenceCounter() : _references(0) { } + virtual ~ReferenceCounter() {} + +private: + unsigned _references; + friend class Ptr; +}; + +} // End of namespace Gob + +#endif // GOB_HELPER_H diff --git a/engines/gob/inter_v1.cpp b/engines/gob/inter_v1.cpp index 5d6be23ecf..cabbfdacc7 100644 --- a/engines/gob/inter_v1.cpp +++ b/engines/gob/inter_v1.cpp @@ -28,6 +28,7 @@ #include "gob/gob.h" #include "gob/inter.h" +#include "gob/helper.h" #include "gob/global.h" #include "gob/util.h" #include "gob/dataio.h" diff --git a/engines/gob/inter_v2.cpp b/engines/gob/inter_v2.cpp index ff4266555a..e0ccc06d94 100644 --- a/engines/gob/inter_v2.cpp +++ b/engines/gob/inter_v2.cpp @@ -30,6 +30,7 @@ #include "gob/gob.h" #include "gob/inter.h" +#include "gob/helper.h" #include "gob/global.h" #include "gob/util.h" #include "gob/dataio.h" diff --git a/engines/gob/inter_v4.cpp b/engines/gob/inter_v4.cpp index 77cd490323..74721d0e6b 100644 --- a/engines/gob/inter_v4.cpp +++ b/engines/gob/inter_v4.cpp @@ -28,6 +28,7 @@ #include "gob/gob.h" #include "gob/inter.h" +#include "gob/helper.h" #include "gob/global.h" #include "gob/draw.h" #include "gob/game.h" diff --git a/engines/gob/inter_v6.cpp b/engines/gob/inter_v6.cpp index 7f85672575..19f6a8bbda 100644 --- a/engines/gob/inter_v6.cpp +++ b/engines/gob/inter_v6.cpp @@ -29,6 +29,7 @@ #include "gob/gob.h" #include "gob/inter.h" +#include "gob/helper.h" #include "gob/global.h" #include "gob/game.h" #include "gob/parse.h" diff --git a/engines/gob/sound/cdrom.cpp b/engines/gob/sound/cdrom.cpp index b68ebd0c5b..ec9b2aaccb 100644 --- a/engines/gob/sound/cdrom.cpp +++ b/engines/gob/sound/cdrom.cpp @@ -29,6 +29,7 @@ #include "gob/gob.h" #include "gob/sound/cdrom.h" +#include "gob/helper.h" namespace Gob { diff --git a/engines/gob/variables.cpp b/engines/gob/variables.cpp index 7b1390abe8..6929045b20 100644 --- a/engines/gob/variables.cpp +++ b/engines/gob/variables.cpp @@ -27,6 +27,7 @@ #include "gob/gob.h" #include "gob/variables.h" +#include "gob/helper.h" namespace Gob { diff --git a/engines/gob/video.h b/engines/gob/video.h index 38fdb2bf6f..9c6611f75e 100644 --- a/engines/gob/video.h +++ b/engines/gob/video.h @@ -30,6 +30,7 @@ #include "common/rect.h" #include "gob/gob.h" +#include "gob/helper.h" namespace Graphics { class PaletteLUT; diff --git a/engines/gob/videoplayer.cpp b/engines/gob/videoplayer.cpp index c392e25db1..7a517959e1 100644 --- a/engines/gob/videoplayer.cpp +++ b/engines/gob/videoplayer.cpp @@ -25,6 +25,7 @@ #include "gob/videoplayer.h" +#include "gob/helper.h" #include "gob/global.h" #include "gob/util.h" #include "gob/dataio.h" -- cgit v1.2.3