classPiiArithmeticBase
#include <PiiArithmeticBase.h>
A template that adds support for arithmetic operators for any data structure that can be iterated over.
Inherited by PiiVector< double, 3 >
Description
This class is intended to be used with the CRTP (curiously
recurring template pattern). The derived class must have have
stl-style begin() and end() functions for
iterators. Information about the element type itself is provided
through the helper type Traits. The required type
definitions are shown in the example below.
// Forward declaration for the rebinder in MyTraits. template <class T> class MyClass; template <class T> struct MyTraits { // The element type typedef T Type; // Iterator type typedef T* Iterator; // Const iterator type typedef const T* ConstIterator; // Rebinder creates a MyClass types with varying content types. template <class U> struct Rebind { typedef MyClass<U> Type; }; }; template <class T> class MyClass : public PiiArithmeticBase<MyClass<T>, MyTraits<T> > { public: typedef PiiArithmeticBase<MyClass<T>, MyTraits<T> > BaseType; // Required iterator functions typename BaseType::ConstIterator begin() const { return _pData; } // const iterator to the beginning typename BaseType::Iterator begin() { return _pData; } // iterator to the beginning typename BaseType::ConstIterator end() const { return _pData + 5; } // const iterator to the end typename BaseType::Iterator end() { return _pData + 5; } // const iterator to the end // The following aren't necessarily required if your class is a POD type // Standard onstructors (implementations may vary) MyClass() { BaseType::operator=(0); } MyClass(const MyClass& other) { *this = other; } // Copy assignment isn't inherited... MyClass& operator= (const MyClass& other) { return BaseType::operator=(other); } private: T _pData[5]; }; // Now, you can do this: MyClass a, b; MyClass c = a + b;
The length of the vector/array is assumed to be fixed. That is, all instances of the derived class should hold the same amount of elements. If this is not the case, your program will (in the best case) crash.
Public types
|
typedef Traits::ConstIterator
|
Stl-style const iterator. |
|
typedef Traits::ConstIterator
|
Qt-style const iterator. |
|
typedef Traits::Iterator
|
Stl-style iterator. |
|
typedef Traits::Iterator
|
Qt-style iterator. |
|
typedef Traits::Type
|
A shorthand for the content type of the derived class. |
|
typedef Traits
|
A typedef for the traits template parameter. |
Public member functions
|
template<class Operation>
Traits::template Rebind< typename Operation::result_type
>::Type
|
(
Apply a binary function to all elements using the corresponding
elements in |
|
template<class Operation>
void
|
(
Apply a binary function to all elements using the corresponding
elements in |
|
template<class Operation>
Traits::template Rebind< typename Operation::result_type
>::Type
|
(
Apply a binary function to all elements using |
|
template<class Operation>
void
|
(
Apply a binary function to all elements using |
|
( )
Returns an stl-style const iterator to the beginning of data. |
|
|
( )
Returns an stl-style const iterator to the end of data. |
|
|
bool
|
(
Returns |
|
Derived
|
(
Multiply corresponding elements by each other. |
|
Derived
|
Multiply all elements by |
|
void
|
Multiply all elements by |
|
void
|
(
Multiply corresponding elements in this by |
|
Derived
|
Add |
|
Derived
|
(
Sum corresponding elements. |
|
void
|
(
Add corresponding elements in |
|
void
|
Add |
|
Derived
|
(
Subtract corresponding elements from each other. |
|
Derived
|
( )
Create a negation of all elements. |
|
Derived
|
Subtract |
|
void
|
(
Subtract corresponding elements in |
|
void
|
Subtract |
|
Derived
|
(
Divide corresponding elements by each other. |
|
Derived
|
Divide all elements by |
|
void
|
(
Divide corresponding elements in this by |
|
void
|
Divide all elements by |
|
Derived &
|
(
Assign the values in |
|
Derived &
|
Set all elements to |
|
bool
|
(
Returns |
|
template<class Operation>
Traits::template Rebind< typename Operation::result_type
>::Type
|
(
Apply a unary function to all elements. |
|
template<class Operation>
void
|
(
Apply a unary function to all elements. |
Function details
-
template<class Operation>
Traits::template Rebind< typename Operation::result_type >::Type binaryOp
(- Operation op
- const typename Traits::template Rebind< typename Operation::second_argument_type >::Type & other
[inline]Apply a binary function to all elements using the corresponding elements in
thisandotheras function parameters.Return a new object.
-
template<class Operation>
void binaryOp
(- Operation op
- const typename Traits::template Rebind< typename Operation::second_argument_type >::Type & other
[inline]Apply a binary function to all elements using the corresponding elements in
thisandotheras function parameters.Modify elements in place.
-
template<class Operation>
Traits::template Rebind< typename Operation::result_type >::Type binaryOp
(- Operation op
- typename Operation::second_argument_type value
[inline]Apply a binary function to all elements using
valueas the second function parameter.Return a new object.
-
template<class Operation>
void binaryOp
(- Operation op
- typename Operation::second_argument_type value
[inline]Apply a binary function to all elements using
valueas the second function parameter.Modify elements in place.
-
Returns an stl-style const iterator to the beginning of data.
-
Returns an stl-style const iterator to the end of data.
-
bool operator!=
(- const Derived & other
[inline]Returns
falseif all elements inthisandotherare equal,trueotherwise. -
Derived operator*
(- const Derived & other
[inline]Multiply corresponding elements by each other.
-
Multiply all elements by
value. -
Multiply all elements by
value. -
void operator*=
(- const Derived & other
[inline]Multiply corresponding elements in this by
other. -
Add
valueto all elements. -
Derived operator+
(- const Derived & other
[inline]Sum corresponding elements.
-
void operator+=
(- const Derived & other
[inline]Add corresponding elements in
otherto this. -
Add
valueto all elements. -
Derived operator-
(- const Derived & other
[inline]Subtract corresponding elements from each other.
-
Derived operator-
()[inline]Create a negation of all elements.
-
Subtract
valuefrom all elements. -
void operator-=
(- const Derived & other
[inline]Subtract corresponding elements in
otherfrom this. -
Subtract
valuefrom all elements. -
Derived operator/
(- const Derived & other
[inline]Divide corresponding elements by each other.
-
Divide all elements by
value. -
void operator/=
(- const Derived & other
[inline]Divide corresponding elements in this by
other. -
Divide all elements by
value. -
Derived & operator=
(- const Derived & other
[inline]Assign the values in
otherto this. -
Set all elements to
value. -
bool operator==
(- const Derived & other
[inline]Returns
trueif all elements inthisandotherare equal,falseotherwise. -
template<class Operation>
Traits::template Rebind< typename Operation::result_type >::Type unaryOp
(- Operation op
[inline]Apply a unary function to all elements.
Return a new object.
-
template<class Operation>
void unaryOp
(- Operation op
[inline]Apply a unary function to all elements.
Modify the elements in place.
Add a note
Not a single note added yet. Be the first, add yours.