Skip to content

Commit

Permalink
COMP: Improve HDF5 DataSpace::ALL initialization
Browse files Browse the repository at this point in the history
And related.

Per: https://www-igm.univ-mlv.fr/~dr/CPP/c++-faq/ctors.html#[10.12]
[10.12] How do I prevent the "static initialization order fiasco" for my static data members?
  • Loading branch information
thewtex committed Sep 17, 2023
1 parent ed2b1bc commit fb5b2f0
Show file tree
Hide file tree
Showing 22 changed files with 41 additions and 136 deletions.
14 changes: 4 additions & 10 deletions Modules/ThirdParty/HDF5/src/itkhdf5/c++/src/H5DaccProp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ namespace H5 {
// the order of creation and deletion of the global constants. See Design Notes
// in "H5PredType.cpp" for information.

// Initialize a pointer for the constant
DSetAccPropList *DSetAccPropList::DEFAULT_ = 0;

//--------------------------------------------------------------------------
// Function: DSetAccPropList::getConstant
// Purpose: Creates a DSetAccPropList object representing the HDF5
Expand All @@ -53,13 +50,9 @@ DSetAccPropList::getConstant()
IdComponent::H5dontAtexit_called = true;
}

// If the constant pointer is not allocated, allocate it. Otherwise,
// throw because it shouldn't be.
if (DEFAULT_ == 0)
DEFAULT_ = new DSetAccPropList(H5P_DATASET_ACCESS);
else
throw PropListIException("DSetAccPropList::getConstant",
"DSetAccPropList::getConstant is being invoked on an allocated DEFAULT_");
// Initialize a pointer for the constant
static DSetAccPropList *DEFAULT_ = new DSetAccPropList(H5P_DATASET_ACCESS);

return (DEFAULT_);
}

Expand All @@ -72,6 +65,7 @@ DSetAccPropList::getConstant()
void
DSetAccPropList::deleteConstants()
{
DSetAccPropList *DEFAULT_ = getConstant();
if (DEFAULT_ != 0)
delete DEFAULT_;
}
Expand Down
2 changes: 0 additions & 2 deletions Modules/ThirdParty/HDF5/src/itkhdf5/c++/src/H5DaccProp.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ class H5_DLLCPP DSetAccPropList : public LinkAccPropList {
static void deleteConstants();

private:
static DSetAccPropList *DEFAULT_;

// Creates the global constant, should only be used by the library
static DSetAccPropList *getConstant();

Expand Down
20 changes: 2 additions & 18 deletions Modules/ThirdParty/HDF5/src/itkhdf5/c++/src/H5DataSpace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ namespace H5 {
// the order of creation and deletion of the global constants. See Design Notes
// in "H5PredType.cpp" for information.

// Initialize a pointer for the constant
DataSpace *DataSpace::ALL_ = 0;

//--------------------------------------------------------------------------
// Function: DataSpace::getConstant
// Creates a DataSpace object representing the HDF5 constant
Expand All @@ -49,21 +46,7 @@ DataSpace::getConstant()
IdComponent::H5dontAtexit_called = true;
}

// If the constant pointer is not allocated, allocate it. Otherwise,
// throw because it shouldn't be.
if (ALL_ == 0)
ALL_ = new DataSpace(H5S_ALL);
else
#ifdef __wasi__
{
fprintf(stderr, "At: DataSpace::getConstant\n");
fprintf(stderr, "DataSpace::getConstant is being invoked on an allocated ALL_\n");
abort();
}
#else
throw DataSpaceIException("DataSpace::getConstant",
"DataSpace::getConstant is being invoked on an allocated ALL_");
#endif
static DataSpace *ALL_ = new DataSpace(H5S_ALL);
return (ALL_);
}

Expand All @@ -75,6 +58,7 @@ DataSpace::getConstant()
void
DataSpace::deleteConstants()
{
DataSpace *ALL_ = getConstant();
if (ALL_ != 0)
delete ALL_;
}
Expand Down
2 changes: 0 additions & 2 deletions Modules/ThirdParty/HDF5/src/itkhdf5/c++/src/H5DataSpace.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ class H5_DLLCPP DataSpace : public IdComponent {

#ifndef DOXYGEN_SHOULD_SKIP_THIS

static DataSpace *ALL_;

// Creates the global constant
static DataSpace *getConstant();

Expand Down
12 changes: 3 additions & 9 deletions Modules/ThirdParty/HDF5/src/itkhdf5/c++/src/H5DcreatProp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ namespace H5 {
// the order of creation and deletion of the global constants. See Design Notes
// in "H5PredType.cpp" for information.

// Initialize a pointer for the constant
DSetCreatPropList *DSetCreatPropList::DEFAULT_ = 0;

//--------------------------------------------------------------------------
// Function: DSetCreatPropList::getConstant
Expand All @@ -59,13 +57,8 @@ DSetCreatPropList::getConstant()
IdComponent::H5dontAtexit_called = true;
}

// If the constant pointer is not allocated, allocate it. Otherwise,
// throw because it shouldn't be.
if (DEFAULT_ == 0)
DEFAULT_ = new DSetCreatPropList(H5P_DATASET_CREATE);
else
throw PropListIException("DSetCreatPropList::getConstant",
"DSetCreatPropList::getConstant is being invoked on an allocated DEFAULT_");
// Initialize a pointer for the constant
static DSetCreatPropList * DEFAULT_ = new DSetCreatPropList(H5P_DATASET_CREATE);
return (DEFAULT_);
}

Expand All @@ -78,6 +71,7 @@ DSetCreatPropList::getConstant()
void
DSetCreatPropList::deleteConstants()
{
DSetCreatPropList * DEFAULT_ = getConstant();
if (DEFAULT_ != 0)
delete DEFAULT_;
}
Expand Down
2 changes: 0 additions & 2 deletions Modules/ThirdParty/HDF5/src/itkhdf5/c++/src/H5DcreatProp.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ class H5_DLLCPP DSetCreatPropList : public ObjCreatPropList {
static void deleteConstants();

private:
static DSetCreatPropList *DEFAULT_;

// Creates the global constant, should only be used by the library
static DSetCreatPropList *getConstant();

Expand Down
13 changes: 3 additions & 10 deletions Modules/ThirdParty/HDF5/src/itkhdf5/c++/src/H5DxferProp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ namespace H5 {
// the order of creation and deletion of the global constants. See Design Notes
// in "H5PredType.cpp" for information.

// Initialize a pointer for the constant
DSetMemXferPropList *DSetMemXferPropList::DEFAULT_ = 0;

//--------------------------------------------------------------------------
// Function: DSetMemXferPropList::getConstant
Expand All @@ -52,14 +50,8 @@ DSetMemXferPropList::getConstant()
IdComponent::H5dontAtexit_called = true;
}

// If the constant pointer is not allocated, allocate it. Otherwise,
// throw because it shouldn't be.
if (DEFAULT_ == 0)
DEFAULT_ = new DSetMemXferPropList(H5P_DATASET_XFER);
else
throw PropListIException(
"DSetMemXferPropList::getConstant",
"DSetMemXferPropList::getConstant is being invoked on an allocated DEFAULT_");
// Initialize a pointer for the constant
DSetMemXferPropList *DEFAULT_ = new DSetMemXferPropList(H5P_DATASET_XFER);
return (DEFAULT_);
}

Expand All @@ -72,6 +64,7 @@ DSetMemXferPropList::getConstant()
void
DSetMemXferPropList::deleteConstants()
{
DSetMemXferPropList *DEFAULT_ = getConstant();
if (DEFAULT_ != 0)
delete DEFAULT_;
}
Expand Down
2 changes: 0 additions & 2 deletions Modules/ThirdParty/HDF5/src/itkhdf5/c++/src/H5DxferProp.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ class H5_DLLCPP DSetMemXferPropList : public PropList {
static void deleteConstants();

private:
static DSetMemXferPropList *DEFAULT_;

// Creates the global constant, should only be used by the library
static DSetMemXferPropList *getConstant();

Expand Down
14 changes: 4 additions & 10 deletions Modules/ThirdParty/HDF5/src/itkhdf5/c++/src/H5FaccProp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ namespace H5 {
// the order of creation and deletion of the global constants. See Design Notes
// in "H5PredType.cpp" for information.

// Initialize a pointer for the constant
FileAccPropList *FileAccPropList::DEFAULT_ = 0;

//--------------------------------------------------------------------------
// Function: FileAccPropList::getConstant
// Creates a FileAccPropList object representing the HDF5 constant
Expand All @@ -56,13 +53,9 @@ FileAccPropList::getConstant()
IdComponent::H5dontAtexit_called = true;
}

// If the constant pointer is not allocated, allocate it. Otherwise,
// throw because it shouldn't be.
if (DEFAULT_ == 0)
DEFAULT_ = new FileAccPropList(H5P_FILE_ACCESS);
else
throw PropListIException("FileAccPropList::getConstant",
"FileAccPropList::getConstant is being invoked on an allocated DEFAULT_");
// Initialize a pointer for the constant
FileAccPropList *DEFAULT_ = new FileAccPropList(H5P_FILE_ACCESS);

return (DEFAULT_);
}

Expand All @@ -76,6 +69,7 @@ FileAccPropList::getConstant()
void
FileAccPropList::deleteConstants()
{
FileAccPropList *DEFAULT_ = getConstant();
if (DEFAULT_ != 0)
delete DEFAULT_;
}
Expand Down
2 changes: 0 additions & 2 deletions Modules/ThirdParty/HDF5/src/itkhdf5/c++/src/H5FaccProp.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ class H5_DLLCPP FileAccPropList : public PropList {
static void deleteConstants();

private:
static FileAccPropList *DEFAULT_;

// Creates the global constant, should only be used by the library
static FileAccPropList *getConstant();

Expand Down
14 changes: 4 additions & 10 deletions Modules/ThirdParty/HDF5/src/itkhdf5/c++/src/H5FcreatProp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ namespace H5 {
// the order of creation and deletion of the global constants. See Design Notes
// in "H5PredType.cpp" for information.

// Initialize a pointer for the constant
FileCreatPropList *FileCreatPropList::DEFAULT_ = 0;

//--------------------------------------------------------------------------
// Function: FileCreatPropList::getConstant
// Purpose Creates a FileCreatPropList object representing the HDF5
Expand All @@ -49,13 +46,9 @@ FileCreatPropList::getConstant()
IdComponent::H5dontAtexit_called = true;
}

// If the constant pointer is not allocated, allocate it. Otherwise,
// throw because it shouldn't be.
if (DEFAULT_ == 0)
DEFAULT_ = new FileCreatPropList(H5P_FILE_CREATE);
else
throw PropListIException("FileCreatPropList::getConstant",
"FileCreatPropList::getConstant is being invoked on an allocated DEFAULT_");
// Initialize a pointer for the constant
FileCreatPropList *DEFAULT_ = new FileCreatPropList(H5P_FILE_CREATE);

return (DEFAULT_);
}

Expand All @@ -68,6 +61,7 @@ FileCreatPropList::getConstant()
void
FileCreatPropList::deleteConstants()
{
FileCreatPropList *DEFAULT_ = getConstant();
if (DEFAULT_ != 0)
delete DEFAULT_;
}
Expand Down
2 changes: 0 additions & 2 deletions Modules/ThirdParty/HDF5/src/itkhdf5/c++/src/H5FcreatProp.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ class H5_DLLCPP FileCreatPropList : public PropList {
static void deleteConstants();

private:
static FileCreatPropList *DEFAULT_;

// Creates the global constant, should only be used by the library
static FileCreatPropList *getConstant();

Expand Down
14 changes: 4 additions & 10 deletions Modules/ThirdParty/HDF5/src/itkhdf5/c++/src/H5LaccProp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ namespace H5 {
// the order of creation and deletion of the global constants. See Design Notes
// in "H5PredType.cpp" for information.

// Initialize a pointer for the constant
LinkAccPropList *LinkAccPropList::DEFAULT_ = 0;

//--------------------------------------------------------------------------
// Function: LinkAccPropList::getConstant
// Creates a LinkAccPropList object representing the HDF5 constant
Expand All @@ -50,13 +47,9 @@ LinkAccPropList::getConstant()
IdComponent::H5dontAtexit_called = true;
}

// If the constant pointer is not allocated, allocate it. Otherwise,
// throw because it shouldn't be.
if (DEFAULT_ == 0)
DEFAULT_ = new LinkAccPropList(H5P_LINK_ACCESS);
else
throw PropListIException("LinkAccPropList::getConstant",
"LinkAccPropList::getConstant is being invoked on an allocated DEFAULT_");
// Initialize a pointer for the constant
LinkAccPropList *DEFAULT_ = new LinkAccPropList(H5P_LINK_ACCESS);

return (DEFAULT_);
}

Expand All @@ -70,6 +63,7 @@ LinkAccPropList::getConstant()
void
LinkAccPropList::deleteConstants()
{
LinkAccPropList *DEFAULT_ = getConstant();
if (DEFAULT_ != 0)
delete DEFAULT_;
}
Expand Down
2 changes: 0 additions & 2 deletions Modules/ThirdParty/HDF5/src/itkhdf5/c++/src/H5LaccProp.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ class H5_DLLCPP LinkAccPropList : public PropList {
static void deleteConstants();

private:
static LinkAccPropList *DEFAULT_;

// Creates the global constant, should only be used by the library
static LinkAccPropList *getConstant();

Expand Down
14 changes: 4 additions & 10 deletions Modules/ThirdParty/HDF5/src/itkhdf5/c++/src/H5LcreatProp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ namespace H5 {
// the order of creation and deletion of the global constants. See Design Notes
// in "H5PredType.cpp" for information.

// Initialize a pointer for the constant
LinkCreatPropList *LinkCreatPropList::DEFAULT_ = 0;

//--------------------------------------------------------------------------
// Function: LinkCreatPropList::getConstant
// Creates a LinkCreatPropList object representing the HDF5 constant
Expand All @@ -50,13 +47,9 @@ LinkCreatPropList::getConstant()
IdComponent::H5dontAtexit_called = true;
}

// If the constant pointer is not allocated, allocate it. Otherwise,
// throw because it shouldn't be.
if (DEFAULT_ == 0)
DEFAULT_ = new LinkCreatPropList(H5P_LINK_CREATE);
else
throw PropListIException("LinkCreatPropList::getConstant",
"LinkCreatPropList::getConstant is being invoked on an allocated DEFAULT_");
// Initialize a pointer for the constant
LinkCreatPropList *DEFAULT_ = new LinkCreatPropList(H5P_LINK_CREATE);

return (DEFAULT_);
}

Expand All @@ -70,6 +63,7 @@ LinkCreatPropList::getConstant()
void
LinkCreatPropList::deleteConstants()
{
LinkCreatPropList *DEFAULT_ = getConstant();
if (DEFAULT_ != 0)
delete DEFAULT_;
}
Expand Down
2 changes: 0 additions & 2 deletions Modules/ThirdParty/HDF5/src/itkhdf5/c++/src/H5LcreatProp.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ class H5_DLLCPP LinkCreatPropList : public PropList {
static void deleteConstants();

private:
static LinkCreatPropList *DEFAULT_;

// Creates the global constant, should only be used by the library
static LinkCreatPropList *getConstant();

Expand Down
14 changes: 4 additions & 10 deletions Modules/ThirdParty/HDF5/src/itkhdf5/c++/src/H5OcreatProp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ namespace H5 {
// the order of creation and deletion of the global constants. See Design Notes
// in "H5PredType.cpp" for information.

// Initialize a pointer for the constant
ObjCreatPropList *ObjCreatPropList::DEFAULT_ = 0;

//--------------------------------------------------------------------------
// Function: ObjCreatPropList::getConstant
// Creates a ObjCreatPropList object representing the HDF5 constant
Expand All @@ -50,13 +47,9 @@ ObjCreatPropList::getConstant()
IdComponent::H5dontAtexit_called = true;
}

// If the constant pointer is not allocated, allocate it. Otherwise,
// throw because it shouldn't be.
if (DEFAULT_ == 0)
DEFAULT_ = new ObjCreatPropList(H5P_OBJECT_CREATE);
else
throw PropListIException("ObjCreatPropList::getConstant",
"ObjCreatPropList::getConstant is being invoked on an allocated DEFAULT_");
// Initialize a pointer for the constant
ObjCreatPropList *DEFAULT_ = new ObjCreatPropList(H5P_OBJECT_CREATE);

return (DEFAULT_);
}

Expand All @@ -70,6 +63,7 @@ ObjCreatPropList::getConstant()
void
ObjCreatPropList::deleteConstants()
{
ObjCreatPropList *DEFAULT_ = getConstant();
if (DEFAULT_ != 0)
delete DEFAULT_;
}
Expand Down
2 changes: 0 additions & 2 deletions Modules/ThirdParty/HDF5/src/itkhdf5/c++/src/H5OcreatProp.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ class H5_DLLCPP ObjCreatPropList : public PropList {
static void deleteConstants();

private:
static ObjCreatPropList *DEFAULT_;

// Creates the global constant, should only be used by the library
static ObjCreatPropList *getConstant();

Expand Down
Loading

0 comments on commit fb5b2f0

Please sign in to comment.