task.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #ifndef __TASK_HPP__
  2. #define __TASK_HPP__
  3. #include <QQueue>
  4. #include <QString>
  5. #include <QVector>
  6. #include <memory>
  7. #include <variant>
  8. #include <vector>
  9. namespace Scheduler {
  10. /**
  11. * Files
  12. */
  13. enum FileType { VIDEO = 1, IMAGE = 2 };
  14. /**
  15. * KFileData
  16. *
  17. * Structure for holding file bytes and metadata
  18. */
  19. struct KFileData {
  20. QString name;
  21. FileType type;
  22. QString path;
  23. QByteArray bytes;
  24. };
  25. /**
  26. * Type namespace
  27. *
  28. * Describes the types of task arguments available for use
  29. */
  30. namespace Type {
  31. static constexpr const char* TEXT = "Text";
  32. static constexpr const char* FILE = "File";
  33. static constexpr const char* STRINGVECTOR = "StringVector";
  34. static constexpr const char* FILEVECTOR = "FileVector";
  35. static constexpr const char* DATETIME = "DateTime";
  36. static constexpr const char* BOOLEAN = "Boolean";
  37. } // namespace Type
  38. namespace VariantIndex {
  39. static const uint8_t BOOLEAN = 0;
  40. static const uint8_t INTEGER = 1;
  41. static const uint8_t QSTRING = 2;
  42. static const uint8_t STRVEC = 3;
  43. static const uint8_t FILEVEC = 4;
  44. } // namespace VariantIndex
  45. inline bool isIndex(uint8_t v, uint8_t i) { return v == i; }
  46. /**
  47. * Forward Declarations
  48. */
  49. class TaskArgumentBase;
  50. class Task;
  51. /**
  52. * Aliases
  53. */
  54. using ArgumentType = const char*;
  55. using TypeVariant = std::variant<bool, int, QString, QVector<QString>, std::vector<KFileData>>;
  56. using TaskIterator = std::vector<std::unique_ptr<TaskArgumentBase>>::iterator;
  57. using TaskArguments = std::vector<std::unique_ptr<TaskArgumentBase>>;
  58. using TaskQueue = QQueue<Task>;
  59. using ArgumentValues = QVector<const QString>;
  60. /**
  61. * The interface expected on our Task Arguments
  62. */
  63. class TaskArgumentBase {
  64. public:
  65. virtual void setValue(TypeVariant v) = 0;
  66. virtual TypeVariant getValue() = 0;
  67. virtual const QString text() = 0;
  68. virtual void clear() = 0;
  69. virtual const QString getStringValue() = 0;
  70. virtual bool isContainer() = 0;
  71. };
  72. /**
  73. * TaskArgument
  74. *
  75. * A templated class providing a generic way for handling arguments whose types can be one from the set defined
  76. * by our TypeVariant alias
  77. */
  78. template <typename T = TypeVariant>
  79. class TaskArgument : TaskArgumentBase {
  80. public:
  81. TaskArgument(QString n, ArgumentType t, T _value) {
  82. name = n;
  83. type = t;
  84. value = _value;
  85. }
  86. /**
  87. * Move Constructor
  88. *
  89. * @constructor
  90. * @param [in] {TaskArgument&&} a The R-value reference to a TaskArgument
  91. */
  92. TaskArgument(TaskArgument&& a) : name(std::move(a.name)), type(std::move(a.type)), value(std::move(a.value)) {}
  93. /**
  94. * text
  95. * @returns {QString} The name of the argument
  96. */
  97. virtual const QString text() { return name; }
  98. /**
  99. * setValue
  100. * @param [in] {TypeVariant} new_value The new value for this argument
  101. */
  102. virtual void setValue(TypeVariant new_value) override { value = new_value; }
  103. virtual const QString getStringValue() override {
  104. if (isIndex(value.index(), VariantIndex::QSTRING)) {
  105. return value;
  106. } else if (isIndex(value.index(), VariantIndex::BOOLEAN)) {
  107. return QString::number(value);
  108. } else if (isIndex(value.index(), VariantIndex::INTEGER)) {
  109. return QString::number(value);
  110. }
  111. }
  112. virtual TypeVariant getValue() override { return std::get<value.index()>(value); }
  113. virtual void clear() override {
  114. if (isIndex(value.index(), VariantIndex::STRVEC)) {
  115. std::get<VariantIndex::STRVEC>(value).clear();
  116. } else if (isIndex(value.index(), VariantIndex::FILEVEC)) {
  117. std::get<VariantIndex::FILEVEC>(value).clear();
  118. } else if (isIndex(value.index(), VariantIndex::QSTRING)) {
  119. std::get<VariantIndex::QSTRING>(value).clear();
  120. } else if (isIndex(value.index(), VariantIndex::INTEGER)) {
  121. std::get<VariantIndex::INTEGER>(value) = 0;
  122. } else if (isIndex(value.index(), VariantIndex::BOOLEAN)) {
  123. std::get<VariantIndex::STRVEC>(value) = false;
  124. }
  125. }
  126. virtual bool isContainer() override {
  127. return (isIndex(value.index(), VariantIndex::STRVEC) || isIndex(value.index(), VariantIndex::FILEVEC));
  128. }
  129. private:
  130. QString name;
  131. ArgumentType type;
  132. TypeVariant value;
  133. };
  134. /**
  135. * The interface expected to be implemented in all Task types
  136. */
  137. class Task {
  138. public:
  139. virtual void setArgument(QString name, TypeVariant arg) = 0;
  140. virtual const TaskArguments getTaskArguments() = 0;
  141. virtual TypeVariant getTaskArgument(QString name) = 0;
  142. virtual ArgumentValues getArgumentValues() = 0;
  143. virtual void defineTaskArguments() = 0;
  144. virtual void setDefaultValues() = 0;
  145. virtual bool isReady() = 0;
  146. virtual void clear() = 0;
  147. virtual ~Task(){};
  148. };
  149. } // namespace Scheduler
  150. #endif // __TASK_HPP__