File utils.h

File List > libmudaq > utils.h

Go to the documentation of this file

#ifndef UTILITY_HPP_
#define UTILITY_HPP_

#include <boost/format.hpp>
#include <iostream>
#include <sstream>
#include <vector>

inline boost::format& eval_format(boost::format& fmt) { return fmt; }
template <typename T, typename... Params>
inline boost::format& eval_format(boost::format& fmt, T arg, Params... parameters) {
    return eval_format(fmt % arg, parameters...);
}
template <typename... Params>
inline std::string eval_str(const std::string& str, Params... parameters) {
    boost::format fmt(str);
    return eval_format(fmt, parameters...).str();
}

inline void ERROR(std::string msg) { std::cerr << "ERROR: " << msg << std::endl; }
template <typename... Params>
inline void ERROR(std::string msg, Params... parameters) {
    std::cerr << "ERROR: " << eval_str(msg, parameters...) << std::endl;
}

inline void DEBUG(std::string msg) { std::cout << "DEBUG: " << msg << std::endl; }
template <typename... Params>
inline void DEBUG(std::string msg, Params... parameters) {
    std::cout << "DEBUG: " << eval_str(msg, parameters...) << std::endl;
}

inline uint8_t graycode_decode(uint8_t gray) {
    uint8_t mask;
    for (mask = gray >> 1; mask != 0; mask = mask >> 1) {
        gray = gray ^ mask;
    }
    return gray;
}
inline uint8_t graycode_encode(uint8_t binary) { return (binary >> 1) ^ binary; }

inline bool fileCheck(const std::string& name) {
    if (FILE* file = fopen(name.c_str(), "r")) {
        std::cout << "Checking file: " << name.c_str() << std::endl;
        fclose(file);
        return true;
    } else {
        return false;
    }
}

template <typename T>
std::vector<T> convert_to_vec(T t, std::string& s, std::string& seperator) {
    std::stringstream stream(s);
    T value;
    std::vector<T> v;
    while (stream >> value) {
        v.push_back(value);
        std::string tmp;
        stream >> tmp;
        if (tmp != seperator) {
            std::cout << "STRING CONVERSION ERROR!\t" << tmp << std::endl;
        }
    }
    return v;
}

template <typename T>
inline void convert_to_string(std::string& s, std::vector<T> const& v, std::string& seperator) {
    std::stringstream stream;
    for (auto tmp : v) stream << tmp << seperator;
    s = stream.str();
}

#endif /* __UTILITY_HPP_UB6F0M92__ */