簡體   English   中英

使用現代C ++中的模板替換函數指針數組

[英]Replace arrays of function pointers using templates in modern c++

我有一些使用函數指針數組的遺留代碼,以模擬內存處理程序。
因此,我現在嘗試使用模板具有相同的功能。
這是我到目前為止嘗試過的:

template <typename R, typename ...ARGS> using function = R(*)(ARGS...);
template<size_t Size> using ReadType    = function<SizedUInt<Size>, const uint32_t>;

// arrays to redeclare
std::array<ReadType<8>,   memory_handler_size> read_8_handler_;
std::array<ReadType<16>,  memory_handler_size> read_16_handler_;
std::array<ReadType<32>,  memory_handler_size> read_32_handler_;

template<size_t Size>
void initializeReadHandler(uint32_t begin,
                           uint32_t end,
                           ReadType<Size> func) {
    begin >>= 16;
    end >>= 16;
    for (uint32_t current = begin; current <= end; ++current) {
         //read_handler_[current & 0xFFFF] = func;
    }
}

如何使用模板initializeReadHandler()函數聲明讀取處理程序數組以對其進行initializeReadHandler()
我不想使用std::function因為我負擔不起性能開銷...

...編輯...
這是基於Yakk的答案,max66注釋和一些小修正(打字錯誤等)的代碼:

template <typename R, typename ...ARGS> using function = R(*)(ARGS...);
template<size_t S> using ReadType    = function<SizedUInt<S>, const uint32_t>;

template<class ReadType>
using ReadHandlerType = std::array<ReadType, memory_handler_size>;

ReadHandler<8> read_8_handler_;
ReadHandler<16> read_16_handler_;
ReadHandler<32> read_32_handler_;

template<size_t S>
void initializeReadHandler(uint32_t begin,
                           uint32_t end,
                           ReadType<S> func) {
    begin >>= 16;
    end >>= 16;

    auto t = std::tie(read_8_handler_, read_16_handler_, read_32_handler_);
    for (uint32_t current = begin; current <= end; ++current) {
        auto& handler = std::get < ReadHandler<S>& >(t);
        handler[current & 0xFFFF] = func;
    }
}

像魅力一樣工作:D謝謝大家!

如果我理解正確,並且您不會維護read_8_handler_read_16_handler_read_32_handler_全局變量(一種),我想您可以將它們包裝為模板函數內的靜態變量(根據Quentin的建議進行了大幅簡化)

 template <std::size_t S>
 std::array<ReadType<S>, memory_handler_size> & getHandler () 
   { static std::array<ReadType<S>, memory_handler_size> h; return h; }

因此,您可以使用推導的Size模板參數來選擇所需的函數

template<size_t Size>
void initializeReadHandler(uint32_t begin,
                           uint32_t end,
                           ReadType<Size> func) {
    begin >>= 16;
    end >>= 16;

    auto & handler = getHandler<Size>();

    for (uint32_t current = begin; current <= end; ++current) {
       handler[current & 0xFFFF] = func;
    }
}

但是,如果可能的話,我建議您將數組(通過引用)作為參數傳遞給函數。

雖然@ max66的答案很好,但是如果您希望絕對地減少代碼更改,則可以在

這不是必需的,但是我喜歡清理類型:

template<class ReadType>
using handler_type = std::array<ReadType,   memory_handler_size>;
template<std::size_t S>
using handler_for_size = handler_type<ReadType<S>>;

handler_for_size<8> read_8_handler_;
handler_for_size<16> read_16_handler_;
handler_for_size<32> read_32_handler_;

然后initializeReadHandler

template<size_t Size>
void initializeReadHandler(uint32_t begin,
                       uint32_t end,
                       ReadType<Size> func) {
  begin >>= 16;
  end >>= 16;
  for (uint32_t current = begin; current <= end; ++current) {
     auto& handler = std::get<handler_type<ReadType<Size>>( std::tie(read_8_handler_, read_16_handler_, read_32_handler_) );
     handler[current & 0xFFFF] = func;
  }
}

在這里,我們使用基於類型的get和對零成本的引用來獲取正確的處理程序數組。

最大的區別是您可以保留全局的read_8_handler_ 如果沒有理由使用全局read_8_handler_ ,請使用@ max66的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM