task.hpp 9.5 KB

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