瀏覽代碼

ctkSingleton - Review API and add test

The set of macros has been simplified, ctkSingletonTestHelper illustrates
how they should be used.
Jean-Christophe Fillion-Robin 14 年之前
父節點
當前提交
29536160e2

+ 2 - 0
Libs/Core/Testing/Cpp/CMakeLists.txt

@@ -30,6 +30,8 @@ SET(Tests_SRCS
   ctkExampleDerivedWorkflowStep.h
   ctkExampleWorkflowStepUsingSignalsAndSlots.cpp
   ctkExampleWorkflowStepUsingSignalsAndSlots.h
+  ctkSingletonTestHelper.cpp
+  ctkSingletonTestHelper.h
   )
 
 SET(Tests_MOC_SRCS

+ 17 - 21
Libs/Core/Testing/Cpp/ctkSingletonTest1.cpp

@@ -19,37 +19,33 @@
 =========================================================================*/
 
 // QT includes
-#include <QApplication>
+#include <QtGlobal>
 
 // CTK includes
-#include "ctkSingleton.h"
+#include "ctkSingletonTestHelper.h"
 
 // STD includes
 #include <cstdlib>
 #include <iostream>
 
 //-----------------------------------------------------------------------------
-class ctkSingletonHelper
-{
-public:
-  CTK_SINGLETON_DECLARE(ctkSingletonHelper);
-};
-
-//-----------------------------------------------------------------------------
-void ctkSingletonHelper::classInitialize()
-{
-}
-
-//-----------------------------------------------------------------------------
 int ctkSingletonTest1(int argc, char * argv [] )
 {
-  QApplication app(argc, argv);
-
-  ctkSingletonHelper ctkObject;
-
-  ctkObject.classInitialize();
-
-
+  Q_UNUSED(argc);
+  Q_UNUSED(argv);
+  
+  if (!ctkSingletonTestHelper::instance())
+    {
+    std::cerr << "Problem with ctkSingletonTestHelper::instance()" << std::endl;
+    return EXIT_FAILURE;
+    }
+  ctkSingletonTestHelper::instance()->registerNorthFace();
+  ctkSingletonTestHelper::instance()->registerNorthFace();
+  if (ctkSingletonTestHelper::instance()->northFaceCount() != 2)
+    {
+    std::cerr << "Problem with ctkSingletonTestHelper" << std::endl;
+    return EXIT_FAILURE;
+    }
   return EXIT_SUCCESS;
 }
 

+ 75 - 0
Libs/Core/Testing/Cpp/ctkSingletonTestHelper.cpp

@@ -0,0 +1,75 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QDebug>
+
+// CTK includes
+#include "ctkSingletonTestHelper.h"
+
+//----------------------------------------------------------------------------
+class ctkSingletonTestHelperPrivate
+{
+public:
+  ctkSingletonTestHelperPrivate();
+  
+  int NorthFaceCount;
+};
+
+//-----------------------------------------------------------------------------
+// ctkSingletonTestHelperPrivate methods
+
+//-----------------------------------------------------------------------------
+ctkSingletonTestHelperPrivate::ctkSingletonTestHelperPrivate()
+{
+  this->NorthFaceCount = 0;
+}
+
+//-----------------------------------------------------------------------------
+// ctkSingletonTestHelper methods
+
+//-----------------------------------------------------------------------------
+ctkSingletonTestHelper::ctkSingletonTestHelper() : d_ptr(new ctkSingletonTestHelperPrivate)
+{
+}
+
+//-----------------------------------------------------------------------------
+ctkSingletonTestHelper* ctkSingletonTestHelper::instance()
+{
+  return Self::Instance;
+}
+
+//-----------------------------------------------------------------------------
+void ctkSingletonTestHelper::registerNorthFace()
+{
+  Q_D(ctkSingletonTestHelper);
+  d->NorthFaceCount++;
+}
+
+//-----------------------------------------------------------------------------
+int ctkSingletonTestHelper::northFaceCount()const
+{
+  Q_D(const ctkSingletonTestHelper);
+  return d->NorthFaceCount;
+}
+
+//-----------------------------------------------------------------------------
+CTK_SINGLETON_DEFINE(ctkSingletonTestHelper)
+

+ 56 - 0
Libs/Core/Testing/Cpp/ctkSingletonTestHelper.h

@@ -0,0 +1,56 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+#ifndef __ctkSingletonTestHelper_h
+#define __ctkSingletonTestHelper_h
+
+// Qt includes
+#include <QtGlobal> // Q_DISABLE_COPY, Q_DECLARE_PRIVATE
+#include <QScopedPointer>
+
+// CTK includes
+#include "ctkSingleton.h"
+
+class ctkSingletonTestHelperPrivate;
+
+class ctkSingletonTestHelper
+{
+public:
+  ctkSingletonTestHelper();
+  
+  static ctkSingletonTestHelper* instance();
+  
+  // Add singleton method here ...
+  void registerNorthFace();
+  int northFaceCount()const;
+  
+protected:
+  CTK_SINGLETON_DECLARE(ctkSingletonTestHelper);
+  QScopedPointer<ctkSingletonTestHelperPrivate> d_ptr;
+  
+private:
+  Q_DISABLE_COPY(ctkSingletonTestHelper);
+  Q_DECLARE_PRIVATE(ctkSingletonTestHelper);
+};
+
+//-----------------------------------------------------------------------------
+CTK_SINGLETON_DECLARE_INITIALIZER(,ctkSingletonTestHelper)
+
+#endif

+ 14 - 19
Libs/Core/ctkSingleton.h

@@ -18,6 +18,12 @@
 
 =========================================================================*/
 
+/**
+\class ctkSingleton ctkSingleton
+\brief Utility macros allowing to declare a singleton
+
+*/
+
 #ifndef __ctkSingleton_h
 #define __ctkSingleton_h
 
@@ -32,12 +38,12 @@
 
 //-----------------------------------------------------------------------------
 /// Should be included as a class protected member
-//
-#define CTK_SINGLETON_DECLARE(NAME)      \
-static NAME* Instance;                    \
-static void classInitialize();            \
-static void classFinalize();              \
-friend class NAME##Initialize;
+#define CTK_SINGLETON_DECLARE(NAME)                \
+static NAME* Instance;                             \
+static void classInitialize();                     \
+static void classFinalize();                       \
+friend class NAME##Initialize;                     \
+typedef NAME Self;
   
 //-----------------------------------------------------------------------------
 /// Help macro allowing to declare the utility class to make sure
@@ -48,7 +54,7 @@ friend class NAME##Initialize;
 /// The instance (NAME##Initializer) will show up in any translation unit
 /// that uses NAME.  It will make sure NAME is initialized before it is used.
 /// 
-#define CTK_SINGLETON_DECLARE_INITIALIZER(EXPORT_DIRECTIVE,NAME)  \
+#define CTK_SINGLETON_DECLARE_INITIALIZER(EXPORT_DIRECTIVE,NAME)   \
 class EXPORT_DIRECTIVE NAME##Initialize                            \
 {                                                                  \
 public:                                                            \
@@ -89,7 +95,7 @@ NAME* NAME::Instance;
 
 //----------------------------------------------------------------------------
 //
-/// This should be added at the end of the CXX file
+/// This should be added at the end of the CPP file
 //
 #define CTK_SINGLETON_DEFINE(NAME)                  \
 void NAME::classInitialize()                        \
@@ -105,15 +111,4 @@ void NAME::classFinalize()                          \
 CTK_SINGLETON_DEFINE_INITIALIZER(NAME)
 
 
-//----------------------------------------------------------------------------
-//
-/// Helper macro
-//
-/// TODO Documentation
-//
-#define CTK_SINGLETON_DECLARE_PRIVATE(PUB)       \
-PUB(const PUB&);                                 \
-void operator=(const PUB&);                      \
-CTK_DECLARE_PRIVATE(PUB);
-
 #endif //__ctkSingleton_h