Prechádzať zdrojové kódy

KBF-37 rename kbf::task::Task --> kbf::Task

Bence Balint 2 rokov pred
rodič
commit
138434da74
4 zmenil súbory, kde vykonal 32 pridanie a 22 odobranie
  1. 19 14
      include/kbf/task.h
  2. 4 3
      src/task.cpp
  3. 7 3
      test/test_queue.cpp
  4. 2 2
      test/test_task.cpp

+ 19 - 14
include/kbf/task.h

@@ -11,18 +11,17 @@
 
 #include "kbf/exception.h"
 
-/**
- * @brief Task handling functions.
- */
-namespace kbf::task {
+namespace kbf {
     /** @brief Tag used for logging. */
     static constexpr const char *const TAG = "kbf::task";
 
     class Task;
 
-    template<class T>
-    std::shared_ptr<Task>
-    start(const std::string &name, void *arg, uint32_t stackSize = 2048, uint32_t priority = 5);
+    namespace task {
+        template<class T>
+        std::shared_ptr<Task>
+        start(const std::string &name, void *arg, uint32_t stackSize = 2048, uint32_t priority = 5);
+    }
 
     /**
      * @brief Base class for tasks.
@@ -31,8 +30,8 @@ namespace kbf::task {
     class Task {
         template<class T>
         friend std::shared_ptr<Task>
-        start(const std::string &name, void *arg,  // NOLINT(readability-redundant-declaration
-              uint32_t stackSize, uint32_t priority);
+        kbf::task::start(const std::string &name, void *arg,  // NOLINT(readability-redundant-declaration
+                         uint32_t stackSize, uint32_t priority);
 
     public:
         /** @brief Name of the RTOS task. */
@@ -45,7 +44,7 @@ namespace kbf::task {
         [[maybe_unused]] const uint32_t priority;
 
         /** @brief Tag used for logging. */
-        static constexpr const char *const TAG = "kbf::task::Task";
+        static constexpr const char *const TAG = "kbf::Task";
 
         Task() = delete;
 
@@ -93,10 +92,11 @@ namespace kbf::task {
         bool running_ = false;
 
         class TaskList {
-            static constexpr const char *const TAG = "kbf::task::TaskList";
+            static constexpr const char *const TAG = "kbf::Task::TaskList";
+
             template<class T>
             friend std::shared_ptr<Task>
-            start(const std::string &name, void *arg,  // NOLINT(readability-redundant-declaration
+            task::start(const std::string &name, void *arg,  // NOLINT(readability-redundant-declaration
                   uint32_t stackSize, uint32_t priority);
 
             friend class Task;
@@ -108,10 +108,15 @@ namespace kbf::task {
             static void remove(uint32_t id);
         };
     };
+}
 
+/**
+ * @brief Task handling.
+ */
+namespace kbf::task {
     /**
      * @brief Starts a task.
-     * @tparam T must be subclass of kbf::task::Task
+     * @tparam T must be subclass of kbf::Task
      * @param name RTOS task name
      * @param arg arguments passed to run()
      * @param stackSize stack size
@@ -121,7 +126,7 @@ namespace kbf::task {
     template<class T>
     std::shared_ptr<Task>
     start(const std::string &name, void *arg, const uint32_t stackSize, const uint32_t priority) {
-        static_assert(std::is_base_of<Task, T>::value, "kbf::task::run(): type must be a kbf::task::Task");
+        static_assert(std::is_base_of<Task, T>::value, "kbf::task::start(): type must be a kbf::task::Task");
         auto task = std::make_shared<T>(name, arg, stackSize, priority);
 
         esp_err_t err = xTaskCreate(Task::runInternal, name.c_str(), stackSize, task.get(), priority, &task->handle);

+ 4 - 3
src/task.cpp

@@ -6,13 +6,14 @@ using std::string;
 using std::vector;
 using std::shared_ptr;
 using std::make_shared;
-using namespace kbf::task;
+
+using namespace kbf;
 
 static uint32_t taskId = 0;
 
 std::vector<std::shared_ptr<Task>> Task::TaskList::tasks;
 
-void Task::TaskList::add(const shared_ptr<Task>& task) {
+void Task::TaskList::add(const shared_ptr<Task> &task) {
     ESP_LOGD(TAG, "%s(%d), refcount=%ld", __func__, task->id, task.use_count());
     tasks.push_back(task);
 };
@@ -26,7 +27,7 @@ void Task::TaskList::remove(uint32_t id) {
         }
     }
     ESP_LOGE(TAG, "%s(%d): not found", __func__, id);
-    throw kbf::exception::KBFError("TaskList::remove(" + std::to_string(id) + ") failed");
+    throw exception::KBFError("TaskList::remove(" + std::to_string(id) + ") failed");
 }
 
 Task::Task(string name, void *arg, const uint32_t stackSize, const uint32_t priority) :

+ 7 - 3
test/test_queue.cpp

@@ -21,7 +21,7 @@ struct PusherArg {
 };
 
 template<typename T>
-class Pusher : public task::Task {
+class Pusher : public Task {
 public:
     Pusher(const std::string &name, void *arg, uint32_t stackSize, uint32_t priority) :
             Task(name, arg, stackSize, priority) {}
@@ -117,14 +117,18 @@ TEST_CASE("Queue timeout", "[kbf_queue]") {
     task::start<Pusher<int>>("test_queue", (void *) &arg);
 
     TEST_ASSERT_TRUE(queue.empty());
-    TEST_ASSERT_EQUAL(values[0], queue.pop(DELAY).value());
+    std::optional<int> current = queue.pop(DELAY + 100);
+    TEST_ASSERT_FALSE(current == std::nullopt);
+    TEST_ASSERT_EQUAL(values[0], current.value());
 
     TEST_ASSERT_TRUE(queue.empty());
     TEST_ASSERT_TRUE(queue.pop(DELAY / 2) == std::nullopt);
 
     kbf::sleep(DELAY);
     TEST_ASSERT_FALSE(queue.empty());
-    TEST_ASSERT_EQUAL(values[1], queue.pop().value());
+    current = queue.pop();
+    TEST_ASSERT_FALSE(current == std::nullopt);
+    TEST_ASSERT_EQUAL(values[1], current.value());
     TEST_ASSERT_TRUE(queue.empty());
 
     TEST_ASSERT_TRUE(queue.pop(DELAY / 2) == std::nullopt);

+ 2 - 2
test/test_task.cpp

@@ -25,7 +25,7 @@ using namespace kbf;
 static atomic<int> counter{0};
 
 /** runs until stopped */
-class Runner : public task::Task {
+class Runner : public Task {
 public:
     Runner(const string &name, void *arg, const uint32_t stackSize, const uint32_t priority) :
             Task(name, arg, stackSize, priority) {}
@@ -52,7 +52,7 @@ struct QuitterArg {
 };
 
 /** stops after KBF_TASK_TEST_CYCLES cycles */
-class Quitter : public task::Task {
+class Quitter : public Task {
 public:
     Quitter(const string &name, void *arg, const uint32_t stackSize, const uint32_t priority) :
             Task(name, arg, stackSize, priority) {}