task.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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, GENERIC = 2, OTHER = 3 };
  11. static constexpr const char* INSTAGRAM_NAME = "Instagram";
  12. /**
  13. * Files
  14. */
  15. enum FileType { VIDEO = 1, IMAGE = 2 };
  16. /**
  17. * KFileData
  18. *
  19. * Structure for holding file bytes and metadata
  20. */
  21. struct KFileData {
  22. QString name;
  23. FileType type;
  24. QString path;
  25. QByteArray bytes;
  26. };
  27. /**
  28. * Type namespace
  29. *
  30. * Describes the types of task arguments available for use
  31. */
  32. namespace Type {
  33. static constexpr const char* TEXT = "Text";
  34. static constexpr const char* FILE = "File";
  35. static constexpr const char* STRINGVECTOR = "StringVector";
  36. static constexpr const char* FILEVECTOR = "FileVector";
  37. static constexpr const char* DATETIME = "DateTime";
  38. static constexpr const char* BOOLEAN = "Boolean";
  39. static constexpr const char* INTEGER = "Integer";
  40. } // namespace Type
  41. namespace VariantIndex {
  42. static const uint8_t BOOLEAN = 0;
  43. static const uint8_t INTEGER = 1;
  44. static const uint8_t STRVEC = 2;
  45. static const uint8_t QSTRING = 3;
  46. static const uint8_t FILEVEC = 4;
  47. } // namespace VariantIndex
  48. inline bool isIndex(uint8_t v, uint8_t i) { return v == i; }
  49. /**
  50. * Forward Declarations
  51. */
  52. class TaskArgumentBase;
  53. class Task;
  54. /**
  55. * Aliases
  56. */
  57. using TaskQueue = QQueue<Task*>;
  58. using ArgumentType = const char*;
  59. using ArgumentValues = QVector<QString>;
  60. using TypeVariant = std::variant<bool, int, QVector<QString>, QString, QVector<KFileData>>;
  61. using TaskIterator = std::vector<TaskArgumentBase*>::iterator;
  62. /**
  63. * The interface expected on our Task Arguments
  64. */
  65. class TaskArgumentBase {
  66. public:
  67. virtual const QString text() = 0;
  68. virtual const QString getStringValue() = 0;
  69. virtual uint8_t getTypeIndex() = 0;
  70. virtual TypeVariant getValue() = 0;
  71. virtual void insert(QString value) = 0;
  72. virtual void insert(KFileData file) = 0;
  73. virtual void remove(TypeVariant value) = 0;
  74. virtual void setValue(TypeVariant v) = 0;
  75. virtual bool isContainer() = 0;
  76. virtual void clear() = 0;
  77. };
  78. /**
  79. * TaskArgument
  80. *
  81. * A templated class providing a generic way for handling arguments whose types can be one from the set defined
  82. * by our TypeVariant alias
  83. */
  84. class TaskArgument : TaskArgumentBase {
  85. public:
  86. TaskArgument(QString n, ArgumentType t, TypeVariant _value) {
  87. name = n;
  88. type = t;
  89. value = _value;
  90. }
  91. /**
  92. * Move Constructor
  93. *
  94. * @constructor
  95. * @param [in] {TaskArgument&&} a The R-value reference to a TaskArgument
  96. */
  97. TaskArgument(TaskArgument&& a) :
  98. name(std::move(a.name)), type(std::move(a.type)), value(std::move(a.value)) {}
  99. /**
  100. * Copy Constructor
  101. *
  102. * @constructor
  103. * @param [in] {TaskArgument&&} a The const reference to a TaskArgument
  104. */
  105. TaskArgument(const TaskArgument& a) :
  106. name(std::move(a.name)), type(std::move(a.type)), value(std::move(a.value)) {}
  107. /**
  108. * text
  109. * @returns {QString} The name of the argument
  110. */
  111. virtual const QString text() override { return name; }
  112. /**
  113. * setValue
  114. * @param [in] {TypeVariant} new_value The new value for this argument
  115. */
  116. virtual void setValue(TypeVariant new_value) override {
  117. value = new_value;
  118. }
  119. /**
  120. * @brief getStringValue
  121. * @return [out] {QString}
  122. */
  123. virtual const QString getStringValue() override {
  124. if (isIndex(value.index(), VariantIndex::QSTRING)) {
  125. return std::get<VariantIndex::QSTRING>(value);
  126. } else if (isIndex(value.index(), VariantIndex::BOOLEAN)) {
  127. return QString::number(std::get<VariantIndex::BOOLEAN>(value));
  128. } else if (isIndex(value.index(), VariantIndex::INTEGER)) {
  129. return QString::number(std::get<VariantIndex::INTEGER>(value));
  130. }
  131. return ""; // Throw?
  132. }
  133. /**
  134. * @brief getValue
  135. * @return [out] {TypeVariant}
  136. */
  137. virtual TypeVariant getValue() override {
  138. if (isIndex(value.index(), VariantIndex::QSTRING)) {
  139. return std::get<VariantIndex::QSTRING>(value);
  140. } else if (isIndex(value.index(), VariantIndex::BOOLEAN)) {
  141. return std::get<VariantIndex::BOOLEAN>(value);
  142. } else if (isIndex(value.index(), VariantIndex::INTEGER)) {
  143. return std::get<VariantIndex::INTEGER>(value);
  144. } else if (isIndex(value.index(), VariantIndex::STRVEC)) {
  145. return std::get<VariantIndex::STRVEC>(value);
  146. } else if (isIndex(value.index(), VariantIndex::FILEVEC)) {
  147. return std::get<VariantIndex::FILEVEC>(value);
  148. }
  149. return ""; // Throw?
  150. }
  151. /**
  152. * @brief clear
  153. */
  154. virtual void clear() override {
  155. if (isIndex(value.index(), VariantIndex::STRVEC)) {
  156. std::get<VariantIndex::STRVEC>(value).clear();
  157. } else if (isIndex(value.index(), VariantIndex::FILEVEC)) {
  158. std::get<VariantIndex::FILEVEC>(value).clear();
  159. } else if (isIndex(value.index(), VariantIndex::QSTRING)) {
  160. std::get<VariantIndex::QSTRING>(value).clear();
  161. } else if (isIndex(value.index(), VariantIndex::INTEGER)) {
  162. std::get<VariantIndex::INTEGER>(value) = 0;
  163. } else if (isIndex(value.index(), VariantIndex::BOOLEAN)) {
  164. std::get<VariantIndex::BOOLEAN>(value) = false;
  165. }
  166. }
  167. /**
  168. * @brief isContainer
  169. * @return [out] {bool}
  170. */
  171. virtual bool isContainer() override {
  172. return (isIndex(value.index(), VariantIndex::STRVEC) || isIndex(value.index(), VariantIndex::FILEVEC));
  173. }
  174. /**
  175. * @brief insert
  176. * @param value
  177. */
  178. virtual void insert(QString string) override {
  179. if (isIndex(value.index(), VariantIndex::STRVEC)) {
  180. std::get<VariantIndex::STRVEC>(value).push_back(string);
  181. } else {
  182. // Unable to push. Throw?
  183. }
  184. }
  185. /**
  186. * @brief insert
  187. * @param file
  188. */
  189. virtual void insert(KFileData file) override {
  190. if (value.index() == VariantIndex::FILEVEC) {
  191. std::get<VariantIndex::FILEVEC>(value).push_back(file);
  192. } else {
  193. // Unable to push. Throw?
  194. }
  195. }
  196. /**
  197. * @brief remove
  198. * @param value
  199. */
  200. virtual void remove(TypeVariant unwanted_value) override {
  201. if (value.index() == VariantIndex::STRVEC && unwanted_value.index() == VariantIndex::QSTRING) {
  202. auto&& container = std::get<VariantIndex::STRVEC>(value);
  203. auto value_to_remove = std::get<VariantIndex::QSTRING>(unwanted_value);
  204. auto it = std::find_if(container.begin(), container.end(), [&value_to_remove](QString s) {
  205. return (s == value_to_remove);
  206. });
  207. if (it != container.end()) {
  208. container.erase(it);
  209. return;
  210. } else {
  211. throw std::out_of_range("Could not find value requested for removal");
  212. }
  213. } else if (value.index() == VariantIndex::FILEVEC && unwanted_value.index() == VariantIndex::QSTRING) {
  214. auto&& container = std::get<VariantIndex::FILEVEC>(value);
  215. auto file_to_remove = std::get<VariantIndex::QSTRING>(unwanted_value);
  216. auto it = std::find_if(container.begin(), container.end(), [&file_to_remove](Scheduler::KFileData f) {
  217. return (f.name == file_to_remove);
  218. });
  219. if (it != container.end()) {
  220. container.erase(it);
  221. return;
  222. } else {
  223. throw std::out_of_range("Could not find value requested for removal");
  224. }
  225. }
  226. throw std::invalid_argument("The value provided does not match any existing container");
  227. }
  228. /**
  229. * @brief getTypeIndex
  230. * @return
  231. */
  232. virtual uint8_t getTypeIndex() override {
  233. return value.index();
  234. }
  235. private:
  236. QString name;
  237. ArgumentType type;
  238. TypeVariant value;
  239. };
  240. using TaskArguments = std::vector<TaskArgument*>;
  241. /**
  242. * The interface expected to be implemented in all Task types
  243. */
  244. class Task {
  245. public:
  246. Task(){};
  247. Task(KFileData);
  248. Task(QVector<KFileData>);
  249. virtual void setArgument(QString name, TypeVariant arg) = 0;
  250. virtual void addArgument(QString name, Scheduler::KFileData file) = 0;
  251. virtual void addArgument(QString name, QString string) = 0;
  252. virtual void removeArgument(QString name, TypeVariant arg) = 0;
  253. virtual const TaskArguments&& getTaskArguments() = 0;
  254. virtual TaskArgument&& getTaskArgument(QString name) = 0;
  255. virtual const TypeVariant getTaskArgumentValue(QString name) = 0;
  256. virtual ArgumentValues getArgumentValues() = 0;
  257. virtual QVector<QString> getArgumentNames() = 0;
  258. virtual TaskType getType() = 0;
  259. virtual int getTaskCode() = 0;
  260. virtual void defineTaskArguments() = 0;
  261. virtual void setDefaultValues() = 0;
  262. virtual const QVector<KFileData> getFiles() = 0;
  263. virtual bool hasFiles() = 0;
  264. virtual bool isReady() = 0;
  265. virtual void clear() = 0;
  266. virtual ~Task(){};
  267. };
  268. } // namespace Scheduler
  269. #endif // __TASK_HPP__