ENH: extend VectorSpace traits to include pTraits_cmptType

- The pTraits_cmptType returns the data type of 'cmptType' (for
  arithmetic and VectorSpace types) or is simply a pass-through.

  This can be combined with the pTraits_nComponents for casting.
  For example,

  function
  (
      reinterpret_cast<pTraits_cmptType<Type>::type*>(buf.data()),
      (buf.size()/pTraits_nComponents<Type>::value)
  );

ENH: extend Foam::identityOp so support array indexing (pass-through)
This commit is contained in:
Mark Olesen 2023-11-05 15:17:35 +01:00
parent df8efcaf62
commit 98ccb7df6b
6 changed files with 95 additions and 8 deletions

View File

@ -115,7 +115,22 @@ int main(int argc, char *argv[])
Info<<"UList: "; print(ulist);
}
{
Foam::identityOp ident;
Info<< nl << "identityOp as functor or array/map" << nl;
for (const label val : labelRange(5))
{
Info<< "value:" << val
<< " () = " << ident(val)
<< " [] = " << ident[val] << nl;
}
}
Info<< "\nEnd\n" << nl;
return 0;
}
// ************************************************************************* //

View File

@ -34,6 +34,7 @@ Description
#include "boolVector.H" // A FixedList pretending to be a vector
#include "vector.H"
#include "tensor.H"
#include "complex.H"
#include "uLabel.H"
#include "Switch.H"
@ -110,6 +111,7 @@ void printTraits()
<< " vector-space=" << Switch::name(is_vectorspace<T>::value)
<< " is_label=" << Switch::name(is_contiguous_label<T>::value)
<< " is_scalar=" << Switch::name(is_contiguous_scalar<T>::value)
<< " cmptType=" << typeid(typename pTraits_cmptType<T>::type).name()
<< endl;
}
@ -120,6 +122,12 @@ void printTraits(const pTraits<T>& p)
Info<< p.typeName << " == " << p << endl;
}
template<class T>
void printDecltype()
{
Info<< "cmptType : " << typeid(T).name() << nl;
}
#pragma GCC diagnostic warning "-Wmaybe-uninitialized"
#pragma GCC diagnostic warning "-Wuninitialized"
@ -129,12 +137,16 @@ int main()
printTraits<bool>();
printTraits<label>();
printTraits<scalar>();
printTraits<vector>();
printTraits<complex>(); // Uses specialized pTraits_...
printTraits<floatVector>();
printTraits<doubleVector>();
printTraits<tensor>();
printTraits<boolVector>();
printTraits<boolVector>(); // Uses specialized pTraits_...
printTraits<word>();
printTraits<std::string>();
Info<< nl;
{
pTraits<bool> b(true);
printTraits(b);
@ -147,6 +159,8 @@ int main()
printTraits(pTraits<scalar>(3.14159));
Info<< nl;
label abc;
Info<< "uninitialized primitive:"<< abc << endl;

View File

@ -94,6 +94,13 @@ struct identityOp
{
return std::forward<T>(val);
}
// Allow use as an identity array/map
template<class T>
constexpr T&& operator[](T&& val) const noexcept
{
return std::forward<T>(val);
}
};
@ -129,10 +136,10 @@ void Swap(T (&a)[N], T (&b)[N])
//- or that are in a state of change.
//
// SeeAlso
// - http://en.cppreference.com/w/cpp/iterator/begin
// - http://en.cppreference.com/w/cpp/iterator/end
// - http://en.cppreference.com/w/cpp/iterator/rbegin
// - http://en.cppreference.com/w/cpp/iterator/rend
// - https://en.cppreference.com/w/cpp/iterator/begin
// - https://en.cppreference.com/w/cpp/iterator/end
// - https://en.cppreference.com/w/cpp/iterator/rbegin
// - https://en.cppreference.com/w/cpp/iterator/rend
namespace stdFoam
{

View File

@ -60,6 +60,12 @@ class boolVector
{
public:
// Typedefs
//- The component type is bool
typedef bool cmptType;
// Member Constants
//- Rank of a vector is 1
@ -176,6 +182,22 @@ public:
template<> struct is_contiguous<boolVector> : std::true_type {};
/*---------------------------------------------------------------------------*\
Specialization pTraits<boolVector>
\*---------------------------------------------------------------------------*/
//- A boolVector has bool data components.
template<>
struct pTraits_cmptType<boolVector> { typedef bool type; };
//- A boolVector has three data components
template<>
struct pTraits_nComponents<boolVector>
:
std::integral_constant<Foam::direction, 3>
{};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam

View File

@ -226,6 +226,20 @@ public:
Specialization pTraits<complex>
\*---------------------------------------------------------------------------*/
//- The underlying component data type for complex is scalar.
// The regular pTraits<T>:cmptType as complex is currently (2023-11)
// likely not quite correct (issue #3018)
template<>
struct pTraits_cmptType<complex> { typedef scalar type; };
//- A complex has two scalar components
template<>
struct pTraits_nComponents<complex>
:
std::integral_constant<Foam::direction, 2>
{};
//- Template specialisation for pTraits<complex>
template<>
class pTraits<complex>
@ -274,7 +288,7 @@ public:
// Constructors
//- Copy construct from primitive
explicit pTraits(const complex& val)
explicit pTraits(const complex& val) noexcept
:
p_(val)
{}

View File

@ -52,7 +52,7 @@ namespace stdFoam
template<class... >
using void_t = void;
} // End namespace Foam
} // End namespace stdFoam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -131,6 +131,21 @@ struct pTraits_rank
{};
//- The underlying component data type: default is pass-through.
template<class T, class = void>
struct pTraits_cmptType { typedef T type; };
//- The underlying component data type for vector-space (or complex).
// Enabled when pTraits<T>:zero has a defined type (ie, exists),
// such as for arithmetic primitives, vector-space etc. where the concept
// of a component type also makes sense.
template<class T>
struct pTraits_cmptType<T, stdFoam::void_t<decltype(pTraits<T>::zero)>>
{
typedef typename pTraits<T>::cmptType type;
};
//- The vector-space number of components: default is 1.
template<class T, class = void>
struct pTraits_nComponents : std::integral_constant<Foam::direction, 1> {};