Commit a00c6e30 authored by Nicholas Ormrod's avatar Nicholas Ormrod Committed by Facebook GitHub Bot

Add ImmutableRequestData

Summary: A simple folly::RequestData implementation.

Reviewed By: praihan

Differential Revision: D31738010

fbshipit-source-id: 3d7c3394fd3daf2dbb2942fe6cdac25207e04e32
parent e5722ec4
......@@ -20,6 +20,7 @@
#include <memory>
#include <mutex>
#include <string>
#include <type_traits>
#include <utility>
#include <folly/SingletonThreadLocal.h>
......@@ -129,6 +130,29 @@ class RequestData {
std::atomic<int> keepAliveCounter_{0};
};
/**
* ImmutableRequestData is a folly::RequestData that holds an immutable value.
* It is thread-safe (a requirement of RequestData) because it is immutable.
*/
template <typename T>
class ImmutableRequestData : public folly::RequestData {
public:
template <
typename... Args,
typename = typename std::enable_if<
std::is_constructible<T, Args...>::value>::type>
explicit ImmutableRequestData(Args&&... args) noexcept(
std::is_nothrow_constructible<T, Args...>::value)
: val_(std::forward<Args>(args)...) {}
const T& value() const { return val_; }
bool hasCallback() override { return false; }
private:
const T val_;
};
using RequestDataItem = std::pair<RequestToken, std::unique_ptr<RequestData>>;
// If you do not call create() to create a unique request context,
......
......@@ -600,3 +600,20 @@ TEST(RequestContextTryGetTest, TryGetTest) {
});
thread.join();
}
TEST(ImmutableRequestTest, simple) {
ImmutableRequestData<int> ird(4);
EXPECT_EQ(ird.value(), 4);
}
TEST(ImmutableRequestTest, type_traits) {
using IRDI = ImmutableRequestData<int>;
auto c1 = std::is_constructible<IRDI, int>::value;
EXPECT_TRUE(c1);
auto n1 = std::is_nothrow_constructible<IRDI, int>::value;
EXPECT_TRUE(n1);
auto c2 = std::is_constructible<IRDI, int, int>::value;
EXPECT_FALSE(c2);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment