Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Modules/Core/Common/ITKKWStyleOverwrite.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ itkIndex\.h SemicolonSpace Disable
itkSize\.h SemicolonSpace Disable
itkOffset\.h SemicolonSpace Disable
itkMultiThreaderBase.h Comments Disable
itkSingleton\.cxx Namespace Disable
10 changes: 6 additions & 4 deletions Modules/Core/Common/include/itkBuildInformation.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkMacro.h"
#include "itkObject.h"
#include <mutex>
#include "itkSingletonMacro.h"

namespace itk
{
Expand All @@ -41,6 +42,8 @@ namespace itk
* \ingroup ITKCommon
*
*/
struct BuildInformationGlobals;

class ITKCommon_EXPORT BuildInformation final: public Object
{
public:
Expand Down Expand Up @@ -90,10 +93,9 @@ class ITKCommon_EXPORT BuildInformation final: public Object
private:
BuildInformation();

/** To lock on the internal variables */
static std::mutex m_Mutex;
static Pointer m_InformationInstance;
static MapType m_Map;
itkGetGlobalDeclarationMacro(BuildInformationGlobals, PimplGlobals);
static BuildInformationGlobals * m_PimplGlobals;

}; // end of class
} // end namespace itk
#endif
7 changes: 6 additions & 1 deletion Modules/Core/Common/include/itkDataObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "itkObject.h"
#include "itkMacro.h"
#include "itkSingletonMacro.h"
#include "itkWeakPointer.h"
#include "itkRealTimeStamp.h"

Expand Down Expand Up @@ -504,7 +505,7 @@ class ITK_FORCE_EXPORT_MACRO(ITKCommon) DataObject:public Object
ModifiedTimeType m_PipelineMTime;

/** Static member that controls global data release after use by filter. */
static bool m_GlobalReleaseDataFlag;
static bool *m_GlobalReleaseDataFlag;

/** Connect the specified process object to the data object. This
* should only be called from a process object. The second parameter
Expand All @@ -522,6 +523,10 @@ class ITK_FORCE_EXPORT_MACRO(ITKCommon) DataObject:public Object
* pipeline (see ConnectSource), then nothing is done. */
bool DisconnectSource(ProcessObject *s, const DataObjectIdentifierType & name);

/** Only used to synchronize the global variable across static libraries.*/
itkGetGlobalDeclarationMacro(bool, GlobalReleaseDataFlag);


/** Friends of DataObject */
friend class ProcessObject;
friend class DataObjectError;
Expand Down
8 changes: 6 additions & 2 deletions Modules/Core/Common/include/itkFloatingPointExceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define itkFloatingPointExceptions_h

#include "itkMacro.h" // for ITKCommon_EXPORT
#include "itkSingletonMacro.h"

namespace itk
{
Expand All @@ -28,6 +29,9 @@ namespace itk
* Allows floating point exceptions to be caught during program execution.
* \ingroup ITKCommon
*/

struct ExceptionGlobals;

class ITKCommon_EXPORT FloatingPointExceptions
{
public:
Expand Down Expand Up @@ -76,9 +80,9 @@ class ITKCommon_EXPORT FloatingPointExceptions
FloatingPointExceptions(const FloatingPointExceptions &) = delete;
void operator=(const FloatingPointExceptions &) = delete;

itkGetGlobalDeclarationMacro(ExceptionGlobals, PimplGlobals);
/** static member that controls what happens during an exception */
static ExceptionAction m_ExceptionAction;
static bool m_Enabled;
static ExceptionGlobals * m_PimplGlobals;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "itkIntTypes.h"
#include "itkMath.h"
#include <mutex>
#include <mutex>
#include "itkSingletonMacro.h"
#include <climits>
#include <ctime>

Expand Down Expand Up @@ -119,6 +119,9 @@ namespace Statistics
* \wikiexample{Utilities/MersenneTwisterRandomVariateGenerator,Random number generator}
* \endwiki
*/

struct MersenneTwisterGlobals;

class ITKCommon_EXPORT MersenneTwisterRandomVariateGenerator:
public RandomVariateGeneratorBase
{
Expand Down Expand Up @@ -278,16 +281,18 @@ class ITKCommon_EXPORT MersenneTwisterRandomVariateGenerator:

private:

/** Only used to synchronize the global variable across static libraries.*/
itkGetGlobalDeclarationMacro(MersenneTwisterGlobals, PimplGlobals);

/** Internal method to actually create a new object. */
static Pointer CreateInstance();

// Local lock to enable concurrent access to singleton
std::mutex m_InstanceLock;

// Static/Global Variable need to be thread-safely accessed
static Pointer m_StaticInstance;
static std::recursive_mutex m_StaticInstanceLock;
static IntegerType m_StaticDiffer;

static MersenneTwisterGlobals *m_PimplGlobals;

}; // end of class

Expand Down
16 changes: 6 additions & 10 deletions Modules/Core/Common/include/itkMultiThreaderBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "itkIntTypes.h"
#include "itkImageRegion.h"
#include "itkImageIORegion.h"
#include "itkSingletonMacro.h"
#include <functional>
#include <thread>

Expand All @@ -55,7 +56,6 @@ namespace itk
*/

struct MultiThreaderBaseGlobals;

class ProcessObject;

class ITKCommon_EXPORT MultiThreaderBase : public Object
Expand Down Expand Up @@ -340,14 +340,6 @@ INTEL_PRAGMA_WARN_POP
ThreadingFunctorType funcP,
ProcessObject* filter);

/** Set/Get the pointer to MultiThreaderBaseGlobals.
* Note that these functions are not part of the public API and should not be
* used outside of ITK. They are an implementation detail and will be
* removed in the future. Also note that SetMultiThreaderBaseGlobals is not
* concurrent thread safe. */
static MultiThreaderBaseGlobals *GetMultiThreaderBaseGlobals();
static void SetMultiThreaderBaseGlobals(MultiThreaderBaseGlobals * multiThreaderBaseGlobals);

/** Updates progress if progress is non-negative and checks for abort.
* If "abort generate data" is set, throws the ProcessAborted exception. */
static void HandleFilterProgress(ProcessObject *filter, float progress = -1.0f);
Expand Down Expand Up @@ -411,7 +403,11 @@ INTEL_PRAGMA_WARN_POP
void *m_SingleData;

private:
static MultiThreaderBaseGlobals * m_MultiThreaderBaseGlobals;

/** Only used to synchronize the global variable across static libraries.*/
itkGetGlobalDeclarationMacro(MultiThreaderBaseGlobals, PimplGlobals);

static MultiThreaderBaseGlobals * m_PimplGlobals;
/** Friends of Multithreader.
* ProcessObject is a friend so that it can call PrintSelf() on its
* Multithreader. */
Expand Down
6 changes: 5 additions & 1 deletion Modules/Core/Common/include/itkObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "itkLightObject.h"
#include "itkEventObject.h"
#include "itkMetaDataDictionary.h"
#include "itkSingletonMacro.h"

namespace itk
{
Expand Down Expand Up @@ -202,14 +203,17 @@ class ITKCommon_EXPORT Object:public LightObject
virtual void SetTimeStamp( const TimeStamp & time );

private:
/** Only used to synchronize the global variable across static libraries.*/
itkGetGlobalDeclarationMacro(bool, GlobalWarningDisplay);

/** Enable/Disable debug messages. */
mutable bool m_Debug{false};

/** Keep track of modification time. */
mutable TimeStamp m_MTime;

/** Global object debug flag. */
static bool m_GlobalWarningDisplay;
static bool *m_GlobalWarningDisplay;

/** Implementation class for Subject/Observer Pattern.
* This is only allocated if used. */
Expand Down
22 changes: 10 additions & 12 deletions Modules/Core/Common/include/itkObjectFactoryBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#define itkObjectFactoryBase_h

#include "itkCreateObjectFunction.h"
#include "itkSingletonMacro.h"
#include <list>
#include <vector>

Expand Down Expand Up @@ -198,14 +199,6 @@ class ITKCommon_EXPORT ObjectFactoryBase:public Object
CreateObjectFunctionBase::Pointer m_CreateObject;
};

/** Set/Get the pointer to ObjectFactoryBasePrivate.
* Note that these functions are not part of the public API and should not be
* used outside of ITK. They are an implementation detail and will be
* removed in the future. Also note that SetObjectFactoryBasePrivate is not
* concurrent thread safe. */
static ObjectFactoryBasePrivate *GetObjectFactoryBase();
static void SynchronizeObjectFactoryBase(ObjectFactoryBasePrivate * objectFactoryBasePrivate);

protected:
void PrintSelf(std::ostream & os, Indent indent) const override;

Expand All @@ -231,6 +224,12 @@ class ITKCommon_EXPORT ObjectFactoryBase:public Object
~ObjectFactoryBase() override;

private:

/** Set/Get the pointer to ObjectFactoryBasePrivate.
* No concurrent thread safe. */
static void SynchronizeObjectFactoryBase(void * objectFactoryBasePrivate);
itkGetGlobalDeclarationMacro(ObjectFactoryBasePrivate, PimplGlobals);

OverRideMap *m_OverrideMap;

/** Initialize the static list of Factories. */
Expand All @@ -257,11 +256,10 @@ class ITKCommon_EXPORT ObjectFactoryBase:public Object
unsigned long m_LibraryDate;
std::string m_LibraryPath;

static bool m_StrictVersionChecking;

// This variable should NOT be accessed directly, but through GetObjectFactoryBase
static ObjectFactoryBasePrivate * m_ObjectFactoryBasePrivate;
static ObjectFactoryBasePrivate * m_PimplGlobals;
};


} // end namespace itk

#endif
115 changes: 115 additions & 0 deletions Modules/Core/Common/include/itkSingleton.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* 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.apache.org/licenses/LICENSE-2.0.txt
*
* 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 itkSingleton_h
#define itkSingleton_h

#include "itkMacro.h"
#include "itkSingletonMacro.h"
#include <map>
#include <functional>

/** \brief A function which does nothing
*
* This function is to be used to mark parameters as unused to suppress
* compiler warning. It can be used when the parameter needs to be named
* (i.e. itkNotUsed cannot be used) but is not always used. It ensures
* that the parameter is not optimized out.
*/
template <typename T>
inline void Unused( const T &) {};

namespace itk
{
/** \class SingletonIndex
* \brief Implementation detail.
*
* \ingroup ITKCommon
*/

class ITKCommon_EXPORT SingletonIndex
{
public:
/** Standard class typedefs. */
typedef SingletonIndex Self;
typedef std::map<std::string, std::tuple<void*, std::function<void(void*)>, std::function<void(void)> > > SingletonData;

// obtain a global registered in the singleton index under the
// globalName, if unknown then nullptr will be returned.
template<typename T>
T *GetGlobalInstance( const char *globalName )
{ return static_cast<T*>(this->GetGlobalInstancePrivate(globalName)); }


// returns true if the globalName has not been registered yet.
//
// It is assumed that the global will remain valid until the start
// of globals being destroyed.
template<typename T>
bool SetGlobalInstance( const char * globalName, T * global, std::function<void(void*)> func, std::function<void(void)> deleteFunc)
{ return this->SetGlobalInstancePrivate(globalName, global, func, deleteFunc);}

/** Set/Get the pointer to GlobalSingleton.
* Note that SetGlobalSingleton is not concurrent thread safe. */
static Self* GetInstance();
static void SetInstance(Self *SingletonIndex);
~SingletonIndex();

private:

// may return nullptr if string is not registered already
//
// access something like a std::map<std::string, void *> or
// registered globals, it may be possible to restrict the held
// classes to be derived from itk::LightObject, so dynamic cast can
// work, and could use some type of Holder<T> class for intrinsic types
void * GetGlobalInstancePrivate( const char *globalName );
// If globalName is already registered than false is return,
// otherwise global is added to the singleton index under globalName
bool SetGlobalInstancePrivate( const char * globalName, void * global, std::function<void(void*)> func, std::function<void(void)> deleteFunc);

/** The static GlobalSingleton. This is initialized to nullptr as the first
* stage of static initialization. It is then populated on the first call to
* itk::Singleton::Modified() but it can be overridden with SetGlobalSingleton().
* */
SingletonData m_GlobalObjects;
static Self* m_Instance;
// static SingletonIndexPrivate * m_GlobalSingleton;
};


// A wrapper for a global variable registered in the singleton index.
template<typename T>
T* Singleton(const char *globalName, std::function<void(void*)> func, std::function<void(void)> deleteFunc)
{
static SingletonIndex * singletonIndex = SingletonIndex::GetInstance();
Unused(singletonIndex);
T* instance = SingletonIndex::GetInstance()->GetGlobalInstance<T>(globalName);
if( instance == nullptr )
{
instance = new T;
if (!SingletonIndex::GetInstance()->SetGlobalInstance<T>(globalName, instance, func, deleteFunc))
{
delete instance;
instance = nullptr;
}
}
return instance;
}
} // end namespace itk

#endif
Loading