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

clang-format #286

Merged
merged 9 commits into from
Nov 27, 2021
97 changes: 93 additions & 4 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,8 +1,97 @@
---
BasedOnStyle: Google
BreakBeforeBraces: Mozilla
IndentWidth: '2'
TabWidth: '2'
BasedOnStyle: Mozilla
AccessModifierOffset: '-4'
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: 'false'
AlignEscapedNewlines: Left
AlignOperands: 'true' #AlignAfterOperator
AlignTrailingComments: 'true'
AllowShortBlocksOnASingleLine: 'true'
AllowShortCaseLabelsOnASingleLine: 'true'
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: 'true'
AllowShortLambdasOnASingleLine: Empty
AllowShortLoopsOnASingleLine: 'false'
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: 'false'
AlwaysBreakTemplateDeclarations: 'Yes'
BinPackArguments: 'true'
BinPackParameters: 'true'
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: 'true'
AfterClass: 'true'
AfterControlStatement: Always
AfterEnum: 'true'
AfterFunction: 'true'
AfterNamespace: 'true'
AfterObjCDeclaration: 'true'
AfterStruct: 'true'
AfterUnion: 'true'
AfterExternBlock: 'true'
BeforeCatch: 'true'
BeforeElse: 'true'
# BeforeLambdaBody: 'false'
# BeforeWhile: 'false'
IndentBraces: 'false'
SplitEmptyFunction: 'false'
SplitEmptyRecord: 'false'
SplitEmptyNamespace: 'false'
BreakBeforeTernaryOperators: 'false'
BreakBeforeBinaryOperators: 'false'
BreakConstructorInitializers: AfterColon
BreakInheritanceList: AfterColon
ColumnLimit: '0'
CompactNamespaces: 'false'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'false'
ContinuationIndentWidth: '2'
Cpp11BracedListStyle: 'true'
FixNamespaceComments: 'true'
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 2
CaseSensitive: true
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
- Regex: '<[[:alnum:].]+>'
Priority: 4
- Regex: '.*'
Priority: 1
SortPriority: 0
IndentCaseLabels: 'true'
IndentWidth: '4'
IndentWrappedFunctionNames: 'true'
Language: Cpp
MaxEmptyLinesToKeep: '3'
NamespaceIndentation: All
PointerAlignment: Left
ReflowComments: 'false'
SortIncludes: 'false'
SortUsingDeclarations: 'false'
SpaceAfterCStyleCast: 'false'
SpaceAfterLogicalNot: 'false'
SpaceAfterTemplateKeyword: 'false'
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeCpp11BracedList: 'false'
SpaceBeforeCtorInitializerColon: 'false'
SpaceBeforeInheritanceColon: 'true'
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: 'true'
SpaceInEmptyParentheses: 'false'
SpacesBeforeTrailingComments: '1'
SpacesInAngles: 'false'
SpacesInCStyleCastParentheses: 'false'
SpacesInContainerLiterals: 'false'
SpacesInParentheses: 'false'
SpacesInSquareBrackets: 'false'
#SpacesInLineCommentPrefix:
# Minimum: 1
# Maximum: -1
Standard: Cpp11
TabWidth: '4'
UseTab: Never

...
13 changes: 12 additions & 1 deletion .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ steps:
environment:
COVERALLS_REPO_TOKEN:
from_secret: coveralls_token
APP_ID:
from_secret: appid
APP_INSTALLATION:
from_secret: appins
PK:
from_secret: pk

commands:
- export DEBIAN_FRONTEND=noninteractive
- export TRAVIS_BRANCH=$DRONE_BRANCH
- export TRAVIS_JOB_ID=$DRONE_BUILD_NUMBER
- export COVERALLS_PULL_REQUEST=$DRONE_PULL_REQUEST
- apt-get -y update
- apt-get -y install libboost-all-dev doxygen mkdocs graphviz zlib1g-dev gcc clang make cmake python3 python3-pip git openssl libssl-dev
- apt-get -y install libboost-all-dev doxygen mkdocs graphviz zlib1g-dev gcc clang clang-format make cmake python3 python3-pip git openssl libssl-dev jq wget curl
- git clone https://github.com/CrowCpp/cpp-coveralls.git
- cd cpp-coveralls
- pip3 install . --no-input
Expand All @@ -33,6 +39,11 @@ steps:
- ctest -V -j4
- cd ..
- coveralls --verbose --exclude-pattern .*/http_parser_merged.h --exclude-pattern .*/TinySHA1.hpp
- git clone https://github.com/CrowCpp/crow-clang-format.git
- cd crow-clang-format
- chmod +x crow-clang-format.sh get-access-token.sh make-jwt.sh
- ./crow-clang-format.sh
- cd ..
- export CC=/usr/bin/clang
- export CXX=/usr/bin/clang++
- mkdir build-clang
Expand Down
121 changes: 62 additions & 59 deletions examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,31 @@

#include <sstream>

class ExampleLogHandler : public crow::ILogHandler {
public:
void log(std::string /*message*/, crow::LogLevel /*level*/) override {
// cerr << "ExampleLogHandler -> " << message;
}
class ExampleLogHandler : public crow::ILogHandler
{
public:
void log(std::string /*message*/, crow::LogLevel /*level*/) override
{
// cerr << "ExampleLogHandler -> " << message;
}
};

struct ExampleMiddleware
{
std::string message;

ExampleMiddleware() : message("foo")
ExampleMiddleware():
message("foo")
{
}

void setMessage(const std::string &newMsg)
void setMessage(const std::string& newMsg)
{
message = newMsg;
}

struct context
{
};
{};

void before_handle(crow::request& /*req*/, crow::response& /*res*/, context& /*ctx*/)
{
Expand All @@ -44,58 +46,57 @@ int main()
app.get_middleware<ExampleMiddleware>().setMessage("hello");

CROW_ROUTE(app, "/")
.name("hello")
([]{
return "Hello World!";
});
.name("hello")([] {
return "Hello World!";
});

CROW_ROUTE(app, "/about")
([](){
([]() {
return "About Crow example.";
});

// a request to /path should be forwarded to /path/
CROW_ROUTE(app, "/path/")
([](){
([]() {
return "Trailing slash test case..";
});


// simple json response
CROW_ROUTE(app, "/json")
([]{
([] {
crow::json::wvalue x({{"message", "Hello, World!"}});
x["message2"] = "Hello, World.. Again!";
return x;
});

CROW_ROUTE(app, "/json-initializer-list-constructor")
([] {
return crow::json::wvalue({
{"first", "Hello world!"}, /* stores a char const* hence a json::type::String */
{"second", std::string("How are you today?")}, /* stores a std::string hence a json::type::String. */
{"third", std::int64_t(54)}, /* stores a 64-bit int hence a std::int64_t. */
{"fourth", std::uint64_t(54)}, /* stores a 64-bit unsigned int hence a std::uint64_t. */
{"fifth", 54}, /* stores an int (as 54 is an int literal) hence a std::int64_t. */
{"sixth", 54u}, /* stores an unsigned int (as 54u is a unsigned int literal) hence a std::uint64_t. */
{"seventh", 2.f}, /* stores a float (as 2.f is a float literal) hence a double. */
{"eighth", 2.}, /* stores a double (as 2. is a double literal) hence a double. */
{"ninth", nullptr}, /* stores a std::nullptr hence json::type::Null . */
{"tenth", true} /* stores a bool hence json::type::True . */
});
return crow::json::wvalue({
{"first", "Hello world!"}, /* stores a char const* hence a json::type::String */
{"second", std::string("How are you today?")}, /* stores a std::string hence a json::type::String. */
{"third", std::int64_t(54)}, /* stores a 64-bit int hence a std::int64_t. */
{"fourth", std::uint64_t(54)}, /* stores a 64-bit unsigned int hence a std::uint64_t. */
{"fifth", 54}, /* stores an int (as 54 is an int literal) hence a std::int64_t. */
{"sixth", 54u}, /* stores an unsigned int (as 54u is a unsigned int literal) hence a std::uint64_t. */
{"seventh", 2.f}, /* stores a float (as 2.f is a float literal) hence a double. */
{"eighth", 2.}, /* stores a double (as 2. is a double literal) hence a double. */
{"ninth", nullptr}, /* stores a std::nullptr hence json::type::Null . */
{"tenth", true} /* stores a bool hence json::type::True . */
});
});

// json list response
CROW_ROUTE(app, "/json_list")
([]{
crow::json::wvalue x(crow::json::wvalue::list({1,2,3}));
([] {
crow::json::wvalue x(crow::json::wvalue::list({1, 2, 3}));
return x;
});

// To see it in action enter {ip}:18080/hello/{integer_between -2^32 and 100} and you should receive
// {integer_between -2^31 and 100} bottles of beer!
CROW_ROUTE(app,"/hello/<int>")
([](int count){
CROW_ROUTE(app, "/hello/<int>")
([](int count) {
if (count > 100)
return crow::response(400);
std::ostringstream os;
Expand All @@ -104,8 +105,8 @@ int main()
});

// Same as above, but using crow::status
CROW_ROUTE(app,"/hello_status/<int>")
([](int count){
CROW_ROUTE(app, "/hello_status/<int>")
([](int count) {
if (count > 100)
return crow::response(crow::status::BAD_REQUEST);
std::ostringstream os;
Expand All @@ -114,18 +115,18 @@ int main()
});

// To see it in action submit {ip}:18080/add/1/2 and you should receive 3 (exciting, isn't it)
CROW_ROUTE(app,"/add/<int>/<int>")
([](crow::response& res, int a, int b){
CROW_ROUTE(app, "/add/<int>/<int>")
([](crow::response& res, int a, int b) {
std::ostringstream os;
os << a+b;
os << a + b;
res.write(os.str());
res.end();
});

// Compile error with message "Handler type is mismatched with URL paramters"
//CROW_ROUTE(app,"/another/<int>")
//([](int a, int b){
//return crow::response(500);
//return crow::response(500);
//});

// more json example
Expand All @@ -140,22 +141,21 @@ int main()
// A simpler way for json example:
// * curl -d '{"a":1,"b":2}' {ip}:18080/add_json
CROW_ROUTE(app, "/add_json")
.methods("POST"_method)
([](const crow::request& req){
auto x = crow::json::load(req.body);
if (!x)
return crow::response(400);
int sum = x["a"].i()+x["b"].i();
std::ostringstream os;
os << sum;
return crow::response{os.str()};
});
.methods("POST"_method)([](const crow::request& req) {
auto x = crow::json::load(req.body);
if (!x)
return crow::response(400);
int sum = x["a"].i() + x["b"].i();
std::ostringstream os;
os << sum;
return crow::response{os.str()};
});

// Example of a request taking URL parameters
// If you want to activate all the functions just query
// {ip}:18080/params?foo='blabla'&pew=32&count[]=a&count[]=b
CROW_ROUTE(app, "/params")
([](const crow::request& req){
([](const crow::request& req) {
std::ostringstream os;

// To get a simple string from the url params
Expand All @@ -165,38 +165,41 @@ int main()

// To get a double from the request
// To see in action submit something like '/params?pew=42'
if(req.url_params.get("pew") != nullptr) {
if (req.url_params.get("pew") != nullptr)
{
double countD = boost::lexical_cast<double>(req.url_params.get("pew"));
os << "The value of 'pew' is " << countD << '\n';
os << "The value of 'pew' is " << countD << '\n';
}

// To get a list from the request
// You have to submit something like '/params?count[]=a&count[]=b' to have a list with two values (a and b)
auto count = req.url_params.get_list("count");
os << "The key 'count' contains " << count.size() << " value(s).\n";
for(const auto& countVal : count) {
for (const auto& countVal : count)
{
os << " - " << countVal << '\n';
}

// To get a dictionary from the request
// You have to submit something like '/params?mydict[a]=b&mydict[abcd]=42' to have a list of pairs ((a, b) and (abcd, 42))
auto mydict = req.url_params.get_dict("mydict");
os << "The key 'dict' contains " << mydict.size() << " value(s).\n";
for(const auto& mydictVal : mydict) {
for (const auto& mydictVal : mydict)
{
os << " - " << mydictVal.first << " -> " << mydictVal.second << '\n';
}

return crow::response{os.str()};
});

CROW_ROUTE(app, "/large")
([]{
return std::string(512*1024, ' ');
([] {
return std::string(512 * 1024, ' ');
});

// Take a multipart/form-data request and print out its body
CROW_ROUTE(app,"/multipart")
([](const crow::request& req){
CROW_ROUTE(app, "/multipart")
([](const crow::request& req) {
crow::multipart::message msg(req);
CROW_LOG_INFO << "body of the first part " << msg.parts[0].body;
return "it works!";
Expand All @@ -207,6 +210,6 @@ int main()
//crow::logger::setHandler(std::make_shared<ExampleLogHandler>());

app.port(18080)
.multithreaded()
.run();
.multithreaded()
.run();
}
Loading