I'm currently struggling with lambda expressions. I created a ConcurrentDictionary which wraps std::map with a mutex. Now I'd like to export all elements that match a specified condition.
template<typename MutexTypeT, typename Key_Type_T, typename Mapped_Type_T>
class CConcurrentDictionary
{
public:
///Constructor
CConcurrentDictionary();
class CSingleElement
{
public:
///the key
Key_Type_T key = { };
///THe Mapped Type
Mapped_Type_T mapped_type = { };
};
template<typename ...Args_T>
std::vector<CSingleElement> exportSelectedData(
const uint32_t u32MutexTimeout,
std::function<bool(const CSingleElement, Args_T &&...)> compareFn,
Args_T&&...CompareArgs...) const;
}
template<typename MutexTypeT, typename Key_Type_T, typename Mapped_Type_T>
template<typename ...Args_T>
auto CConcurrentDictionary<MutexTypeT, Key_Type_T, Mapped_Type_T>::exportSelectedData(
const uint32_t u32MutexTimeout,
std::function<bool(const CSingleElement, Args_T &&...)> compareFn,
Args_T&&...CompareArgs...) const ->
std::vector<CSingleElement>
{
//lock mutex...
std::vector<CSingleElement> vecRes;
for (const auto &single_element : dictionary)
{
if(compareFn(single_element, CompareArgs...))
{
//this element mathes the preconditions
vecRes.push_back(single_element);
}
}
return vecRes;
}
However when I try to use this class like this
class CTraceCmdDuration
{
public:
///Cmd Datatype
using CmdType_T = uint32_t;
class CCmdSummary
{
public:
/**
* SIngle Long Running Cmd Summary
* @param CmdIdArg Cmd ID
* @param u32MaxCmdDurationArg Max CmdDuration
*/
CCmdSummary(const CmdType_T CmdIdArg, const uint32_t u32MaxCmdDurationArg);
///Id of this cmd
const CmdType_T CmdId;
///Duration of this cmd
const uint32_t u32MaxCmdDuration;
};
/**
* Exports all Cmds to a vector with took longer than u32MaxCmdDuration
* @param u32MaxCmdDuration Maximal Cmd Processing duration time. Cmds that took longer than this will be exported
* @param u32MutexTimeout Mutex Timeout
* @return List with all long running cmds
*/
std::vector<CCmdSummary> ExportLongRunningCmds(const uint32_t u32MaxCmdDuration, const uint32_t u32MutexTimeout) const;
}
auto CTraceCmdDuration::ExportLongRunningCmds(const uint32_t u32MaxCmdDuration, const uint32_t u32MutexTimeout) const ->
std::vector<CCmdSummary>
{
auto lambda = [u32MaxCmdDuration](const CombinedDictElement& singleElement)
{
const bool bRes = (u32MaxCmdDuration < singleElement.mapped_type.u32MaxProcessingDuration);
return bRes;
};
auto vecRes = dict.exportSelectedData(u32MutexTimeout, lambda, u32MaxCmdDuration);
return vecRes;
}
This unfortunately produces no matching call to function errors
error: no matching function for call to 'NConcurrent_Container::CConcurrentDictionary<NMutex::CEmbos_Mutex, long unsigned int, NDebug::CTraceCmdDuration::CProcessingInfo>::exportSelectedData(const uint32_t&, NDebug::CTraceCmdDuration::ExportLongRunningCmds(uint32_t, uint32_t) const::__lambda0&, const uint32_t&) const'
auto vecRes = dict.exportSelectedData(u32MutexTimeout, lambda, u32MaxCmdDuration);
What is wrong with my lambda expression? The Idea was to pass the max allowed time duration as a capture and every single stored element in the std::map as an argument.
Or do you have a better Idea? Could you help me out here?
Edit: Thx for your answer this works well if I pass a static function but how would I pass the lambda as a template argument?
static bool CompareDuaration(const CSingleElement&singleElement, const uint32_t u32MaxDuration);
auto CTraceCmdDuration::ExportLongRunningCmds(const uint32_t u32MaxCmdDuration, const uint32_t u32MutexTimeout) const ->
std::vector<CombinedDictElement>
{
auto vecRes = dict.exportSelectedData(u32MutexTimeout, lambda, u32MaxCmdDuration);
return vecRes;
}
This works but
auto CTraceCmdDuration::ExportLongRunningCmds(const uint32_t u32MaxCmdDuration, const uint32_t u32MutexTimeout) const ->
std::vector<CombinedDictElement>
{
auto lambda = [u32MaxCmdDuration](const CombinedDictElement& singleElement)
{
const bool bRes = (u32MaxCmdDuration < singleElement.mapped_type.u32MaxProcessingDuration);
return bRes;
};
auto vecRes = dict.exportSelectedData(u32MutexTimeout, lambda, u32MaxCmdDuration);
return vecRes;
}
gives me a compile error
error: no match for call to '(CTraceCmdDuration::ExportLongRunningCmds(uint32_t, uint32_t) const::__lambda0) (CConcurrentDictionary<NMutex::CEmbos_Mutex, long unsigned int, CTraceCmdDuration::CProcessingInfo>::CSingleElement&, const long unsigned int&)'
if(compareFn(singleElement, compareArgs...))
It seams like passing lambdas to templates does not work so well. What am I Missing?
CombinedDictElement?