[c ++调用模板提供的(静态)函数

问题描述 投票:0回答:1

我正在尝试为FreeRTOS任务创建C ++包装器。这里的挑战是,freertos需要采用一个c函数指针,如此处所述https://www.freertos.org/xTaskCreateStatic.html

现在我想到了这个

template<typename Functor_T, uint32_t u32StackSize>
class CFreeRTOS_Task
{
public:
    ///Constructor
    CFreeRTOS_Task(const std::string& strTaskNameArg);
    ///Destructor
    ~CFreeRTOS_Task();
private:
    ///the Name of this task
    const std::string strTaskName;
    ///the task handle
    TaskHandle_t task;
    ///the task control block
    StaticTask_t task_tcb;

    ///is the task currently running (can be accessed from multiple threads => atomic)
    std::atomic<bool> bTaskRunning;


    ///the actual stack
    StackType_t stack[u32StackSize] = {};

    ///the task function to pass to freertos
    static void TaskFunction(void * pvParameters);

};


//+++++++++++++++++++++++++  Implementation +++++++++++++++++++++++++++++++++++++++++

template<typename Functor_T, uint32_t u32StackSize>
CFreeRTOS_Task<Functor_T, u32StackSize>::CFreeRTOS_Task(const std::string& strTaskNameArg):
    strTaskName(strTaskNameArg)
{
    task = xTaskCreateStatic(
        TaskFunction, /* Function that implements the task. */
        strTaskName.c_str(), /* Text name for the task. */
        u32StackSize, /* Number of indexes in the xStack array. */
        (void *) 1, /* Parameter passed into the task. */
        tskIDLE_PRIORITY,/* Priority at which the task is created. */
        stack, /* Array to use as the task's stack. */
        &task_tcb); /* Variable to hold the task's data structure. */
    bTaskRunning = true;
}


template<typename Functor_T, uint32_t u32StackSize>
CFreeRTOS_Task<Functor_T, u32StackSize>::~CFreeRTOS_Task()
{
    if(bTaskRunning)
    {
        //terminate task...
        bTaskRunning = false;
    }
}

template<typename Functor_T, uint32_t u32StackSize>
void CFreeRTOS_Task<Functor_T, u32StackSize>::TaskFunction(void * pvParameters)
{
    //do some initilisation
    for(;;)
    {
        //call the user provided task function
        Functor_T();
        osDelay(10);
    }

    //shutdown this task (common to all freertos tasks)
}

现在我的实例看起来像这样

///test task function
static void TestTaskFunc();


///Test task instance
static CFreeRTOS_Task<TestTaskFunc,10> testTask("test_task");


static void TestTaskFunc()
{
    volatile uint32_t test = 0;
}

但是我遇到2个编译器错误

error: type/value mismatch at argument 1 in template parameter list for 'template<class Functor_T, long unsigned int u32StackSize> class NRTOS_Wrapper::CFreeRTOS_Task'
 static CFreeRTOS_Task<TestTaskFunc,10> testTask("test_task");
                                      ^
 note:   expected a type, got 'NRTOS_Wrapper::TestTaskFunc'
 error: invalid conversion from 'const char*' to 'int' [-fpermissive]

您能帮我找出我在这里想念的东西吗?

thx:)

c++ templates freertos
1个回答
0
投票

CFreeRTOS_Task的模板类型需要可调用,但前提是TestTaskFunc不会推断出该类型,而您需要decltype它。

static CFreeRTOS_Task<decltype(TestTaskFunc), 10> testTask("test_task");
//                    ^^^^^^^^
© www.soinside.com 2019 - 2024. All rights reserved.