Commit 3ee441e2 authored by Tomáš Jakubec's avatar Tomáš Jakubec
Browse files

operator[] for traited classes

parent bedc1e62
Loading
Loading
Loading
Loading
+51 −5
Original line number Diff line number Diff line
#define CONSOLE_COLORED_OUTPUT
#include "../src/Debug/Debug.h"
#include "../src/Traits/TraitsAlgorithm/TraitsAlgorithm.h"
//#include "../src/Traits/Traits.h"
#include "../src/UnstructuredMesh/UnstructuredMesh.h"
#include "../src/Traits/MemberApproach/MemberApproach.h"
#include "../src/Traits/Traits.h"
#include "../src/Singleton/Singleton.h"
#include <chrono>
#include <functional>
@@ -296,7 +297,7 @@ struct ExportTest {
MAKE_ATTRIBUTE_TRAIT(ExportTest, attrInt, attrDouble, attrChar, attrStr, attrTempData, attrVec);
MAKE_ATTRIBUTE_TRAIT_ARITHMETIC(ExportTest, attrInt, attrDouble, attrTempData);

#include "../src/Traits/TraitsAlgorithm/TraitsAlgorithm.h"


struct dataStruct {
    int iD;
@@ -334,10 +335,43 @@ struct NumStruct2 {
    double data2;

    NumStruct2(double d1 = 0.0, double d2 = 0.0): data1(d1), data2(d2){}

    template<unsigned int... Idxs>
    auto& operator[](integer_sequence<unsigned int, Idxs...>){
        return getTraitedAttribute<Idxs...>(*this);
    }
};
MAKE_ATTRIBUTE_TRAIT(NumStruct2, data1, data2);


struct TraitedStruct {
    Vector<3, double>sarr = {1,2,3};
};
MAKE_ATTRIBUTE_TRAIT(TraitedStruct, sarr);
namespace ns {


class TraitedClass{
public:
    double d1 = 1;
    double d2 = 2;
    TraitedStruct ts;
public:
    explicit TraitedClass() {DBGMSG("TC construtor");}
    //TraitedClass(const TraitedClass& rhs) = default;
    //TraitedClass(TraitedClass&&) = default;
    friend class Traits<TraitedClass>;

    template<unsigned int... Idxs>
    auto& operator[](integer_sequence<unsigned int, Idxs...>){
        return getTraitedAttribute<Idxs...>(*this);
    }
};
}


MAKE_ATTRIBUTE_TRAIT(ns::TraitedClass, d1, d2, ts);

void testTraitsAlgorithms() {
    ExportTest e1, e2;
    ExportTest res = e1 + e2;
@@ -351,8 +385,20 @@ void testTraitsAlgorithms() {
           max(-(ns + 4 * ns2)),
           max(abs(-(ns + 4 * ns2))),
           log(ns), exp(log(ns)),
           pow(sqrt(ns), 2)
           pow(sqrt(ns), 2),
           abs(pow(e1,2))
           );

    DBGVAR(abs(ns::TraitedClass()));

    std::vector<ns::TraitedClass> vv;
    vv.resize(5, ns::TraitedClass());

    integer_sequence<unsigned int, 0> d1;
    integer_sequence<unsigned int, 1> d2;
    integer_sequence<unsigned int, 2,0> sarr;
    DBGVAR(abs(vv), pow(vv,2.5), ns[d1], vv[0][d1], vv[0][d2], vv[0][sarr]);

}


@@ -2374,8 +2420,8 @@ int main()
    //testPrivateTrait();
    //testJson();
    //testTestTraits();
    //testTraitsAlgorithms();
    testNumericTraitsPerformance();
    testTraitsAlgorithms();
    //testNumericTraitsPerformance();
    //testTraitsTuple();
    //testFactorial();
    return 0;
+2 −2
Original line number Diff line number Diff line
@@ -313,13 +313,13 @@ struct TraitedAttributeGetter<Index>{
} //Impl


template <typename ArythmeticTraitT, unsigned int ...Indexes, typename = typename std::enable_if<HasDefaultArithmeticTraits<ArythmeticTraitT>::value>::type>
template <unsigned int ...Indexes, typename ArythmeticTraitT, typename = typename std::enable_if<HasDefaultArithmeticTraits<ArythmeticTraitT>::value>::type>
auto& getTraitedAttribute(ArythmeticTraitT& arg){
    return Impl::TraitedAttributeGetter<Indexes...>::get(arg);
}


template <typename ArythmeticTraitT, unsigned int ...Indexes, typename = typename std::enable_if<HasDefaultArithmeticTraits<ArythmeticTraitT>::value>::type>
template <unsigned int ...Indexes, typename ArythmeticTraitT, typename = typename std::enable_if<HasDefaultArithmeticTraits<ArythmeticTraitT>::value>::type>
auto& getTraitedAttribute(ArythmeticTraitT* arg){
    return Impl::TraitedAttributeGetter<Indexes...>::get(arg);
}
+100 −48
Original line number Diff line number Diff line
@@ -463,9 +463,9 @@ operator/(TraitT&& op1, const Real& op2) noexcept {
template< typename T1, typename T2 >
struct Pow
{
   static auto evaluate( const T1& a, const T2& b ) -> decltype( std::pow(a,b) )
   static auto evaluate( const T1& a, const T2& b ) -> decltype( pow(a,b) )
   {
      return std::pow(a,b);
      return pow(a,b);
   }
};

@@ -510,6 +510,49 @@ pow(const Real& op1, TraitT&& op2) noexcept {
}


/*
template <typename T, typename Real>
typename std::enable_if<
    !std::is_class<T>::value,
    T
>::type
pow(const T& arg, const Real& p) noexcept {

    return  std::pow(arg, p);
}
*/
template <typename T, typename Real>
typename std::enable_if<
    IsIndexable<T>::value,
    T
>::type
pow(const T& array, const Real& p) noexcept {

    T resArray(array);

    for (decltype (resArray.size()) index = 0; index < resArray.size(); index++){
        resArray[index] = pow(resArray[index], p);

    }
    return  resArray;
}

template <typename T,typename Real>
typename std::enable_if<
    IsTNLIndexable<T>::value,
    T
>::type
pow(const T& array, const Real& p) noexcept {

    T resArray(array);

    for (decltype (resArray.getSize()) index = 0; index < resArray.getSize(); index++){
        resArray[index] = pow(resArray[index], p);

    }
    return  resArray;
}


////
/// Exp
@@ -554,7 +597,7 @@ exp(const TraitT& op1) noexcept {


////
/// Exp
/// Log
///

template< typename T1 >
@@ -790,12 +833,16 @@ operator-(TraitT&& op1) noexcept {
template< typename T1 >
struct Abs
{
   static auto evaluate( const T1& a ) -> decltype( std::abs(a) )
   {
      return std::abs(a);

    static auto evaluate( const T1& a ) -> T1 {
        return abs(a);
    }

};




template <typename TraitT>
typename std::enable_if<HasDefaultArithmeticTraits<TraitT>::value, TraitT>::type
abs(const TraitT& op1) noexcept {
@@ -816,6 +863,51 @@ abs(TraitT&& op1) noexcept {



template <typename T>
typename std::enable_if<
    !std::is_class<T>::value,
    T
>::type
abs(const T& arg) noexcept {

    return  std::abs(arg);
}

template <typename T>
typename std::enable_if<
    IsIndexable<T>::value,
    T
>::type
abs(const T& array) noexcept {

    T resArray(array);

    for (decltype (resArray.size()) index = 0; index < resArray.size(); index++){
        resArray[index] = abs(resArray[index]);

    }
    return  resArray;
}
/*
template <typename T>
typename std::enable_if<
    IsTNLIndexable<T>::value,
    T
>::type
abs(const T& array) noexcept {

    T resArray(array);

    for (decltype (resArray.getSize()) index = 0; index < resArray.getSize(); index++){
        resArray[index] = abs(resArray[index]);

    }
    return  resArray;
}
*/






@@ -1003,50 +1095,10 @@ min(const TraitT& op1) noexcept {



template <typename T>
typename std::enable_if<
    !std::is_class<T>::value,
    T
>::type
abs(const T& arg) noexcept {

    return  std::abs(arg);
}

template <typename T>
typename std::enable_if<
    IsIndexable<T>::value,
    T
>::type
abs(const T& array) noexcept {

    T resArray(array);

    for (decltype (resArray.size()) index = 0; index < resArray.size(); index++){
        resArray[index] = abs(resArray[index]);

    }
    return  resArray;
}


template <typename T>
typename std::enable_if<
    IsTNLIndexable<T>::value,
    T
>::type
abs(const T& array) noexcept {

    T resArray(array);

    for (decltype (resArray.getSize()) index = 0; index < resArray.getSize(); index++){
        resArray[index] = abs(resArray[index]);

    }
    return  resArray;
}

#endif // TRAITSALGORITHM_H


#endif // TRAITSALGORITHM_H