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

Update .clang-format rules and add 'make format' command #115 #116

Merged
merged 1 commit into from
Oct 2, 2020
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
71 changes: 66 additions & 5 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,6 +1,67 @@
---
# https://clang.llvm.org/docs/ClangFormatStyleOptions.html
# DO NOT MOVE THE LINE BELOW OTHERWISE THESE OPTIONS APPLY TO NON C++ FILES
Language: Cpp
BasedOnStyle: WebKit
---
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveMacros: 'true'
AlignConsecutiveAssignments: 'true'
AlignConsecutiveDeclarations: 'true'
AlignEscapedNewlines: Right
AlignOperands: 'true'
AlignTrailingComments: 'true'
AllowAllArgumentsOnNextLine: 'true'
AllowAllConstructorInitializersOnNextLine: 'false'
AllowAllParametersOfDeclarationOnNextLine: 'true'
AllowShortBlocksOnASingleLine: 'true'
AllowShortCaseLabelsOnASingleLine: 'false'
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: 'false'
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: 'true'
AlwaysBreakTemplateDeclarations: 'Yes'
BinPackArguments: 'false'
BinPackParameters: 'false'
BreakAfterJavaFieldAnnotations: 'true'
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Allman
BreakBeforeTernaryOperators: 'true'
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
BreakStringLiterals: 'false'
ColumnLimit: '120'
CompactNamespaces: 'false'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'true'
ConstructorInitializerIndentWidth: '4'
ContinuationIndentWidth: '4'
Cpp11BracedListStyle: 'true'
FixNamespaceComments: 'true'
IncludeBlocks: Preserve
IndentCaseLabels: 'true'
IndentPPDirectives: BeforeHash
IndentWidth: '4'
IndentWrappedFunctionNames: 'true'
KeepEmptyLinesAtTheStartOfBlocks: 'false'
Language: Cpp
MaxEmptyLinesToKeep: '1'
NamespaceIndentation: None
PointerAlignment: Left
ReflowComments: 'true'
SortIncludes: 'true'
SortUsingDeclarations: 'true'
SpaceAfterCStyleCast: 'false'
SpaceAfterLogicalNot: 'false'
SpaceAfterTemplateKeyword: 'false'
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeCpp11BracedList: 'false'
SpaceBeforeCtorInitializerColon: 'true'
SpaceBeforeInheritanceColon: 'true'
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: 'true'
SpaceInEmptyParentheses: 'false'
SpacesInAngles: 'false'
SpacesInCStyleCastParentheses: 'false'
SpacesInContainerLiterals: 'false'
SpacesInParentheses: 'false'
SpacesInSquareBrackets: 'false'
Standard: Cpp11
UseTab: Never
...
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
format:
# Inlcude *.hpp|*.h|*.cpp but ignore catch lib as well as RelWithDebInfo|Release|Debug|build
find . \( -name '*.hpp' -or -name '*.h' -or -name '*.cpp' \) \
-and -not -name '*catch*' \
-and -not -iwholename '*/RelWithDebInfo/*' \
-and -not -iwholename '*/Release/*' \
-and -not -iwholename '*/Debug/*' \
-and -not -iwholename '*/build/*' \
-exec clang-format -i --style=file {} \;
29 changes: 13 additions & 16 deletions examples/async_simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,32 @@

static auto on_complete(std::unique_ptr<lift::Request> request_ptr, lift::Response response) -> void
{
if (response.LiftStatus() == lift::LiftStatus::SUCCESS) {
std::cout
<< "Completed " << request_ptr->Url()
<< " ms:" << response.TotalTime().count() << std::endl;
} else {
std::cout
<< "Error: " << request_ptr->Url() << " : "
<< lift::to_string(response.LiftStatus()) << std::endl;
if (response.LiftStatus() == lift::LiftStatus::SUCCESS)
{
std::cout << "Completed " << request_ptr->Url() << " ms:" << response.TotalTime().count() << std::endl;
}
else
{
std::cout << "Error: " << request_ptr->Url() << " : " << lift::to_string(response.LiftStatus()) << std::endl;
}
}

int main()
{
using namespace std::chrono_literals;

std::vector<std::string> urls = {
"http://www.example.com",
"http://www.google.com",
"http://www.reddit.com"
};
std::vector<std::string> urls = {"http://www.example.com", "http://www.google.com", "http://www.reddit.com"};

lift::EventLoop event_loop {};
lift::EventLoop event_loop{};

/**
* Create asynchronous requests for each url and inject them into
* the event loop with 50ms pause between injection
* and an additional 250ms timeout per each request.
*/
std::chrono::milliseconds timeout = 250ms;
for (auto& url : urls) {
for (auto& url : urls)
{
std::cout << "Requesting " << url << std::endl;
auto request_ptr = lift::Request::make_unique(url, timeout, on_complete);
event_loop.StartRequest(std::move(request_ptr));
Expand All @@ -46,7 +42,8 @@ int main()
}

// Now wait for all the requests to finish before cleaning up.
while (event_loop.ActiveRequestCount() > 0) {
while (event_loop.ActiveRequestCount() > 0)
{
std::this_thread::sleep_for(100ms);
}

Expand Down
46 changes: 24 additions & 22 deletions examples/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,63 @@
#include <thread>
#include <vector>

static auto print_usage(
const std::string& program_name) -> void
static auto print_usage(const std::string& program_name) -> void
{
std::cout << program_name << " <url> <duration_seconds> <connections> <threads>" << std::endl;
}

static auto print_stats(
std::chrono::seconds duration,
uint64_t threads,
uint64_t total_success,
uint64_t total_error) -> void
static auto print_stats(std::chrono::seconds duration, uint64_t threads, uint64_t total_success, uint64_t total_error)
-> void
{
auto total = total_success + total_error;
std::cout << "Thread Stats Avg\n";
std::cout << " Req/sec " << (total / static_cast<double>(threads) / duration.count()) << "\n";

std::cout << "Global Stats\n";
std::cout << " " << total << " requests in " << duration.count() << "s\n";
if (total_error > 0) {
if (total_error > 0)
{
std::cout << " " << total_error << " errors\n";
}
std::cout << " Req/sec: " << (total / static_cast<double>(duration.count())) << "\n";
}

int main(int argc, char* argv[])
{
if (argc < 5) {
if (argc < 5)
{
print_usage(argv[0]);
return 0;
}

using namespace std::chrono_literals;

std::string url(argv[1]);
auto duration = std::chrono::seconds { std::stoul(argv[2]) };
uint64_t connections = std::stoul(argv[3]);
uint64_t threads = std::stoul(argv[4]);
auto duration = std::chrono::seconds{std::stoul(argv[2])};
uint64_t connections = std::stoul(argv[3]);
uint64_t threads = std::stoul(argv[4]);

std::atomic<uint64_t> success { 0 };
std::atomic<uint64_t> error { 0 };
std::atomic<uint64_t> success{0};
std::atomic<uint64_t> error{0};

{
std::vector<std::unique_ptr<lift::EventLoop>> loops;
for (uint64_t i = 0; i < threads; ++i) {
for (uint64_t i = 0; i < threads; ++i)
{
auto event_loop_ptr = std::make_unique<lift::EventLoop>();

for (uint64_t j = 0; j < connections; ++j) {
for (uint64_t j = 0; j < connections; ++j)
{
auto& event_loop = *event_loop_ptr;

auto request_ptr = lift::Request::make_unique(
url,
1s,
[&event_loop, &success, &error](lift::RequestPtr req_ptr, lift::Response response) {
if (response.LiftStatus() == lift::LiftStatus::SUCCESS) {
url, 1s, [&event_loop, &success, &error](lift::RequestPtr req_ptr, lift::Response response) {
if (response.LiftStatus() == lift::LiftStatus::SUCCESS)
{
++success;
} else {
}
else
{
++error;
}

Expand All @@ -80,7 +81,8 @@ int main(int argc, char* argv[])

std::this_thread::sleep_for(duration);

for (auto& thread : loops) {
for (auto& thread : loops)
{
thread->Stop();
}
}
Expand Down
19 changes: 11 additions & 8 deletions examples/readme.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <lift/Lift.hpp>
#include <iostream>
#include <lift/Lift.hpp>

int main()
{
Expand All @@ -8,18 +8,19 @@ int main()
*/

// Synchronous requests can be created on the stack.
lift::Request request { "http://www.example.com" };
lift::Request request{"http://www.example.com"};
// This is the blocking synchronous HTTP call.
auto response = request.Perform();
std::cout << "LiftStatus: " << lift::to_string(response.LiftStatus()) << "\n";
std::cout << "HTTP Status Code: " << lift::http::to_string(response.StatusCode()) << "\n";
for (const auto& header : response.Headers()) {
for (const auto& header : response.Headers())
{
std::cout << header.Name() << ": " << header.Value() << "\n";
}
std::cout << response.Data();

// Creating the event loop starts it immediately, it spawns a background thread for executing requests.
lift::EventLoop loop {};
lift::EventLoop loop{};

// Create the request just like we did in the sync version, but now provide a lambda for on completion.
// NOTE: that the Lambda is executed ON the Lift event loop background thread. If you want to handle
Expand All @@ -29,11 +30,12 @@ int main()
// to the lift::EventLoop! lift::Request::make_unique() is a handy function to easily do so.
auto request_ptr = lift::Request::make_unique(
"http://www.example.com",
std::chrono::seconds { 10 }, // Give the request 10 seconds to complete or timeout.
std::chrono::seconds{10}, // Give the request 10 seconds to complete or timeout.
[](lift::RequestPtr req_ptr, lift::Response response) {
std::cout << "LiftStatus: " << lift::to_string(response.LiftStatus()) << "\n";
std::cout << "HTTP Status Code: " << lift::http::to_string(response.StatusCode()) << "\n";
for (const auto& header : response.Headers()) {
for (const auto& header : response.Headers())
{
std::cout << header.Name() << ": " << header.Value() << "\n";
}
std::cout << response.Data();
Expand All @@ -44,8 +46,9 @@ int main()
loop.StartRequest(std::move(request_ptr));

// Block on this main thread until the lift event loop background thread has completed the request, or timed out.
while (loop.ActiveRequestCount() > 0) {
std::this_thread::sleep_for(std::chrono::milliseconds { 10 });
while (loop.ActiveRequestCount() > 0)
{
std::this_thread::sleep_for(std::chrono::milliseconds{10});
}

return 0;
Expand Down
7 changes: 4 additions & 3 deletions examples/synch_simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
int main()
{
{
lift::Request request { "http://www.example.com" };
lift::Request request{"http://www.example.com"};
std::cout << "Requesting http://www.example.com" << std::endl;
const auto& response = request.Perform();
std::cout << response.Data() << std::endl;
}

{
lift::Request request { "http://www.google.com" };
lift::Request request{"http://www.google.com"};
std::cout << "Requesting http://www.google.com" << std::endl;
const auto& response = request.Perform();
std::cout << response.Data() << std::endl;

for (const auto& header : response.Headers()) {
for (const auto& header : response.Headers())
{
std::cout << header.Name() << ": " << header.Value() << "\n";
}
}
Expand Down
6 changes: 3 additions & 3 deletions inc/lift/Const.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

#include <cstdint>

namespace lift {

namespace lift
{
static constexpr uint64_t HEADER_DEFAULT_MEMORY_BYTES = 4096;
static constexpr uint64_t HEADER_DEFAULT_COUNT = 16;
static constexpr uint64_t HEADER_DEFAULT_COUNT = 16;

} // namespace lift
15 changes: 6 additions & 9 deletions inc/lift/Escape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,24 @@

#include <string_view>

namespace lift {

namespace lift
{
/**
* @param data Data to HTTP escape.
* @return An HTTP escaped representation of data.
*/
auto escape(
std::string_view data) -> std::string;
auto escape(std::string_view data) -> std::string;

/**
* @param escaped_data Data to HTTP unescape recursively until it has nothing left to unescape.
* @return An HTTP unescaped representation of the data.
*/
auto unescape_recurse(
std::string escaped_data) -> std::string;
auto unescape_recurse(std::string escaped_data) -> std::string;

/**
* @param escaped_data Data to HTTP unescape.
* @return An HTTP unescaped representation of the data.
*/
auto unescape(
std::string_view escaped_data) -> std::string;
auto unescape(std::string_view escaped_data) -> std::string;

} // lift
} // namespace lift
Loading