mardi 8 janvier 2019

Function and inner function same arguments

Could somebody please help me how to achieve that my function accepts only that argument types that the function can be called inside it?

I have a Logger class what can be started with a HardwareSerial object in in the setup() function of an arduino code. Then in the loop() I would like to call the Logger.print() function that should accepts only that arguments what HardwareSerial.print() can be called.

Here is my ugly and not working tries:

template <typename... ARGS>
size_t print(const ARGS &... args) {
    if (serial != NULL) {
        if (sizeof...(args) == 2) {
            return this->serial->print(args[0], args[1]);
        } else if (sizeof...(args) == 1) {
            return this->serial->print(args[0]);
        }
    }
    return 0;
}

template <typename T>
size_t print(const T &t, typename std::enable_if<std::is_convertible<const __FlashStringHelper *, T>::value ||
                                                     std::is_base_of<const String &, T>::value ||
                                                     std::is_array<T>::value ||
                                                     //std::is_same<char[std::extent<T>::value], T>::value ||
                                                     std::is_same<char, T>::value ||
                                                     std::is_same<char *, T>::value ||
                                                     std::is_same<const char *, T>::value ||
                                                     std::is_same<unsigned char, T>::value ||
                                                     std::is_same<int, T>::value ||
                                                     std::is_same<unsigned int, T>::value ||
                                                     std::is_same<long, T>::value ||
                                                     std::is_same<unsigned long, T>::value ||
                                                     std::is_same<double, T>::value ||
                                                     std::is_convertible<const Printable &, T>::value ||
                                                     std::is_convertible<struct tm *, T>::value,
                                                 T>::type * = 0) {
    if (serial != NULL) {
        return this->serial->print(t);
    }
    retrun 0;
}

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire