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

Traits readme

parent 24821406
Loading
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -8,6 +8,6 @@ of any dimension and topology. Furthermore, the library provides additional tool
during the development of UnstructuredMesh and its functionalities.

The tools developed as part of GTMesh:
- [unstructured mesh](src/UnstructuredMesh/) with simple and user friendly inferface
- user friendly, simple and generic [debugging tool](src/Debug/)
- [Traits](src/Traits/), a tool describing C++ data structures and
- [unstructured mesh](src/GTMesh/UnstructuredMesh/) with simple and user friendly inferface
- user friendly, simple and generic [debugging tool](src/GTMesh/Debug/)
- [Traits](src/GTMesh/Traits/), a tool describing C++ data structures and
+1 −0
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ HEADERS += \
    ../src/UnitTests/UnstructuredMesh/MeshSetup.h

DISTFILES += \
    ../src/GTMesh/Traits/README.md \
    ../src/UnitTests/Debug/DBGVAR_JSONTest.cpp \
    ../src/UnitTests/Debug/VariableExportTest.cpp \
    ../src/UnitTests/Traits/ArithmeticTraitsTest.cpp \
+125 −0
Original line number Diff line number Diff line
## Class Traits
___
class traits provided by the Traits class template is a tool
for annotation of classes and structures. Traits provides
unified access to the attributes the classes.

Presentation of the MemberAccess class utilized by the Traits class:
```c++
class qties {
        double density;
        Vector<3, double> momentum;

        Vector<3, double> getVelocity(){return momentum/density;}
        void setVelocity(const Vector<3, double> velocity){momentum = velocity * density;}
}qInstance;

MemberAccess refDensity(&qties::density);
MemberAccess refMomentum(&qties::momentum);
MemberAccess refVelocity(std::make_pair(&qties::getVelocity, &qties::setVelocity));

refDensity.getAttr(qInstance) = 3; // Set the density directly

// When setting the value of velocity, the value of density must be already set
refVelocity.setValue(qInstance, {1,2,3}); // Set the velocity using the set function

refMomentum.getValue(qInstance); // Returns the copy of the momentum, i.e. {3, 6, 9}
```


### Default Traits
The concept of class traits works with the term default traits.
The default traits denotes a system of publishing a single
instance of Traits for a traited class (the class the traits are defined for).
The publishing of the default traits is done by specialization of the
DefautlTraits (or DefautlIOTraits, DefautlArithmeticTraits) class.
The specialization must have the following members:
- `traitsType`: alias of the utilized Traits class template,
- `getTraits`: member function returning the global instance of class traits,
- `size`: member function returning the number of traited attributes (number
of attributes the class traits accesses).

For an easier definition of the specialization of DefaultTraits serves the following macros:
- `MAKE_CUSTOM_TRAITS`: creates a specialization of DefaultTraits from various types of member accesses (e.g.
pointer to a member, a pair of getter and setter (member or global) functions) and names given to the attributes,
- `MAKE_NAMED_ATTRIBUTE_TRAITS`: creates a specialization of DefaultTraits from the names of the member attributes
of the traited class and names given to the attributes,
- `MAKE_ATTRIBUTE_TRAITS`: creates a specialization of DefaultTraits from the names of the member attributes
of the traited class and names given to the attributes.

Similarly to the MAKE_CUSTOM_TRAITS and other macros, there are macros MAKE_CUSTOM_TRAITS_IO and MAKE_CUSTOM_TRAITS_ARITHMETIC
following the same name conventions.

By default the DefautlIOTraits and DefautlArithmeticTraits inheriths the DefaultTraits.
Therefore, when the specialization of DefaultTraits is defined. It is automatically
utilized by DefautlIOTraits and DefautlArithmeticTraits. However, it is still possible
to override the system implementation of DefautlIOTraits and DefautlArithmeticTraits
by creating an appropriate specialization.

The DefautlIOTraits class is to be utilized for input and output of the instances of the traited classes
(e.g. JSON or VTK output). The DefautlArithmeticTraits is utilized in the automatically generated arithmetic
operations for the traited classes (e.g. max, min, operator+, etc.) see [TraitsAlgorithm](./TraitsAlgorithm).

Additionally, there is a serie of macros defining default traits for class templates. Those macros
folloes the same naming conventions and have `_TEMPLATE` prepended to `TRAIT` (e.g. MAKE_CUSTOM_TEMPLATE_TRAIT).

### Examples
___
Example of definition of traits for class template
```c++
struct flowQ{
        double rho; // density
        Vector<3, double> p; // momentum
};

MAKE_CUSTOM_TRAIT(flowQ, "density", &flowQ::rho, "momentum", &flowQ::p);
MAKE_NAMED_ATTRIBUTE_TRAIT(flowQ, "density", rho, "momentum", p);
MAKE_ATTRIBUTE_TRAIT(flowQ, rho, p);
```

An example of definition different DefautlIOTraits and DefautlArithmeticTraits for
single traited class.
```c++
class qties {
        double density;
        Vector<3, double> momentum;

        Vector<3, double> getVelocity(){return momentum/density;}
        void setVelocity(const Vector<3, double> velocity){momentum = velocity * density;}
};

MAKE_CUSTOM_TRAIT_IO(flowQ, "density", &flowQ::rho, "velocity", std::make_pair(&qties::getVelocity, &qties::setVelocity));
MAKE_ATTRIBUTE_TRAIT_ARITHMETIC(flowQ, rho, p);
```

Example of definition of traits for class template
```c++
template<unsigned int Dimension, typename Real>
struct flowQ{
        Real rho; // density
        Vector<Dimension, Real> p; // momentum
};

MAKE_CUSTOM_TEMPLATE_TRAIT(
        (flowQ<Dimension, Real>),
        (unsigned int Dimension, typename Real),
        "density", (&flowQ<Dimension, Real>::rho), "momentum", (&flowQ<Dimension, Real>::p)
);

MAKE_NAMED_ATTRIBUTE_TEMPLATE_TRAIT(
        (flowQ<Dimension, Real>),
        (unsigned int Dimension, typename Real),
        "density", rho, "momentum", p
);

MAKE_ATTRIBUTE_TEMPLATE_TRAIT(
        (flowQ<Dimension, Real>),
        (unsigned int Dimension, typename Real),
        rho, p
);
```


_IMPORTANT_: Despite the examples showing more ways of definition of default traits,
only one of the specialization is allowed for each traited class and default traits.