1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
/*=========================================================================
*
* Copyright UMC Utrecht and contributors
*
* 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 elxDefaultConstruct_h
#define elxDefaultConstruct_h
#include <itkLightObject.h>
#include <cassert>
namespace elastix
{
/// Allows default-constructing an `itk::LightObject` derived object without calling `New()`.
/// May improve the runtime performance, by avoiding heap allocation and pointer indirection.
///
/// Follows C++ Core Guidelines, September 23, 2022, "Prefer scoped objects, don't heap-allocate unnecessarily", from
/// http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rr-scoped
///
/// \note While `New()` may use a factory (`itk::ObjectFactory`) to create the object, `DefaultConstruct` just
/// default-constructs the object.
template <typename TObject>
class DefaultConstruct : public TObject
{
public:
ITK_DISALLOW_COPY_AND_MOVE(DefaultConstruct);
/// Public default-constructor. Just calls the (typically protected) default-constructor of `TObject`.
DefaultConstruct() = default;
/// Public destructor. Just calls the (typically protected) destructor of `TObject`.
~DefaultConstruct() override
{
// Note: This assertion may fail when a filter is default-constructed _before_ its input, e.g.:
//
// {
// DefaultConstruct<itk::TransformixFilter<ImageType>> transformixFilter{};
// DefaultConstruct<ImageType> movingImage{};
// transformixFilter.SetMovingImage(&movingImage);
// } // <== Assertion failure!
//
// Such a failure may be solved by reordering the declarations (declaring the input first), or by declaring the
// input by their static `New()` member function (instead of by `DefaultConstruct`).
assert(this->itk::LightObject::m_ReferenceCount == 1);
// Suppress warning "Trying to delete object with non-zero reference count."
this->itk::LightObject::m_ReferenceCount = 0;
}
};
} // namespace elastix
#endif
|