Skip to content
Browse files

Initial commit

Code from: http://blog.objectmentor.com/articles/2009/06/12/hiding-global-methods-a-c-example

Signed-off-by: Stefan Näwe <stefan.naewe+github@googlemail.com>
  • Loading branch information...
0 parents commit c1cb0cc9fd7764624a605a5a74137bc111eb51e3 Stefan Näwe committed Jun 13, 2009
22 RealSdlLayerInstanceDelegator.cpp
@@ -0,0 +1,22 @@
+#include "RealSdlLayerInstanceDelegator.h"
+#include "sdl.h"
+
+RealSdlLayerInstanceDelegator::RealSdlLayerInstanceDelegator(){}
+
+RealSdlLayerInstanceDelegator::~RealSdlLayerInstanceDelegator(){}
+
+const SDL_version *RealSdlLayerInstanceDelegator::SDL_Linked_Version() {
+ return ::SDL_Linked_Version();
+}
+
+int RealSdlLayerInstanceDelegator::SDL_Init(int bitMask) {
+ return ::SDL_Init(bitMask);
+}
+
+void *RealSdlLayerInstanceDelegator::SDL_SetVideoMode(int x, int y, int bitDepth, int mode) {
+ return ::SDL_SetVideoMode(x, y, bitDepth, mode);
+}
+
+void RealSdlLayerInstanceDelegator::SDL_WM_SetCaption(const char *title, int someIntValue) {
+ ::SDL_WM_SetCaption(title, someIntValue);
+}
14 RealSdlLayerInstanceDelegator.h
@@ -0,0 +1,14 @@
+#include "SdlLayerInstanceDelegator.h"
+
+class RealSdlLayerInstanceDelegator : public SdlLayerInstanceDelegator
+{
+public:
+ RealSdlLayerInstanceDelegator();
+ virtual ~RealSdlLayerInstanceDelegator();
+
+ const SDL_version *SDL_Linked_Version();
+ int SDL_Init(int bitMask);
+ void *SDL_SetVideoMode(int x, int y, int bitDepth, int mode);
+ void SDL_WM_SetCaption(const char *title, int someIntValue);
+};
+
5 RunAllTests.cpp
@@ -0,0 +1,5 @@
+#include <CppUTest/CommandLineTestRunner.h>
+
+int main(int ac, char **av) {
+ return CommandLineTestRunner::RunAllTests(ac, av);
+}
16 SdlHardwareLayer.h
@@ -0,0 +1,16 @@
+#include <string>
+
+class SdlHardwareLayer {
+public:
+ SdlHardwareLayer();
+ virtual ~SdlHardwareLayer();
+ void initializeHardware();
+
+private:
+ int m_x;
+ int m_y;
+ int m_bitDepth;
+ std::string m_title;
+ void *m_screen;
+};
+
69 SdlHardwareLayerTest.h
@@ -0,0 +1,69 @@
+#include <CppUTest/TestHarness.h>
+
+#include "sdl.h"
+#include "SdlLayerFactory.h"
+#include "SdlHardwareLayer.h"
+#include "TestDoubleSdlLayerInstanceDelegator.h"
+
+TEST_GROUP(SdlHardwareLayerTest) {
+ virtual void setup() {
+ original = SdlLayerFactory::getInstance();
+ layer = new SdlHardwareLayer;
+ }
+
+ virtual void teardown() {
+ if(SdlLayerFactory::getInstance() != original)
+ delete SdlLayerFactory::getInstance();
+
+ SdlLayerFactory::replaceInstance(original);
+ delete layer;
+ }
+
+ SdlLayerInstanceDelegator *original;
+ SdlHardwareLayer *layer;
+};
+
+struct MajorMinorSdlhardwareLayer : public TestDoubleSdlLayerInstanceDelegator {
+ struct SDL_version v;
+
+ MajorMinorSdlhardwareLayer() {
+ v.major = 0;
+ v.minor = 0;
+ }
+
+ const SDL_version *SDL_Linked_Version() {
+ return &v;
+ }
+};
+
+TEST(SdlHardwareLayerTest, ItGeneratsErrorWithLowMajorMinorVersionNumber) {
+ SdlLayerFactory::replaceInstance(new MajorMinorSdlhardwareLayer);
+ try {
+ layer->initializeHardware();
+ FAIL("Should have thrown int value");
+ } catch (int value) {
+ LONGS_EQUAL(1, value);
+ }
+}
+
+struct InitFailingSdlHardwareLayer : public MajorMinorSdlhardwareLayer {
+ InitFailingSdlHardwareLayer () {
+ v.major = 3;
+ v.minor = 3;
+ }
+
+ int SDL_Init(int bitMask) {
+ return -1;
+ }
+};
+
+TEST(SdlHardwareLayerTest, ItGeneratesErrorWhenInitReturnsLessThan0) {
+ SdlLayerFactory::replaceInstance(new InitFailingSdlHardwareLayer);
+ try {
+ layer->initializeHardware();
+ FAIL("Should have thrown int value");
+ } catch (int value) {
+ LONGS_EQUAL(2, value);
+ }
+}
+
30 SdlHardwarelayer.cpp
@@ -0,0 +1,30 @@
+#include "SdlHardwareLayer.h"
+
+#include "sdl.h"
+#include "SdlLayerFactory.h"
+#include "SdlLayerInstanceDelegator.h"
+
+SdlHardwareLayer::SdlHardwareLayer() {
+}
+
+SdlHardwareLayer::~SdlHardwareLayer() {
+}
+
+void SdlHardwareLayer::initializeHardware() {
+ const SDL_version * sdlVersion = SdlLayerFactory::getInstance()->SDL_Linked_Version();
+ if (sdlVersion->minor < 2 && sdlVersion->major < 2) {
+ throw 1;
+ }
+ else if (SdlLayerFactory::getInstance()->SDL_Init(SDL_INIT_VIDEO |
+ SDL_INIT_AUDIO |
+ SDL_INIT_NOPARACHUTE) < 0) {
+ throw 2;
+ }
+ else {
+ m_screen = SDL_SetVideoMode(m_x, m_y, m_bitDepth, SDL_DOUBLEBUF);
+ if (0 != m_screen) {
+ SDL_WM_SetCaption(m_title.c_str(), 0);
+ }
+ }
+}
+
18 SdlLayerFactory.cpp
@@ -0,0 +1,18 @@
+#include "SdlLayerFactory.h"
+
+SdlLayerInstanceDelegator *SdlLayerFactory::instance = 0;
+
+SdlLayerFactory::SdlLayerFactory(){}
+
+SdlLayerFactory::~SdlLayerFactory(){}
+
+SdlLayerInstanceDelegator *SdlLayerFactory::getInstance() {
+ return instance;
+}
+
+SdlLayerInstanceDelegator *SdlLayerFactory::replaceInstance(SdlLayerInstanceDelegator *replacement) {
+ SdlLayerInstanceDelegator *original = instance;
+ instance = replacement;
+ return original;
+}
+
15 SdlLayerFactory.h
@@ -0,0 +1,15 @@
+class SdlLayerInstanceDelegator;
+
+class SdlLayerFactory
+{
+public:
+ static SdlLayerInstanceDelegator *getInstance();
+ static SdlLayerInstanceDelegator *replaceInstance(SdlLayerInstanceDelegator *replacement);
+
+private:
+ static SdlLayerInstanceDelegator *instance;
+
+ SdlLayerFactory();
+ ~SdlLayerFactory();
+};
+
5 SdlLayerInstanceDelegator.cpp
@@ -0,0 +1,5 @@
+#include "SdlLayerInstanceDelegator.h"
+
+SdlLayerInstanceDelegator::SdlLayerInstanceDelegator(){}
+
+SdlLayerInstanceDelegator::~SdlLayerInstanceDelegator(){}
19 SdlLayerInstanceDelegator.h
@@ -0,0 +1,19 @@
+struct SDL_version;
+
+class SdlLayerInstanceDelegator {
+public:
+ virtual ~SdlLayerInstanceDelegator(void) = 0;
+
+ virtual const SDL_version *SDL_Linked_Version() = 0;
+ virtual int SDL_Init(int bitMask) = 0;
+ virtual void *SDL_SetVideoMode(int x, int y, int bitDepth, int mode) = 0;
+ virtual void SDL_WM_SetCaption(const char *title, int someIntValue) = 0;
+
+protected:
+ SdlLayerInstanceDelegator();
+
+private:
+ SdlLayerInstanceDelegator(const SdlLayerInstanceDelegator &rhs);
+ SdlLayerInstanceDelegator &operator = (const SdlLayerInstanceDelegator &);
+};
+
12 TestDoubleSdlLayerInstanceDelegator.cpp
@@ -0,0 +1,12 @@
+#include "SdlLayerInstanceDelegator.h"
+
+class TestDoubleSdlLayerInstanceDelegator: public SdlLayerInstanceDelegator {
+public:
+ TestDoubleSdlLayerInstanceDelegator();
+ virtual ~TestDoubleSdlLayerInstanceDelegator();
+
+ const SDL_version *SDL_Linked_Version();
+ int SDL_Init(int bitMask);
+ void *SDL_SetVideoMode(int x, int y, int bitDepth, int mode);
+ void SDL_WM_SetCaption(const char *title, int someIntValue);
+};
16 sdl.h
@@ -0,0 +1,16 @@
+extern "C" {
+ const int SDL_INIT_VIDEO = 1;
+ const int SDL_INIT_AUDIO = 2;
+ const int SDL_INIT_NOPARACHUTE = 4;
+ const int SDL_DOUBLEBUF = 8;
+
+ struct SDL_version {
+ int major;
+ int minor;
+ };
+
+ const SDL_version *SDL_Linked_Version();
+ int SDL_Init(int bitMask);
+ void *SDL_SetVideoMode(int x, int y, int bitDepth, int mode);
+ void SDL_WM_SetCaption(const char *title, int someIntValue);
+}
16 sdl_implementation.cpp
@@ -0,0 +1,16 @@
+#include "sdl.h"
+
+const SDL_version *SDL_Linked_Version() {
+ return 0;
+}
+
+int SDL_Init(int bitMask) {
+ return - 1;
+}
+
+void *SDL_SetVideoMode(int x, int y, int bitDepth, int mode) {
+ return 0;
+}
+
+void SDL_WM_SetCaption(const char *title, int someIntValue){}
+

0 comments on commit c1cb0cc

Please sign in to comment.
Something went wrong with that request. Please try again.