task.hpp 5.1 KB

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