Commit 000f704e authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: FixedList empty(), size(), max_size() now constexpr static (#1160)

- this allows their use as templates parameters
parent c52d70cc
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -68,6 +68,36 @@ Ostream& printInfo
}
template<class T, unsigned N>
void compileInfo()
{
// Info<< typeid(decltype(FixedList<T, N>)).name() << nl;
// Info<< " holds: "
// << typeid(decltype(FixedList<T, N>::value_type())).name() << nl;
Info<< "max_size:"
<< FixedList<T, N>::max_size() << nl;
}
template<class FixedListType>
typename std::enable_if
<(FixedListType::max_size() == 2), bool>::type
is_pair()
{
return true;
}
template<class FixedListType>
typename std::enable_if<(FixedListType::max_size() != 2), std::string>::type
is_pair()
{
return "not really at all";
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
......@@ -86,6 +116,18 @@ int main(int argc, char *argv[])
args.found("default") || args.options().empty();
typedef FixedList<scalar,2> scalar2Type;
typedef FixedList<label,3> label3Type;
// Compile-time info
compileInfo<label, 5>();
Info<< "pair: " << is_pair<scalar2Type>() << nl;
Info<< "pair: " << is_pair<label3Type>() << nl;
Info<< "max_size:" << scalar2Type::max_size() << nl;
if (defaultTests || args.found("iter"))
{
Info<< nl
......
......@@ -90,7 +90,7 @@ protected:
// Protected Member Functions
//- True if there are two or more entries and all entries have
// identical values.
//- identical values.
inline bool uniform() const;
//- Write the FixedList with its compound type
......@@ -99,7 +99,7 @@ protected:
public:
// STL type definitions
// STL Type Definitions
//- The value type the FixedList contains
typedef T value_type;
......@@ -197,16 +197,16 @@ public:
// This can be used (with caution) when interfacing with C code
inline T* data();
//- Return the first element of the list
//- The first element of the list, position [0]
inline T& first();
//- Return first element of the list
//- The first element of the list, position [0]
inline const T& first() const;
//- Return the last element of the list
//- The last element of the list, position [N-1]
inline T& last();
//- Return the last element of the list
//- The last element of the list, position [N-1]
inline const T& last() const;
......@@ -279,7 +279,7 @@ public:
inline void transfer(FixedList<T, N>& list);
// Member operators
// Member Operators
//- Return element of FixedList
inline T& operator[](const label i);
......@@ -357,22 +357,22 @@ public:
inline const_reverse_iterator rend() const;
// STL member functions
// STL Member Functions
//- Return the number of elements in the FixedList
inline label size() const;
//- Always false since zero-sized FixedList is compile-time disabled.
static constexpr bool empty() noexcept { return !N; }
//- Return size of the largest possible FixedList
inline label max_size() const;
//- Return the number of elements in the FixedList
static constexpr label size() noexcept { return N; }
//- Always false since zero-sized FixedList is compile-time disabled.
inline bool empty() const;
//- The dimensioned size (template parameter N) of the FixedList
static constexpr unsigned max_size() noexcept { return N; }
//- Swap lists by swapping the content of the individual list elements
inline void swap(FixedList<T, N>& list);
// STL member operators
// STL Member Operators
//- Equality operation on FixedLists of the same type.
// Returns true when the FixedLists are element-wise equal
......
......@@ -548,27 +548,6 @@ Foam::FixedList<T, N>::crend() const
}
template<class T, unsigned N>
inline Foam::label Foam::FixedList<T, N>::size() const
{
return N;
}
template<class T, unsigned N>
inline Foam::label Foam::FixedList<T, N>::max_size() const
{
return N;
}
template<class T, unsigned N>
inline bool Foam::FixedList<T, N>::empty() const
{
return false;
}
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
template<class T, unsigned N>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment