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

Finally the references into classes correctly works. Need refactor and

documentation.
parent c8d4de4b
Loading
Loading
Loading
Loading
+66 −3
Original line number Diff line number Diff line
#ifndef MACROFOREACH_H
#define MACROFOREACH_H

/*
 * Macros FOR_EACH are supposed to repeat
 * any other mocro for each argument passed as
 * variadic parameters.
*/



#define FOR_EACH_NARG(...) FOR_EACH_NARG_(__VA_ARGS__, FOR_EACH_RSEQ_N())
#define FOR_EACH_NARG_(...) FOR_EACH_ARG_N(__VA_ARGS__)
#define FOR_EACH_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, N, ...) N
@@ -83,10 +91,65 @@

#define FOR_EACH_EVEN_(N, what, ...) CONCATENATE(FOR_EACH_EVEN_, N)(what, __VA_ARGS__)
#define FOR_EACH_EVEN(what, ...) FOR_EACH_EVEN_(FOR_EACH_NARG(__VA_ARGS__), what, __VA_ARGS__)
/*

#define DUMMY
#define FOR_EACH_ODD_(N, what, ...) CONCATENATE(FOR_EACH_EVEN_, N)(what, __VA_ARGS__)
#define FOR_EACH_ODD(what, ...) FOR_EACH_EVEN_(FOR_EACH_NARG( , __VA_ARGS__), what, , __VA_ARGS__)
*/


#define FOR_EACH_2ARGS_00(what, ...)
#define FOR_EACH_2ARGS_02(what, x1, x2, ...)   what(x1, x2)
#define FOR_EACH_2ARGS_04(what, x1, x2, ...)   what(x1, x2), FOR_EACH_2ARGS_02(what, __VA_ARGS__)
#define FOR_EACH_2ARGS_06(what, x1, x2, ...)   what(x1, x2), FOR_EACH_2ARGS_04(what, __VA_ARGS__)
#define FOR_EACH_2ARGS_08(what, x1, x2, ...)   what(x1, x2), FOR_EACH_2ARGS_06(what, __VA_ARGS__)
#define FOR_EACH_2ARGS_10(what, x1, x2, ...)   what(x1, x2), FOR_EACH_2ARGS_08(what, __VA_ARGS__)
#define FOR_EACH_2ARGS_12(what, x1, x2, ...)   what(x1, x2), FOR_EACH_2ARGS_10(what, __VA_ARGS__)
#define FOR_EACH_2ARGS_14(what, x1, x2, ...)   what(x1, x2), FOR_EACH_2ARGS_12(what, __VA_ARGS__)
#define FOR_EACH_2ARGS_16(what, x1, x2, ...)   what(x1, x2), FOR_EACH_2ARGS_14(what, __VA_ARGS__)
#define FOR_EACH_2ARGS_18(what, x1, x2, ...)   what(x1, x2), FOR_EACH_2ARGS_16(what, __VA_ARGS__)
#define FOR_EACH_2ARGS_20(what, x1, x2, ...)   what(x1, x2), FOR_EACH_2ARGS_18(what, __VA_ARGS__)
#define FOR_EACH_2ARGS_22(what, x1, x2, ...)   what(x1, x2), FOR_EACH_2ARGS_20(what, __VA_ARGS__)
#define FOR_EACH_2ARGS_24(what, x1, x2, ...)   what(x1, x2), FOR_EACH_2ARGS_22(what, __VA_ARGS__)
#define FOR_EACH_2ARGS_26(what, x1, x2, ...)   what(x1, x2), FOR_EACH_2ARGS_24(what, __VA_ARGS__)
#define FOR_EACH_2ARGS_28(what, x1, x2, ...)   what(x1, x2), FOR_EACH_2ARGS_26(what, __VA_ARGS__)
#define FOR_EACH_2ARGS_30(what, x1, x2, ...)   what(x1, x2), FOR_EACH_2ARGS_28(what, __VA_ARGS__)
#define FOR_EACH_2ARGS_32(what, x1, x2, ...)   what(x1, x2), FOR_EACH_2ARGS_30(what, __VA_ARGS__)
#define FOR_EACH_2ARGS_34(what, x1, x2, ...)   what(x1, x2), FOR_EACH_2ARGS_32(what, __VA_ARGS__)
#define FOR_EACH_2ARGS_36(what, x1, x2, ...)   what(x1, x2), FOR_EACH_2ARGS_34(what, __VA_ARGS__)
#define FOR_EACH_2ARGS_38(what, x1, x2, ...)   what(x1, x2), FOR_EACH_2ARGS_36(what, __VA_ARGS__)
#define FOR_EACH_2ARGS_40(what, x1, x2, ...)   what(x1, x2), FOR_EACH_2ARGS_38(what, __VA_ARGS__)


#define FOR_EACH_2ARGS_(N, what, ...) CONCATENATE(FOR_EACH_2ARGS_, N)(what, __VA_ARGS__)
#define FOR_EACH_2ARGS(what, ...) FOR_EACH_2ARGS_(FOR_EACH_NARG(__VA_ARGS__), what, __VA_ARGS__)


#define FOR_EACH_3ARGS_1STAT_00(what, ...)
#define FOR_EACH_3ARGS_1STAT_02(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2)
#define FOR_EACH_3ARGS_1STAT_04(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2), FOR_EACH_3ARGS_1STAT_02(what, x_stat, __VA_ARGS__)
#define FOR_EACH_3ARGS_1STAT_06(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2), FOR_EACH_3ARGS_1STAT_04(what, x_stat, __VA_ARGS__)
#define FOR_EACH_3ARGS_1STAT_08(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2), FOR_EACH_3ARGS_1STAT_06(what, x_stat, __VA_ARGS__)
#define FOR_EACH_3ARGS_1STAT_10(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2), FOR_EACH_3ARGS_1STAT_08(what, x_stat, __VA_ARGS__)
#define FOR_EACH_3ARGS_1STAT_12(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2), FOR_EACH_3ARGS_1STAT_10(what, x_stat, __VA_ARGS__)
#define FOR_EACH_3ARGS_1STAT_14(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2), FOR_EACH_3ARGS_1STAT_12(what, x_stat, __VA_ARGS__)
#define FOR_EACH_3ARGS_1STAT_16(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2), FOR_EACH_3ARGS_1STAT_14(what, x_stat, __VA_ARGS__)
#define FOR_EACH_3ARGS_1STAT_18(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2), FOR_EACH_3ARGS_1STAT_16(what, x_stat, __VA_ARGS__)
#define FOR_EACH_3ARGS_1STAT_20(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2), FOR_EACH_3ARGS_1STAT_18(what, x_stat, __VA_ARGS__)
#define FOR_EACH_3ARGS_1STAT_22(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2), FOR_EACH_3ARGS_1STAT_20(what, x_stat, __VA_ARGS__)
#define FOR_EACH_3ARGS_1STAT_24(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2), FOR_EACH_3ARGS_1STAT_22(what, x_stat, __VA_ARGS__)
#define FOR_EACH_3ARGS_1STAT_26(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2), FOR_EACH_3ARGS_1STAT_24(what, x_stat, __VA_ARGS__)
#define FOR_EACH_3ARGS_1STAT_28(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2), FOR_EACH_3ARGS_1STAT_26(what, x_stat, __VA_ARGS__)
#define FOR_EACH_3ARGS_1STAT_30(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2), FOR_EACH_3ARGS_1STAT_28(what, x_stat, __VA_ARGS__)
#define FOR_EACH_3ARGS_1STAT_32(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2), FOR_EACH_3ARGS_1STAT_30(what, x_stat, __VA_ARGS__)
#define FOR_EACH_3ARGS_1STAT_34(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2), FOR_EACH_3ARGS_1STAT_32(what, x_stat, __VA_ARGS__)
#define FOR_EACH_3ARGS_1STAT_36(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2), FOR_EACH_3ARGS_1STAT_34(what, x_stat, __VA_ARGS__)
#define FOR_EACH_3ARGS_1STAT_38(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2), FOR_EACH_3ARGS_1STAT_36(what, x_stat, __VA_ARGS__)
#define FOR_EACH_3ARGS_1STAT_40(what,x_stat, x1, x2, ...)   what(x_stat, x1, x2), FOR_EACH_3ARGS_1STAT_38(what, x_stat, __VA_ARGS__)


#define FOR_EACH_3ARGS_1STAT_(N, what, x_stat, ...) CONCATENATE(FOR_EACH_3ARGS_1STAT_, N)(what, x_stat, __VA_ARGS__)
#define FOR_EACH_3ARGS_1STAT(what, x_stat, ...) FOR_EACH_3ARGS_1STAT_(FOR_EACH_NARG(__VA_ARGS__), what, x_stat, __VA_ARGS__)




#endif // MACROFOREACH_H
+110 −22
Original line number Diff line number Diff line
@@ -261,56 +261,142 @@ struct tempData {
    }

};
MAKE_ATRIBUTE_TRAITS(tempData,"density"s, &tempData::density, "momentum"s, std::make_pair(&tempData::getMomentum, &tempData::setMomentum));


//MAKE_NAMED_ATRIBUTE_TRAIT(tempData, "density", density, "velocity", velocity);
//MAKE_ATRIBUTE_TRAIT(tempData, density, velocity);

MAKE_CUSTOM_ATTRIBUTE_TRAIT(tempData, "density", &tempData::density, "momentum", std::make_pair(&tempData::getMomentum, &tempData::setMomentum))
/*
template<>
class Traits<tempData>{
public:
    static Traits<tempData, double, Vector<3,double>> tr;
    using ttype = Traits<tempData, double, Vector<3,double>>;
    const static ttype tr;
};

Traits<tempData, double, Vector<3,double>> Traits<tempData>::tr("density"s, &tempData::density, "momentum"s, std::make_pair(&tempData::getMomentum, &tempData::setMomentum));
const Traits<tempData>::ttype Traits<tempData>::tr("density", &tempData::density, "momentum"s, std::make_pair(&tempData::getMomentum, &tempData::setMomentum));
*/

using tempDataTraits = Traits<tempData, double, Vector<3,double>>;
#define STR(x) #x
#define CATTR(Class, attr) STR(typename MemberReferenceType<decltype(&Class::attr)>::type)
#define TEST_M(Class, name, attr) name, CATTR(Class, attr)
void testMemberRef(){


void testMemberRef(){
    tempData d;

    //DBGVAR(Traits<tempData>::ttype::getName<0>());

    Traits<tempData>::tr.getReference<0>()->setValue(&d, 0.0);
    DBGVAR(Traits<tempData>::tr.getReference<0>()->getValue(&d));
    Traits<tempData>::ttype::getReference<0>()->setValue(&d, 42.15);
    Traits<tempData>::ttype::getReference<1>()->setValue(&d, {42.15,84.30,42.15});

    DBGVAR(Traits<tempData>::ttype::getName<0>(),(Traits<tempData>::ttype::getReference<0>()->getValue(&d)), Traits<tempData>::ttype::getName<1>(),(Traits<tempData, double, Vector<3,double>>::getReference<1>()->getValue(&d)), d.velocity);

}

     auto temp = MemberReferenceType<decltype (&tempData::getData)>::type(42.15);
     auto temp2 = MemberReferenceType<std::pair<decltype (&tempData::getMomentum), decltype (&tempData::setMomentum)>>::type();


     DBGVAR(temp,temp2);

    static tempDataTraits tr("density"s, &tempData::density, "momentum"s, std::make_pair(&tempData::getMomentum, &tempData::setMomentum));
    tempData d;

    /*Traits<tempData, double, Vector<3,double>>::getReference<0>() = &ref; //new MemberReference<tempData, double>::SuperRef<double tempData::*>(&tempData::density);
    Traits<tempData, double, Vector<3,double>>::getReference<1>() = new MemberReference<tempData, Vector<3, double>>::SuperRef(std::make_pair(&tempData::getMomentum, &tempData::setMomentum));

#include "../Unstructured_mesh/UnstructuredMesh/MeshDataContainer/Singleton.h"
/*
Test of order of constructors
*/
    tr.getReference<0>()->setValue(&d, 0.0);
    DBGVAR(Traits<tempData>::tr.getReference<0>()->getValue(&d));
    tempDataTraits::getReference<0>()->setValue(&d, 42.15);
    tempDataTraits::getReference<1>()->setValue(&d, {42.15,84.30,42.15});

    DBGVAR((tempDataTraits::getReference<0>()->getValue(&d)), (Traits<tempData, double, Vector<3,double>>::getReference<1>()->getValue(&d)), d.velocity);
struct mem{
    std::string s;
    mem(){DBGVAR(s);}
};

template<typename statMem>
class C1 {
    template<unsigned int Index>
    struct mem{
        std::string s;
        mem(){DBGVAR(s);}
    };

public:
    C1(){
        Singleton<mem<1>>::getInstance().s = "ahoj";
        DBGVAR("C1", Singleton<mem<1>>::getInstance().s);

    }
    static std::string& getS() {return Singleton<mem<1>>::getInstance().s;}

};


template <typename dummy>
class C2 {
public:
    static C1<mem> c;
    C2() {DBGVAR(c.getS());}
};
template <typename dummy> C1<mem> C2<dummy>::c;


template<typename statMem>
class C1_wrong {

public:

    static statMem s;

    C1_wrong(){
        s.s = "ahoj";
        DBGVAR("C1_wrong", s.s);

    }
    static std::string& getS() {return s.s;}

};
template <typename statMem> statMem C1_wrong<statMem>::s;



template <typename dummy>
class C2_wrong {
public:
    static C1_wrong<mem> c;
    C2_wrong() {DBGVAR(c.getS());}
};
template <typename dummy> C1_wrong<mem> C2_wrong<dummy>::c;



class C1_ {
public:
    static mem s1;
    C1_(){
        s1.s = "ahoj";
        DBGVAR("C1_", s1.s);

    }

};

mem C1_::s1;

class C2_ {
public:
    static C1_ c;
    C2_(){DBGVAR(c.s1.s);}
};
C1_ C2_::c;


using C = C1<mem>;

void testConstrucorOrder() {
    C2_ c;
    C2<void> c1;
    DBGVAR(C2_::c.s1.s, C2<void>::c.getS(), C::getS(), C2_wrong<void>::c.getS());
}


void testOrig() {
@@ -381,16 +467,18 @@ public:
int main()
{

    Base b1(0.0);
    /*Base b1(0.0);

    Base b2(std::pair<char,int>{'1',3});

    DBGVAR(b2.first,b2.second);
    */
    testMemberRef();

    testConstrucorOrder();
/*
    std::function<int(int)> fce = [&b1](int i){return b1.data + 42 + i;};

    std::cout << fce(2);

*/
    return 0;
}
+35 −0
Original line number Diff line number Diff line
#ifndef SINGLETON_H
#define SINGLETON_H
#include <type_traits>



template<class Class>
/**
 * @brief The Singleton class
 */
class Singleton {
    //static_assert (std::is_trivially_constructible<Class>::value, "The class in singleton must be trivially constructible.");
public:
    static Class& getInstance(){
        if (p == nullptr) {
            p = new Class; // the class must be trivially constructible
        }
        return *p;
    }
protected:
    static Class* p;
    Singleton() {}
private:
    // disable move and copy options
    Singleton(Singleton const&) = delete;
    Singleton(Singleton const&&) = delete;
    Singleton& operator=(Singleton const&) = delete;
    Singleton& operator=(Singleton const&&) = delete;

};
template <class Class> Class* Singleton<Class>::p = nullptr;



#endif // SINGLETON_H
+47 −25
Original line number Diff line number Diff line
#ifndef TRAITS_H
#define TRAITS_H
#include "MemberApproach.h"
#include "../debug/Debug.h"
#include <string>
#include <memory>

#include "Singleton.h"

template<typename Class, typename...Types>
class Traits {
@@ -15,31 +16,46 @@ class Traits {
    struct MemRefs: public MemRefs<Index - 1>{
        std::unique_ptr<MemberApproach<Class, type<Index>>> ref = nullptr;
        std::string name;
        MemRefs(){
            DBGVAR(this->name, Index);
        }
    };

    template<typename Dummy>
    struct MemRefs<0, Dummy>{
        std::unique_ptr<MemberApproach<Class, type<0>>> ref = nullptr;
        std::string name;
        MemRefs(){
            DBGVAR(this->name, 0);
        }
    };


    using refs = Singleton<MemRefs<sizeof... (Types) - 1, void>>;

    template<unsigned int Pos = 0, typename ref, typename...Refs>
    static void _makeReferences(std::string name, ref member,Refs... refsAndNames) {
    static void _makeReferences(const std::string& name, ref member,Refs... refsAndNames) {
        _makeReferences<Pos, ref>(name, member);
        _makeReferences<Pos+1>(refsAndNames...);
    }

    template<unsigned int Pos, typename ref>
    static void _makeReferences(std::string name, ref member) {
        refs.MemRefs<Pos, void>::name = name;
    static void _makeReferences(const std::string& name, ref member) {
        refs::getInstance().MemRefs<Pos, void>::name = name;
        DBGVAR((refs::getInstance().MemRefs<Pos, void>::name));
        getReference<Pos>() = std::unique_ptr<MemberApproach<Class, type<Pos>>>(new typename MemberReference<Class, type<Pos>>::template SuperRef<ref>(member));
    }


    template<unsigned int Pos, typename ref>
    static void _makeReferences(const char* name, ref member) {
        refs::getInstance().MemRefs<Pos, void>::name = name;
        DBGVAR((refs::getInstance().MemRefs<Pos, void>::name));
        getReference<Pos>() = std::unique_ptr<MemberApproach<Class, type<Pos>>>(new typename MemberReference<Class, type<Pos>>::template SuperRef<ref>(member));
    }


public:
    static MemRefs<sizeof... (Types) - 1, void> refs;



    static constexpr unsigned int size(){
@@ -49,13 +65,20 @@ public:

    template<unsigned int Index>
    static std::unique_ptr<MemberApproach<Class, type<Index>>>& getReference(){
        return refs.MemRefs<Index, void>::ref;
        return refs::getInstance().MemRefs<Index, void>::ref;
    }

    template<unsigned int Index>
    static std::string& getName(){
        return refs::getInstance().MemRefs<Index, void>::name;
    }


    template<typename...Refs>
    Traits(Refs... refsAndNames){
        DBGMSG("making refs");
        makeReferences(refsAndNames...);
        DBGVAR(getName<0>(), getName<1>());
    }

    template<typename...Refs>
@@ -65,8 +88,6 @@ public:

};

template<typename Class, typename... Types>
typename Traits<Class, Types...>::template MemRefs<sizeof... (Types) - 1> Traits<Class,Types...>::refs;



@@ -96,32 +117,33 @@ struct MemberReferenceType <std::pair<MemberType (Class::*)(), void (Class::*)(c


template<typename Class>
class Traits<Class>{
class Traits<Class>: public std::false_type{
    static_assert (true, "The Traits template must be specialized for given type and must contain Traits references using variadic Traits.");
};



#include "../../../Macros/MacroForEach.h"


#define _MAKE_ATRIBUTE_TRAITS(Class, ...)    \
#define MEMREF_TYPE_CUSTOM(name, memberRef) typename MemberReferenceType<decltype(memberRef)>::type
#define MAKE_CUSTOM_ATTRIBUTE_TRAIT(Class,...)\
template<>                              \
class Traits<Class>{                 \
class Traits<Class>: public std::true_type{                 \
public:                             \
    static Traits<Class, double, Vector<3,double>> tr;   \
    using ttype = Traits<Class, FOR_EACH_2ARGS(MEMREF_TYPE_CUSTOM, __VA_ARGS__)>; \
    const static ttype tr;   \
    Traits(){DBGMSG("TRAITS");}\
}; \
Traits<Class, double, Vector<3,double>> Traits<Class>::   \
tr(__VA_ARGS__); \
const Traits<Class>::ttype Traits<Class>::tr(__VA_ARGS__);

#define NAME_AND_REF(Class, name, member) name, &Class::member

#define MAKE_NAMED_ATRIBUTE_TRAIT(Class, ...) MAKE_CUSTOM_ATTRIBUTE_TRAIT(Class, FOR_EACH_3ARGS_1STAT(NAME_AND_REF, Class, __VA_ARGS__))

#define NAME_ATT(attribute) #attribute, attribute
#define MAKE_ATRIBUTE_TRAIT(Class, ...) MAKE_NAMED_ATRIBUTE_TRAIT(Class, FOR_EACH(NAME_ATT, __VA_ARGS__))

#define MAKE_ATRIBUTE_TRAITS(Class, ...)    \
template<>                              \
class Traits<Class>{                 \
public:                                 \
    static Traits<Class, double, Vector<3,double>> tr;   \
};                                      \
Traits<Class, double, Vector<3,double>> Traits<Class>::   \
tr(__VA_ARGS__); \


#endif // TRAITS_H
+1 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ HEADERS += \
    InlineArrayOperations.h \
    UnstructuredMesh/MeshDataContainer/MemberApproach.h \
    UnstructuredMesh/MeshDataContainer/MeshDataContainer.h \
    UnstructuredMesh/MeshDataContainer/Singleton.h \
    UnstructuredMesh/MeshDataContainer/Traits.h \
    UnstructuredMesh/MeshElements/CellBoundaryConnection.h \
    UnstructuredMesh/MeshElements/CellConnection.h \
Loading