Commit 364b053e authored by Dennis Jenkins's avatar Dennis Jenkins

clang-format most header files.

parent 16fbe079
This diff is collapsed.
......@@ -6,21 +6,21 @@
#pragma once
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <iostream>
#include <cstring>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#define TRY(...) \
do { \
auto ret = __VA_ARGS__; \
if (ret < 0) { \
const char* str = #__VA_ARGS__; \
const char *str = #__VA_ARGS__; \
std::ostringstream oss; \
oss << str << ": "; \
if (errno == 0) { \
......@@ -45,13 +45,13 @@
} \
return ret; \
}(); \
(void) 0
(void)0
struct PrintException {
void operator()(std::exception_ptr exc) const {
try {
std::rethrow_exception(exc);
} catch (const std::exception& e) {
} catch (const std::exception &e) {
std::cerr << "An exception occured: " << e.what() << std::endl;
}
}
......@@ -61,4 +61,3 @@ struct PrintException {
// Until we require C++17 compiler with [[maybe_unused]]
#define UNUSED(x) (void)(x);
......@@ -5,22 +5,21 @@
#include <limits>
// Allow compile-time overload
namespace Pistache
{
namespace Const
{
static constexpr size_t MaxBacklog = 128;
static constexpr size_t MaxEvents = 1024;
static constexpr size_t MaxBuffer = 4096;
static constexpr size_t DefaultWorkers = 1;
namespace Pistache {
namespace Const {
static constexpr size_t MaxBacklog = 128;
static constexpr size_t MaxEvents = 1024;
static constexpr size_t MaxBuffer = 4096;
static constexpr size_t DefaultWorkers = 1;
static constexpr size_t DefaultTimerPoolSize = 128;
static constexpr size_t DefaultTimerPoolSize = 128;
// Defined from CMakeLists.txt in project root
static constexpr size_t DefaultMaxRequestSize = 4096;
static constexpr size_t DefaultMaxResponseSize = std::numeric_limits<uint32_t>::max();
static constexpr size_t ChunkSize = 1024;
// Defined from CMakeLists.txt in project root
static constexpr size_t DefaultMaxRequestSize = 4096;
static constexpr size_t DefaultMaxResponseSize =
std::numeric_limits<uint32_t>::max();
static constexpr size_t ChunkSize = 1024;
static constexpr uint16_t HTTP_STANDARD_PORT = 80;
static constexpr uint16_t HTTP_STANDARD_PORT = 80;
} // namespace Const
} // namespace Pistache
......@@ -7,20 +7,20 @@
#pragma once
#include <ctime>
#include <string>
#include <list>
#include <map>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <pistache/optional.h>
#include <pistache/http_defs.h>
#include <pistache/optional.h>
namespace Pistache {
namespace Http {
struct Cookie {
friend std::ostream& operator<<(std::ostream& os, const Cookie& cookie);
friend std::ostream &operator<<(std::ostream &os, const Cookie &cookie);
Cookie(std::string name, std::string value);
......@@ -37,33 +37,30 @@ struct Cookie {
std::map<std::string, std::string> ext;
static Cookie fromRaw(const char* str, size_t len);
static Cookie fromString(const std::string& str);
static Cookie fromRaw(const char *str, size_t len);
static Cookie fromString(const std::string &str);
private:
void write(std::ostream& os) const;
void write(std::ostream &os) const;
};
std::ostream& operator<<(std::ostream& os, const Cookie& cookie);
std::ostream &operator<<(std::ostream &os, const Cookie &cookie);
class CookieJar {
public:
using HashMapCookies = std::unordered_map<std::string, Cookie>; // "value" -> Cookie
using Storage = std::unordered_map<std::string, HashMapCookies>; // "name" -> Hashmap("value" -> Cookie)
using HashMapCookies =
std::unordered_map<std::string, Cookie>; // "value" -> Cookie
using Storage = std::unordered_map<
std::string, HashMapCookies>; // "name" -> Hashmap("value" -> Cookie)
struct iterator : std::iterator<std::bidirectional_iterator_tag, Cookie> {
explicit iterator(const Storage::const_iterator& _iterator)
: iter_storage(_iterator)
, iter_cookie_values()
, iter_storage_end()
{ }
iterator(const Storage::const_iterator& _iterator, const Storage::const_iterator& end)
: iter_storage(_iterator)
, iter_cookie_values()
, iter_storage_end(end)
{
if(iter_storage != iter_storage_end) {
explicit iterator(const Storage::const_iterator &_iterator)
: iter_storage(_iterator), iter_cookie_values(), iter_storage_end() {}
iterator(const Storage::const_iterator &_iterator,
const Storage::const_iterator &end)
: iter_storage(_iterator), iter_cookie_values(), iter_storage_end(end) {
if (iter_storage != iter_storage_end) {
iter_cookie_values = iter_storage->second.begin();
}
}
......@@ -72,33 +69,31 @@ public:
return iter_cookie_values->second; // return iter_storage->second;
}
const Cookie* operator->() const {
return &(iter_cookie_values->second);
}
const Cookie *operator->() const { return &(iter_cookie_values->second); }
iterator& operator++() {
iterator &operator++() {
++iter_cookie_values;
if(iter_cookie_values == iter_storage->second.end()) {
if (iter_cookie_values == iter_storage->second.end()) {
++iter_storage;
if(iter_storage != iter_storage_end)
if (iter_storage != iter_storage_end)
iter_cookie_values = iter_storage->second.begin();
}
return *this;
}
iterator operator++(int) {
iterator ret(iter_storage,iter_storage_end);
iterator ret(iter_storage, iter_storage_end);
++iter_cookie_values;
if(iter_cookie_values == iter_storage->second.end()) {
if (iter_cookie_values == iter_storage->second.end()) {
++iter_storage;
if(iter_storage != iter_storage_end) // this check is important
if (iter_storage != iter_storage_end) // this check is important
iter_cookie_values = iter_storage->second.begin();
}
return ret;
}
bool operator !=(iterator other) const {
bool operator!=(iterator other) const {
return iter_storage != other.iter_storage;
}
......@@ -109,30 +104,27 @@ public:
private:
Storage::const_iterator iter_storage;
HashMapCookies::const_iterator iter_cookie_values;
Storage::const_iterator iter_storage_end; // we need to know where main hashmap ends.
Storage::const_iterator
iter_storage_end; // we need to know where main hashmap ends.
};
CookieJar();
void add(const Cookie& cookie);
void add(const Cookie &cookie);
void removeAllCookies();
void addFromRaw(const char *str, size_t len);
Cookie get(const std::string& name) const;
Cookie get(const std::string &name) const;
bool has(const std::string& name) const;
bool has(const std::string &name) const;
iterator begin() const {
return iterator(cookies.begin(), cookies.end());
}
iterator begin() const { return iterator(cookies.begin(), cookies.end()); }
iterator end() const {
return iterator(cookies.end());
}
iterator end() const { return iterator(cookies.end()); }
private:
Storage cookies;
};
} // namespace Net
} // namespace Http
} // namespace Pistache
This diff is collapsed.
This diff is collapsed.
......@@ -6,9 +6,9 @@
#pragma once
#include <pistache/http.h>
#include <pistache/listener.h>
#include <pistache/net.h>
#include <pistache/http.h>
namespace Pistache {
namespace Http {
......@@ -18,19 +18,19 @@ public:
struct Options {
friend class Endpoint;
Options& threads(int val);
Options& threadsName(const std::string& val);
Options& flags(Flags<Tcp::Options> flags);
Options& flags(Tcp::Options tcp_opts) {
Options &threads(int val);
Options &threadsName(const std::string &val);
Options &flags(Flags<Tcp::Options> flags);
Options &flags(Tcp::Options tcp_opts) {
flags(Flags<Tcp::Options>(tcp_opts));
return *this;
}
Options& backlog(int val);
Options& maxRequestSize(size_t val);
Options& maxResponseSize(size_t val);
Options &backlog(int val);
Options &maxRequestSize(size_t val);
Options &maxResponseSize(size_t val);
[[deprecated("Replaced by maxRequestSize(val)")]]
Options& maxPayload(size_t val);
[[deprecated("Replaced by maxRequestSize(val)")]] Options &
maxPayload(size_t val);
private:
int threads_;
......@@ -42,18 +42,17 @@ public:
Options();
};
Endpoint();
explicit Endpoint(const Address& addr);
explicit Endpoint(const Address &addr);
template<typename... Args>
void initArgs(Args&& ...args) {
template <typename... Args> void initArgs(Args &&... args) {
listener.init(std::forward<Args>(args)...);
}
void init(const Options& options = Options());
void setHandler(const std::shared_ptr<Handler>& handler);
void init(const Options &options = Options());
void setHandler(const std::shared_ptr<Handler> &handler);
void bind();
void bind(const Address& addr);
void bind(const Address &addr);
void serve();
void serveThreaded();
......@@ -123,25 +122,20 @@ public:
*
* [1] https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set_verify.html
*/
void useSSLAuth(std::string ca_file, std::string ca_path = "", int (*cb)(int, void *) = NULL);
void useSSLAuth(std::string ca_file, std::string ca_path = "",
int (*cb)(int, void *) = NULL);
bool isBound() const {
return listener.isBound();
}
bool isBound() const { return listener.isBound(); }
Port getPort() const {
return listener.getPort();
}
Port getPort() const { return listener.getPort(); }
Async::Promise<Tcp::Listener::Load> requestLoad(const Tcp::Listener::Load& old);
Async::Promise<Tcp::Listener::Load>
requestLoad(const Tcp::Listener::Load &old);
static Options options();
private:
template<typename Method>
void serveImpl(Method method)
{
template <typename Method> void serveImpl(Method method) {
#define CALL_MEMBER_FN(obj, pmf) ((obj).*(pmf))
if (!handler_)
throw std::runtime_error("Must call setHandler() prior to serve()");
......@@ -157,15 +151,14 @@ private:
Tcp::Listener listener;
};
template<typename Handler>
void listenAndServe(Address addr, const Endpoint::Options& options = Endpoint::options())
{
template <typename Handler>
void listenAndServe(Address addr,
const Endpoint::Options &options = Endpoint::options()) {
Endpoint endpoint(addr);
endpoint.init(options);
endpoint.setHandler(make_handler<Handler>());
endpoint.serve();
}
} // namespace Http
} // namespace Pistache
......@@ -5,19 +5,15 @@
namespace Pistache {
namespace Tcp {
class SocketError: public std::runtime_error
{
class SocketError : public std::runtime_error {
public:
explicit SocketError(const char* what_arg) : std::runtime_error(what_arg)
{ }
explicit SocketError(const char *what_arg) : std::runtime_error(what_arg) {}
};
class ServerError: public std::runtime_error
{
class ServerError : public std::runtime_error {
public:
explicit ServerError(const char* what_arg) : std::runtime_error(what_arg)
{ }
explicit ServerError(const char *what_arg) : std::runtime_error(what_arg) {}
};
} // Tcp
} // Pistache
\ No newline at end of file
} // namespace Tcp
} // namespace Pistache
\ No newline at end of file
......@@ -6,48 +6,37 @@
#pragma once
#include <type_traits>
#include <climits>
#include <iostream>
#include <type_traits>
namespace Pistache {
// Looks like gcc 4.6 does not implement std::underlying_type
namespace detail {
template<size_t N> struct TypeStorage;
template<> struct TypeStorage<sizeof(uint8_t)> {
typedef uint8_t Type;
};
template<> struct TypeStorage<sizeof(uint16_t)> {
typedef uint16_t Type;
};
template<> struct TypeStorage<sizeof(uint32_t)> {
typedef uint32_t Type;
};
template<> struct TypeStorage<sizeof(uint64_t)> {
typedef uint64_t Type;
};
template<typename T> struct UnderlyingType {
template <size_t N> struct TypeStorage;
template <> struct TypeStorage<sizeof(uint8_t)> { typedef uint8_t Type; };
template <> struct TypeStorage<sizeof(uint16_t)> { typedef uint16_t Type; };
template <> struct TypeStorage<sizeof(uint32_t)> { typedef uint32_t Type; };
template <> struct TypeStorage<sizeof(uint64_t)> { typedef uint64_t Type; };
template <typename T> struct UnderlyingType {
typedef typename TypeStorage<sizeof(T)>::Type Type;
};
};
template<typename Enum>
struct HasNone {
template<typename U>
template <typename Enum> struct HasNone {
template <typename U>
static auto test(U *) -> decltype(U::None, std::true_type());
template<typename U>
static auto test(...) -> std::false_type;
template <typename U> static auto test(...) -> std::false_type;
static constexpr bool value =
std::is_same<decltype(test<Enum>(0)), std::true_type>::value;
};
}
};
} // namespace detail
template<typename T>
class Flags {
template <typename T> class Flags {
public:
typedef typename detail::UnderlyingType<T>::Type Type;
......@@ -55,23 +44,19 @@ public:
static_assert(detail::HasNone<T>::value, "The enumartion needs a None value");
static_assert(static_cast<Type>(T::None) == 0, "None should be 0");
Flags() : val(T::None) {
}
Flags() : val(T::None) {}
explicit Flags(T _val) : val(_val) {
}
explicit Flags(T _val) : val(_val) {}
#define DEFINE_BITWISE_OP_CONST(Op) \
Flags<T> operator Op (T rhs) const { \
Flags<T> operator Op(T rhs) const { \
return Flags<T>( \
static_cast<T>(static_cast<Type>(val) Op static_cast<Type>(rhs)) \
); \
static_cast<T>(static_cast<Type>(val) Op static_cast<Type>(rhs))); \
} \
\
Flags<T> operator Op (Flags<T> rhs) const { \
Flags<T> operator Op(Flags<T> rhs) const { \
return Flags<T>( \
static_cast<T>(static_cast<Type>(val) Op static_cast<Type>(rhs.val)) \
); \
static_cast<T>(static_cast<Type>(val) Op static_cast<Type>(rhs.val))); \
}
DEFINE_BITWISE_OP_CONST(|)
......@@ -81,17 +66,14 @@ public:
#undef DEFINE_BITWISE_OP_CONST
#define DEFINE_BITWISE_OP(Op) \
Flags<T>& operator Op##=(T rhs) { \
val = static_cast<T>( \
static_cast<Type>(val) Op static_cast<Type>(rhs) \
); \
Flags<T> &operator Op##=(T rhs) { \
val = static_cast<T>(static_cast<Type>(val) Op static_cast<Type>(rhs)); \
return *this; \
} \
\
Flags<T>& operator Op##=(Flags<T> rhs) { \
val = static_cast<T>( \
static_cast<Type>(val) Op static_cast<Type>(rhs.val) \
); \
Flags<T> &operator Op##=(Flags<T> rhs) { \
val = \
static_cast<T>(static_cast<Type>(val) Op static_cast<Type>(rhs.val)); \
return *this; \
}
......@@ -105,18 +87,14 @@ public:
return static_cast<Type>(val) & static_cast<Type>(flag);
}
Flags<T>& setFlag(T flag) {
Flags<T> &setFlag(T flag) {
*this |= flag;
return *this;
}
Flags<T>& toggleFlag(T flag) {
return *this ^= flag;
}
Flags<T> &toggleFlag(T flag) { return *this ^= flag; }
operator T() const {
return val;
}
operator T() const { return val; }
private:
T val;
......@@ -125,19 +103,18 @@ private:
} // namespace Pistache
#define DEFINE_BITWISE_OP(Op, T) \
inline T operator Op (T lhs, T rhs) { \
inline T operator Op(T lhs, T rhs) { \
typedef Pistache::detail::UnderlyingType<T>::Type UnderlyingType; \
return static_cast<T>( \
static_cast<UnderlyingType>(lhs) Op static_cast<UnderlyingType>(rhs) \
); \
return static_cast<T>(static_cast<UnderlyingType>(lhs) \
Op static_cast<UnderlyingType>(rhs)); \
}
#define DECLARE_FLAGS_OPERATORS(T) \
DEFINE_BITWISE_OP(&, T) \
DEFINE_BITWISE_OP(|, T)
template<typename T>
std::ostream& operator<<(std::ostream& os, Pistache::Flags<T> flags) {
template <typename T>
std::ostream &operator<<(std::ostream &os, Pistache::Flags<T> flags) {
typedef typename Pistache::detail::UnderlyingType<T>::Type UnderlyingType;
auto val = static_cast<UnderlyingType>(static_cast<T>(flags));
......@@ -147,4 +124,3 @@ std::ostream& operator<<(std::ostream& os, Pistache::Flags<T> flags) {
return os;
}
This diff is collapsed.
......@@ -6,11 +6,11 @@
#pragma once
#include <string>
#include <ostream>
#include <stdexcept>
#include <chrono>
#include <functional>
#include <ostream>
#include <stdexcept>
#include <string>
namespace Pistache {
namespace Http {
......@@ -66,7 +66,8 @@ namespace Http {
CODE(413, Request_Entity_Too_Large, "Request Entity Too Large") \
CODE(414, RequestURI_Too_Long, "Request-URI Too Long") \
CODE(415, Unsupported_Media_Type, "Unsupported Media Type") \
CODE(416, Requested_Range_Not_Satisfiable, "Requested Range Not Satisfiable") \
CODE(416, Requested_Range_Not_Satisfiable, \
"Requested Range Not Satisfiable") \
CODE(417, Expectation_Failed, "Expectation Failed") \
CODE(418, I_m_a_teapot, "I'm a teapot") \
CODE(421, Misdirected_Request, "Misdirected Request") \
......@@ -75,9 +76,11 @@ namespace Http {
CODE(424, Failed_Dependency, "Failed Dependency") \
CODE(426, Upgrade_Required, "Upgrade Required") \
CODE(428, Precondition_Required, "Precondition Required") \
CODE(429, Too_Many_Requests, "Too Many Requests")\
CODE(431, Request_Header_Fields_Too_Large, "Request Header Fields Too Large") \
CODE(444, Connection_Closed_Without_Response, "Connection Closed Without Response") \
CODE(429, Too_Many_Requests, "Too Many Requests") \
CODE(431, Request_Header_Fields_Too_Large, \
"Request Header Fields Too Large") \
CODE(444, Connection_Closed_Without_Response, \
"Connection Closed Without Response") \
CODE(451, Unavailable_For_Legal_Reasons, "Unavailable For Legal Reasons") \
CODE(499, Client_Closed_Request, "Client Closed Request") \
CODE(500, Internal_Server_Error, "Internal Server Error") \
......@@ -85,12 +88,13 @@ namespace Http {
CODE(502, Bad_Gateway, "Bad Gateway") \
CODE(503, Service_Unavailable, "Service Unavailable") \
CODE(504, Gateway_Timeout, "Gateway Timeout") \
CODE(505, HTTP_Version_Not_Supported, "HTTP Version Not Supported")\
CODE(505, HTTP_Version_Not_Supported, "HTTP Version Not Supported") \
CODE(506, Variant_Also_Negotiates, "Variant Also Negotiates") \
CODE(507, Insufficient_Storage, "Insufficient Storage") \
CODE(508, Loop_Detected, "Loop Detected") \
CODE(510, Not_Extended, "Not Extended") \
CODE(511, Network_Authentication_Required, "Network Authentication Required")\
CODE(511, Network_Authentication_Required, \
"Network Authentication Required") \
CODE(599, Network_Connect_Timeout_Error, "Network Connect Timeout Error")
// 3.4. Character Sets
......@@ -98,27 +102,26 @@ namespace Http {
// http://www.iana.org/assignments/character-sets/character-sets.xhtml
#define CHARSETS \
CHARSET(UsAscii, "us-ascii") \
CHARSET(Iso-8859-1, "iso-8859-1") \
CHARSET(Iso-8859-2, "iso-8859-2") \
CHARSET(Iso-8859-3, "iso-8859-3") \
CHARSET(Iso-8859-4, "iso-8859-4") \
CHARSET(Iso-8859-5, "iso-8859-5") \
CHARSET(Iso-8859-6, "iso-8859-6") \
CHARSET(Iso-8859-7, "iso-8859-7") \
CHARSET(Iso-8859-8, "iso-8859-8") \
CHARSET(Iso-8859-9, "iso-8859-9") \
CHARSET(Iso-8859-10, "iso-8859-10") \
CHARSET(Shift-JIS, "shift_jis") \
CHARSET(Iso - 8859 - 1, "iso-8859-1") \
CHARSET(Iso - 8859 - 2, "iso-8859-2") \
CHARSET(Iso - 8859 - 3, "iso-8859-3") \
CHARSET(Iso - 8859 - 4, "iso-8859-4") \
CHARSET(Iso - 8859 - 5, "iso-8859-5") \
CHARSET(Iso - 8859 - 6, "iso-8859-6") \
CHARSET(Iso - 8859 - 7, "iso-8859-7") \
CHARSET(Iso - 8859 - 8, "iso-8859-8") \
CHARSET(Iso - 8859 - 9, "iso-8859-9") \
CHARSET(Iso - 8859 - 10, "iso-8859-10") \
CHARSET(Shift - JIS, "shift_jis") \
CHARSET(Utf7, "utf-7") \
CHARSET(Utf8, "utf-8") \
CHARSET(Utf16, "utf-16") \
CHARSET(Utf16-BE, "utf-16be") \
CHARSET(Utf16-LE, "utf-16le") \
CHARSET(Utf16 - BE, "utf-16be") \
CHARSET(Utf16 - LE, "utf-16le") \
CHARSET(Utf32, "utf-32") \
CHARSET(Utf32-BE, "utf-32be") \
CHARSET(Utf32-LE, "utf-32le") \
CHARSET(Unicode-11, "unicode-1-1")
CHARSET(Utf32 - BE, "utf-32be") \
CHARSET(Utf32 - LE, "utf-32le") \
CHARSET(Unicode - 11, "unicode-1-1")
enum class Method {
#define METHOD(m, _) m,
......@@ -137,28 +140,29 @@ enum class Version {
Http11 // HTTP/1.1
};
enum class ConnectionControl {
Close,
KeepAlive,
Ext
};
enum class ConnectionControl { Close, KeepAlive, Ext };
enum class Expectation {
Continue,
Ext
};
enum class Expectation { Continue, Ext };
class CacheDirective {
public:
enum Directive { NoCache, NoStore, MaxAge, MaxStale, MinFresh,
NoTransform, OnlyIfCached,
Public, Private, MustRevalidate, ProxyRevalidate, SMaxAge,
Ext };
enum Directive {
NoCache,
NoStore,
MaxAge,
MaxStale,
MinFresh,
NoTransform,
OnlyIfCached,
Public,
Private,
MustRevalidate,
ProxyRevalidate,
SMaxAge,
Ext
};
CacheDirective()
: directive_()
, data()
{ }
CacheDirective() : directive_(), data() {}
explicit CacheDirective(Directive directive);
CacheDirective(Directive directive, std::chrono::seconds delta);
......@@ -182,44 +186,36 @@ private:
class FullDate {
public:
using time_point = std::chrono::system_clock::time_point;
FullDate()
: date_()
{ }
enum class Type {
RFC1123,
RFC850,
AscTime
};
FullDate() : date_() {}
enum class Type { RFC1123, RFC850, AscTime };
explicit FullDate(time_point date)
: date_(date)
{ }
explicit FullDate(time_point date) : date_(date) {}
time_point date() const { return date_; }
void write(std::ostream& os, Type type = Type::RFC1123) const;
void write(std::ostream &os, Type type = Type::RFC1123) const;
static FullDate fromString(const std::string& str);
static FullDate fromString(const std::string &str);
private:
time_point date_;
};
const char* methodString(Method method);
const char* versionString(Version version);
const char* codeString(Code code);
const char *methodString(Method method);
const char *versionString(Version version);
const char *codeString(Code code);
std::ostream& operator<<(std::ostream& os, Version version);
std::ostream& operator<<(std::ostream& os, Method method);
std::ostream& operator<<(std::ostream& os, Code code);
std::ostream &operator<<(std::ostream &os, Version version);
std::ostream &operator<<(std::ostream &os, Method method);
std::ostream &operator<<(std::ostream &os, Code code);
struct HttpError : public std::exception {
HttpError(Code code, std::string reason);
HttpError(int code, std::string reason);
~HttpError() noexcept { }
~HttpError() noexcept {}
const char* what() const noexcept { return reason_.c_str(); }
const char *what() const noexcept { return reason_.c_str(); }
int code() const { return code_; }
std::string reason() const { return reason_; }
......@@ -234,8 +230,7 @@ private:
namespace std {
template<>
struct hash<Pistache::Http::Method> {
template <> struct hash<Pistache::Http::Method> {
size_t operator()(Pistache::Http::Method method) const {
return std::hash<int>()(static_cast<int>(method));
}
......
This diff is collapsed.
......@@ -6,11 +6,11 @@
#pragma once
#include <functional>
#include <algorithm>
#include <functional>
#include <memory>
#include <unordered_map>
#include <vector>
#include <memory>
#include <pistache/http_header.h>
#include <pistache/type_checkers.h>
......@@ -19,21 +19,21 @@ namespace Pistache {
namespace Http {
namespace Header {
std::string
toLowercase(std::string str);
std::string toLowercase(std::string str);
struct LowercaseHash {
size_t operator()(const std::string& key) const {
size_t operator()(const std::string &key) const {
return std::hash<std::string>{}(toLowercase(key));
}
};
bool LowercaseEqualStatic(const std::string& dynamic, const std::string& statik);
bool LowercaseEqualStatic(const std::string &dynamic,
const std::string &statik);
struct LowercaseEqual {
bool operator()(const std::string& left, const std::string& right) const {
bool operator()(const std::string &left, const std::string &right) const {
return std::equal(left.begin(), left.end(), right.begin(), right.end(),
[] (const char& a, const char& b) {
[](const char &a, const char &b) {
return std::tolower(a) == std::tolower(b);
});
};
......@@ -41,161 +41,127 @@ struct LowercaseEqual {
class Collection {
public:
Collection()
: headers()
, rawHeaders()
{ }
template<typename H>
typename std::enable_if<
IsHeader<H>::value, std::shared_ptr<const H>
>::type
Collection() : headers(), rawHeaders() {}
template <typename H>
typename std::enable_if<IsHeader<H>::value, std::shared_ptr<const H>>::type
get() const {
return std::static_pointer_cast<const H>(get(H::Name));
}
template<typename H>
typename std::enable_if<
IsHeader<H>::value, std::shared_ptr<H>
>::type
get() {
template <typename H>
typename std::enable_if<IsHeader<H>::value, std::shared_ptr<H>>::type get() {
return std::static_pointer_cast<H>(get(H::Name));
}
template<typename H>
typename std::enable_if<
IsHeader<H>::value, std::shared_ptr<const H>
>::type
template <typename H>
typename std::enable_if<IsHeader<H>::value, std::shared_ptr<const H>>::type
tryGet() const {
return std::static_pointer_cast<const H>(tryGet(H::Name));
}
template<typename H>
typename std::enable_if<
IsHeader<H>::value, std::shared_ptr<H>
>::type
template <typename H>
typename std::enable_if<IsHeader<H>::value, std::shared_ptr<H>>::type
tryGet() {
return std::static_pointer_cast<H>(tryGet(H::Name));
}
Collection& add(const std::shared_ptr<Header>& header);
Collection& addRaw(const Raw& raw);
Collection &add(const std::shared_ptr<Header> &header);
Collection &addRaw(const Raw &raw);
template<typename H, typename ...Args>
typename std::enable_if<
IsHeader<H>::value, Collection&
>::type
add(Args&& ...args) {
template <typename H, typename... Args>
typename std::enable_if<IsHeader<H>::value, Collection &>::type
add(Args &&... args) {
return add(std::make_shared<H>(std::forward<Args>(args)...));
}
template<typename H>
typename std::enable_if<
IsHeader<H>::value, bool
>::type
remove() {
template <typename H>
typename std::enable_if<IsHeader<H>::value, bool>::type remove() {
return remove(H::Name);
}
std::shared_ptr<const Header> get(const std::string& name) const;
std::shared_ptr<Header> get(const std::string& name);
Raw getRaw(const std::string& name) const;
std::shared_ptr<const Header> get(const std::string &name) const;
std::shared_ptr<Header> get(const std::string &name);
Raw getRaw(const std::string &name) const;
std::shared_ptr<const Header> tryGet(const std::string& name) const;
std::shared_ptr<Header> tryGet(const std::string& name);
Optional<Raw> tryGetRaw(const std::string& name) const;
std::shared_ptr<const Header> tryGet(const std::string &name) const;
std::shared_ptr<Header> tryGet(const std::string &name);
Optional<Raw> tryGetRaw(const std::string &name) const;
template<typename H>
typename std::enable_if<
IsHeader<H>::value, bool
>::type
has() const {
template <typename H>
typename std::enable_if<IsHeader<H>::value, bool>::type has() const {
return has(H::Name);
}
bool has(const std::string& name) const;
bool has(const std::string &name) const;
std::vector<std::shared_ptr<Header>> list() const;
const std::unordered_map<std::string,Raw,LowercaseHash,LowercaseEqual>& rawList() const {
const std::unordered_map<std::string, Raw, LowercaseHash, LowercaseEqual> &
rawList() const {
return rawHeaders;
}
bool remove(const std::string& name);
bool remove(const std::string &name);
void clear();
private:
std::pair<bool, std::shared_ptr<Header>> getImpl(const std::string& name) const;
std::unordered_map<
std::string,
std::shared_ptr<Header>,
LowercaseHash,
LowercaseEqual
> headers;
std::unordered_map<
std::string,
Raw,
LowercaseHash,
LowercaseEqual
> rawHeaders;
std::pair<bool, std::shared_ptr<Header>>
getImpl(const std::string &name) const;
std::unordered_map<std::string, std::shared_ptr<Header>, LowercaseHash,
LowercaseEqual>
headers;
std::unordered_map<std::string, Raw, LowercaseHash, LowercaseEqual>
rawHeaders;
};
class Registry {
public:
Registry(const Registry&) = delete;
Registry& operator=(const Registry&) = delete;
static Registry& instance();
Registry(const Registry &) = delete;
Registry &operator=(const Registry &) = delete;
static Registry &instance();
template<typename H, REQUIRES(IsHeader<H>::value)>
void registerHeader() {
template <typename H, REQUIRES(IsHeader<H>::value)> void registerHeader() {
registerHeader(H::Name, []() -> std::unique_ptr<Header> {
return std::unique_ptr<Header>(new H());
});
}
std::vector<std::string> headersList();
std::unique_ptr<Header> makeHeader(const std::string& name);
bool isRegistered(const std::string& name);
std::unique_ptr<Header> makeHeader(const std::string &name);
bool isRegistered(const std::string &name);
private:
Registry();
~Registry();
using RegistryFunc = std::function<std::unique_ptr<Header>()>;
using RegistryStorageType = std::unordered_map<
std::string,
RegistryFunc,
LowercaseHash,
LowercaseEqual>;
using RegistryStorageType = std::unordered_map<std::string, RegistryFunc,
LowercaseHash, LowercaseEqual>;
void registerHeader(const std::string& name, RegistryFunc func);
void registerHeader(const std::string &name, RegistryFunc func);
RegistryStorageType registry;
};
template<typename H>
struct Registrar {
template <typename H> struct Registrar {
static_assert(IsHeader<H>::value, "Registrar only works with header types");
Registrar() {
Registry::instance().registerHeader<H>();
}
Registrar() { Registry::instance().registerHeader<H>(); }
};
/* Crazy macro machinery to generate a unique variable name
* Don't touch it !
*/
#define CAT(a, b) CAT_I(a, b)
#define CAT_I(a, b) a ## b
#define CAT_I(a, b) a##b
#define UNIQUE_NAME(base) CAT(base, __LINE__)
#define RegisterHeader(Header) \
Registrar<Header> UNIQUE_NAME(CAT(CAT_I(__, Header), __))
} // namespace Header
} // namespace Http
} // namespace Pistache
......@@ -8,40 +8,31 @@
namespace Pistache {
template<typename Map>
struct FlatMapIteratorAdapter {
template <typename Map> struct FlatMapIteratorAdapter {
typedef typename Map::key_type Key;
typedef typename Map::mapped_type Value;
typedef typename Map::const_iterator const_iterator;
explicit FlatMapIteratorAdapter(const_iterator _it)
: it(_it)
{ }
explicit FlatMapIteratorAdapter(const_iterator _it) : it(_it) {}
FlatMapIteratorAdapter& operator++() {
FlatMapIteratorAdapter &operator++() {
++it;
return *this;
}
const Value& operator*() {
return it->second;
}
const Value &operator*() { return it->second; }
bool operator==(FlatMapIteratorAdapter other) {
return other.it == it;
}
bool operator==(FlatMapIteratorAdapter other) { return other.it == it; }
bool operator!=(FlatMapIteratorAdapter other) {
return !(*this == other);
}
bool operator!=(FlatMapIteratorAdapter other) { return !(*this == other); }
private:
const_iterator it;
};
template<typename Map>
template <typename Map>
FlatMapIteratorAdapter<Map>
makeFlatMapIterator(const Map&, typename Map::const_iterator it) {
makeFlatMapIterator(const Map &, typename Map::const_iterator it) {
return FlatMapIteratorAdapter<Map>(it);
}
......
......@@ -6,20 +6,19 @@
#pragma once
#include <pistache/tcp.h>
#include <pistache/async.h>
#include <pistache/config.h>
#include <pistache/flags.h>
#include <pistache/net.h>
#include <pistache/os.h>
#include <pistache/flags.h>
#include <pistache/async.h>
#include <pistache/reactor.h>
#include <pistache/config.h>
#include <pistache/tcp.h>
#include <sys/resource.h>
#include <vector>
#include <memory>
#include <thread>
#include <vector>
#ifdef PISTACHE_USE_SSL
#include <openssl/ssl.h>
......@@ -35,7 +34,6 @@ void setSocketOptions(Fd fd, Flags<Options> options);
class Listener {
public:
struct Load {
using TimePoint = std::chrono::system_clock::time_point;
double global;
......@@ -48,16 +46,15 @@ public:
Listener();
~Listener();
explicit Listener(const Address& address);
void init(
size_t workers,
explicit Listener(const Address &address);
void init(size_t workers,
Flags<Options> options = Flags<Options>(Options::None),
const std::string& workersName = "",
const std::string &workersName = "",
int backlog = Const::MaxBacklog);
void setHandler(const std::shared_ptr<Handler>& handler);
void setHandler(const std::shared_ptr<Handler> &handler);
void bind();
void bind(const Address& address);
void bind(const Address &address);
bool isBound() const;
Port getPort() const;
......@@ -67,15 +64,17 @@ public:
void shutdown();
Async::Promise<Load> requestLoad(const Load& old);
Async::Promise<Load> requestLoad(const Load &old);
Options options() const;
Address address() const;
void pinWorker(size_t worker, const CpuSet& set);
void pinWorker(size_t worker, const CpuSet &set);
void setupSSL(const std::string &cert_path, const std::string &key_path, bool use_compression);
void setupSSLAuth(const std::string &ca_file, const std::string &ca_path, int (*cb)(int, void *));
void setupSSL(const std::string &cert_path, const std::string &key_path,
bool use_compression);
void setupSSLAuth(const std::string &ca_file, const std::string &ca_path,
int (*cb)(int, void *));
private:
Address addr_;
......@@ -95,8 +94,8 @@ private:
Aio::Reactor::Key transportKey;
void handleNewConnection();
int acceptConnection(struct sockaddr_in& peer_addr) const;
void dispatchPeer(const std::shared_ptr<Peer>& peer);
int acceptConnection(struct sockaddr_in &peer_addr) const;
void dispatchPeer(const std::shared_ptr<Peer> &peer);
bool useSSL_;
void *ssl_ctx_;
......
This diff is collapsed.
......@@ -6,11 +6,11 @@
#pragma once
#include <string>
#include <unordered_map>
#include <stdexcept>
#include <cassert>
#include <cmath>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <pistache/optional.h>
......@@ -19,30 +19,30 @@ namespace Http {
namespace Mime {
#define MIME_TYPES \
TYPE(Star , "*") \
TYPE(Text , "text") \
TYPE(Image , "image") \
TYPE(Audio , "audio") \
TYPE(Video , "video") \
TYPE(Star, "*") \
TYPE(Text, "text") \
TYPE(Image, "image") \
TYPE(Audio, "audio") \
TYPE(Video, "video") \
TYPE(Application, "application") \
TYPE(Message , "message") \
TYPE(Multipart , "multipart")
TYPE(Message, "message") \
TYPE(Multipart, "multipart")
#define MIME_SUBTYPES \
SUB_TYPE(Star , "*") \
SUB_TYPE(Plain , "plain") \
SUB_TYPE(Html , "html") \
SUB_TYPE(Xhtml , "xhtml") \
SUB_TYPE(Xml , "xml") \
SUB_TYPE(Star, "*") \
SUB_TYPE(Plain, "plain") \
SUB_TYPE(Html, "html") \
SUB_TYPE(Xhtml, "xhtml") \
SUB_TYPE(Xml, "xml") \
SUB_TYPE(Javascript, "javascript") \
SUB_TYPE(Css , "css") \
SUB_TYPE(Css, "css") \
\
SUB_TYPE(OctetStream , "octet-stream") \
SUB_TYPE(Json , "json") \
SUB_TYPE(JsonSchema , "schema+json") \
SUB_TYPE(JsonSchemaInstance , "schema-instance+json") \
SUB_TYPE(FormUrlEncoded , "x-www-form-urlencoded") \
SUB_TYPE(FormData , "form-data") \
SUB_TYPE(OctetStream, "octet-stream") \
SUB_TYPE(Json, "json") \
SUB_TYPE(JsonSchema, "schema+json") \
SUB_TYPE(JsonSchemaInstance, "schema-instance+json") \
SUB_TYPE(FormUrlEncoded, "x-www-form-urlencoded") \
SUB_TYPE(FormData, "form-data") \
\
SUB_TYPE(Png, "png") \
SUB_TYPE(Gif, "gif") \
......@@ -50,14 +50,13 @@ namespace Mime {
SUB_TYPE(Jpeg, "jpeg")
#define MIME_SUFFIXES \
SUFFIX(Json , "json" , "JavaScript Object Notation") \
SUFFIX(Ber , "ber" , "Basic Encoding Rules") \
SUFFIX(Der , "der" , "Distinguished Encoding Rules") \
SUFFIX(Json, "json", "JavaScript Object Notation") \
SUFFIX(Ber, "ber", "Basic Encoding Rules") \
SUFFIX(Der, "der", "Distinguished Encoding Rules") \
SUFFIX(Fastinfoset, "fastinfoset", "Fast Infoset") \
SUFFIX(Wbxml , "wbxml" , "WAP Binary XML") \
SUFFIX(Zip , "zip" , "ZIP file storage") \
SUFFIX(Xml , "xml" , "Extensible Markup Language")
SUFFIX(Wbxml, "wbxml", "WAP Binary XML") \
SUFFIX(Zip, "zip", "ZIP file storage") \
SUFFIX(Xml, "xml", "Extensible Markup Language")
enum class Type {
#define TYPE(val, _) val,
......@@ -86,16 +85,14 @@ enum class Suffix {
// 3.9 Quality Values
class Q {
public:
// typedef uint8_t Type;
typedef uint16_t Type;
explicit Q(Type val)
: val_()
{
explicit Q(Type val) : val_() {
if (val > 100) {
throw std::runtime_error("Invalid quality value, must be in the [0; 100] range");
throw std::runtime_error(
"Invalid quality value, must be in the [0; 100] range");
}
val_ = val;
......@@ -114,9 +111,7 @@ private:
Type val_;
};
inline bool operator==(Q lhs, Q rhs) {
return lhs.value() == rhs.value();
}
inline bool operator==(Q lhs, Q rhs) { return lhs.value() == rhs.value(); }
// 3.7 Media Types
class MediaType {
......@@ -124,85 +119,53 @@ public:
enum Parse { DoParse, DontParse };
MediaType()
: top_(Type::None)
, sub_(Subtype::None)
, suffix_(Suffix::None)
, raw_()
, rawSubIndex()
, rawSuffixIndex()
, params()
, q_()
{ }
: top_(Type::None), sub_(Subtype::None), suffix_(Suffix::None), raw_(),
rawSubIndex(), rawSuffixIndex(), params(), q_() {}
explicit MediaType(std::string raw, Parse parse = DontParse)
: top_(Type::None)
, sub_(Subtype::None)
, suffix_(Suffix::None)
, raw_()
, rawSubIndex()
, rawSuffixIndex()
, params()
, q_()
{
: top_(Type::None), sub_(Subtype::None), suffix_(Suffix::None), raw_(),
rawSubIndex(), rawSuffixIndex(), params(), q_() {
if (parse == DoParse) {
parseRaw(raw.c_str(), raw.length());
}
else {
} else {
raw_ = std::move(raw);
}
}
MediaType(Mime::Type top, Mime::Subtype sub)
: top_(top)
, sub_(sub)
, suffix_(Suffix::None)
, raw_()
, rawSubIndex()
, rawSuffixIndex()
, params()
, q_()
{ }
: top_(top), sub_(sub), suffix_(Suffix::None), raw_(), rawSubIndex(),
rawSuffixIndex(), params(), q_() {}
MediaType(Mime::Type top, Mime::Subtype sub, Mime::Suffix suffix)
: top_(top)
, sub_(sub)
, suffix_(suffix)
, raw_()
, rawSubIndex()
, rawSuffixIndex()
, params()
, q_()
{ }
: top_(top), sub_(sub), suffix_(suffix), raw_(), rawSubIndex(),
rawSuffixIndex(), params(), q_() {}
void parseRaw(const char* str, size_t len);
static MediaType fromRaw(const char* str, size_t len);
void parseRaw(const char *str, size_t len);
static MediaType fromRaw(const char *str, size_t len);
static MediaType fromString(const std::string& str);
static MediaType fromString(std::string&& str);
static MediaType fromString(const std::string &str);
static MediaType fromString(std::string &&str);
static MediaType fromFile(const char* fileName);
static MediaType fromFile(const char *fileName);
Mime::Type top() const { return top_; }
Mime::Subtype sub() const { return sub_; }
Mime::Suffix suffix() const { return suffix_; }
std::string rawSub() const {
return rawSubIndex.splice(raw_);
}
std::string rawSub() const { return rawSubIndex.splice(raw_); }
std::string raw() const { return raw_; }
const Optional<Q>& q() const { return q_; }
const Optional<Q> &q() const { return q_; }
void setQuality(Q quality);
Optional<std::string> getParam(const std::string& name) const;
void setParam(const std::string& name, std::string value);
Optional<std::string> getParam(const std::string &name) const;
void setParam(const std::string &name, std::string value);
std::string toString() const;
bool isValid() const;
private:
private:
Mime::Type top_;
Mime::Subtype sub_;
Mime::Suffix suffix_;
......@@ -217,7 +180,7 @@ private:
size_t beg;
size_t end;
std::string splice(const std::string& str) const {
std::string splice(const std::string &str) const {
assert(end >= beg);
return str.substr(beg, end - beg + 1);
}
......@@ -231,13 +194,12 @@ private:
Optional<Q> q_;
};
inline bool operator==(const MediaType& lhs, const MediaType& rhs) {
return lhs.top() == rhs.top() &&
lhs.sub() == rhs.sub() &&
inline bool operator==(const MediaType &lhs, const MediaType &rhs) {
return lhs.top() == rhs.top() && lhs.sub() == rhs.sub() &&
lhs.suffix() == rhs.suffix();
}
inline bool operator!=(const MediaType& lhs, const MediaType& rhs){
inline bool operator!=(const MediaType &lhs, const MediaType &rhs) {
return !operator==(lhs, rhs);
}
......@@ -246,7 +208,10 @@ inline bool operator!=(const MediaType& lhs, const MediaType& rhs){
} // namespace Pistache
#define MIME(top, sub) \
Pistache::Http::Mime::MediaType(Pistache::Http::Mime::Type::top, Pistache::Http::Mime::Subtype::sub)
Pistache::Http::Mime::MediaType(Pistache::Http::Mime::Type::top, \
Pistache::Http::Mime::Subtype::sub)
#define MIME3(top, sub, suffix) \
Pistache::Http::Mime::MediaType(Pistache::Http::Mime::Type::top, Pistache::Http::Mime::Subtype::sub, Pistache::Http::Mime::Suffix::suffix)
Pistache::Http::Mime::MediaType(Pistache::Http::Mime::Type::top, \
Pistache::Http::Mime::Subtype::sub, \
Pistache::Http::Mime::Suffix::suffix)
......@@ -6,14 +6,14 @@
#pragma once
#include <string>
#include <cstring>
#include <stdexcept>
#include <limits>
#include <stdexcept>
#include <string>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#ifndef _KERNEL_FASTOPEN
#define _KERNEL_FASTOPEN
......@@ -31,7 +31,7 @@ class AddrInfo {
public:
// Disable copy and assign.
AddrInfo(const AddrInfo &) = delete;
AddrInfo& operator=(const AddrInfo &) = delete;
AddrInfo &operator=(const AddrInfo &) = delete;
// Default construction: do nothing.
AddrInfo() : addrs(nullptr) {}
......@@ -54,9 +54,7 @@ public:
return ::getaddrinfo(node, service, hints, &addrs);
}
const struct addrinfo *get_info_ptr() const {
return addrs;
}
const struct addrinfo *get_info_ptr() const { return addrs; }
private:
struct addrinfo *addrs;
......@@ -65,7 +63,7 @@ private:
class Port {
public:
Port(uint16_t port = 0);
explicit Port(const std::string& data);
explicit Port(const std::string &data);
operator uint16_t() const { return port; }
......@@ -84,7 +82,6 @@ private:
uint16_t port;
};
class IP {
private:
int port;
......@@ -93,10 +90,12 @@ private:
struct sockaddr_in addr;
struct sockaddr_in6 addr6;
};
public:
IP();
IP(uint8_t a, uint8_t b, uint8_t c, uint8_t d);
IP(uint16_t a, uint16_t b, uint16_t c, uint16_t d, uint16_t e, uint16_t f, uint16_t g, uint16_t h);
IP(uint16_t a, uint16_t b, uint16_t c, uint16_t d, uint16_t e, uint16_t f,
uint16_t g, uint16_t h);
explicit IP(struct sockaddr *);
static IP any();
static IP loopback();
......@@ -105,44 +104,42 @@ public:
int getFamily() const;
int getPort() const;
std::string toString() const;
void toNetwork(in_addr_t*) const;
void toNetwork(struct in6_addr*) const;
void toNetwork(in_addr_t *) const;
void toNetwork(struct in6_addr *) const;
// Returns 'true' if the system has IPV6 support, false if not.
static bool supported();
};
using Ipv4 = IP;
using Ipv6 = IP;
class AddressParser {
public:
explicit AddressParser(const std::string& data);
const std::string& rawHost() const;
const std::string& rawPort() const;
explicit AddressParser(const std::string &data);
const std::string &rawHost() const;
const std::string &rawPort() const;
bool hasColon() const;
int family() const;
private:
private:
std::string host_;
std::string port_;
bool hasColon_ = false;
int family_ = 0;
};
class Address {
public:
Address();
Address(std::string host, Port port);
explicit Address(std::string addr);
explicit Address(const char* addr);
explicit Address(const char *addr);
Address(IP ip, Port port);
Address(const Address& other) = default;
Address(Address&& other) = default;
Address(const Address &other) = default;
Address(Address &&other) = default;
Address &operator=(const Address& other) = default;
Address &operator=(Address&& other) = default;
Address &operator=(const Address &other) = default;
Address &operator=(Address &&other) = default;
static Address fromUnix(struct sockaddr *addr);
......@@ -151,24 +148,21 @@ public:
int family() const;
private:
void init(const std::string& addr);
void init(const std::string &addr);
IP ip_;
Port port_;
};
class Error : public std::runtime_error {
public:
explicit Error(const char* message);
explicit Error(const char *message);
explicit Error(std::string message);
static Error system(const char* message);
static Error system(const char *message);
};
template<typename T>
struct Size { };
template <typename T> struct Size {};
template<typename T>
size_t
digitsCount(T val) {
template <typename T> size_t digitsCount(T val) {
size_t digits = 0;
while (val % 10) {
++digits;
......@@ -179,15 +173,11 @@ digitsCount(T val) {
return digits;
}
template<>
struct Size<const char*> {
size_t operator()(const char *s) const {
return std::strlen(s);
}
template <> struct Size<const char *> {
size_t operator()(const char *s) const { return std::strlen(s); }
};
template<size_t N>
struct Size<char[N]> {
template <size_t N> struct Size<char[N]> {
constexpr size_t operator()(const char (&)[N]) const {
// We omit the \0
......@@ -196,11 +186,8 @@ struct Size<char[N]> {
};
#define DEFINE_INTEGRAL_SIZE(Int) \
template<> \
struct Size<Int> { \
size_t operator()(Int val) const { \
return digitsCount(val); \
} \
template <> struct Size<Int> { \
size_t operator()(Int val) const { return digitsCount(val); } \
}
DEFINE_INTEGRAL_SIZE(uint8_t);
......@@ -212,18 +199,12 @@ DEFINE_INTEGRAL_SIZE(int32_t);
DEFINE_INTEGRAL_SIZE(uint64_t);
DEFINE_INTEGRAL_SIZE(int64_t);
template<>
struct Size<bool> {
constexpr size_t operator()(bool) const {
return 1;
}
template <> struct Size<bool> {
constexpr size_t operator()(bool) const { return 1; }
};
template<>
struct Size<char> {
constexpr size_t operator()(char) const {
return 1;
}
template <> struct Size<char> {
constexpr size_t operator()(char) const { return 1; }
};
} // namespace Pistache
This diff is collapsed.
......@@ -6,18 +6,17 @@
#pragma once
#include <pistache/config.h>
#include <pistache/common.h>
#include <pistache/config.h>
#include <pistache/flags.h>
#include <memory>
#include <bitset>
#include <chrono>
#include <memory>
#include <vector>
#include <bitset>
#include <sched.h>
namespace Pistache {
using Fd = int;
......@@ -33,14 +32,14 @@ public:
explicit CpuSet(std::initializer_list<size_t> cpus);
void clear();
CpuSet& set(size_t cpu);
CpuSet& unset(size_t cpu);
CpuSet &set(size_t cpu);
CpuSet &unset(size_t cpu);
CpuSet& set(std::initializer_list<size_t> cpus);
CpuSet& unset(std::initializer_list<size_t> cpus);
CpuSet &set(std::initializer_list<size_t> cpus);
CpuSet &unset(std::initializer_list<size_t> cpus);
CpuSet& setRange(size_t begin, size_t end);
CpuSet& unsetRange(size_t begin, size_t end);
CpuSet &setRange(size_t begin, size_t end);
CpuSet &unsetRange(size_t begin, size_t end);
bool isSet(size_t cpu) const;
size_t count() const;
......@@ -53,10 +52,7 @@ private:
namespace Polling {
enum class Mode {
Level,
Edge
};
enum class Mode { Level, Edge };
enum class NotifyOn {
None = 0,
......@@ -72,9 +68,7 @@ DECLARE_FLAGS_OPERATORS(NotifyOn)
struct Tag {
friend class Epoll;
explicit constexpr Tag(uint64_t value)
: value_(value)
{ }
explicit constexpr Tag(uint64_t value) : value_(value) {}
constexpr uint64_t value() const { return value_; }
......@@ -102,16 +96,18 @@ public:
~Epoll();
void addFd(Fd fd, Flags<NotifyOn> interest, Tag tag, Mode mode = Mode::Level);
void addFdOneShot(Fd fd, Flags<NotifyOn> interest, Tag tag, Mode mode = Mode::Level);
void addFdOneShot(Fd fd, Flags<NotifyOn> interest, Tag tag,
Mode mode = Mode::Level);
void removeFd(Fd fd);
void rearmFd(Fd fd, Flags<NotifyOn> interest, Tag tag, Mode mode = Mode::Level);
void rearmFd(Fd fd, Flags<NotifyOn> interest, Tag tag,
Mode mode = Mode::Level);
int poll(std::vector<Event>& events,
const std::chrono::milliseconds timeout = std::chrono::milliseconds(-1)) const;
int poll(std::vector<Event> &events, const std::chrono::milliseconds timeout =
std::chrono::milliseconds(-1)) const;
private:
static int toEpollEvents(const Flags<NotifyOn>& interest);
static int toEpollEvents(const Flags<NotifyOn> &interest);
static Flags<NotifyOn> toNotifyOn(int events);
Fd epoll_fd;
};
......@@ -122,7 +118,7 @@ class NotifyFd {
public:
NotifyFd();
Polling::Tag bind(Polling::Epoll& poller);
Polling::Tag bind(Polling::Epoll &poller);
bool isBound() const;
......
This diff is collapsed.
......@@ -6,14 +6,13 @@
#pragma once
#include <type_traits>
#include <memory>
#include <type_traits>
namespace Pistache {
/* In a sense, a Prototype is just a class that provides a clone() method */
template<typename Class>
struct Prototype {
template <typename Class> struct Prototype {
virtual ~Prototype() {}
virtual std::shared_ptr<Class> clone() const = 0;
};
......@@ -25,4 +24,5 @@ private: \
std::shared_ptr<Base> clone() const override { \
return std::make_shared<Class>(*this); \
} \
\
public:
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -4,4 +4,4 @@ namespace Pistache {
#define REQUIRES(condition) typename std::enable_if<(condition), int>::type = 0
}
\ No newline at end of file
} // namespace Pistache
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment