??xml version="1.0" encoding="utf-8" standalone="yes"?>
2 //声明需要的选项
3 options_description desc("Allowed options");
4 desc.add_options()
5 ("help,h", "produce help message")
6 ("person,p", value<string>()->default_value("world"), "who");
看第4?行,是不是感觉很怪?q种方式体现了函数式~程中最大的特点Q函数是一cd|引用资料来说,所?#8220;函数是一cd|First Class ValueQ?#8221;指的是函数和值是同等的概念,一个函数可以作为另外一个函数的参数Q也可以作ؓg用。如果函数可以作Zcdg用,那么我们可以写Z些函敎ͼ使得q些函数接受其它函数作ؓ参数q返回另外一个函数。比如定义了f和g两个函数Q用compose(f,g)的风格就可以生成另外一个函敎ͼ使得q个函数执行f(g(x))的操作,则可Ucompose为高阶函敎ͼHigher-order FunctionQ?br>
program_options里的q种方式是怎么实现的呢Q通过分析boost的源代码Q我们自己来写个cM的实现看看:
test.h
2
3 #include <iostream>
4 using namespace std;
5
6 class Test;
7
8 class Test_easy_init
9 {
10 public:
11 Test_easy_init(Test* owner):m_owner(owner){}
12
13 Test_easy_init & operator () (const char* name);
14 Test_easy_init & operator () (const char* name,int id);
15 private:
16 Test* m_owner;
17 };
18
19
20 class Test
21 {
22 public:
23 void add(const char* name);
24 void add(const char* name,int id);
25
26 Test_easy_init add_some();
27
28 };
test.cpp
2
3 Test_easy_init & Test_easy_init::operator () (const char* name,int id)
4 {
5
6 m_owner->add(name,id);
7 return *this;
8 }
9
10
11 Test_easy_init & Test_easy_init::operator () (const char* name)
12 {
13
14 m_owner->add(name);
15 return *this;
16 }
17
18 Test_easy_init Test::add_some()
19 {
20 return Test_easy_init(this);
21 }
22
23
24 void Test::add(const char* name)
25 {
26 cout<<"add:"<<name<<endl;
27 }
28
29 void Test::add(const char* name,int id)
30 {
31 cout<<"add:"<<name<<"-"<<id<<endl;
32 }
使用方式Q?br>
2
3 t1.add_some()
4 ("hello",1)
5 ("no id")
6 ("hello2",2);
是不是很有意思。add_some()Ҏq回一个Test_easy_initcȝ对象QTest_easy_initc重载了操作W?)Q操作符()Ҏq回Test_easy_initcd象自w的引用。?
]]>
]]>
做过一个简单的试Q?0万次内存池调用的效率大概比直接分配释攑ֆ存提高了30-50%。但是前提是内存池不能加锁(pthread_mutexQ,加锁的内存池效率和直接分配内存的效率差不多,有时候还要多点点。(试的环境是每次2KQ?个双核CPUQFREEBSD7Q?br>
代码实现Q?br>
struct memblock
{
int used;
void* data;
struct memblock* next;
struct memblock* createnext;
};
{
int size;//memblock大小
int unused;//I闲的memblock大小
int datasize;//每次分配的数据大?是memblock.data)
struct memblock* free_linkhead;//I闲memblock链表?/span>
struct memblock* create_linkhead;//所有创建的memblock链表_内存池释攄时候用,防止内存池释攄gq有memblock未归q的情况
};
typedef void (*free_callback)(void*);//释放回调函数Q释放membloc.data用,可以单的直接用free函数
void mempool_init(int initialSize,int datasize);//初始化mempool
void mempool_dealloc(struct mempool* pool,free_callback callback);//释放mempool
void* mempool_get(struct mempool* pool);//获取一个memblock
void mempool_release(struct mempool* pool,struct memblock* block);//归还一个memblock
/*********************************
* mempool
* ******************************/
//malloc一个memblock
static struct memblock* mempool_allocblock( struct mempool* pool );
//------------------implement--------
void*
mempool_init( int initialSize, int datasize )
{
struct mempool* pool = malloc( sizeof( struct mempool ) );
pool->unused = 0;
pool->datasize = datasize;
pool->free_linkhead = NULL;
//预先初始化initialSize个内存块
pool->create_linkhead = NULL;
int i;
for ( i = 0; i < initialSize; i++ ) {
struct memblock* block = mempool_allocblock( pool );
mempool_release( pool, block );
}
return ( pool );
}
void
mempool_dealloc( struct mempool* pool, free_callback callback )
{
struct memblock* block = NULL;
//所有创建的memblock释放?/span>
while ( pool->create_linkhead != NULL ) {
block = pool->create_linkhead;
pool->create_linkhead = pool->create_linkhead->createnext;
//执行free回调?/span>
if ( callback ) {
( *callback )( block->data );
}
free( block );
}
free( pool );
L_DEBUG( "%s:size(%d),unused(%d)", __func__, pool->size, pool->unused );
}
static struct memblock*
mempool_allocblock( struct mempool* pool )
{
struct memblock* block = malloc( sizeof( struct memblock ) );
block->data = malloc( sizeof( pool->datasize ) );
block->next = NULL;
block->used = 1;//表示已?br>
//加入所有创建的memblock的链表头
block->createnext = pool->create_linkhead;
pool->create_linkhead = block;
pool->size++;
return ( block );
}
void
mempool_release( struct mempool* pool, struct memblock* block )
{
if ( block == NULL ) {
L_WARN( "%s:release a NULL!", __func__ );
return;
}
if ( block->used != 1 ) {
L_WARN( "%s:used!=1", __func__ );
return;
}
//归q的内存块放到空闲链表头?/span>
block->used = 0;//表示I闲
block->next = pool->free_linkhead;
pool->free_linkhead = block;
pool->unused++;//I闲?1
}
void*
mempool_get( struct mempool* pool )
{
struct memblock* block = NULL;
if ( pool->free_linkhead ) {
//从空闲链表头取出一个内存块
block = pool->free_linkhead;
pool->free_linkhead = pool->free_linkhead->next;
block->next = NULL;
block->used = 1;//表示已?/span>
pool->unused--;//I闲内存块数-1
}
else {
//没有I闲的内存块Q创Z?/span>
block = mempool_allocblock( pool );
}
return ( block );
}
]]>