2019-09-16 03:40:08 -05:00
|
|
|
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
// vi: set et ts=4 sw=4 sts=4:
|
2020-02-07 08:44:22 -06:00
|
|
|
/*****************************************************************************
|
|
|
|
* See the file COPYING for full copying permissions. *
|
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
*****************************************************************************/
|
2019-09-16 03:40:08 -05:00
|
|
|
/*!
|
|
|
|
* \file
|
2020-02-07 08:44:22 -06:00
|
|
|
* \ingroup Properties
|
|
|
|
* \ingroup TypeTraits
|
|
|
|
* \author Timo Koch
|
|
|
|
* \brief The Opm property system, traits with inheritance
|
2019-09-16 03:40:08 -05:00
|
|
|
*/
|
2020-02-07 08:44:22 -06:00
|
|
|
#ifndef OPM_PROPERTY_SYSTEM_HH
|
|
|
|
#define OPM_PROPERTY_SYSTEM_HH
|
2019-09-16 03:40:08 -05:00
|
|
|
|
2024-03-22 09:51:45 -05:00
|
|
|
#include <dune/common/classname.hh>
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <ostream>
|
2019-09-16 03:40:08 -05:00
|
|
|
#include <tuple>
|
2020-02-07 08:44:22 -06:00
|
|
|
#include <type_traits>
|
2019-09-16 03:40:08 -05:00
|
|
|
|
|
|
|
namespace Opm {
|
|
|
|
namespace Properties {
|
|
|
|
|
2020-02-07 08:44:22 -06:00
|
|
|
//! a tag to mark properties as undefined
|
|
|
|
struct UndefinedProperty {};
|
|
|
|
|
2020-03-11 07:17:08 -05:00
|
|
|
template <class TypeTag, class MyTypeTag>
|
|
|
|
struct Splices
|
|
|
|
{
|
2020-03-13 05:25:43 -05:00
|
|
|
using type = std::tuple<>;
|
2020-03-11 07:17:08 -05:00
|
|
|
};
|
2020-03-06 09:08:39 -06:00
|
|
|
|
2020-02-07 08:44:22 -06:00
|
|
|
//! implementation details for template meta programming
|
|
|
|
namespace Detail {
|
|
|
|
|
|
|
|
//! check if a property P is defined
|
|
|
|
template<class P>
|
|
|
|
constexpr auto isDefinedProperty(int)
|
|
|
|
-> decltype(std::integral_constant<bool, !std::is_same<typename P::type, UndefinedProperty>::value>{})
|
|
|
|
{ return {}; }
|
|
|
|
|
|
|
|
//! fall back if a Property is defined
|
|
|
|
template<class P>
|
|
|
|
constexpr std::true_type isDefinedProperty(...) { return {}; }
|
|
|
|
|
|
|
|
//! check if a TypeTag inherits from other TypeTags
|
|
|
|
template<class T>
|
|
|
|
constexpr auto hasParentTypeTag(int)
|
2024-03-22 05:43:44 -05:00
|
|
|
-> decltype(std::declval<typename T::InheritsFrom>(), std::true_type{})
|
2020-02-07 08:44:22 -06:00
|
|
|
{ return {}; }
|
|
|
|
|
|
|
|
//! fall back if a TypeTag doesn't inherit
|
|
|
|
template<class T>
|
|
|
|
constexpr std::false_type hasParentTypeTag(...) { return {}; }
|
|
|
|
|
|
|
|
//! helper alias to concatenate multiple tuples
|
|
|
|
template<class ...Tuples>
|
|
|
|
using ConCatTuples = decltype(std::tuple_cat(std::declval<Tuples>()...));
|
|
|
|
|
|
|
|
//! helper struct to get the first property that is defined in the TypeTag hierarchy
|
|
|
|
template<class TypeTag, template<class,class> class Property, class TTagList>
|
|
|
|
struct GetDefined;
|
|
|
|
|
2020-03-06 09:08:39 -06:00
|
|
|
//! helper struct to iterate over the TypeTag hierarchy
|
2020-02-07 08:44:22 -06:00
|
|
|
template<class TypeTag, template<class,class> class Property, class TTagList, class Enable>
|
|
|
|
struct GetNextTypeTag;
|
|
|
|
|
|
|
|
template<class TypeTag, template<class,class> class Property, class LastTypeTag>
|
|
|
|
struct GetNextTypeTag<TypeTag, Property, std::tuple<LastTypeTag>, std::enable_if_t<hasParentTypeTag<LastTypeTag>(int{}), void>>
|
|
|
|
{ using type = typename GetDefined<TypeTag, Property, typename LastTypeTag::InheritsFrom>::type; };
|
|
|
|
|
|
|
|
template<class TypeTag, template<class,class> class Property, class LastTypeTag>
|
|
|
|
struct GetNextTypeTag<TypeTag, Property, std::tuple<LastTypeTag>, std::enable_if_t<!hasParentTypeTag<LastTypeTag>(int{}), void>>
|
|
|
|
{ using type = UndefinedProperty; };
|
|
|
|
|
|
|
|
template<class TypeTag, template<class,class> class Property, class FirstTypeTag, class ...Args>
|
|
|
|
struct GetNextTypeTag<TypeTag, Property, std::tuple<FirstTypeTag, Args...>, std::enable_if_t<hasParentTypeTag<FirstTypeTag>(int{}), void>>
|
|
|
|
{ using type = typename GetDefined<TypeTag, Property, ConCatTuples<typename FirstTypeTag::InheritsFrom, std::tuple<Args...>>>::type; };
|
|
|
|
|
|
|
|
template<class TypeTag, template<class,class> class Property, class FirstTypeTag, class ...Args>
|
|
|
|
struct GetNextTypeTag<TypeTag, Property, std::tuple<FirstTypeTag, Args...>, std::enable_if_t<!hasParentTypeTag<FirstTypeTag>(int{}), void>>
|
|
|
|
{ using type = typename GetDefined<TypeTag, Property, std::tuple<Args...>>::type; };
|
|
|
|
|
|
|
|
template<class TypeTag, template<class,class> class Property, class LastTypeTag>
|
|
|
|
struct GetDefined<TypeTag, Property, std::tuple<LastTypeTag>>
|
|
|
|
{
|
|
|
|
// For clang, the following alias triggers compiler warnings if instantiated
|
|
|
|
// from something like `GetPropType<..., DeprecatedProperty>`, even if that is
|
|
|
|
// contained in a diagnostic pragma construct that should prevent these warnings.
|
|
|
|
// As a workaround, also add the pragmas around this line.
|
|
|
|
// See the discussion in MR 1647 for more details.
|
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
2019-09-16 03:40:08 -05:00
|
|
|
#endif
|
2020-02-07 08:44:22 -06:00
|
|
|
using LastType = Property<TypeTag, LastTypeTag>;
|
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic pop
|
2019-09-16 03:40:08 -05:00
|
|
|
#endif
|
2020-02-07 08:44:22 -06:00
|
|
|
using type = std::conditional_t<isDefinedProperty<LastType>(int{}), LastType,
|
|
|
|
typename GetNextTypeTag<TypeTag, Property, std::tuple<LastTypeTag>, void>::type>;
|
2019-09-16 03:40:08 -05:00
|
|
|
};
|
|
|
|
|
2020-02-07 08:44:22 -06:00
|
|
|
template<class TypeTag, template<class,class> class Property, class FirstTypeTag, class ...Args>
|
|
|
|
struct GetDefined<TypeTag, Property, std::tuple<FirstTypeTag, Args...>>
|
2019-09-16 03:40:08 -05:00
|
|
|
{
|
2020-02-07 08:44:22 -06:00
|
|
|
// See the comment above.
|
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
#endif
|
|
|
|
using FirstType = Property<TypeTag, FirstTypeTag>;
|
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#endif
|
|
|
|
using type = std::conditional_t<isDefinedProperty<FirstType>(int{}), FirstType,
|
|
|
|
typename GetNextTypeTag<TypeTag, Property, std::tuple<FirstTypeTag, Args...>, void>::type>;
|
2019-09-16 03:40:08 -05:00
|
|
|
};
|
|
|
|
|
2020-03-11 07:17:08 -05:00
|
|
|
|
2020-03-13 05:25:43 -05:00
|
|
|
//! helper struct to get the first property that is defined in the TypeTag hierarchy
|
2020-03-11 07:17:08 -05:00
|
|
|
template<class TypeTag, class TTagList>
|
2020-03-13 05:25:43 -05:00
|
|
|
struct GetDefinedSplice;
|
2020-03-11 07:17:08 -05:00
|
|
|
|
2020-03-13 05:25:43 -05:00
|
|
|
//! helper struct to iterate over the TypeTag hierarchy
|
2020-03-11 07:17:08 -05:00
|
|
|
template<class TypeTag, class TTagList, class Enable>
|
2020-03-13 05:25:43 -05:00
|
|
|
struct GetNextSpliceTypeTag;
|
2020-03-11 07:17:08 -05:00
|
|
|
|
|
|
|
template<class TypeTag, class LastTypeTag>
|
2020-03-13 05:25:43 -05:00
|
|
|
struct GetNextSpliceTypeTag<TypeTag, std::tuple<LastTypeTag>, std::enable_if_t<hasParentTypeTag<LastTypeTag>(int{}), void>>
|
|
|
|
{ using type = typename GetDefinedSplice<TypeTag, typename LastTypeTag::InheritsFrom>::type; };
|
2020-03-11 07:17:08 -05:00
|
|
|
|
|
|
|
template<class TypeTag, class LastTypeTag>
|
2020-03-13 05:25:43 -05:00
|
|
|
struct GetNextSpliceTypeTag<TypeTag, std::tuple<LastTypeTag>, std::enable_if_t<!hasParentTypeTag<LastTypeTag>(int{}), void>>
|
|
|
|
{ using type = std::tuple<>; };
|
2020-03-11 07:17:08 -05:00
|
|
|
|
|
|
|
template<class TypeTag, class FirstTypeTag, class ...Args>
|
2020-03-13 05:25:43 -05:00
|
|
|
struct GetNextSpliceTypeTag<TypeTag, std::tuple<FirstTypeTag, Args...>, std::enable_if_t<hasParentTypeTag<FirstTypeTag>(int{}), void>>
|
|
|
|
{ using type = typename GetDefinedSplice<TypeTag, ConCatTuples<typename FirstTypeTag::InheritsFrom, std::tuple<Args...>>>::type; };
|
2020-03-11 07:17:08 -05:00
|
|
|
|
|
|
|
template<class TypeTag, class FirstTypeTag, class ...Args>
|
2020-03-13 05:25:43 -05:00
|
|
|
struct GetNextSpliceTypeTag<TypeTag, std::tuple<FirstTypeTag, Args...>, std::enable_if_t<!hasParentTypeTag<FirstTypeTag>(int{}), void>>
|
|
|
|
{ using type = typename GetDefinedSplice<TypeTag, std::tuple<Args...>>::type; };
|
|
|
|
|
|
|
|
//! check if a splice S is defined
|
|
|
|
template<class S>
|
|
|
|
constexpr auto isDefinedSplice(int)
|
|
|
|
-> decltype(std::integral_constant<bool, !std::is_same<typename S::type, std::tuple<>>::value>{})
|
|
|
|
{ return {}; }
|
|
|
|
|
|
|
|
//! fall back if a splice is defined
|
|
|
|
template<class S>
|
|
|
|
constexpr std::true_type isDefinedSplice(...) { return {}; }
|
2020-03-11 07:17:08 -05:00
|
|
|
|
|
|
|
template<class TypeTag, class LastTypeTag>
|
2020-03-13 05:25:43 -05:00
|
|
|
struct GetDefinedSplice<TypeTag, std::tuple<LastTypeTag>>
|
2020-03-11 07:17:08 -05:00
|
|
|
{
|
2020-03-13 05:25:43 -05:00
|
|
|
using LastSplice = Splices<TypeTag, LastTypeTag>;
|
2020-03-13 10:08:49 -05:00
|
|
|
using nexttuple = typename GetNextSpliceTypeTag<TypeTag,
|
|
|
|
ConCatTuples<
|
|
|
|
std::tuple<LastTypeTag>,
|
|
|
|
typename LastSplice::type
|
|
|
|
>,
|
|
|
|
void>::type;
|
2020-03-13 05:25:43 -05:00
|
|
|
|
|
|
|
using type = std::conditional_t<isDefinedSplice<LastSplice>(int{}),
|
|
|
|
ConCatTuples<nexttuple, typename LastSplice::type>,
|
|
|
|
nexttuple>;
|
2020-03-11 07:17:08 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class TypeTag, class FirstTypeTag, class ...Args>
|
2020-03-13 05:25:43 -05:00
|
|
|
struct GetDefinedSplice<TypeTag, std::tuple<FirstTypeTag, Args...>>
|
2020-03-11 07:17:08 -05:00
|
|
|
{
|
2020-03-13 05:25:43 -05:00
|
|
|
using FirstSplice = Splices<TypeTag, FirstTypeTag>;
|
2020-03-13 10:08:49 -05:00
|
|
|
using nexttuple = typename GetNextSpliceTypeTag<TypeTag,
|
|
|
|
ConCatTuples<
|
|
|
|
std::tuple<FirstTypeTag, Args...>,
|
|
|
|
typename FirstSplice::type
|
|
|
|
>,
|
|
|
|
void>::type;
|
2020-03-11 07:17:08 -05:00
|
|
|
|
2020-03-13 05:25:43 -05:00
|
|
|
using type = std::conditional_t<isDefinedSplice<FirstSplice>(int{}),
|
|
|
|
ConCatTuples<typename FirstSplice::type, nexttuple>,
|
|
|
|
nexttuple>;
|
|
|
|
};
|
2020-03-11 07:17:08 -05:00
|
|
|
|
2020-03-13 05:25:43 -05:00
|
|
|
//! helper struct to extract get the Property specilization given a TypeTag, asserts that the property is defined
|
2020-02-07 08:44:22 -06:00
|
|
|
template<class TypeTag, template<class,class> class Property>
|
|
|
|
struct GetPropImpl
|
2019-09-16 03:40:08 -05:00
|
|
|
{
|
2020-03-13 10:08:49 -05:00
|
|
|
using tuple = typename Detail::GetDefinedSplice<TypeTag, std::tuple<TypeTag>>::type;
|
2020-03-06 09:08:39 -06:00
|
|
|
using type = typename Detail::GetDefined<TypeTag,
|
|
|
|
Property,
|
2020-03-13 05:25:43 -05:00
|
|
|
ConCatTuples<std::tuple<TypeTag>, tuple>
|
2020-03-06 09:08:39 -06:00
|
|
|
>::type;
|
2020-02-07 08:44:22 -06:00
|
|
|
static_assert(!std::is_same<type, UndefinedProperty>::value, "Property is undefined!");
|
2019-09-16 03:40:08 -05:00
|
|
|
};
|
|
|
|
|
2020-03-13 10:08:49 -05:00
|
|
|
template<class TypeTag, class SpliceTypeTag, template<class,class> class Property>
|
2020-03-13 05:25:43 -05:00
|
|
|
struct GetSplicePropImpl
|
|
|
|
{
|
2020-03-13 10:08:49 -05:00
|
|
|
using type = typename Detail::GetDefined<TypeTag, Property, std::tuple<TypeTag, SpliceTypeTag>>::type;
|
2020-03-13 05:25:43 -05:00
|
|
|
static_assert(!std::is_same<type, std::tuple<>>::value, "Splice is undefined!");
|
|
|
|
};
|
|
|
|
|
2024-03-22 09:51:45 -05:00
|
|
|
template <typename, class = void>
|
|
|
|
struct has_name : public std::false_type {};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct has_name<T, decltype((void)T::name, void())>
|
|
|
|
: public std::true_type {};
|
|
|
|
|
2020-02-07 08:44:22 -06:00
|
|
|
} // end namespace Detail
|
|
|
|
} // end namespace Property
|
2019-09-16 03:40:08 -05:00
|
|
|
|
2020-02-07 08:44:22 -06:00
|
|
|
//! get the type of a property (equivalent to old macro GET_PROP(...))
|
|
|
|
template<class TypeTag, template<class,class> class Property>
|
|
|
|
using GetProp = typename Properties::Detail::GetPropImpl<TypeTag, Property>::type;
|
2019-09-16 03:40:08 -05:00
|
|
|
|
2020-02-07 08:44:22 -06:00
|
|
|
// See the comment above.
|
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
#endif
|
|
|
|
//! get the type alias defined in the property (equivalent to old macro GET_PROP_TYPE(...))
|
|
|
|
template<class TypeTag, template<class,class> class Property>
|
|
|
|
using GetPropType = typename Properties::Detail::GetPropImpl<TypeTag, Property>::type::type;
|
|
|
|
|
2020-03-13 10:08:49 -05:00
|
|
|
template<class TypeTag, class SpliceTypeTag, template<class,class> class Property>
|
|
|
|
using GetSplicePropType = typename Properties::Detail::GetSplicePropImpl<TypeTag, SpliceTypeTag, Property>::type::type;
|
2020-03-13 05:25:43 -05:00
|
|
|
|
2020-02-07 08:44:22 -06:00
|
|
|
//! get the value data member of a property
|
|
|
|
template<class TypeTag, template<class,class> class Property>
|
|
|
|
constexpr auto getPropValue() { return Properties::Detail::GetPropImpl<TypeTag, Property>::type::value; }
|
2024-03-22 09:51:45 -05:00
|
|
|
|
|
|
|
//! get the name data member of a property
|
|
|
|
template<class TypeTag, template<class,class> class Property>
|
|
|
|
auto getPropName()
|
|
|
|
{
|
|
|
|
using type = typename Properties::Detail::GetPropImpl<TypeTag,Property>::type;
|
|
|
|
if constexpr (Properties::Detail::has_name<type>::value) {
|
|
|
|
return Properties::Detail::GetPropImpl<TypeTag, Property>::type::name;
|
|
|
|
} else {
|
|
|
|
std::string paramName = Dune::className<type>();
|
|
|
|
paramName.replace(0, std::strlen("Opm::Properties::"), "");
|
|
|
|
const auto pos = paramName.find_first_of('<');
|
|
|
|
paramName.erase(pos);
|
|
|
|
return paramName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 08:44:22 -06:00
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#endif
|
2019-09-16 03:40:08 -05:00
|
|
|
|
2020-02-07 08:44:22 -06:00
|
|
|
} // end namespace Opm
|
2019-09-16 03:40:08 -05:00
|
|
|
|
|
|
|
#endif
|