diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2020-01-13 08:39:02 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2020-01-13 08:42:08 +0300 |
commit | 6913c6d965d64481ed09fc06a43154f35e8bd914 (patch) | |
tree | fa8d3f360a651a31fba4b91a09a5bba505a904c4 | |
parent | Travis: only run clang-format on master (diff) | |
download | math-server-6913c6d965d64481ed09fc06a43154f35e8bd914.tar.gz math-server-6913c6d965d64481ed09fc06a43154f35e8bd914.zip |
clang-format all the code
Diffstat (limited to '')
30 files changed, 212 insertions, 305 deletions
diff --git a/client/client.hpp b/client/client.hpp index 2f917c1..3d68157 100644 --- a/client/client.hpp +++ b/client/client.hpp @@ -18,19 +18,15 @@ namespace math::client { class Client { public: explicit Client(const Settings& settings) - : Client{make_input_reader(settings), make_transport(settings)} - { } + : Client{make_input_reader(settings), make_transport(settings)} {} Client(input::ReaderPtr&& input_reader, TransportPtr&& transport) - : m_input_reader{std::move(input_reader)} - , m_transport{std::move(transport)} - { } + : m_input_reader{std::move(input_reader)}, m_transport{std::move(transport)} {} void run() { - m_input_reader->for_each_input([this] (const std::string& input) { - m_transport->send_query(input, [] (const std::string& reply) { - std::cout << reply << '\n'; - }); + m_input_reader->for_each_input([this](const std::string& input) { + m_transport->send_query(input, + [](const std::string& reply) { std::cout << reply << '\n'; }); return true; }); } @@ -54,4 +50,4 @@ private: TransportPtr m_transport; }; -} +} // namespace math::client diff --git a/client/error.hpp b/client/error.hpp index 2ae0075..b8a7b35 100644 --- a/client/error.hpp +++ b/client/error.hpp @@ -12,9 +12,7 @@ namespace math::client { class Error : public std::runtime_error { public: - explicit Error(const std::string& what) - : std::runtime_error{"client error: " + what} - { } + explicit Error(const std::string& what) : std::runtime_error{"client error: " + what} {} }; -} +} // namespace math::client diff --git a/client/input.hpp b/client/input.hpp index 9c22b03..0295f3b 100644 --- a/client/input.hpp +++ b/client/input.hpp @@ -19,14 +19,12 @@ namespace math::client::input { class Error : public client::Error { public: - explicit Error(const std::string& what) - : client::Error{"input error: " + what} { - } + explicit Error(const std::string& what) : client::Error{"input error: " + what} {} }; class Reader { public: - using InputHandler = std::function<bool (const std::string&)>; + using InputHandler = std::function<bool(const std::string&)>; virtual ~Reader() = default; @@ -37,13 +35,9 @@ using ReaderPtr = std::unique_ptr<input::Reader>; class FileReader : public Reader { public: - explicit FileReader(const std::string& path) - : m_path{path} - { } + explicit FileReader(const std::string& path) : m_path{path} {} - bool for_each_input(const InputHandler& process) const override { - return enum_lines(process); - } + bool for_each_input(const InputHandler& process) const override { return enum_lines(process); } private: bool enum_lines(const InputHandler& process) const { @@ -73,9 +67,7 @@ private: class MultiFileReader : public Reader { public: - explicit MultiFileReader(const std::vector<std::string>& paths) - : m_paths{paths} - { } + explicit MultiFileReader(const std::vector<std::string>& paths) : m_paths{paths} {} bool for_each_input(const InputHandler& process) const override { for (const auto& path : m_paths) { @@ -101,13 +93,9 @@ inline input::ReaderPtr make_file_reader(const std::vector<std::string>& paths) class StringReader : public Reader { public: - explicit StringReader(const std::string& input) - : m_input{input} - { } + explicit StringReader(const std::string& input) : m_input{input} {} - bool for_each_input(const InputHandler& process) const override { - return process(m_input); - } + bool for_each_input(const InputHandler& process) const override { return process(m_input); } private: const std::string m_input; @@ -141,4 +129,4 @@ inline input::ReaderPtr make_console_reader() { return std::make_unique<input::ConsoleReader>(); } -} +} // namespace math::client::input diff --git a/client/settings.hpp b/client/settings.hpp index 2e72dba..8c26a56 100644 --- a/client/settings.hpp +++ b/client/settings.hpp @@ -25,42 +25,30 @@ struct Settings { bool exit_with_usage() const { return m_vm.count("help"); } - bool input_from_string() const { - return m_vm.count("command"); - } + bool input_from_string() const { return m_vm.count("command"); } - bool input_from_files() const { - return !input_from_string() && !m_files.empty(); - } + bool input_from_files() const { return !input_from_string() && !m_files.empty(); } - bool input_from_console() const { - return !input_from_string() && !input_from_files(); - } + bool input_from_console() const { return !input_from_string() && !input_from_files(); } boost::program_options::variables_map m_vm; }; class SettingsParser { public: - explicit SettingsParser(const std::string& argv0) - : m_prog_name{extract_filename(argv0)} - { + explicit SettingsParser(const std::string& argv0) : m_prog_name{extract_filename(argv0)} { m_visible.add_options()("help,h", "show this message and exit"); + m_visible.add_options()("command,c", boost::program_options::value(&m_settings.m_input), + "evaluate the argument expression and exit"); m_visible.add_options()( - "command,c", - boost::program_options::value(&m_settings.m_input), - "evaluate the argument expression and exit"); - m_visible.add_options()( - "host,H", - boost::program_options::value(&m_settings.m_host)->default_value("localhost"), + "host,H", boost::program_options::value(&m_settings.m_host)->default_value("localhost"), "server host address"); - m_visible.add_options()( - "port,p", - boost::program_options::value(&m_settings.m_port)->default_value(NetworkTransport::DEFAULT_PORT), - "server port number"); + m_visible.add_options()("port,p", + boost::program_options::value(&m_settings.m_port) + ->default_value(NetworkTransport::DEFAULT_PORT), + "server port number"); m_hidden.add_options()( - "files", - boost::program_options::value<std::vector<std::string>>(&m_settings.m_files), + "files", boost::program_options::value<std::vector<std::string>>(&m_settings.m_files), "shouldn't be visible"); m_positional.add("files", -1); } @@ -72,12 +60,11 @@ public: Settings parse(int argc, char* argv[]) { boost::program_options::options_description all; all.add(m_hidden).add(m_visible); - boost::program_options::store( - boost::program_options::command_line_parser{argc, argv} - .options(all) - .positional(m_positional) - .run(), - m_settings.m_vm); + boost::program_options::store(boost::program_options::command_line_parser{argc, argv} + .options(all) + .positional(m_positional) + .run(), + m_settings.m_vm); if (m_settings.exit_with_usage()) { return m_settings; } @@ -85,9 +72,7 @@ public: return m_settings; } - void usage() const { - std::cout << *this; - } + void usage() const { std::cout << *this; } void usage_error(const std::exception& e) const { std::cerr << "usage error: " << e.what() << '\n'; @@ -114,4 +99,4 @@ private: } }; -} +} // namespace math::client diff --git a/client/transport.hpp b/client/transport.hpp index 04e6c4b..dbb142f 100644 --- a/client/transport.hpp +++ b/client/transport.hpp @@ -19,18 +19,16 @@ namespace transport { class Error : public client::Error { public: - explicit Error(const std::string& msg) - : client::Error{"transport error: " + msg} - { } + explicit Error(const std::string& msg) : client::Error{"transport error: " + msg} {} }; -} +} // namespace transport class Transport { public: virtual ~Transport() = default; - using ProcessResult = std::function<void (const std::string&)>; + using ProcessResult = std::function<void(const std::string&)>; virtual void send_query(const std::string&, const ProcessResult&) = 0; }; @@ -42,8 +40,7 @@ public: static constexpr auto DEFAULT_PORT = "18000"; NetworkTransport(const std::string& host, const std::string& port) - : m_host{host}, m_port{port} - { } + : m_host{host}, m_port{port} {} protected: const std::string m_host; @@ -52,7 +49,7 @@ protected: class BlockingNetworkTransport : public NetworkTransport { public: - BlockingNetworkTransport(const std::string &host, const std::string& port) + BlockingNetworkTransport(const std::string& host, const std::string& port) : NetworkTransport{host, port}, m_socket{m_io_context} { try { connect(); @@ -100,10 +97,9 @@ private: boost::asio::streambuf m_buffer; }; -inline TransportPtr make_blocking_network_transport( - const std::string& host, const std::string& port) { - +inline TransportPtr make_blocking_network_transport(const std::string& host, + const std::string& port) { return std::make_unique<BlockingNetworkTransport>(host, port); } -} +} // namespace math::client diff --git a/server/common/error.hpp b/server/common/error.hpp index bdcbd43..8a37175 100644 --- a/server/common/error.hpp +++ b/server/common/error.hpp @@ -12,9 +12,7 @@ namespace math::server { class Error : public std::runtime_error { public: - explicit Error(const std::string& what) - : std::runtime_error{"server error: " + what} - { } + explicit Error(const std::string& what) : std::runtime_error{"server error: " + what} {} }; -} +} // namespace math::server diff --git a/server/common/log.hpp b/server/common/log.hpp index 5b61c58..cfb01a5 100644 --- a/server/common/log.hpp +++ b/server/common/log.hpp @@ -10,7 +10,6 @@ #include <boost/system/error_code.hpp> #include <ctime> - #include <iomanip> #include <iostream> #include <sstream> @@ -23,7 +22,9 @@ namespace math::server::log { namespace details { -inline std::thread::id get_tid() { return std::this_thread::get_id(); } +inline std::thread::id get_tid() { + return std::this_thread::get_id(); +} inline std::string get_timestamp() { const auto now = boost::posix_time::second_clock::universal_time(); @@ -37,7 +38,7 @@ inline void log(const std::string& msg) { std::clog << get_timestamp() << " | " << get_tid() << " | " << msg << '\n'; } -} +} // namespace details template <typename... Args> inline void log(const std::string_view& fmt, Args&&... args) { @@ -53,4 +54,4 @@ inline void error(const boost::system::error_code& ec) { details::log(ec.message()); } -} +} // namespace math::server::log diff --git a/server/lexer/details/parse.cpp b/server/lexer/details/parse.cpp index 15a4428..ccabb7e 100644 --- a/server/lexer/details/parse.cpp +++ b/server/lexer/details/parse.cpp @@ -9,7 +9,6 @@ #include <boost/regex.hpp> #include <cstddef> - #include <exception> #include <optional> #include <regex> @@ -20,7 +19,7 @@ namespace math::server::lexer::details { namespace { // This approach gives GCC on Travis an "internal compiler error": -//template <template <typename> class MatchResultsT> +// template <template <typename> class MatchResultsT> template <typename MatchResultsT> class RegexMatcher { @@ -61,7 +60,8 @@ protected: // This is a hacky attempt to describe a C-like grammar for floating-point // numbers using a regex (the tests seem to pass though). // A proper NFA would be better, I guess. - static constexpr std::string_view NUMBER_REGEX{R"REGEX(^(?:\d+(?:\.\d*)?|\.\d+)(e[+-]?(\d*))?)REGEX"}; + static constexpr std::string_view NUMBER_REGEX{ + R"REGEX(^(?:\d+(?:\.\d*)?|\.\d+)(e[+-]?(\d*))?)REGEX"}; private: bool matched_e() const { return this->m_match[1].matched; } @@ -81,8 +81,7 @@ public: private: static const std::regex& get_regex() { static constexpr auto flags = - std::regex_constants::ECMAScript | - std::regex_constants::icase; + std::regex_constants::ECMAScript | std::regex_constants::icase; static const std::regex regex{NUMBER_REGEX.data(), NUMBER_REGEX.length(), flags}; return regex; } @@ -98,19 +97,16 @@ public: private: static const boost::regex& get_regex() { static constexpr boost::regex::flag_type flags = - boost::regex::ECMAScript | - boost::regex::icase; + boost::regex::ECMAScript | boost::regex::icase; static const boost::regex regex{NUMBER_REGEX.data(), NUMBER_REGEX.length(), flags}; return regex; } }; template <typename MatchResultsT> -std::optional<double> parse_number( - const std::string_view& input, - RegexNumberMatcher<MatchResultsT>&& matcher, - std::string_view& token) { - +std::optional<double> parse_number(const std::string_view& input, + RegexNumberMatcher<MatchResultsT>&& matcher, + std::string_view& token) { if (!matcher.match(input)) { return {}; } @@ -162,10 +158,8 @@ private: }; template <typename MatchResultsT> -std::string_view parse_whitespace( - const std::string_view& input, - RegexWhitespaceMatcher<MatchResultsT>&& matcher) { - +std::string_view parse_whitespace(const std::string_view& input, + RegexWhitespaceMatcher<MatchResultsT>&& matcher) { if (matcher.match_regex(input)) { return matcher.to_view(); } @@ -173,11 +167,10 @@ std::string_view parse_whitespace( } bool starts_with(const std::string_view& a, const std::string_view& b) noexcept { - return a.length() >= b.length() - && a.compare(0, b.length(), b) == 0; + return a.length() >= b.length() && a.compare(0, b.length(), b) == 0; } -} +} // namespace namespace impl { @@ -207,7 +200,7 @@ std::string_view boost_parse_whitespace(const std::string_view& input) { return parse_whitespace(input, BoostWhitespaceMatcher{}); } -} +} // namespace impl std::optional<double> parse_number(const std::string_view& input, std::string_view& token) { return impl::boost_parse_number(input, token); @@ -218,7 +211,8 @@ std::optional<double> parse_number(const std::string_view& input) { return parse_number(input, token); } -std::optional<token::Type> parse_const_token(const std::string_view& input, std::string_view& token) { +std::optional<token::Type> parse_const_token(const std::string_view& input, + std::string_view& token) { for (const auto type : token::const_tokens()) { const auto str = token::type_to_string(type); if (starts_with(input, str)) { @@ -238,4 +232,4 @@ std::string_view parse_whitespace(const std::string_view& input) { return impl::boost_parse_whitespace(input); } -} +} // namespace math::server::lexer::details diff --git a/server/lexer/details/parse.hpp b/server/lexer/details/parse.hpp index 693dd35..32215f7 100644 --- a/server/lexer/details/parse.hpp +++ b/server/lexer/details/parse.hpp @@ -23,7 +23,7 @@ std::optional<double> boost_parse_number(const std::string_view&); std::string_view std_parse_whitespace(const std::string_view&); std::string_view boost_parse_whitespace(const std::string_view&); -} +} // namespace impl // Exposed for testing: std::string_view parse_whitespace(const std::string_view&); @@ -32,4 +32,4 @@ std::optional<double> parse_number(const std::string_view&); std::optional<token::Type> parse_const_token(const std::string_view&, std::string_view&); std::optional<token::Type> parse_const_token(const std::string_view&); -} +} // namespace math::server::lexer::details diff --git a/server/lexer/error.hpp b/server/lexer/error.hpp index ae6828d..72b3caa 100644 --- a/server/lexer/error.hpp +++ b/server/lexer/error.hpp @@ -13,9 +13,7 @@ namespace math::server { class LexerError : public Error { public: - explicit LexerError(const std::string &what) - : Error{"lexer error: " + what} - { } + explicit LexerError(const std::string& what) : Error{"lexer error: " + what} {} }; -} +} // namespace math::server diff --git a/server/lexer/input.hpp b/server/lexer/input.hpp index 7cf25ec..b5db77e 100644 --- a/server/lexer/input.hpp +++ b/server/lexer/input.hpp @@ -8,16 +8,13 @@ #include "error.hpp" #include <cstddef> - #include <string_view> namespace math::server::lexer { class Input { public: - explicit Input(const std::string_view& input) - : m_pos{0}, m_input{input} - { } + explicit Input(const std::string_view& input) : m_pos{0}, m_input{input} {} const std::string_view& get_input() const { return m_input; } @@ -35,13 +32,11 @@ public: m_input.remove_prefix(len); } - void consume(const std::string_view& sub) { - consume(sub.length()); - } + void consume(const std::string_view& sub) { consume(sub.length()); } private: std::size_t m_pos; std::string_view m_input; }; -} +} // namespace math::server::lexer diff --git a/server/lexer/lexer.cpp b/server/lexer/lexer.cpp index cf24189..9b4e500 100644 --- a/server/lexer/lexer.cpp +++ b/server/lexer/lexer.cpp @@ -3,9 +3,10 @@ // For details, see https://github.com/egor-tensin/math-server. // Distributed under the MIT License. +#include "lexer.hpp" + #include "details/parse.hpp" #include "error.hpp" -#include "lexer.hpp" #include "token.hpp" #include "token_type.hpp" @@ -17,13 +18,9 @@ namespace math::server { -Lexer::Lexer(const std::string_view& input) - : Lexer{lexer::Input{input}} { -} - -Lexer::Lexer(const lexer::Input& input) - : m_input{input} { +Lexer::Lexer(const std::string_view& input) : Lexer{lexer::Input{input}} {} +Lexer::Lexer(const lexer::Input& input) : m_input{input} { consume_token(); } @@ -38,7 +35,7 @@ bool Lexer::for_each_token(const TokenProcessor& process) { std::vector<Lexer::ParsedToken> Lexer::get_tokens() { std::vector<ParsedToken> tokens; - for_each_token([&tokens] (const ParsedToken& token) { + for_each_token([&tokens](const ParsedToken& token) { tokens.emplace_back(token); return true; }); @@ -122,4 +119,4 @@ Lexer::ParsedToken Lexer::parse_token() const { throw LexerError{"invalid input at: " + std::string{m_input.get_input()}}; } -} +} // namespace math::server diff --git a/server/lexer/lexer.hpp b/server/lexer/lexer.hpp index 68950cb..8848837 100644 --- a/server/lexer/lexer.hpp +++ b/server/lexer/lexer.hpp @@ -24,19 +24,15 @@ public: using Token = lexer::Token; using ParsedToken = lexer::ParsedToken; using Type = Token::Type; - using TokenProcessor = std::function<bool (const ParsedToken&)>; + using TokenProcessor = std::function<bool(const ParsedToken&)>; bool for_each_token(const TokenProcessor& process); std::vector<ParsedToken> get_tokens(); - bool has_token() const { - return peek_token().has_value(); - } + bool has_token() const { return peek_token().has_value(); } - std::optional<ParsedToken> peek_token() const { - return m_token_buffer; - } + std::optional<ParsedToken> peek_token() const { return m_token_buffer; } void drop_token(); std::optional<ParsedToken> drop_token_of_type(Type type); @@ -55,4 +51,4 @@ private: std::optional<ParsedToken> m_token_buffer; }; -} +} // namespace math::server diff --git a/server/lexer/token.cpp b/server/lexer/token.cpp index 79c3a63..9bcd9f3 100644 --- a/server/lexer/token.cpp +++ b/server/lexer/token.cpp @@ -3,21 +3,25 @@ // For details, see https://github.com/egor-tensin/math-server. // Distributed under the MIT License. -#include "error.hpp" #include "token.hpp" + +#include "error.hpp" #include "token_type.hpp" #include <cmath> - #include <limits> #include <variant> namespace math::server::lexer { namespace { -static constexpr double nan() { return std::numeric_limits<double>::quiet_NaN(); } +static constexpr double nan() { + return std::numeric_limits<double>::quiet_NaN(); +} -static bool is_nan(double x) { return std::isnan(x); } +static bool is_nan(double x) { + return std::isnan(x); +} static bool numbers_equal(double x, double y) { if (is_nan(x) && is_nan(y)) { @@ -26,19 +30,15 @@ static bool numbers_equal(double x, double y) { return x == y; } -} - -Token::Token(Type type) - : m_type{type} { +} // namespace +Token::Token(Type type) : m_type{type} { if (token::token_has_value(type)) { throw LexerError{"internal: must have a value: " + token::type_to_int_string(type)}; } } -Token::Token(double value) - : m_type{Type::NUMBER}, m_value{value} -{ } +Token::Token(double value) : m_type{Type::NUMBER}, m_value{value} {} bool Token::operator==(const Token& other) const { if (m_type != other.m_type) { @@ -50,7 +50,8 @@ bool Token::operator==(const Token& other) const { if (m_type == Type::NUMBER) { return numbers_equal(as_number(), other.as_number()); } - throw LexerError{"internal: can't compare tokens of type: " + token::type_to_int_string(m_type)}; + throw LexerError{"internal: can't compare tokens of type: " + + token::type_to_int_string(m_type)}; } double Token::as_number() const { @@ -73,4 +74,4 @@ std::ostream& operator<<(std::ostream& os, const Token& token) { return os; } -} +} // namespace math::server::lexer diff --git a/server/lexer/token.hpp b/server/lexer/token.hpp index f351d02..aca3b50 100644 --- a/server/lexer/token.hpp +++ b/server/lexer/token.hpp @@ -8,7 +8,6 @@ #include "token_type.hpp" #include <cstddef> - #include <string_view> #include <utility> #include <variant> @@ -39,8 +38,7 @@ private: class ParsedToken : public Token { public: ParsedToken(Token&& token, std::size_t pos, const std::string_view& view) - : Token{std::move(token)}, m_pos{pos}, m_view{view} { - } + : Token{std::move(token)}, m_pos{pos}, m_view{view} {} std::size_t get_pos() const { return m_pos; } @@ -51,4 +49,4 @@ private: std::string_view m_view; }; -} +} // namespace math::server::lexer diff --git a/server/lexer/token_type.cpp b/server/lexer/token_type.cpp index 6a42a3d..037a761 100644 --- a/server/lexer/token_type.cpp +++ b/server/lexer/token_type.cpp @@ -3,9 +3,10 @@ // For details, see https://github.com/egor-tensin/math-server. // Distributed under the MIT License. -#include "error.hpp" #include "token_type.hpp" +#include "error.hpp" + #include <functional> #include <map> #include <ostream> @@ -21,9 +22,7 @@ using FromStringMap = std::map<std::string, Type, std::greater<std::string>>; class ToStringConverter { public: - ToStringConverter() : m_map{to_string_map()} { - validate(); - } + ToStringConverter() : m_map{to_string_map()} { validate(); } const ToStringMap& map() const { return m_map; } @@ -43,16 +42,15 @@ private: return map; } - void validate() const { - check_for_duplicates(); - } + void validate() const { check_for_duplicates(); } void check_for_duplicates() const { std::unordered_set<std::string> strings; for (const auto& [type, str] : m_map) { const auto [_, inserted] = strings.emplace(str); if (!inserted) { - throw std::logic_error{"multiple tokens have the same string representation: " + str}; + throw std::logic_error{"multiple tokens have the same string representation: " + + str}; } } } @@ -67,9 +65,7 @@ const ToStringMap& to_string_map() { class FromStringConverter { public: - FromStringConverter(const ToStringMap& to_string) - : m_map{build_map(to_string)} { - } + FromStringConverter(const ToStringMap& to_string) : m_map{build_map(to_string)} {} const FromStringMap& map() const { return m_map; } @@ -79,7 +75,8 @@ private: for (const auto& [type, str] : to_string) { const auto [_, inserted] = from_string.emplace(str, type); if (!inserted) { - throw std::logic_error{"multiple tokens have the same string representation: " + str}; + throw std::logic_error{"multiple tokens have the same string representation: " + + str}; } } return from_string; @@ -110,7 +107,7 @@ private: TypeSet m_set; }; -} +} // namespace TypeInt type_to_int(Type type) { return static_cast<TypeInt>(type); @@ -167,4 +164,4 @@ std::ostream& operator<<(std::ostream& os, const Type& type) { return os; } -} +} // namespace math::server::lexer::token diff --git a/server/lexer/token_type.hpp b/server/lexer/token_type.hpp index 2040d28..438e2dd 100644 --- a/server/lexer/token_type.hpp +++ b/server/lexer/token_type.hpp @@ -40,4 +40,4 @@ Type type_from_string(const std::string&); std::ostream& operator<<(std::ostream&, const Type&); -} +} // namespace math::server::lexer::token diff --git a/server/main/server.cpp b/server/main/server.cpp index aef7d00..72034b1 100644 --- a/server/main/server.cpp +++ b/server/main/server.cpp @@ -4,19 +4,18 @@ // Distributed under the MIT License. #include "server.hpp" -#include "session.hpp" -#include "session_manager.hpp" -#include "settings.hpp" #include "../common/error.hpp" #include "../common/log.hpp" +#include "session.hpp" +#include "session_manager.hpp" +#include "settings.hpp" #include <boost/asio.hpp> #include <boost/system/error_code.hpp> #include <boost/system/system_error.hpp> #include <cstddef> - #include <exception> #include <thread> #include <vector> @@ -40,17 +39,12 @@ void configure_acceptor(boost::asio::ip::tcp::acceptor& acceptor, unsigned short } } -} +} // namespace -Server::Server(const Settings& settings) - : Server{settings.m_port, settings.m_threads} -{ } +Server::Server(const Settings& settings) : Server{settings.m_port, settings.m_threads} {} Server::Server(unsigned short port, std::size_t threads) - : m_numof_threads{threads} - , m_signals{m_io_context} - , m_acceptor{m_io_context} { - + : m_numof_threads{threads}, m_signals{m_io_context}, m_acceptor{m_io_context} { wait_for_signal(); configure_acceptor(m_acceptor, port); @@ -60,7 +54,7 @@ Server::Server(unsigned short port, std::size_t threads) void Server::run() { std::vector<std::thread> threads{m_numof_threads}; for (std::size_t i = 0; i < m_numof_threads; ++i) { - threads[i] = std::thread{[this] () { m_io_context.run(); }}; + threads[i] = std::thread{[this]() { m_io_context.run(); }}; } for (std::size_t i = 0; i < m_numof_threads; ++i) { @@ -73,9 +67,8 @@ void Server::wait_for_signal() { m_signals.add(SIGINT); m_signals.add(SIGTERM); - m_signals.async_wait([this] (const boost::system::error_code& ec, int signo) { - handle_signal(ec, signo); - }); + m_signals.async_wait( + [this](const boost::system::error_code& ec, int signo) { handle_signal(ec, signo); }); } catch (const boost::system::system_error& e) { throw Error{e.what()}; } @@ -98,10 +91,9 @@ void Server::handle_signal(const boost::system::error_code& ec, int signo) { void Server::accept() { const auto session = m_session_mgr.make_session(m_io_context); - m_acceptor.async_accept(session->socket(), - [session, this] (const boost::system::error_code& ec) { - handle_accept(session, ec); - }); + m_acceptor.async_accept( + session->socket(), + [session, this](const boost::system::error_code& ec) { handle_accept(session, ec); }); } void Server::handle_accept(SessionPtr session, const boost::system::error_code& ec) { @@ -114,4 +106,4 @@ void Server::handle_accept(SessionPtr session, const boost::system::error_code& accept(); } -} +} // namespace math::server diff --git a/server/main/server.hpp b/server/main/server.hpp index 6233bff..456f075 100644 --- a/server/main/server.hpp +++ b/server/main/server.hpp @@ -38,4 +38,4 @@ private: SessionManager m_session_mgr; }; -} +} // namespace math::server diff --git a/server/main/session.cpp b/server/main/session.cpp index 900e5b4..3917514 100644 --- a/server/main/session.cpp +++ b/server/main/session.cpp @@ -4,11 +4,11 @@ // Distributed under the MIT License. #include "session.hpp" -#include "session_manager.hpp" #include "../common/error.hpp" #include "../common/log.hpp" #include "../parser/parser.hpp" +#include "session_manager.hpp" #include <boost/asio.hpp> #include <boost/lexical_cast.hpp> @@ -16,7 +16,6 @@ #include <boost/system/system_error.hpp> #include <cstddef> - #include <exception> #include <string> #include <utility> @@ -38,11 +37,10 @@ std::string calc_reply(const std::string& input) { return reply; } -} +} // namespace Session::Session(SessionManager& mgr, boost::asio::io_context& io_context) - : m_session_mgr{mgr}, m_strand{io_context}, m_socket{io_context} -{ } + : m_session_mgr{mgr}, m_strand{io_context}, m_socket{io_context} {} boost::asio::ip::tcp::socket& Session::socket() { return m_socket; @@ -69,10 +67,12 @@ void Session::read() { const auto self = shared_from_this(); // Stop at LF - boost::asio::async_read_until(m_socket, m_buffer, '\n', boost::asio::bind_executor(m_strand, - [this, self] (const boost::system::error_code& ec, std::size_t bytes) { - handle_read(ec, bytes); - })); + boost::asio::async_read_until( + m_socket, m_buffer, '\n', + boost::asio::bind_executor( + m_strand, [this, self](const boost::system::error_code& ec, std::size_t bytes) { + handle_read(ec, bytes); + })); } void Session::handle_read(const boost::system::error_code& ec, std::size_t bytes) { @@ -99,10 +99,12 @@ void Session::write(const std::string& output) { // Include CR (so that Windows' telnet client works) os << output << "\r\n"; - boost::asio::async_write(m_socket, m_buffer, boost::asio::bind_executor(m_strand, - [this, self] (const boost::system::error_code& ec, std::size_t bytes) { - handle_write(ec, bytes); - })); + boost::asio::async_write( + m_socket, m_buffer, + boost::asio::bind_executor( + m_strand, [this, self](const boost::system::error_code& ec, std::size_t bytes) { + handle_write(ec, bytes); + })); } void Session::handle_write(const boost::system::error_code& ec, std::size_t bytes) { @@ -115,4 +117,4 @@ void Session::handle_write(const boost::system::error_code& ec, std::size_t byte read(); } -} +} // namespace math::server diff --git a/server/main/session.hpp b/server/main/session.hpp index 31be327..2ab8f26 100644 --- a/server/main/session.hpp +++ b/server/main/session.hpp @@ -9,7 +9,6 @@ #include <boost/system/error_code.hpp> #include <cstddef> - #include <memory> #include <string> @@ -44,4 +43,4 @@ private: boost::asio::streambuf m_buffer; }; -} +} // namespace math::server diff --git a/server/main/session_manager.cpp b/server/main/session_manager.cpp index d73e998..753750d 100644 --- a/server/main/session_manager.cpp +++ b/server/main/session_manager.cpp @@ -3,10 +3,10 @@ // For details, see https://github.com/egor-tensin/math-server. // Distributed under the MIT License. -#include "session.hpp" #include "session_manager.hpp" #include "../common/log.hpp" +#include "session.hpp" #include <memory> #include <mutex> @@ -40,4 +40,4 @@ void SessionManager::stop_all() { m_sessions.clear(); } -} +} // namespace math::server diff --git a/server/main/session_manager.hpp b/server/main/session_manager.hpp index 66b8d67..762bfc5 100644 --- a/server/main/session_manager.hpp +++ b/server/main/session_manager.hpp @@ -7,8 +7,8 @@ #include <boost/asio.hpp> -#include <mutex> #include <memory> +#include <mutex> #include <unordered_set> namespace math::server { @@ -32,4 +32,4 @@ private: std::unordered_set<SessionPtr> m_sessions; }; -} +} // namespace math::server diff --git a/server/main/settings.hpp b/server/main/settings.hpp index a334fe7..11982da 100644 --- a/server/main/settings.hpp +++ b/server/main/settings.hpp @@ -9,7 +9,6 @@ #include <boost/program_options.hpp> #include <cstddef> - #include <exception> #include <iostream> #include <string> @@ -33,29 +32,23 @@ struct Settings { class SettingsParser { public: - explicit SettingsParser(const std::string& argv0) - : m_prog_name{extract_filename(argv0)} - { + explicit SettingsParser(const std::string& argv0) : m_prog_name{extract_filename(argv0)} { m_visible.add_options()("help,h", "show this message and exit"); - m_visible.add_options()( - "port,p", - boost::program_options::value(&m_settings.m_port)->default_value(Settings::DEFAULT_PORT), - "server port number"); - m_visible.add_options()( - "threads,n", - boost::program_options::value(&m_settings.m_threads)->default_value(Settings::default_threads()), - "number of threads"); + m_visible.add_options()("port,p", + boost::program_options::value(&m_settings.m_port) + ->default_value(Settings::DEFAULT_PORT), + "server port number"); + m_visible.add_options()("threads,n", + boost::program_options::value(&m_settings.m_threads) + ->default_value(Settings::default_threads()), + "number of threads"); } - static const char* get_short_description() { - return "[-h|--help] [-p|--port] [-n|--threads]"; - } + static const char* get_short_description() { return "[-h|--help] [-p|--port] [-n|--threads]"; } Settings parse(int argc, char* argv[]) { boost::program_options::store( - boost::program_options::command_line_parser{argc, argv} - .options(m_visible) - .run(), + boost::program_options::command_line_parser{argc, argv}.options(m_visible).run(), m_settings.m_vm); if (m_settings.exit_with_usage()) { return m_settings; @@ -64,9 +57,7 @@ public: return m_settings; } - void usage() const { - std::cout << *this; - } + void usage() const { std::cout << *this; } void usage_error(const std::exception& e) const { std::cerr << "usage error: " << e.what() << '\n'; @@ -91,4 +82,4 @@ private: } }; -} +} // namespace math::server diff --git a/server/parser/error.hpp b/server/parser/error.hpp index 66d6e45..6862a8f 100644 --- a/server/parser/error.hpp +++ b/server/parser/error.hpp @@ -13,9 +13,7 @@ namespace math::server { class ParserError : public Error { public: - explicit ParserError(const std::string& what) - : Error{"parser error: " + what} - { } + explicit ParserError(const std::string& what) : Error{"parser error: " + what} {} }; -} +} // namespace math::server diff --git a/server/parser/operator.hpp b/server/parser/operator.hpp index fd45aa2..9030c65 100644 --- a/server/parser/operator.hpp +++ b/server/parser/operator.hpp @@ -5,10 +5,9 @@ #pragma once -#include "error.hpp" - #include "../lexer/token.hpp" #include "../lexer/token_type.hpp" +#include "error.hpp" #include <cmath> @@ -72,9 +71,7 @@ public: } } - bool is_left_associative() const { - return !is_right_associative(); - } + bool is_left_associative() const { return !is_right_associative(); } double exec(double lhs, double rhs) const { switch (m_type) { @@ -103,11 +100,9 @@ public: } private: - explicit BinaryOp(const Token& token) - : m_type{token.get_type()} - { } + explicit BinaryOp(const Token& token) : m_type{token.get_type()} {} Type m_type; }; -} +} // namespace math::server::parser diff --git a/server/parser/parser.hpp b/server/parser/parser.hpp index 0a8c761..844b36e 100644 --- a/server/parser/parser.hpp +++ b/server/parser/parser.hpp @@ -5,11 +5,10 @@ #pragma once +#include "../lexer/lexer.hpp" #include "error.hpp" #include "operator.hpp" -#include "../lexer/lexer.hpp" - #include <optional> #include <string_view> @@ -24,9 +23,7 @@ public: // a finer algorithm for parsing arithmetic expressions. // Reference: https://en.wikipedia.org/wiki/Operator-precedence_parser - explicit Parser(const std::string_view& input) - : m_lexer{input} - { } + explicit Parser(const std::string_view& input) : m_lexer{input} {} double exec() { const auto result = exec_dmas(); @@ -38,9 +35,7 @@ public: private: // DMAS as in Division, Multiplication, Addition and Subtraction - double exec_dmas() { - return exec_binary_op(exec_factor(), parser::BinaryOp::min_precedence()); - } + double exec_dmas() { return exec_binary_op(exec_factor(), parser::BinaryOp::min_precedence()); } // Exponentiation operator double exec_exp() { @@ -61,7 +56,8 @@ private: { const auto acc_left_assoc = next.is_left_associative() && next_prec > prev_prec; - const auto acc_right_assoc = next.is_right_associative() && next_prec == prev_prec; + const auto acc_right_assoc = + next.is_right_associative() && next_prec == prev_prec; const auto acc = acc_left_assoc || acc_right_assoc; if (!acc) { @@ -109,7 +105,8 @@ private: if (m_lexer.drop_token_of_type(Type::LEFT_PAREN).has_value()) { const auto inner = exec_dmas(); - if (!m_lexer.has_token() || !m_lexer.drop_token_of_type(Type::RIGHT_PAREN).has_value()) { + if (!m_lexer.has_token() || + !m_lexer.drop_token_of_type(Type::RIGHT_PAREN).has_value()) { throw ParserError{"missing closing ')'"}; } return inner; @@ -125,4 +122,4 @@ private: Lexer m_lexer; }; -} +} // namespace math::server diff --git a/test/benchmarks/lexer.cpp b/test/benchmarks/lexer.cpp index 5475c82..9384ee2 100644 --- a/test/benchmarks/lexer.cpp +++ b/test/benchmarks/lexer.cpp @@ -34,7 +34,7 @@ protected: }; }; -} +} // namespace BENCHMARK_F(NumberExamples, StdParseNumber)(benchmark::State& state) { using namespace math::server::lexer::details; diff --git a/test/unit_tests/lexer.cpp b/test/unit_tests/lexer.cpp index c47e9f2..c43b879 100644 --- a/test/unit_tests/lexer.cpp +++ b/test/unit_tests/lexer.cpp @@ -9,8 +9,8 @@ #include <server/lexer/token.hpp> #include <server/lexer/token_type.hpp> -#include <boost/test/data/test_case.hpp> #include <boost/test/data/monomorphic.hpp> +#include <boost/test/data/test_case.hpp> #include <boost/test/unit_test.hpp> #include <ostream> @@ -135,7 +135,7 @@ const std::vector<Expected> expected{ }}, }; -} +} // namespace get_tokens::valid namespace get_tokens::invalid { @@ -149,32 +149,29 @@ const std::vector<std::string> error_msg{ "server error: lexer error: invalid input at: & 456", }; -} -} - -BOOST_DATA_TEST_CASE( - test_get_tokens_valid, - bdata::make(get_tokens::valid::input) ^ get_tokens::valid::expected, - input, - expected) { +} // namespace get_tokens::invalid +} // namespace +BOOST_DATA_TEST_CASE(test_get_tokens_valid, + bdata::make(get_tokens::valid::input) ^ get_tokens::valid::expected, + input, + expected) { Lexer lexer{input}; const auto actual = lexer.get_tokens(); - BOOST_CHECK_EQUAL_COLLECTIONS(actual.cbegin(), actual.cend(), - expected.m_tokens.cbegin(), - expected.m_tokens.cend()); + BOOST_CHECK_EQUAL_COLLECTIONS(actual.cbegin(), actual.cend(), expected.m_tokens.cbegin(), + expected.m_tokens.cend()); } -BOOST_DATA_TEST_CASE( - test_get_tokens_invalid, - bdata::make(get_tokens::invalid::input) ^ get_tokens::invalid::error_msg, - input, - error_msg) { - - BOOST_REQUIRE_THROW(do { - Lexer lexer{input}; - lexer.get_tokens(); - } while (0), LexerError); +BOOST_DATA_TEST_CASE(test_get_tokens_invalid, + bdata::make(get_tokens::invalid::input) ^ get_tokens::invalid::error_msg, + input, + error_msg) { + BOOST_REQUIRE_THROW( + do { + Lexer lexer{input}; + lexer.get_tokens(); + } while (0), + LexerError); try { Lexer lexer{input}; diff --git a/test/unit_tests/parser.cpp b/test/unit_tests/parser.cpp index 231be3d..2e90992 100644 --- a/test/unit_tests/parser.cpp +++ b/test/unit_tests/parser.cpp @@ -6,8 +6,8 @@ #include <server/parser/error.hpp> #include <server/parser/parser.hpp> -#include <boost/test/data/test_case.hpp> #include <boost/test/data/monomorphic.hpp> +#include <boost/test/data/test_case.hpp> #include <boost/test/unit_test.hpp> #include <string> @@ -83,7 +83,7 @@ const std::vector<double> expected{ -3.9375, }; -} +} // namespace exec::valid namespace exec::invalid { @@ -111,29 +111,27 @@ const std::vector<std::string> error_msg{ "server error: parser error: expected a binary operator", }; -} -} - -BOOST_DATA_TEST_CASE( - test_exec_valid, - bdata::make(exec::valid::input) ^ exec::valid::expected, - input, - expected) { +} // namespace exec::invalid +} // namespace +BOOST_DATA_TEST_CASE(test_exec_valid, + bdata::make(exec::valid::input) ^ exec::valid::expected, + input, + expected) { Parser parser{input}; BOOST_TEST(parser.exec() == expected); } -BOOST_DATA_TEST_CASE( - test_exec_invalid, - bdata::make(exec::invalid::input) ^ exec::invalid::error_msg, - input, - error_msg) { - - BOOST_REQUIRE_THROW(do { - Parser parser{input}; - parser.exec(); - } while (0), ParserError); +BOOST_DATA_TEST_CASE(test_exec_invalid, + bdata::make(exec::invalid::input) ^ exec::invalid::error_msg, + input, + error_msg) { + BOOST_REQUIRE_THROW( + do { + Parser parser{input}; + parser.exec(); + } while (0), + ParserError); try { Parser parser{input}; |