From b5b0cbd599aefa4f8243de1368858023c0a5de5c Mon Sep 17 00:00:00 2001 From: flamingcow Date: Thu, 9 May 2019 23:36:31 -0700 Subject: [PATCH] Add InTransaction(), fix param storage --- request.cc | 4 ++-- request.h | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/request.cc b/request.cc index 5659452..4db52fd 100644 --- a/request.cc +++ b/request.cc @@ -33,14 +33,14 @@ uint16_t Request::RequestId() { } 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) { 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); if (iter == params_.end()) { static const std::string_view none; diff --git a/request.h b/request.h index 8436173..2cd185b 100644 --- a/request.h +++ b/request.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -22,7 +23,7 @@ class Request { void AddParam(const std::string_view& key, const std::string_view& value); 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(); void WriteHeader(const std::string_view& name, const std::string_view& value); @@ -33,6 +34,9 @@ class Request { template void WriteBody(const std::string_view& first, Args... more); + template + T InTransaction(const std::function& callback); + private: Header OutputHeader(); iovec OutputVec(); @@ -55,4 +59,10 @@ void Request::WriteBody(const std::string_view& first, Args... more) { WriteBody(more...); } +template +T Request::InTransaction(const std::function& callback) { + std::lock_guard l(output_mu_); + return callback(); +} + } // namespace firecgi