Add InTransaction(), fix param storage

This commit is contained in:
flamingcow
2019-05-09 23:36:31 -07:00
parent 8bd0813161
commit b5b0cbd599
2 changed files with 13 additions and 3 deletions

View File

@@ -33,14 +33,14 @@ uint16_t Request::RequestId() {
} }
void Request::AddParam(const std::string_view& key, const std::string_view& value) { void Request::AddParam(const std::string_view& key, const std::string_view& value) {
params_.try_emplace(std::string(key), std::string(value)); params_.try_emplace(key, value);
} }
void Request::SetBody(const std::string_view& body) { void Request::SetBody(const std::string_view& body) {
body_ = body; body_ = body;
} }
const std::string_view& Request::GetParam(const std::string& key) { const std::string_view& Request::GetParam(const std::string_view& key) {
auto iter = params_.find(key); auto iter = params_.find(key);
if (iter == params_.end()) { if (iter == params_.end()) {
static const std::string_view none; static const std::string_view none;

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <functional>
#include <mutex> #include <mutex>
#include <unordered_map> #include <unordered_map>
@@ -22,7 +23,7 @@ class Request {
void AddParam(const std::string_view& key, const std::string_view& value); void AddParam(const std::string_view& key, const std::string_view& value);
void SetBody(const std::string_view& in); void SetBody(const std::string_view& in);
const std::string_view& GetParam(const std::string& key); const std::string_view& GetParam(const std::string_view& key);
const std::string_view& GetBody(); const std::string_view& GetBody();
void WriteHeader(const std::string_view& name, const std::string_view& value); void WriteHeader(const std::string_view& name, const std::string_view& value);
@@ -33,6 +34,9 @@ class Request {
template<typename...Args> template<typename...Args>
void WriteBody(const std::string_view& first, Args... more); void WriteBody(const std::string_view& first, Args... more);
template<typename T>
T InTransaction(const std::function<T()>& callback);
private: private:
Header OutputHeader(); Header OutputHeader();
iovec OutputVec(); iovec OutputVec();
@@ -55,4 +59,10 @@ void Request::WriteBody(const std::string_view& first, Args... more) {
WriteBody(more...); WriteBody(more...);
} }
template<typename T>
T Request::InTransaction(const std::function<T()>& callback) {
std::lock_guard<std::recursive_mutex> l(output_mu_);
return callback();
}
} // namespace firecgi } // namespace firecgi