本文整理汇总了C++中folly类的典型用法代码示例。如果您正苦于以下问题:C++ folly类的具体用法?C++ folly怎么用?C++ folly使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了folly类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: parseJson
void WedgeProductInfo::parse(std::string data) {
try {
dynamic info = parseJson(data)[kInfo];
productInfo_.oem = folly::to<std::string>(info[kSysMfg].asString());
productInfo_.product = folly::to<std::string>(info[kProdName].asString());
productInfo_.serial = folly::to<std::string>(info[kSerialNum].asString());
productInfo_.mfgDate = folly::to<std::string>(info[kSysMfgDate].asString());
productInfo_.systemPartNumber =
folly::to<std::string>(info[kSysAmbPartNum].asString());
productInfo_.assembledAt = folly::to<std::string>(info[kAmbAt].asString());
productInfo_.pcbManufacturer =
folly::to<std::string>(info[kPcbMfg].asString());
productInfo_.assetTag =
folly::to<std::string>(info[kProdAssetTag].asString());
productInfo_.partNumber =
folly::to<std::string>(info[kProdPartNum].asString());
productInfo_.odmPcbPartNumber = folly::to<std::string>
(info[kOdmPcbPartNum].asString());
productInfo_.odmPcbSerial = folly::to<std::string>
(info[kOdmPcbSerialNum].asString());
productInfo_.fbPcbPartNumber = folly::to<std::string>
(info[kFbPcbPartNum].asString());
productInfo_.version = info[kVersion].asInt();
productInfo_.subVersion = info[kSubVersion].asInt();
productInfo_.productionState = info[kProductionState].asInt();
productInfo_.productVersion = info[kProdVersion].asInt();
productInfo_.bmcMac = folly::to<std::string>(info[kLocalMac].asString());
productInfo_.mgmtMac = folly::to<std::string>(info[kExtMacBase].asString());
auto macBase = MacAddress(info[kExtMacBase].asString()).u64HBO() + 1;
productInfo_.macRangeStart = MacAddress::fromHBO(macBase).toString();
productInfo_.macRangeSize = info[kExtMacSize].asInt() - 1;
} catch (const std::exception& err) {
LOG(ERROR) << err.what();
}
}
示例2: TEST
TEST(Json, SortKeys) {
folly::json::serialization_opts opts_on, opts_off;
opts_on.sort_keys = true;
opts_off.sort_keys = false;
dynamic value = dynamic::object
("foo", "bar")
("junk", 12)
("another", 32.2)
("a",
{
dynamic::object("a", "b")
("c", "d"),
12.5,
"Yo Dawg",
{ "heh" },
nullptr
}
)
;
std::string sorted_keys =
R"({"a":[{"a":"b","c":"d"},12.5,"Yo Dawg",["heh"],null],)"
R"("another":32.2,"foo":"bar","junk":12})";
EXPECT_EQ(value, parseJson(folly::json::serialize(value, opts_on)));
EXPECT_EQ(value, parseJson(folly::json::serialize(value, opts_off)));
EXPECT_EQ(sorted_keys, folly::json::serialize(value, opts_on));
}
示例3: open
bool MmapFile::init() {
int fd = open(path_.c_str(), O_RDONLY);
if (fd < 0) {
Logger::Error(format("Unable to open {}: {}",
path_, folly::errnoStr(errno)).str());
return false;
}
struct stat fs;
if (fstat(fd, &fs) != 0) {
Logger::Error(format("Unable to fstat {}: {}",
path_, folly::errnoStr(errno)).str());
close(fd);
return false;
}
backing_fd_ = fd;
backing_mem_size_ = fs.st_size;
backing_mem_ = mmap(nullptr, backing_mem_size_, PROT_READ, MAP_PRIVATE,
backing_fd_, 0);
if (backing_mem_ == (void*) -1) {
Logger::Error(format("Unable to mmap {}: {}",
path_, folly::errnoStr(errno)).str());
close(backing_fd_);
return false;
}
backing_mem_end_ = (char*) backing_mem_ + backing_mem_size_;
read_ptr_ = static_cast<char*>(backing_mem_);
initialized_ = true;
return true;
}
示例4: makeUnicastRoute
std::unique_ptr<UnicastRoute>
makeUnicastRoute(std::string prefixStr, std::string nxtHop,
AdminDistance distance=AdminDistance::MAX_ADMIN_DISTANCE) {
std::vector<std::string> vec;
folly::split("/", prefixStr, vec);
EXPECT_EQ(2, vec.size());
auto nr = std::make_unique<UnicastRoute>();
nr->dest.ip = toBinaryAddress(IPAddress(vec.at(0)));
nr->dest.prefixLength = folly::to<uint8_t>(vec.at(1));
nr->nextHopAddrs.push_back(toBinaryAddress(IPAddress(nxtHop)));
nr->adminDistance = distance;
nr->__isset.adminDistance = true;
return nr;
}
示例5: TEST
TEST(GitBlob, testDeserializeManaged) {
string blobHash("3a8f8eb91101860fd8484154885838bf322964d0");
Hash hash(blobHash);
string contents("{\n \"breakConfig\": true\n}\n");
auto buf = IOBuf::create(1024);
folly::io::Appender appender(buf.get(), 0);
appender.printf("blob %zu", contents.size());
appender.write<uint8_t>(0);
appender.push(StringPiece(contents));
// Sanity check that we are the only user of the newly-created IOBuf
EXPECT_FALSE(buf->isShared()) << "newly created IOBuf should not be shared";
auto blob = deserializeGitBlob(hash, buf.get());
EXPECT_EQ(hash, blob->getHash());
EXPECT_TRUE(buf->isShared())
<< "deserializeGitBlob() should return a blob that shares the same "
<< "IOBuf data, instead of copying it";
EXPECT_EQ(contents, StringPiece{blob->getContents().clone()->coalesce()});
// Make sure the blob contents are still valid after freeing
// our copy of the IOBuf
buf.reset();
EXPECT_EQ(contents, StringPiece{blob->getContents().clone()->coalesce()});
}
示例6: process
virtual void process(std::unique_ptr<ResponseChannel::Request> req,
std::unique_ptr<folly::IOBuf> buf,
protocol::PROTOCOL_TYPES protType,
Cpp2RequestContext* context,
async::TEventBase* eb,
concurrency::ThreadManager* tm) {
assert(iface_);
auto reqd = makeMoveWrapper(std::move(req));
auto bufd = makeMoveWrapper(std::move(buf));
tm->add(FunctionRunner::create([=] () mutable {
(*bufd)->coalesce();
uint64_t resp;
char* data;
iface_->process(
(*reqd).release(), eb, (*bufd)->writableData(),
(*bufd)->length(), protType);
}));
}
示例7: ds
TEST(Dynamic, GetDefault) {
const auto s = make_long_string();
dynamic ds(s);
dynamic tmp(s);
dynamic d1 = dynamic::object("key1", s);
dynamic d2 = dynamic::object("key2", s);
dynamic d3 = dynamic::object("key3", s);
dynamic d4 = dynamic::object("key4", s);
// lvalue - lvalue
dynamic ayy("ayy");
EXPECT_EQ(ds, d1.getDefault("key1", ayy));
EXPECT_EQ(ds, d1.getDefault("key1", ayy));
EXPECT_EQ(ds, d1.getDefault("not-a-key", tmp));
EXPECT_EQ(ds, tmp);
// lvalue - rvalue
EXPECT_EQ(ds, d1.getDefault("key1", "ayy"));
EXPECT_EQ(ds, d1.getDefault("key1", "ayy"));
EXPECT_EQ(ds, d1.getDefault("not-a-key", std::move(tmp)));
EXPECT_NE(ds, tmp);
// rvalue - lvalue
tmp = s;
EXPECT_EQ(ds, std::move(d1).getDefault("key1", ayy));
EXPECT_NE(ds, d1["key1"]);
EXPECT_EQ(ds, std::move(d2).getDefault("not-a-key", tmp));
EXPECT_EQ(dynamic(dynamic::object("key2", s)), d2);
EXPECT_EQ(ds, tmp);
// rvalue - rvalue
EXPECT_EQ(ds, std::move(d3).getDefault("key3", std::move(tmp)));
EXPECT_NE(ds, d3["key3"]);
EXPECT_EQ(ds, tmp);
EXPECT_EQ(ds, std::move(d4).getDefault("not-a-key", std::move(tmp)));
EXPECT_EQ(dynamic(dynamic::object("key4", s)), d4);
EXPECT_NE(ds, tmp);
}
示例8: MacAddress
TEST(MacAddress, toString) {
EXPECT_EQ("12:34:56:78:9a:bc",
MacAddress::fromHBO(0x123456789abc).toString());
EXPECT_EQ("12:34:56:78:9a:bc", MacAddress("12:34:56:78:9a:bc").toString());
EXPECT_EQ("01:23:45:67:89:ab", MacAddress("01-23-45-67-89-ab").toString());
EXPECT_EQ("01:23:45:67:89:ab", MacAddress("0123456789ab").toString());
}
示例9: main
int main(int argc, char* argv[]) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
google::InitGoogleLogging(argv[0]);
google::InstallFailureSignalHandler();
std::vector<HTTPServer::IPConfig> IPs = {
{SocketAddress(FLAGS_ip, FLAGS_http_port, true), Protocol::HTTP},
{SocketAddress(FLAGS_ip, FLAGS_spdy_port, true), Protocol::SPDY},
};
if (FLAGS_threads <= 0) {
FLAGS_threads = sysconf(_SC_NPROCESSORS_ONLN);
CHECK(FLAGS_threads > 0);
}
HTTPServerOptions options;
options.threads = static_cast<size_t>(FLAGS_threads);
options.idleTimeout = std::chrono::milliseconds(60000);
options.shutdownOn = {SIGINT, SIGTERM};
options.enableContentCompression = true;
options.handlerFactories = RequestHandlerChain()
.addThen<HangmanHandlerFactory>()
.build();
HTTPServer server(std::move(options));
server.bind(IPs);
// Start HTTPServer mainloop in a separate thread
std::thread t([&] () {
server.start();
});
t.join();
return 0;
}
示例10: createChannel
TEST_F(FuseChannelTest, testDestroyWithPendingRequests) {
auto channel = createChannel();
auto completeFuture = performInit(channel.get());
// Send several lookup requests
auto id1 = fuse_.sendLookup(FUSE_ROOT_ID, "foobar");
auto id2 = fuse_.sendLookup(FUSE_ROOT_ID, "some_file.txt");
auto id3 = fuse_.sendLookup(FUSE_ROOT_ID, "main.c");
auto req1 = dispatcher_.waitForLookup(id1);
auto req2 = dispatcher_.waitForLookup(id2);
auto req3 = dispatcher_.waitForLookup(id3);
// Destroy the channel object
channel.reset();
// The completion future still should not be ready, since the lookup
// requests are still pending.
EXPECT_FALSE(completeFuture.isReady());
auto checkLookupResponse = [](const FakeFuse::Response& response,
uint64_t requestID,
fuse_entry_out expected) {
EXPECT_EQ(requestID, response.header.unique);
EXPECT_EQ(0, response.header.error);
EXPECT_EQ(
sizeof(fuse_out_header) + sizeof(fuse_entry_out), response.header.len);
EXPECT_EQ(
ByteRange(
reinterpret_cast<const uint8_t*>(&expected), sizeof(expected)),
ByteRange(response.body.data(), response.body.size()));
};
// Respond to the lookup requests
auto response1 = genRandomLookupResponse(9);
req1.promise.setValue(response1);
auto received = fuse_.recvResponse();
checkLookupResponse(received, id1, response1);
// We don't have to respond in order; respond to request 3 before 2
auto response3 = genRandomLookupResponse(19);
req3.promise.setValue(response3);
received = fuse_.recvResponse();
checkLookupResponse(received, id3, response3);
// The completion future still shouldn't be ready since there is still 1
// request outstanding.
EXPECT_FALSE(completeFuture.isReady());
auto response2 = genRandomLookupResponse(12);
req2.promise.setValue(response2);
received = fuse_.recvResponse();
checkLookupResponse(received, id2, response2);
// The completion future should be ready now that the last request
// is done.
EXPECT_TRUE(completeFuture.isReady());
std::move(completeFuture).get(kTimeout);
}
示例11: parseJson
BENCHMARK(toJson, iters) {
dynamic something = parseJson(
"{\"old_value\":40,\"changed\":true,\"opened\":false,\"foo\":[1,2,3,4,5,6]}"
);
for (size_t i = 0; i < iters; i++) {
toJson(something);
}
}
示例12: ds
TEST(Dynamic, At) {
const dynamic cd = dynamic::object("key1", make_long_string());
dynamic dd = dynamic::object("key1", make_long_string());
dynamic md = dynamic::object("key1", make_long_string());
dynamic ds(make_long_string());
EXPECT_EQ(ds, cd.at("key1"));
EXPECT_EQ(ds, cd.at("key1"));
dd.at("key1").getString() += " hello";
EXPECT_EQ(dynamic(make_long_string() + " hello"), dd.at("key1"));
EXPECT_EQ(dynamic(make_long_string() + " hello"), dd.at("key1"));
EXPECT_EQ(ds, std::move(md).at("key1"));
EXPECT_NE(ds, md.at("key1"));
}
示例13: decltype
TEST(make_array, deduced_common_type) {
auto arr = make_array(1.0, 2.5f, 3, 4, 5);
static_assert(
is_same<typename decltype(arr)::value_type, double>::value,
"Wrong array type");
EXPECT_EQ(arr.size(), 5);
}
示例14: parseJson
TEST(OpenRTB, DeviceTest) {
dynamic deviceObj = parseJson(readFile("testdata/device.json"));
Device device;
device.deserialize(deviceObj);
std::function<void()> checkFunction = [&](){
EXPECT_EQ(device.ip, deviceObj.at("ip").asString().toStdString());
EXPECT_EQ(device.language.get(), deviceObj.at("language").asString().toStdString());
EXPECT_EQ(device.carrier.get(), deviceObj.at("carrier").asString().toStdString());
EXPECT_EQ(device.model.get(), deviceObj.at("model").asString().toStdString());
EXPECT_EQ(device.os.get(), deviceObj.at("os").asString().toStdString());
EXPECT_EQ(device.osv.get(), deviceObj.at("osv").asString().toStdString());
EXPECT_EQ(device.make.get(), deviceObj.at("make").asString().toStdString());
//geo
EXPECT_EQ(device.geo.get().city, deviceObj.at("geo").at("city").asString().toStdString());
EXPECT_EQ(device.geo.get().country, deviceObj.at("geo").at("country").asString().toStdString());
EXPECT_EQ(device.geo.get().metro, deviceObj.at("geo").at("metro").asString().toStdString());
EXPECT_EQ(device.geo.get().region, deviceObj.at("geo").at("region").asString().toStdString());
EXPECT_EQ(device.geo.get().zip, deviceObj.at("geo").at("zip").asString().toStdString());
};
checkFunction();
//serialize object
deviceObj = device.serialize();
//check again with new serialized device object
checkFunction();
}
示例15: fn
// Using ScopeGuard to invoke a std::function
BENCHMARK(scope_guard_std_function, iters) {
std::function<void()> fn(doNothing);
for (size_t n = 0; n < iters; ++n) {
auto g = makeGuard(fn);
(void)g;
}
}