progress.h 2.3 KB
Newer Older
1 2 3 4 5 6 7 8 9
#pragma once

#include "ros_bridge/rapidjson/include/rapidjson/document.h"

#include <vector>

namespace ros_bridge {
//! @brief Namespace containing classes and methodes ros message generation.
namespace messages {
10 11
//! @brief Namespace containing classes and methodes for geometry_msgs
//! generation.
12
namespace nemo_msgs {
13 14
//! @brief Namespace containing methodes for geometry_msgs/Point32 message
//! generation.
15 16
namespace progress {

17
std::string messageType();
18 19

//! @brief C++ representation of nemo_msgs/Progress message
20 21 22
template <class IntType = long,
          template <class, class...> class ContainterType = std::vector>
class GenericProgress {
23
public:
24 25 26 27 28 29 30 31 32
  GenericProgress() {}
  GenericProgress(const ContainterType<IntType> &progress)
      : _progress(progress) {}
  GenericProgress(const GenericProgress &p) : _progress(p.progress()) {}

  virtual const ContainterType<IntType> &progress(void) const {
    return _progress;
  }
  virtual ContainterType<IntType> &progress(void) { return _progress; }
33

34 35 36 37 38 39 40
  bool operator==(const GenericProgress &other) const {
    return this->_progress == other._progress;
  }

  bool operator!=(const GenericProgress &other) const {
    return !this->operator==(other);
  }
41 42

protected:
43
  ContainterType<IntType> _progress;
44 45 46 47
};
typedef GenericProgress<> Progress;

template <class ProgressType>
48 49 50 51 52 53 54 55 56
bool toJson(const ProgressType &p, rapidjson::Value &value,
            rapidjson::Document::AllocatorType &allocator) {
  rapidjson::Value jProgress(rapidjson::kArrayType);
  for (unsigned long i = 0; i < std::uint64_t(p.progress().size()); ++i) {
    jProgress.PushBack(rapidjson::Value().SetInt(std::int32_t(p.progress()[i])),
                       allocator);
  }
  value.AddMember("progress", jProgress, allocator);
  return true;
57 58 59
}

template <class ProgressType>
60 61 62 63 64
bool fromJson(const rapidjson::Value &value, ProgressType &p) {
  if (!value.HasMember("progress") || !value["progress"].IsArray()) {
    assert(false);
    return false;
  }
65

66 67 68 69 70 71 72
  const auto &jsonProgress = value["progress"];
  unsigned long sz = jsonProgress.Size();
  p.progress().clear();
  p.progress().reserve(sz);
  for (unsigned long i = 0; i < sz; ++i)
    p.progress().push_back(std::int32_t(jsonProgress[i].GetInt()));
  return true;
73 74 75 76 77 78
}

} // namespace progress
} // namespace nemo_msgs
} // namespace messages
} // namespace ros_bridge