第3月第13天 cpp模版 Iterator模式 proactor
1.模版除了传参,还可以自动创建。而传指针只是传参而已。
template <class TYPE, class FUNCTOR, class ACE_LOCK, typename TIME_POLICY = ACE_Default_Time_Policy>
class ACE_Timer_List_T : public ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK, TIME_POLICY>
{
public:
/// Type of iterator
typedef ACE_Timer_List_Iterator_T<TYPE, FUNCTOR, ACE_LOCK, TIME_POLICY> Iterator; /// Iterator is a friend
friend class ACE_Timer_List_Iterator_T<TYPE, FUNCTOR, ACE_LOCK, TIME_POLICY>; typedef ACE_Timer_Node_T<TYPE> Node;
/// Type inherited from
typedef ACE_Timer_Queue_T<TYPE, FUNCTOR, ACE_LOCK, TIME_POLICY> Base_Timer_Queue;
typedef ACE_Free_List<Node> FreeList; // = Initialization and termination methods.
/**
* Default constructor. @a upcall_functor is the instance of the
* FUNCTOR to be used by the list. If @a upcall_functor is 0, a
* default FUNCTOR will be created. @a freelist is the freelist of
* timer nodes. If 0, then a default freelist will be created.
*/
ACE_Timer_List_T (FUNCTOR* upcall_functor = , FreeList* freelist = ,
TIME_POLICY const & time_policy = TIME_POLICY());
template<typename TYPE, typename FUNCTOR>
class ACE_Timer_Queue_Upcall_Base
: public ACE_Abstract_Timer_Queue<TYPE>
, private ACE_Copy_Disabled
{
public:
// Constructor
explicit ACE_Timer_Queue_Upcall_Base(FUNCTOR * upcall_functor = ); /// Destructor
virtual ~ACE_Timer_Queue_Upcall_Base (void); /// Accessor to the upcall functor
FUNCTOR & upcall_functor (void); protected:
/// Upcall functor
FUNCTOR *upcall_functor_; /// To delete or not to delete is the question?
bool const delete_upcall_functor_;
}; template <class TYPE, class FUNCTOR> ACE_INLINE
ACE_Timer_Queue_Upcall_Base<TYPE, FUNCTOR>::ACE_Timer_Queue_Upcall_Base (FUNCTOR * upcall_functor)
: ACE_Abstract_Timer_Queue<TYPE>()
, ACE_Copy_Disabled()
, upcall_functor_(upcall_functor)
, delete_upcall_functor_ (upcall_functor == )
{
ACE_TRACE ("ACE_Timer_Queue_Upcall_Base::ACE_Timer_Queue_Upcall_Base"); if (upcall_functor != )
{
return;
} ACE_NEW (upcall_functor_, FUNCTOR);
}
2.Iterator模式
template <class TYPE>
class ACE_Timer_Queue_Iterator_T
{
public:
// = Initialization and termination methods.
/// Constructor.
ACE_Timer_Queue_Iterator_T (void); /// Destructor.
virtual ~ACE_Timer_Queue_Iterator_T (void); /// Positions the iterator at the earliest node in the Timer Queue
virtual void first (void) = ; /// Positions the iterator at the next node in the Timer Queue
virtual void next (void) = ; /// Returns true when there are no more nodes in the sequence
virtual bool isdone (void) const = ; /// Returns the node at the current position in the sequence
virtual ACE_Timer_Node_T<TYPE> *item (void) = ;
};
实现 ACE_Timer_Hash_T, ACE_Timer_Heap_T , ACE_Timer_List_T, ACE_Timer_Wheel_T
http://blog.csdn.net/u010700335/article/details/41214839
http://blogs.readthedocs.io/zh_CN/latest/Timer_mgr.html
3.proactor
1)
proactor关联了很多异步操作。Asynch_IO里的异步操作会调用proactor->create_asynch 。比如:ACE_Asynch_Accept::open => proactor->create_asynch_accept () => ACE_WIN32_Asynch_Accept
ACE_Asynch_Read_Stream::open => proactor->create_asynch_read_stream () => ACE_WIN32_Asynch_Read_Stream。
Receiver调用this->rs_.read (*mb, mb->size ()- 1) => ACE_Asynch_Read_Stream::read => ACE_WIN32_Asynch_Read_Stream::read。
有数据时proactor会调用 ACE_WIN32_Asynch_Read_Stream_Result::complete => handler->handle_read_stream (result)就回到Receiver。
ACE_Asynch_Read_Stream_Impl *
ACE_Proactor::create_asynch_read_stream (void)
{
return this->implementation ()->create_asynch_read_stream ();
} ACE_Asynch_Write_Stream_Impl *
ACE_Proactor::create_asynch_write_stream (void)
{
return this->implementation ()->create_asynch_write_stream ();
} ACE_Asynch_Read_Dgram_Impl *
ACE_Proactor::create_asynch_read_dgram (void)
{
return this->implementation ()->create_asynch_read_dgram ();
} ACE_Asynch_Write_Dgram_Impl *
ACE_Proactor::create_asynch_write_dgram (void)
{
return this->implementation ()->create_asynch_write_dgram ();
} ACE_Asynch_Read_File_Impl *
ACE_Proactor::create_asynch_read_file (void)
{
return this->implementation ()->create_asynch_read_file ();
} ACE_Asynch_Write_File_Impl *
ACE_Proactor::create_asynch_write_file (void)
{
return this->implementation ()->create_asynch_write_file ();
} ACE_Asynch_Accept_Impl *
ACE_Proactor::create_asynch_accept (void)
{
return this->implementation ()->create_asynch_accept ();
} ACE_Asynch_Connect_Impl *
ACE_Proactor::create_asynch_connect (void)
{
return this->implementation ()->create_asynch_connect ();
} ACE_Asynch_Transmit_File_Impl *
ACE_Proactor::create_asynch_transmit_file (void)
{
return this->implementation ()->create_asynch_transmit_file ();
}
2)ACE_Asynch_Acceptor
ACE_Asynch_Acceptor an example of the Acceptor Pattern.
proactor需要定义ACE_Asynch_Acceptor。ACE_Asynch_Acceptor包含asynch_accept_对象,asynch_accept_对象的open方法创建了implementation_,implementation_的win32实现ACE_WIN32_Asynch_Accept又定义了proactor。
ACE_WIN32_Asynch_Accept::accept会创建ACE_WIN32_Asynch_Accept_Result,ACE_WIN32_Asynch_Accept_Result继承ACE_WIN32_Asynch_Result。ACE_WIN32_Asynch_Result::post_completion 就会调用win32_proactor。ACE_WIN32_Proactor::handle_events就会得到ACE_WIN32_Asynch_Result了。
ACE_WIN32_Asynch_Result包含handler_,这个handler_就是ACE_Asynch_Acceptor对象。ACE_Asynch_Acceptor的handle_accept函数被调用时就会根据模板创建HANDLER *new_handler,然后调用open。
http://blog.csdn.net/ghosthjt/article/details/7624184
ACE_Asynch_Acceptor<Receiver> acceptor; int Rc = - ; if ( host == NULL ) // Acceptor
{
// Simplify , initial read with zero size
Rc = acceptor.open (ACE_INET_Addr (port),,); }
void Receiver::open (ACE_HANDLE handle,
ACE_Message_Block &message_block)
{
...
template <class HANDLER>
class ACE_Asynch_Acceptor : public ACE_Handler
{
public:
/// A do nothing constructor.
ACE_Asynch_Acceptor (void); /// Virtual destruction
virtual ~ACE_Asynch_Acceptor (void);
...
/// Asynch_Accept used to make life easier :-)
ACE_Asynch_Accept asynch_accept_;
class ACE_Export ACE_WIN32_Asynch_Accept : public virtual ACE_Asynch_Accept_Impl,
public ACE_WIN32_Asynch_Operation
{
public:
/// Constructor.
ACE_WIN32_Asynch_Accept (ACE_WIN32_Proactor *win32_proactor); /**
* This starts off an asynchronous accept. The asynchronous accept
* call also allows any initial data to be returned to the
* <handler>. Upto <bytes_to_read> will be read and stored in the
* <message_block>. The <accept_handle> will be used for the
* <accept> call. If (<accept_handle> == INVALID_HANDLE), a new
* handle will be created.
*
* <message_block> must be specified. This is because the address of
* the new connection is placed at the end of this buffer.
*/
int accept (ACE_Message_Block &message_block,
size_t bytes_to_read,
ACE_HANDLE accept_handle,
const void *act,
int priority,
int signal_number = ); /// Destructor.
~ACE_WIN32_Asynch_Accept (void); // Methods belong to ACE_WIN32_Asynch_Operation base class. These
// methods are defined here to avoid VC++ warnings. They route the
// call to the ACE_WIN32_Asynch_Operation base class. /**
* Initializes the factory with information which will be used with
* each asynchronous call. If (<handle> == ACE_INVALID_HANDLE),
* <ACE_Handler::handle> will be called on the <handler> to get the
* correct handle.
*/
int open (ACE_Handler &handler,
ACE_HANDLE handle,
const void *completion_key,
ACE_Proactor *proactor); /**
* This cancels all pending accepts operations that were issued by
* the calling thread. The function does not cancel asynchronous
* operations issued by other threads.
*/
int cancel (void); /// Return the underlying proactor.
ACE_Proactor* proactor (void) const;
};
class ACE_Export ACE_Asynch_Accept : public ACE_Asynch_Operation
{ public:
/// A do nothing constructor.
ACE_Asynch_Accept (void); /// Destructor.
virtual ~ACE_Asynch_Accept (void); /**
* Initializes the factory with information which will be used with
* each asynchronous call. If (<handle> == ACE_INVALID_HANDLE),
* <ACE_Handler::handle> will be called on the <handler> to get the
* correct handle.
*/
int open (ACE_Handler &handler,
ACE_HANDLE handle = ACE_INVALID_HANDLE,
const void *completion_key = ,
ACE_Proactor *proactor = ); /**
* This starts off an asynchronous accept. The asynchronous accept
* call also allows any initial data to be returned to the
* <handler>. Upto <bytes_to_read> will be read and stored in the
* <message_block>. The <accept_handle> will be used for the
* <accept> call. If (<accept_handle> == INVALID_HANDLE), a new
* handle will be created. Priority of the
* operation is specified by <priority>. On POSIX4-Unix, this is
* supported. Works like <nice> in Unix. Negative values are not
* allowed. 0 means priority of the operation same as the process
* priority. 1 means priority of the operation is one less than
* process. And so forth. On Win32, this is a no-op.
*
* <message_block> must be specified. This is because the address of
* the new connection is placed at the end of this buffer.
* <signal_number> is the POSIX4 real-time signal number to be used
* for the operation. <signal_number> ranges from ACE_SIGRTMIN to
* ACE_SIGRTMAX. This argument is a no-op on non-POSIX4 systems.
*/
int accept (ACE_Message_Block &message_block,
size_t bytes_to_read,
ACE_HANDLE accept_handle = ACE_INVALID_HANDLE,
const void *act = ,
int priority = ,
int signal_number = ACE_SIGRTMIN); /// Return the underlying implementation class.
// (this should be protected...)
virtual ACE_Asynch_Operation_Impl *implementation (void) const; protected:
/// Delegation/implementation class that all methods will be
/// forwarded to.
ACE_Asynch_Accept_Impl *implementation_;
int
ACE_Asynch_Accept::open (ACE_Handler &handler,
ACE_HANDLE handle,
const void *completion_key,
ACE_Proactor *proactor)
{
// Get a proactor for/from the user.
proactor = this->get_proactor (proactor, handler); // Now let us get the implementation initialized.
if ((this->implementation_ = proactor->create_asynch_accept ()) == )
return -; // Call the <open> method of the base class.
return ACE_Asynch_Operation::open (handler,
handle,
completion_key,
proactor);
}
第3月第13天 cpp模版 Iterator模式 proactor的更多相关文章
- 第13章 模版方法模式(Template Method)
原文 第13章 模版方法模式(Template Method) 模板模式 模板模式 举例:模拟下数据库的update方法,先删除在插入. 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
- Java设计模式の模版方法模式
概述 模板方法模式是类的行为模式.准备一个抽象类,将部分逻辑以具体方法以及具体构造函数的形式实现,然后声明一些抽象方法来迫使子类实现剩余的逻辑.不同的子类可以以不同的方式实现这些抽象方法,从而对剩余的 ...
- Head First 设计模式笔记(模版方法模式)
1.定义: 在一个方法中定义一个算法骨架,而将一些步骤延迟到子类中.模版方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤. 2.类图: 3.说明: 模版方法可以理解为一个方法里面包 ...
- [Head First设计模式]云南米线馆中的设计模式——模版方法模式
系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...
- JS常用的设计模式(10)——模版方法模式
模式方法是预先定义一组算法,先把算法的不变部分抽象到父类,再将另外一些可变的步骤延迟到子类去实现.听起来有点像工厂模式( 非前面说过的简单工厂模式 ). 最大的区别是,工厂模式的意图是根据子类的实现最 ...
- 月半小夜曲下的畅想--DOCTYPE模式
月半小夜曲下的畅想--DOCTYPE模式 @(css3 box-sizing)[doctype声明|quirks模式|妙瞳] DOCTYPE文档类型标签,该标签是将特定的标准通用标记语言或者XML文档 ...
- Python设计模式——模版方法模式
1.模版方法模式 做题的列子: 需求:有两个学生,要回答问题,写出自己的答案 #encoding=utf-8 __author__ = 'kevinlu1010@qq.com' class Stude ...
- 【设计模式 - 23】之模版方法模式(Template)
1 模式简介 模版方法模式的定义: 模版方法模式在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中.模版方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤. 模版方法模 ...
- Chapter 10 模版方法模式
我们要完成在某一细节层次一致的一个过程或一系列步骤,但其个别步骤在更详细的层次上的实现可能不同时,我们通常考虑用模版模式来处理. 模版方法模式:定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.模 ...
随机推荐
- Codeforces Round #378 (Div. 2)
A: 思路: 水题,没啥意思; B: 思路: 暴力,也没啥意思; C: 思路: 思维,可以发现从前往后和为b[i]的分成一块,然后这一块里面如果都相同就没法开始吃,然后再暴力找到那个最大的且能一开始就 ...
- POJ 2955 Brackets --最大括号匹配,区间DP经典题
题意:给一段左右小.中括号串,求出这一串中最多有多少匹配的括号. 解法:此问题具有最优子结构,dp[i][j]表示i~j中最多匹配的括号,显然如果i,j是匹配的,那么dp[i][j] = dp[i+1 ...
- jdbc java数据库连接 5)CallableStatement 接口
CallableStatement执行存储过程(也是预编译语言) 首先在sql中执行以下带有输入参数的代码: DELIMITER $ CREATE PROCEDURE pro_findById(IN ...
- jQuery ScrollPagination修改之后
jQuery ScrollPagination修改之后代码 /* ** Anderson Ferminiano ** contato@andersonferminiano.com -- feel fr ...
- libpng使用
自己的实现 unsigned int component(png_const_bytep row, png_uint_32 x, unsigned int c, unsigned int bit_de ...
- 用C#实现封装
用C#实现封装 1.属性对外公开类似于类的接口实现对字段的访问;2.字段为private只能在内部被直接访问,如果当属性为只读,那么可以将形参直接对字段赋值.(有没有更好的方法?);3.可以通过关键字 ...
- c语言之【#ifdef】
电脑程序语句,我们可以用它区隔一些与特定头文件.程序库和其他文件版本有关的代码. 1 2 3 #ifdef 语句1 // 程序2 #endif 可翻译为:如果宏定义了语句1则程序2. 作用:我 ...
- jQuery1.9之后使用on()绑定 动态生成元素的 事件无效
来自互联网: 需要绑定a的父级元素(此元素必须为静态元素,不是后来动态生成的),然后设定on()方法的selector参数才行: $('p').on('mouseenter', 'a', functi ...
- jsonp接口的xss防范
防范方式也很简单,只要在header里输出类型设置为javascript即可: 1 header('Content-type: text/javascript;charset=utf-8');
- Linux shell编程
1. 批量添加用户的小脚本: for name in xp wrg lct do useradd $name echo red | passwd --stdin $name done 对echo re ...