00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "common/test/run.hpp"
00025 #include "common/util.hpp"
00026
00027 #include "testtargetobj.hpp"
00028 #include "common/singletonsubclass.hpp"
00029
00030 #include <boost/lexical_cast.hpp>
00031 #include <boost/format.hpp>
00032 #include <iostream>
00033
00034 using boost::lexical_cast;
00035 using boost::format;
00036 using util::isnil;
00037 using std::string;
00038 using std::cout;
00039
00040
00041 namespace lumiera
00042 {
00043 namespace test
00044 {
00045
00046
00052 class Interface : public TestTargetObj
00053 {
00054 public:
00055 static int cnt;
00056 static void setCountParam (uint c) { Interface::cnt = c; }
00057
00058 virtual string identify() { return "Interface"; }
00059
00060 protected:
00061 Interface () : TestTargetObj(cnt) {}
00062 virtual ~Interface() {}
00063
00064 friend class singleton::StaticCreate<Interface>;
00065 friend class singleton::HeapCreate<Interface>;
00066 };
00067
00068 int Interface::cnt = 0;
00069
00070
00071 class Impl : public Interface
00072 {
00073 public:
00074 virtual string identify() { return "Implementation"; }
00075 };
00076
00077
00078
00079 class Impl_XXX : public Impl { };
00080 class Unrelated { };
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 class SingletonSubclass_test : public Test
00095 {
00096
00097 virtual void run(Arg arg)
00098 {
00099 uint num= isnil(arg)? 1 : lexical_cast<uint>(arg[1]);
00100
00101 cout << format("using the Singleton should create TargetObj(%d)...\n") % num;
00102
00103 Interface::setCountParam(num);
00104
00105
00106 singleton::UseSubclass<Impl> typeinfo;
00107
00108
00109
00110 SingletonSubclassFactory<Interface> instance (typeinfo);
00111
00112
00113
00114 Interface& t1 = instance();
00115 Interface& t2 = instance();
00116
00117 ASSERT ( &t1 == &t2, "not a Singleton, got two different instances." );
00118
00119 cout << "calling a non-static method on the Singleton-"
00120 << t1.identify() << "\n"
00121 << string (t1) << "\n";
00122
00123 #ifdef DEBUG
00124 verify_error_detection ();
00125 #endif
00126 }
00127
00128
00129
00130 void verify_error_detection ()
00131 {
00132
00133 singleton::UseSubclass<Impl_XXX> more_special_type;
00134
00135 try
00136 {
00137 SingletonSubclassFactory<Interface> instance (more_special_type);
00138 cout << "was able to re-configure the SingletonSubclassFactory "
00139 "with another type. This should be detected in debug mode\n";
00140 }
00141 catch (...)
00142 {
00143 ASSERT (lumiera_error () == error::LUMIERA_ERROR_ASSERTION);
00144 }
00145
00146
00147
00148
00149
00150
00151 }
00152 };
00153
00154
00155
00157 LAUNCHER (SingletonSubclass_test, "unit common");
00158
00159
00160
00161 }
00162
00163 }