.Heindl Solutions - Conversion of standard C++ data structures to HalconCpp::HTuple

Conversion of standard C++ data structures to HalconCpp::HTuple

2020-04-01 by Andreas Heindl



Conversion to HTuple

When working with the HALCON/CPP interface one often needs to convert C++ data structures to HalconCpp::HTuple. We present here some example code that can be used for this task.

Please note: We have tested this code and it seems to work but we take no responsibility for its correctness. Suggestions for its improvement are always welcome.

HalconCpp::HTuple to_htuple(const char* str) {
    return HalconCpp::HTuple(str);
}

HalconCpp::HTuple to_htuple(const std::string& s) {
    return HalconCpp::HTuple(s.c_str());
}

template<typename T>
HalconCpp::HTuple to_htuple(T /*obj*/) {
    static_assert(!std::is_same_v<T,T>, "not implemented");
}

template<typename T>
HalconCpp::HTuple to_htuple(std::vector<T> obj) {
    return HalconCpp::HTuple(obj.data(),static_cast<Hlong>(obj.size()));
}

HalconCpp::HTuple to_htuple(const std::vector<const char*>& obj) {
    if (obj.empty()) {
        return {};
    }
    const auto s = static_cast<Hlong>(obj.size());
    HalconCpp::HTuple t;
    t[s-1] = "";
    for (Hlong i = 0; i < s; ++i) {
        t[i] = obj.at(static_cast<size_t>(i));
    }
    return t;
}

HalconCpp::HTuple to_htuple(const std::vector<std::string>& obj) {
    const auto s = static_cast<Hlong>(obj.size());
    HalconCpp::HTuple t(HalconCpp::HTuple(s),HalconCpp::HTuple(""));
    for (Hlong i = 0; i < s; ++i) {
        t[i] = obj.at(static_cast<size_t>(i)).data();
    }
    return t;
}

HalconCpp::HTuple to_htuple(std::vector<bool> obj) {
    HalconCpp::HTuple t;
    for (const bool b: obj) {
        t.Append(b ? 1 : 0);
    }
    return t;
}