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
25 changes: 3 additions & 22 deletions Modules/Core/Common/include/itkMetaDataObject.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#ifndef itkMetaDataObject_hxx
#define itkMetaDataObject_hxx

#include "itkMetaDataObjectDetail.h"

namespace itk
{
Expand Down Expand Up @@ -59,38 +60,18 @@ MetaDataObject<MetaDataObjectType>::SetMetaDataObjectValue(const MetaDataObjectT
Self::Assign(m_MetaDataObjectValue, newValue);
}

namespace
{
template <class T, class = void>
struct has_Print : std::false_type
{};

template <class T>
struct has_Print<T, std::void_t<decltype(std::declval<T>().Print(std::declval<std::ostream &>()))>> : std::true_type
{};

template <class T, class = void>
struct has_output_operator : std::false_type
{};

template <class T>
struct has_output_operator<T, std::void_t<decltype(std::declval<std::ostream &>() << std::declval<T>())>>
: std::true_type
{};
} // namespace

template <typename MetaDataObjectType>
void
MetaDataObject<MetaDataObjectType>::Print(std::ostream & os) const
{
// future c++20 feature
// constexpr bool hasPrint = false; requires( const &MetaDataObjectType obj ) { obj.Print(os); };

if constexpr (has_Print<MetaDataObjectType>::value)
if constexpr (MetaDataObjectDetail::has_Print<MetaDataObjectType>::value)
{
m_MetaDataObjectValue.Print(os);
}
else if constexpr (has_output_operator<MetaDataObjectType>::value)
else if constexpr (MetaDataObjectDetail::has_output_operator<MetaDataObjectType>::value)
{
os << m_MetaDataObjectValue;
}
Expand Down
50 changes: 50 additions & 0 deletions Modules/Core/Common/include/itkMetaDataObjectDetail.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* 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
*
* https://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 itkMetaDataObjectDetail_h
#define itkMetaDataObjectDetail_h

#include <type_traits>
#include <utility>

namespace itk
{

// Implementation details for MetaDataObject meta programming
namespace MetaDataObjectDetail
{
template <class T, class = void>
struct has_Print : std::false_type
{};

template <class T>
struct has_Print<T, std::void_t<decltype(std::declval<T>().Print(std::declval<std::ostream &>()))>> : std::true_type
{};

template <class T, class = void>
struct has_output_operator : std::false_type
{};

template <class T>
struct has_output_operator<T, std::void_t<decltype(std::declval<std::ostream &>() << std::declval<T>())>>
: std::true_type
{};
} // namespace MetaDataObjectDetail
} // namespace itk

#endif