diff --git a/src/butil/object_pool.h b/src/butil/object_pool.h index c8690821ef..5bb312b9e2 100644 --- a/src/butil/object_pool.h +++ b/src/butil/object_pool.h @@ -75,21 +75,10 @@ template inline bool local_pool_free_empty() { } // Get an object typed |T|. The object should be cleared before usage. -// NOTE: T must be default-constructible. -template inline T* get_object() { - return ObjectPool::singleton()->get_object(); -} - -// Get an object whose constructor is T(arg1) -template -inline T* get_object(const A1& arg1) { - return ObjectPool::singleton()->get_object(arg1); -} - -// Get an object whose constructor is T(arg1, arg2) -template -inline T* get_object(const A1& arg1, const A2& arg2) { - return ObjectPool::singleton()->get_object(arg1, arg2); +// NOTE: If there are no arguments, T must be default-constructible. +template +inline T* get_object(Args... args) { + return ObjectPool::singleton()->get_object(std::forward(args)...); } // Return the object |ptr| back. The object is NOT destructed and will be diff --git a/src/butil/object_pool_inl.h b/src/butil/object_pool_inl.h index 0055239ded..ab8d88d199 100644 --- a/src/butil/object_pool_inl.h +++ b/src/butil/object_pool_inl.h @@ -147,7 +147,7 @@ class BAIDU_CACHELINE_ALIGNMENT ObjectPool { // which may include parenthesis because when T is POD, "new T()" // and "new T" are different: former one sets all fields to 0 which // we don't want. -#define BAIDU_OBJECT_POOL_GET(...) \ +#define BAIDU_OBJECT_POOL_GET(CTOR_ARGS) \ /* Fetch local free ptr */ \ if (_cur_free.nfree) { \ BAIDU_OBJECT_POOL_FREE_ITEM_NUM_SUB1; \ @@ -164,7 +164,7 @@ class BAIDU_CACHELINE_ALIGNMENT ObjectPool { /* Fetch memory from local block */ \ if (_cur_block && _cur_block->nitem < BLOCK_NITEM) { \ auto item = _cur_block->items + _cur_block->nitem; \ - obj = new (item->void_data()) T(__VA_ARGS__); \ + obj = new (item->void_data()) T CTOR_ARGS; \ if (!ObjectPoolValidator::validate(obj)) { \ obj->~T(); \ return NULL; \ @@ -176,7 +176,7 @@ class BAIDU_CACHELINE_ALIGNMENT ObjectPool { _cur_block = add_block(&_cur_block_index); \ if (_cur_block != NULL) { \ auto item = _cur_block->items + _cur_block->nitem; \ - obj = new (item->void_data()) T(__VA_ARGS__); \ + obj = new (item->void_data()) T CTOR_ARGS; \ if (!ObjectPoolValidator::validate(obj)) { \ obj->~T(); \ return NULL; \ @@ -191,14 +191,9 @@ class BAIDU_CACHELINE_ALIGNMENT ObjectPool { BAIDU_OBJECT_POOL_GET(); } - template - inline T* get(const A1& a1) { - BAIDU_OBJECT_POOL_GET(a1); - } - - template - inline T* get(const A1& a1, const A2& a2) { - BAIDU_OBJECT_POOL_GET(a1, a2); + template + inline T* get(Args... args) { + BAIDU_OBJECT_POOL_GET((std::forward(args)...)); } #undef BAIDU_OBJECT_POOL_GET diff --git a/src/butil/resource_pool.h b/src/butil/resource_pool.h index cc0fb00056..84da80525b 100644 --- a/src/butil/resource_pool.h +++ b/src/butil/resource_pool.h @@ -92,21 +92,10 @@ namespace butil { // Get an object typed |T| and write its identifier into |id|. // The object should be cleared before usage. -// NOTE: T must be default-constructible. -template inline T* get_resource(ResourceId* id) { - return ResourcePool::singleton()->get_resource(id); -} - -// Get an object whose constructor is T(arg1) -template -inline T* get_resource(ResourceId* id, const A1& arg1) { - return ResourcePool::singleton()->get_resource(id, arg1); -} - -// Get an object whose constructor is T(arg1, arg2) -template -inline T* get_resource(ResourceId* id, const A1& arg1, const A2& arg2) { - return ResourcePool::singleton()->get_resource(id, arg1, arg2); +// NOTE: If there are no arguments, T must be default-constructible. +template +inline T* get_resource(ResourceId* id, Args... args) { + return ResourcePool::singleton()->get_resource(id, std::forward(args)...); } // Return the object associated with identifier |id| back. The object is NOT diff --git a/src/butil/resource_pool_inl.h b/src/butil/resource_pool_inl.h index 316e37f395..115bceda5d 100644 --- a/src/butil/resource_pool_inl.h +++ b/src/butil/resource_pool_inl.h @@ -164,7 +164,7 @@ class BAIDU_CACHELINE_ALIGNMENT ResourcePool { // which may include parenthesis because when T is POD, "new T()" // and "new T" are different: former one sets all fields to 0 which // we don't want. -#define BAIDU_RESOURCE_POOL_GET(...) \ +#define BAIDU_RESOURCE_POOL_GET(CTOR_ARGS) \ /* Fetch local free id */ \ if (_cur_free.nfree) { \ const ResourceId free_id = _cur_free.ids[--_cur_free.nfree]; \ @@ -187,7 +187,7 @@ class BAIDU_CACHELINE_ALIGNMENT ResourcePool { if (_cur_block && _cur_block->nitem < BLOCK_NITEM) { \ id->value = _cur_block_index * BLOCK_NITEM + _cur_block->nitem; \ auto item = _cur_block->items + _cur_block->nitem; \ - p = new (item->void_data()) T(__VA_ARGS__); \ + p = new (item->void_data()) T CTOR_ARGS; \ if (!ResourcePoolValidator::validate(p)) { \ p->~T(); \ return NULL; \ @@ -200,7 +200,7 @@ class BAIDU_CACHELINE_ALIGNMENT ResourcePool { if (_cur_block != NULL) { \ id->value = _cur_block_index * BLOCK_NITEM + _cur_block->nitem; \ auto item = _cur_block->items + _cur_block->nitem; \ - p = new (item->void_data()) T(__VA_ARGS__); \ + p = new (item->void_data()) T CTOR_ARGS; \ if (!ResourcePoolValidator::validate(p)) { \ p->~T(); \ return NULL; \ @@ -215,14 +215,9 @@ class BAIDU_CACHELINE_ALIGNMENT ResourcePool { BAIDU_RESOURCE_POOL_GET(); } - template - inline T* get(ResourceId* id, const A1& a1) { - BAIDU_RESOURCE_POOL_GET(a1); - } - - template - inline T* get(ResourceId* id, const A1& a1, const A2& a2) { - BAIDU_RESOURCE_POOL_GET(a1, a2); + template + inline T* get(ResourceId* id, Args... args) { + BAIDU_RESOURCE_POOL_GET((std::forward(args)...)); } #undef BAIDU_RESOURCE_POOL_GET