Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix universal reference #2862

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/brpc/versioned_ref_with_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ typename std::enable_if<butil::is_void<Ret>::value, Ret>::type ReturnEmpty() {}
template<typename... Args> \
typename std::enable_if<decltype( \
Test<class_type, Args...>(0))::value, return_type>::type \
Call(class_type* obj, Args... args) { \
Call(class_type* obj, Args&&... args) { \
BAIDU_CASSERT((butil::is_result_same< \
return_type, decltype(&T::func_name), T, Args...>::value), \
"Params or return type mismatch"); \
Expand All @@ -112,7 +112,7 @@ typename std::enable_if<butil::is_void<Ret>::value, Ret>::type ReturnEmpty() {}
template<typename... Args> \
typename std::enable_if<!decltype( \
Test<class_type, Args...>(0))::value, return_type>::type \
Call(class_type* obj, Args... args) { \
Call(class_type* obj, Args&&... args) { \
return ReturnEmpty<return_type>(); \
} \
}
Expand All @@ -128,13 +128,13 @@ typename std::enable_if<butil::is_void<Ret>::value, Ret>::type ReturnEmpty() {}
//
// CRTP
// Derived classes implement 6 functions :
// 1. (required) int OnCreated(Args... args) :
// 1. (required) int OnCreated(Args&&... args) :
// Will be called in Create() to initialize T init when T is created successfully.
// If initialization fails, return non-zero. VersionedRefWithId will be `SetFailed'
// and Create() returns non-zero.
// 2. (required) void BeforeRecycled() :
// Will be called in Dereference() before T is recycled.
// 3. (optional) void OnFailed(Args... args) :
// 3. (optional) void OnFailed(Args&&... args) :
// Will be called in SetFailed() when VersionedRefWithId is set failed successfully.
// 4. (optional) void BeforeAdditionalRefReleased() :
// Will be called in ReleaseAdditionalReference() before additional ref is released.
Expand Down Expand Up @@ -212,7 +212,7 @@ class VersionedRefWithId {
// `args' will be passed to OnCreated() directly.
// Returns 0 on success, -1 otherwise.
template<typename ... Args>
static int Create(VRefId* id, Args... args);
static int Create(VRefId* id, Args&&... args);

// Place the VersionedRefWithId associated with identifier `id' into
// unique_ptr `ptr', which will be released automatically when out
Expand Down Expand Up @@ -246,10 +246,10 @@ class VersionedRefWithId {
// This function is lock-free.
// Returns -1 when the Socket was already SetFailed(), 0 otherwise.
template<typename... Args>
static int SetFailedById(VRefId id, Args... args);
static int SetFailedById(VRefId id, Args&&... args);

template<typename... Args>
int SetFailed(Args... args);
int SetFailed(Args&&... args);

bool Failed() const {
return VersionOfVRef(_versioned_ref.load(butil::memory_order_relaxed))
Expand Down Expand Up @@ -290,7 +290,7 @@ friend void DereferenceVersionedRefWithId<>(T* r);
}

template<typename... Args>
int SetFailedImpl(Args... args);
int SetFailedImpl(Args&&... args);

// Release the reference. If no one is addressing this VersionedRefWithId,
// it will be recycled automatically and T::BeforeRecycled() will be called.
Expand Down Expand Up @@ -351,7 +351,7 @@ void DereferenceVersionedRefWithId(T* r) {

template <typename T>
template<typename ... Args>
int VersionedRefWithId<T>::Create(VRefId* id, Args... args) {
int VersionedRefWithId<T>::Create(VRefId* id, Args&&... args) {
resource_id_t slot;
T* const t = butil::get_resource(&slot, Forbidden());
if (t == NULL) {
Expand Down Expand Up @@ -458,7 +458,7 @@ void VersionedRefWithId<T>::ReAddress(VersionedRefWithIdUniquePtr<T>* ptr) {

template<typename T>
template<typename... Args>
int VersionedRefWithId<T>::SetFailedById(VRefId id, Args... args) {
int VersionedRefWithId<T>::SetFailedById(VRefId id, Args&&... args) {
VersionedRefWithIdUniquePtr<T> ptr;
if (Address(id, &ptr) != 0) {
return -1;
Expand All @@ -468,13 +468,13 @@ int VersionedRefWithId<T>::SetFailedById(VRefId id, Args... args) {

template<typename T>
template<typename... Args>
int VersionedRefWithId<T>::SetFailed(Args... args) {
int VersionedRefWithId<T>::SetFailed(Args&&... args) {
return SetFailedImpl(std::forward<Args>(args)...);
}

template<typename T>
template<typename... Args>
int VersionedRefWithId<T>::SetFailedImpl(Args... args) {
int VersionedRefWithId<T>::SetFailedImpl(Args&&... args) {
const uint32_t id_ver = VersionOfVRefId(_this_id);
uint64_t vref = _versioned_ref.load(butil::memory_order_relaxed);
for (;;) {
Expand Down
2 changes: 1 addition & 1 deletion src/butil/object_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ template <typename T> inline bool local_pool_free_empty() {
// Get an object typed |T|. The object should be cleared before usage.
// NOTE: If there are no arguments, T must be default-constructible.
template <typename T, typename... Args>
inline T* get_object(Args... args) {
inline T* get_object(Args&&... args) {
return ObjectPool<T>::singleton()->get_object(std::forward<Args>(args)...);
}

Expand Down
25 changes: 4 additions & 21 deletions src/butil/object_pool_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class BAIDU_CACHELINE_ALIGNMENT ObjectPool {
}

template<typename... Args>
inline T* get(Args... args) {
inline T* get(Args&&... args) {
BAIDU_OBJECT_POOL_GET((std::forward<Args>(args)...));
}

Expand Down Expand Up @@ -235,28 +235,11 @@ class BAIDU_CACHELINE_ALIGNMENT ObjectPool {
return true;
}

inline T* get_object() {
template <typename... Args>
inline T* get_object(Args&&... args) {
LocalPool* lp = get_or_new_local_pool();
if (BAIDU_LIKELY(lp != NULL)) {
return lp->get();
}
return NULL;
}

template <typename A1>
inline T* get_object(const A1& arg1) {
LocalPool* lp = get_or_new_local_pool();
if (BAIDU_LIKELY(lp != NULL)) {
return lp->get(arg1);
}
return NULL;
}

template <typename A1, typename A2>
inline T* get_object(const A1& arg1, const A2& arg2) {
LocalPool* lp = get_or_new_local_pool();
if (BAIDU_LIKELY(lp != NULL)) {
return lp->get(arg1, arg2);
return lp->get(std::forward<Args>(args)...);
}
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion src/butil/resource_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ namespace butil {
// The object should be cleared before usage.
// NOTE: If there are no arguments, T must be default-constructible.
template <typename T, typename... Args>
inline T* get_resource(ResourceId<T>* id, Args... args) {
inline T* get_resource(ResourceId<T>* id, Args&&... args) {
return ResourcePool<T>::singleton()->get_resource(id, std::forward<Args>(args)...);
}

Expand Down
25 changes: 4 additions & 21 deletions src/butil/resource_pool_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class BAIDU_CACHELINE_ALIGNMENT ResourcePool {
}

template<typename... Args>
inline T* get(ResourceId<T>* id, Args... args) {
inline T* get(ResourceId<T>* id, Args&&... args) {
BAIDU_RESOURCE_POOL_GET((std::forward<Args>(args)...));
}

Expand Down Expand Up @@ -277,28 +277,11 @@ class BAIDU_CACHELINE_ALIGNMENT ResourcePool {
return NULL;
}

inline T* get_resource(ResourceId<T>* id) {
template<typename... Args>
inline T* get_resource(ResourceId<T>* id, Args&&... args) {
LocalPool* lp = get_or_new_local_pool();
if (__builtin_expect(lp != NULL, 1)) {
return lp->get(id);
}
return NULL;
}

template <typename A1>
inline T* get_resource(ResourceId<T>* id, const A1& arg1) {
LocalPool* lp = get_or_new_local_pool();
if (__builtin_expect(lp != NULL, 1)) {
return lp->get(id, arg1);
}
return NULL;
}

template <typename A1, typename A2>
inline T* get_resource(ResourceId<T>* id, const A1& arg1, const A2& arg2) {
LocalPool* lp = get_or_new_local_pool();
if (__builtin_expect(lp != NULL, 1)) {
return lp->get(id, arg1, arg2);
return lp->get(id, std::forward<Args>(args)...);
}
return NULL;
}
Expand Down
Loading