/** * * AOPAdvice.h * An Tao * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license * that can be found in the License file. * * Drogon * */ #pragma once #include "impl_forwards.h" #include #include #include #include namespace drogon { class AopAdvice { public: static AopAdvice &instance() { static AopAdvice inst; return inst; } // Getters? bool hasPreRoutingAdvices() const { return !preRoutingAdvices_.empty(); } bool hasPostRoutingAdvices() const { return !postRoutingAdvices_.empty(); } bool hasPreHandlingAdvices() const { return !preHandlingAdvices_.empty(); } // Setters? void registerNewConnectionAdvice( std::function advice) { newConnectionAdvices_.emplace_back(std::move(advice)); } void registerHttpResponseCreationAdvice( std::function advice) { responseCreationAdvices_.emplace_back(std::move(advice)); } void registerSyncAdvice( std::function advice) { syncAdvices_.emplace_back(std::move(advice)); } void registerPreRoutingObserver( std::function advice) { preRoutingObservers_.emplace_back(std::move(advice)); } void registerPreRoutingAdvice( std::function advice) { preRoutingAdvices_.emplace_back(std::move(advice)); } void registerPostRoutingObserver( std::function advice) { postRoutingObservers_.emplace_back(std::move(advice)); } void registerPostRoutingAdvice( std::function advice) { postRoutingAdvices_.emplace_back(std::move(advice)); } void registerPreHandlingObserver( std::function advice) { preHandlingObservers_.emplace_back(std::move(advice)); } void registerPreHandlingAdvice( std::function advice) { preHandlingAdvices_.emplace_back(std::move(advice)); } void registerPostHandlingAdvice( std::function advice) { postHandlingAdvices_.emplace_back(std::move(advice)); } void registerPreSendingAdvice( std::function advice) { preSendingAdvices_.emplace_back(std::move(advice)); } // Executors bool passNewConnectionAdvices(const trantor::TcpConnectionPtr &conn) const; void passResponseCreationAdvices(const HttpResponsePtr &resp) const; HttpResponsePtr passSyncAdvices(const HttpRequestPtr &req) const; void passPreRoutingObservers(const HttpRequestImplPtr &req) const; void passPreRoutingAdvices( const HttpRequestImplPtr &req, std::function &&callback) const; void passPostRoutingObservers(const HttpRequestImplPtr &req) const; void passPostRoutingAdvices( const HttpRequestImplPtr &req, std::function &&callback) const; void passPreHandlingObservers(const HttpRequestImplPtr &req) const; void passPreHandlingAdvices( const HttpRequestImplPtr &req, std::function &&callback) const; void passPostHandlingAdvices(const HttpRequestImplPtr &req, const HttpResponsePtr &resp) const; void passPreSendingAdvices(const HttpRequestImplPtr &req, const HttpResponsePtr &resp) const; private: using SyncAdvice = std::function; using SyncReqObserver = std::function; using SyncObserver = std::function; using AsyncAdvice = std::function; // If we want to add aop functions anytime, we can add a mutex here std::vector> newConnectionAdvices_; std::vector> responseCreationAdvices_; std::vector syncAdvices_; std::vector preRoutingObservers_; std::vector preRoutingAdvices_; std::vector postRoutingObservers_; std::vector postRoutingAdvices_; std::vector preHandlingObservers_; std::vector preHandlingAdvices_; std::vector postHandlingAdvices_; std::vector preSendingAdvices_; }; } // namespace drogon