// @HEADER // ***************************************************************************** // Sacado Package // // Copyright 2006 NTESS and the Sacado contributors. // SPDX-License-Identifier: LGPL-2.1-or-later // ***************************************************************************** // @HEADER #ifndef SACADO_TEMPLATE_CONTAINER_HPP #define SACADO_TEMPLATE_CONTAINER_HPP #include // While this code does not directly use C++11 features, it uses mpl::vector, // which does #include "Sacado_ConfigDefs.h" #include "Sacado_mpl_size.hpp" #include "Sacado_mpl_find.hpp" #include "Sacado_mpl_for_each.hpp" #include "Sacado_mpl_apply.hpp" #include "Sacado_mpl_begin.hpp" #include "Sacado_mpl_end.hpp" #include "Sacado_mpl_deref.hpp" #include "Sacado_mpl_next.hpp" #include "Sacado_mpl_enable_if.hpp" namespace Sacado { namespace Impl { // Forward declaration template ::type, typename Iter2 =typename mpl::end::type> struct TupleSeq; // Forward declaration template ::type, typename Iter2 = typename mpl::end::type, typename Enabled = void> struct GetTupleSeq; } // namespace Impl //! Container class to manager template instantiations of a template class /*! * This class provides a generic container class for managing multiple * instantiations of a class ObjectT with the values of T specified * through a type sequence. Objects of type ObjectT must have value * semantics, be default constructable and copyable/assignable (for objects * that don't satisfy these requirements, one would typically wrap them in * a (smart) pointer. Methods are provided to access the object of type * ObjectT for a given type T, as well as initialize them. One would * typically operate on the contained objects using mpl::for_each(), e.g., * * template class MyClass; * typedef mpl::vector< double, Fad::DFad > MyTypes; * typedef TemplateContainer< MyTypes, MyClass<_> > MyObjects; * MyObjects myObjects; * mpl::for_each([](auto x) { * typedef decltype(x) T; * myObjects.get() = T; * }); * * (Note: This requires generalized lambda's introduced in C++14. For * C++11, provide a functor that implements the lambda body). */ template class TemplateContainer { //! Our container for storing each object typedef Impl::TupleSeq tuple_type; //! Helper class for building objects in container template struct BuildObject { tuple_type& objects; const BuilderOpT& builder; BuildObject(tuple_type& objects_, const BuilderOpT& builder_) : objects(objects_), builder(builder_) {} template void operator()(T x) const { Impl::GetTupleSeq::apply(objects) = builder(x); } }; public: //! Type sequence containing template types typedef TypeSeq types; //! The default builder class for building objects for each ScalarT struct DefaultBuilderOp { //! Returns a new object of type ObjectT template typename Sacado::mpl::apply::type operator() (T) const { return typename Sacado::mpl::apply::type(); } }; //! Default constructor TemplateContainer() {} //! Destructor ~TemplateContainer() {} //! Get object corrensponding ObjectT template typename Sacado::mpl::apply::type& get() { return Impl::GetTupleSeq::apply(objects); } //! Get object corrensponding ObjectT template const typename Sacado::mpl::apply::type& get() const { return Impl::GetTupleSeq::apply(objects); } //! Build objects for each type T template void build(const BuilderOpT& builder) { Sacado::mpl::for_each_no_kokkos( BuildObject(objects,builder)); } private: //! Stores type of objects of each type tuple_type objects; }; // Wrap call to mpl::for_each so you don't have to specify the container // or type sequence template void container_for_each(TemplateContainer& container, const FunctorT& op) { typedef TemplateContainer Container; Sacado::mpl::for_each f(op); } // Wrap call to mpl::for_each so you don't have to specify the container // or type sequence template void container_for_each_no_kokkos(TemplateContainer& container, const FunctorT& op) { typedef TemplateContainer Container; Sacado::mpl::for_each_no_kokkos f(op); } namespace mpl { // Give TemplateContainer begin<> and end<> iterators for for_each template struct begin< TemplateContainer > { typedef typename begin::type type; }; template struct end< TemplateContainer > { typedef typename end::type type; }; } namespace Impl { // Container class to store our tuple sequence template struct TupleSeq : TupleSeq::type, Iter2> { typedef typename mpl::apply::type>::type type; type tail; }; template struct TupleSeq {}; // Helper class to get a value out of the tuple sequence from a given type template struct GetTupleSeq {}; template struct GetTupleSeq< T, TypeSeq, ObjectT, Iter1, Iter2, typename mpl::enable_if_c< std::is_same< T, typename mpl::deref::type >::value >::type > { static typename TupleSeq::type& apply(TupleSeq& t) { return t.tail; } static const typename TupleSeq::type& apply(const TupleSeq& t) { return t.tail; } }; template struct GetTupleSeq< T, TypeSeq, ObjectT, Iter1, Iter2, typename mpl::enable_if_c< !std::is_same< T, typename mpl::deref::type >::value >::type > : GetTupleSeq< T, TypeSeq, ObjectT, typename mpl::next::type, Iter2> {}; template struct GetTupleSeq< T, TypeSeq, ObjectT, Iter1, Iter1, void> {}; } // namespace Impl } #endif