summaryrefslogtreecommitdiff
path: root/tests/utils
diff options
context:
space:
mode:
authorAndreas Baumann <abaumann@yahoo.com>2012-08-19 12:52:54 +0200
committerAndreas Baumann <abaumann@yahoo.com>2012-08-19 12:52:54 +0200
commit2e81cca31437f06293f0f19ef9c9c27f3ee24676 (patch)
treeeaf92e400fa0d3969b7d5be64c75423002845468 /tests/utils
parentd1ec47b36afeb73ac96bb9794e5ad4105b62466e (diff)
downloadcrawler-2e81cca31437f06293f0f19ef9c9c27f3ee24676.tar.gz
crawler-2e81cca31437f06293f0f19ef9c9c27f3ee24676.tar.bz2
playing with singleton
Diffstat (limited to 'tests/utils')
-rwxr-xr-xtests/utils/GNUmakefile4
-rwxr-xr-xtests/utils/Makefile.W324
-rw-r--r--tests/utils/test3.MUST0
-rw-r--r--tests/utils/test3.cpp96
4 files changed, 102 insertions, 2 deletions
diff --git a/tests/utils/GNUmakefile b/tests/utils/GNUmakefile
index 8fec5ce..1acc028 100755
--- a/tests/utils/GNUmakefile
+++ b/tests/utils/GNUmakefile
@@ -11,7 +11,8 @@ INCLUDE_LIBS =
TEST_CPP_BINS = \
test1$(EXE) \
- test2$(EXE)
+ test2$(EXE) \
+ test3$(EXE)
OBJS =
@@ -27,3 +28,4 @@ local_distclean:
local_test:
@./exec_test test1 "TypeList and TypeTraits"
@./exec_test test2 "TypeInfo C++ demangle"
+ @./exec_test test3 "Singleton"
diff --git a/tests/utils/Makefile.W32 b/tests/utils/Makefile.W32
index c2d74e7..a3b0b92 100755
--- a/tests/utils/Makefile.W32
+++ b/tests/utils/Makefile.W32
@@ -17,7 +17,8 @@ INCLUDE_LIBS = \
TEST_CPP_BINS = \
test1.exe \
- test2.exe
+ test2.exe \
+ test3.exe
OBJS =
@@ -36,3 +37,4 @@ local_distclean:
local_test:
@-exec_test test1 "TypeList and TypeTraits"
@-exec_test test2 "TypeInfo C++ demangle"
+ @-exec_test test3 "Singleton"
diff --git a/tests/utils/test3.MUST b/tests/utils/test3.MUST
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/utils/test3.MUST
diff --git a/tests/utils/test3.cpp b/tests/utils/test3.cpp
new file mode 100644
index 0000000..489276b
--- /dev/null
+++ b/tests/utils/test3.cpp
@@ -0,0 +1,96 @@
+#include "Singleton.hpp"
+
+#include <string>
+#include <iostream>
+
+using namespace std;
+
+class Logger : public Singleton< Logger >
+{
+ public:
+ void log( string s )
+ {
+ cout << s << endl;
+ }
+
+ friend class Singleton< Logger >;
+
+ protected:
+ Logger( )
+ {
+ cout << "Logger created" << endl;
+ }
+
+ public:
+ ~Logger( )
+ {
+ cout << "Logger destroyed" << endl;
+ }
+};
+
+/* this works, and two loggers can coexist, but they have different type
+class DerivedLogger : public Logger
+{
+ public:
+ void log( string s )
+ {
+ cout << "derived " << s << endl;
+ }
+};
+*/
+
+class X : public Singleton< X >
+{
+ public:
+ void doSomething( )
+ {
+ logger.log( "X is doing something" );
+ }
+
+ friend class Singleton< X >;
+
+ protected:
+ X ( ) : logger( Singleton< Logger >::instance( ) )
+ {
+ logger.log( "Creating singleton X" );
+ }
+
+ public:
+ ~X( )
+ {
+ logger.log( "Destroying singleton X" );
+ }
+
+ private:
+ Logger &logger;
+};
+
+void f( )
+{
+ Logger &logger = Singleton< Logger >::instance( );
+ logger.log( "bla" );
+}
+
+int main( void )
+{
+ f( );
+ X &x = Singleton< X >::instance( );
+ x.doSomething( );
+
+ // this would copy the object, noncopyable ensure we have
+ // only a private copy cnostructor
+ //Logger l1 = Singleton< Logger >::instance( );
+
+ // Logger anotherOne; this doesn't work, as expected
+
+ // DerivedLogger anotherOne; the same here
+
+ // see explanation above
+ //DerivedLogger &l2 = Singleton< DerivedLogger >::instance( );
+ //l2.log( "blu" );
+
+ // we can't accidently destroy the logger
+ //delete &logger;
+
+ return 0;
+}