【发布时间】:2016-03-30 19:14:45
【问题描述】:
我写了一个模板类
template<typename T, typename U>
class ANotifier : public ACancelable
{
public:
virtual std::weak_ptr<ACancelableToken> RegisterNotification( U msgType, std::function<void(T&)> fn ) = 0;
virtual void Notify( U msgType, T& value ) = 0;
};
template<typename T, typename U>
class ASingleNotifier : public ANotifier<T, U>
{
...
}
template<typename T, typename U>
class AMultiNotifier : public ANotifier<T, U>
{
...
}
基类在 lambda 中提供函数签名。每次实现函数或想要创建特定函数签名的堆栈变量时,我都必须重复定义函数签名。
我想知道是否有办法创建 typedef
像这样的
template<typename T, typename U>
class ANotifier : public ACancelable
{
public:
typedef std::function<void(T&)> NotificationFn
virtual std::weak_ptr<ACancelableToken> RegisterNotification( U msgType, NotificationFn fn ) = 0;
virtual void Notify( U msgType, T& value ) = 0;
};
template<typename T, typename U>
class ASingleNotifier : public ANotifier<T, U>
{
...
virtual std::weak_ptr<ACancelableToke> RegisterNotification( U msgType, NotificationFn fn );
}
template<typename T, typename U>
class AMultiNotifier : public ANotifier<T, U>
{
...
virtual std::weak_ptr<ACancelableToke> RegisterNotification( U msgType, NotificationFn fn );
}
是否需要在每个模板子类中声明一个 typedef。此外,当实际为特定类型的函数创建堆栈变量时,我将如何声明 fn.例如。 NotificationFn<uint32_t> fn;
我使用了上面的内容,但出现此错误:
/Users/kartik/Projects/pancam/hardware/neolib/inc/ANotifier.h:36:76: 错误:未知类型名称“NotificationFnOld”虚拟 std::weak_ptr RegisterNotification(U msgType, NotificationFnOld fn)
其中NotificationFnOld 与NotificationFn 相同
我想要做的是能够在一个地方更改函数签名(即添加/rem 参数)并将其绑定到 typedef。我不介意在每个子类中声明新的 typedef,只要我可以在一个地方更改签名(当然我将不得不更改函数实现,但我想避免修改中间变量和东西)。
编辑
我将 typedef 修改为 using,但仍然出现类似错误。这是我用的...
而不是typedef std::function<void(T&)> NotificationFn;
我用using NotificationFnOld = std::function<void( T& )>;
这是我得到的错误。
/Users/kartik/Projects/pancam/hardware/neolib/inc/ANotifier.h:36:76: 错误:未知类型名称“NotificationFnOld”虚拟 std::weak_ptr RegisterNotification(U msgType, NotificationFnOld fn)
覆盖函数RegisterNotification原型中的子类ASingleNotifier报错。当在ANotifier 中声明的原型中使用别名时,它不会抱怨。如何在我的子类中干净利落地使用别名?
注意:NotificationFnOld 和 NotificationFn 在这个问题中可以互换使用,我实际上在我的代码中只使用了NotificationFnOld。
编辑
需要详细信息,这是我正在尝试的代码。
与ACancelable 应该没有关系,但如果需要更多信息,我会提供。
template<typename T, typename U>
class ANotifier : public ACancelable
{
public:
using NotificationFn = std::function<void( T&, std::weak_ptr<ACancelableToken> )>;
using NotificationFnOld = std::function<void( T& )>;
virtual std::weak_ptr<ACancelableToken> RegisterNotification( U msgType, std::function<void( T&, std::shared_ptr<ACancelableToken> )> fn ) = 0;
virtual void Notify( U msgType, T& value ) = 0;
};
template<typename T, typename U>
class ASingleNotifier : public ANotifier<T, U>
{
public:
virtual ~ASingleNotifier()
{ }
virtual std::weak_ptr<ACancelableToken> RegisterNotification( U msgType, std::function<void( T&, std::shared_ptr<ACancelableToken> )> fn )
{
std::weak_ptr<ACancelableToken> retval;
std::unique_lock <std::mutex> lk( m_mtx );
std::shared_ptr <ATypedCancelableToken<U>> tmp( std::make_shared<ATypedCancelableToken<U>>( *this, msgType ));
if( m_notifierMap.find( msgType ) == m_notifierMap.end() ) {
m_notifierMap[ msgType ] = std::make_pair( fn, tmp );
retval = tmp;
}
return retval;
}
virtual void CancelWith( std::shared_ptr<ACancelableToken> spBaseToken ) const
{
try {
auto spToken = std::dynamic_pointer_cast<ATypedCancelableToken<U>>( spBaseToken );
std::unique_lock<std::mutex> lk( this->m_mtx );
m_notifierMap.erase( spToken->m_msgType );
}
catch ( std::bad_cast exp ) { }
}
virtual void Notify( U msgType, T& value )
{
m_mtx.lock();
auto it = m_notifierMap.find( msgType );
if ( it != m_notifierMap.end() && it->second.first ) {
auto fn = it->second.first;
m_mtx.unlock();
fn( value );
}else {
m_mtx.unlock();
}
}
protected:
mutable std::map <U, std::pair<std::function<void( T&, std::shared_ptr<ACancelableToken> )>, std::shared_ptr<ATypedCancelableToken<U>>>> m_notifierMap;
mutable std::mutex m_mtx;
};
template<typename T, typename U>
class AMultiNotifier : public ANotifier<T,U>
{
protected:
class AmnCancellableToken : public ATypedCancelableToken<U>
{
public:
AmnCancellableToken( const ACancelable& cancellable,
U msgType,
typename std::list<std::pair<std::function<void( T&, std::shared_ptr<ACancelableToken> )>,std::shared_ptr<AmnCancellableToken>>>::iterator it ) :
ATypedCancelableToken<U>{ cancellable, msgType }, m_it{ it } { }
~AmnCancellableToken() {}
const typename std::list<std::pair<std::function<void( T&, std::shared_ptr<ACancelableToken> )>,std::shared_ptr<AmnCancellableToken>>>::iterator m_it;
};
public:
~AMultiNotifier() { }
virtual std::weak_ptr<ACancelableToken> RegisterNotification( U msgType, std::function<void( T&, std::shared_ptr<ACancelableToken> )> fn )
{
std::weak_ptr<ACancelableToken> retval;
std::unique_lock <std::mutex> lk( m_mtx );
std::shared_ptr<AmnCancellableToken> token;
m_notifierMap[ msgType ].push_back( std::make_pair( fn, token) );
auto it = m_notifierMap[msgType].end();
token = std::make_shared<AmnCancellableToken>( *this, msgType, --it );
m_notifierMap[ msgType ].back().second = token;
retval = token;
return retval;
}
virtual void CancelWith( std::shared_ptr<ACancelableToken> spBaseToken ) const
{
try {
auto spToken = std::dynamic_pointer_cast<AmnCancellableToken>( spBaseToken );
std::unique_lock<std::mutex> lk( this->m_mtx );
if ( !m_notifierMap[ spToken->m_msgType ].empty()) { //If the list of handler is not empty
m_notifierMap[ spToken->m_msgType ].erase( spToken->m_it ); //Delete the handler in list
if ( m_notifierMap[ spToken->m_msgType ].empty()) //If the list is now empty
m_notifierMap.erase( spToken->m_msgType ); // Delete the msgType Key element
}
} catch ( std::bad_cast exp ) { }
}
virtual void Notify( U msgType, T& value )
{
m_mtx.lock();
auto anotherCopy = m_notifierMap;
m_mtx.unlock();
typename std::map<U, std::list<std::pair<std::function<void( T&, std::shared_ptr<ACancelableToken> )>, std::shared_ptr<AmnCancellableToken>>>>::iterator ait =
anotherCopy.find( msgType );
if( ait != anotherCopy.end() &&
!ait->second.empty() ) {
for( auto ait2 = ait->second.begin(); ait2 != ait->second.end(); ait2++ )
if( ait2->first )
ait2->first( value );
}
}
protected:
mutable std::map <U, std::list<std::pair<std::function<void( T&, std::shared_ptr<ACancelableToken> )>,std::shared_ptr<AmnCancellableToken>>>> m_notifierMap;
mutable std::mutex m_mtx;
};
感谢您的帮助/建议
卡提克
【问题讨论】:
-
我在提供的代码中没有看到任何 lambda。
-
你在尝试 typedef 方式时会出错吗?
-
C++11
using语法比旧的typedef机制读起来更清晰,请考虑改用它。 -
另外,你的 typedef 后面需要一个分号...
标签: c++ templates lambda typedef