progress_array.h 2.29 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6 7
#ifndef PROGRESS_ARRAY_H
#define PROGRESS_ARRAY_H

#pragma once

#include "labeled_progress.h"

8 9
#include <QJsonArray>
#include <QJsonObject>
Valentin Platzgummer's avatar
Valentin Platzgummer committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <QVector>

namespace ros_bridge {
//! @brief Namespace containing classes and methodes ros message generation.
namespace messages {
//! @brief Namespace containing classes and methodes for geometry_msgs
//! generation.
namespace nemo_msgs {
//! @brief Namespace containing methodes for nemo_msgs/progress_array message
//! generation.
namespace progress_array {

std::string messageType();

namespace {
const char *progressArrayKey = "progress_array";
} // namespace

//! @brief C++ representation of nemo_msgs/progress_array message
template <template <class, class...> class Container = QVector>
class GenericProgressArray {
public:
  typedef Container<labeled_progress::LabeledProgress> ContainerType;
  GenericProgressArray() {}

  ContainerType &progress_array() { return _progres_array; }

  const ContainerType &progress_array() const { return _progres_array; }

  void setProgressArray(const ContainerType &c) { _progres_array = c; }

protected:
  Container<labeled_progress::LabeledProgress> _progres_array;
};

typedef GenericProgressArray<> ProgressArray;

47 48 49
template <class Array> bool toJson(const Array &array, QJsonObject &value) {
  QJsonArray jsonArray;

Valentin Platzgummer's avatar
Valentin Platzgummer committed
50
  for (const auto &lp : array.progress_array()) {
51 52 53
    QJsonObject jsonLp;
    if (labeled_progress::toJson(lp, jsonLp)) {
      jsonArray.append(std::move(jsonLp));
Valentin Platzgummer's avatar
Valentin Platzgummer committed
54 55 56 57
    } else {
      return false;
    }
  }
58
  value[progressArrayKey] = std::move(jsonArray);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
59 60 61
  return true;
}

62 63
template <class Array> bool fromJson(const QJsonObject &value, Array &p) {
  if (!value.contains(progressArrayKey) || !value[progressArrayKey].isArray()) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
64 65 66
    return false;
  }

67
  const auto jsonArray = value[progressArrayKey].toArray();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
68
  p.progress_array().clear();
69 70
  p.progress_array().reserve(jsonArray.size());
  for (long i = 0; i < jsonArray.size(); ++i) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
71
    labeled_progress::LabeledProgress lp;
72 73
    if (!jsonArray[i].isObject() ||
        !labeled_progress::fromJson(jsonArray[i].toObject(), lp)) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
      return false;
    } else {
      p.progress_array().push_back(lp);
    }
  }

  return true;
}

} // namespace progress_array
} // namespace nemo_msgs
} // namespace messages
} // namespace ros_bridge

#endif // PROGRESS_ARRAY_H