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

Change of the exportVariable concept. The logic was transferred to

printers classes. Moreover, it is possible to customise the export by
defining the PrintTo function.
parent d9052745
Loading
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
#ifndef PRINTANY_H
#define PRINTANY_H

#include <iomanip>
struct PrintAny {

    template <typename T>
    static void print(std::ostream& ost, const T& var, ...) {
        ost << typeid (T).name() << "(";
        ost << std::hex;
        for (char* byte = (char*)&var; byte < (char*)&var + sizeof (T); byte++) {
            ost << std::setfill('0') << std::setw(2) << ((int)(*byte) & 0xff);
            if (byte < (char*)&var + sizeof (T) - 1) {
                ost << ' ';
            }
        }
        ost << std::dec;
        ost << ")";
    }

    template <typename T>
    static void print(const T& var, ...) {
        for (char* byte = (char*)&var; byte < (char*)&var + sizeof (T); byte++) {
            printf("%02X",((int)(*byte) & 0xff));
            if (byte < (char*)&var + sizeof (T) - 1) {
                printf(" ");
            }
        }
    }

};

#endif // PRINTANY_H
+36 −0
Original line number Diff line number Diff line
#ifndef PRINTCUSTOM_H
#define PRINTCUSTOM_H
#include "../VariableExport.h"
#include <GTMesh/Traits/CustomTypeTraits.h>

/**
 * @brief Prints an object with defined PrintTo
 * function.
 */
struct PrintCustom {
    static int print(...) {return 0;}

    template <typename T>
    using IsPrintableToStream = Impl::void_t<decltype(PrintTo(std::declval<const T&>(), std::declval<std::ostream&>()))>;


    template <typename T>
    using IsPrintableToStdio = Impl::void_t<decltype(PrintTo(std::declval<const T&>()))>;


    template <typename T>
    static
    IsPrintableToStream<T>
    print(std::ostream& ost, const T& var, ...) {
        PrintTo(var, ost);
    }

    template <typename T>
    static
    IsPrintableToStdio<T>
    print(const T& var, ...) {
        PrintTo(var);
    }
};

#endif // PRINTCUSTOM_H
+67 −0
Original line number Diff line number Diff line
#ifndef PRINTEXPORTABLE_H
#define PRINTEXPORTABLE_H
#include <type_traits>
#include <GTMesh/Traits/CustomTypeTraits.h>

struct PrintExportable {
    static int print(...) {return 0;}

    /**
     * Prints the variable with. Valid if the
     * type T has overaloaded the operator<<
     * for ostream.
     */
    template <typename T, std::enable_if_t<IsExportable<T>::value, bool> = true>
    static void print(std::ostream& ost, const T& var, ...) {
        ost << var;
    }

    template <typename T>
    static void print(std::ostream& ost, const bool& var, ...) {
        ost << (var == true ? "true" : "false");
    }

    static void print(const double& d, ...)
    {
        printf("%g", d);
    }

    static void print(const long double& d, ...)
    {
        printf("%Lg", d);
    }

    static void print(const int& d, ...)
    {
        printf("%d", d);
    }


    static void print(const short& d, ...)
    {
        printf("%hd", d);
    }

    static void print(const unsigned int& d, ...)
    {
        printf("%ud", d);
    }


    static void print(const long long& d, ...)
    {
        printf("%lld", d);
    }

    static void print(const unsigned long long& d, ...)
    {
        printf("%llu", d);
    }

    static void print(const bool& b, ...)
    {
        printf("%s", (b == true ? "true" : "false"));
    }
};

#endif // PRINTEXPORTABLE_H
+71 −0
Original line number Diff line number Diff line
#ifndef PRINTINDEXABLE_H
#define PRINTINDEXABLE_H
#include "../VariableExport.h"
#include <GTMesh/Traits/CustomTypeTraits.h>

struct PrintIndexable {
    static int print(...) {return 0;}

    template <typename Indexable, std::enable_if_t<IsIndexable<Indexable>::value, bool> = true>
    static auto size(const Indexable& vec) {
        return vec.size();
    }

    template <typename Indexable, std::enable_if_t<IsTNLIndexable<Indexable>::value, bool> = true>
    static auto size(const Indexable& vec) {
        return vec.getSize();
    }

    template< typename Indexable, std::enable_if_t<IsIndexable<Indexable>::value || IsTNLIndexable<Indexable>::value, bool> = true >
    static void print(std::ostream& ost, const Indexable& vec) {
        ost << "[ ";
        for (decltype (size(vec))i = 0; i < size(vec); i++){
            VariableExport<>::exportVariable(ost, vec[i]);
            if (i <  size(vec) - 1){
                ost << ", ";
            }
        }
        ost << " ]";
    }


    template< typename Indexable, typename ... TraitsTypes, std::enable_if_t<IsIndexable<Indexable>::value || IsTNLIndexable<Indexable>::value, bool> = true >
    static void print(std::ostream& ost, const Indexable& vec, const std::tuple<TraitsTypes...>& traitsTuple) {
        ost << "[ ";
        for (decltype (size(vec))i = 0; i < size(vec); i++){
            VariableExport<>::exportVariable(ost, vec[i], traitsTuple);
            if (i <  size(vec) - 1){
                ost << ", ";
            }
        }
        ost << " ]";
    }


    template< typename Indexable, std::enable_if_t<IsIndexable<Indexable>::value || IsTNLIndexable<Indexable>::value, bool> = true >
    static void print(const Indexable& vec) {
        printf("[ ");
        for (decltype (size(vec))i = 0; i < size(vec); i++){
            VariableExport<VARIABLE_EXPORT_METHOD_STDIO>::exportVariable(vec[i]);
            if (i < size(vec) - 1){
                printf(", ");
            }
        }
        printf(" ]");
    }


    template< typename Indexable, typename ... TraitsTypes, std::enable_if_t<IsIndexable<Indexable>::value || IsTNLIndexable<Indexable>::value, bool> = true >
    static void print(const Indexable& vec, const std::tuple<TraitsTypes...>& traitsTuple) {
        printf("[ ");
        for (decltype (size(vec))i = 0; i < size(vec); i++){
            VariableExport<VARIABLE_EXPORT_METHOD_STDIO>::exportVariable(vec[i], traitsTuple);
            if (i < size(vec) - 1){
                printf(", ");
            }
        }
        printf(" ]");
    }
};

#endif // PRINTINDEXABLE_H
+73 −0
Original line number Diff line number Diff line
#ifndef PRINTITERABLE_H
#define PRINTITERABLE_H
#include "../VariableExport.h"
#include <GTMesh/Traits/CustomTypeTraits.h>

struct PrintIterable {
    static int print(...) {return 0;}

    template<typename T>
    using isIterable = std::enable_if_t < IsIterable<T>::value, bool >;

    template< typename Iterable,
              isIterable<Iterable> = true >
    static void print(std::ostream& ost, const Iterable& list) {
        auto it = list.begin();
        ost << "[ ";
        while (it != list.end()){
            VariableExport<>::exportVariable(ost, *it);
            if (++it != list.end()){
                ost << ", ";
            }
        }
        ost << " ]";
    }

    template< typename Iterable,
              typename ... TraitsTypes,
              isIterable<Iterable> = true >
    static void print(std::ostream& ost, const Iterable& list, const std::tuple<TraitsTypes...>& traitsTuple) {
        auto it = list.begin();
        ost << "[ ";
        while (it != list.end()){
            VariableExport<>::exportVariable(ost, *it, traitsTuple);
            if (++it != list.end()){
                ost << ", ";
            }
        }
        ost << " ]";
    }

    template< typename Iterable,
              isIterable<Iterable> = true >
    static void print(const Iterable &list)
    {
        auto it = list.begin();
        printf("[ ");
        while (it != list.end()){
            VariableExport<VARIABLE_EXPORT_METHOD_STDIO>::exportVariable(*it);
            if (++it != list.end()){
                printf(", ");
            }
        }
        printf(" ]");
    }

    template< typename Iterable,
              typename ... TraitsTypes,
              isIterable<Iterable> = true >
    static void print(const Iterable &list, const std::tuple<TraitsTypes...>& traitsTuple)
    {
        auto it = list.begin();
        printf("[ ");
        while (it != list.end()){
            VariableExport<VARIABLE_EXPORT_METHOD_STDIO>::exportVariable(*it, traitsTuple);
            if (++it != list.end()){
                printf(", ");
            }
        }
        printf(" ]");
    }
};

#endif // PRINTITERABLE_H
Loading