//////////////////////////////////////////////////////////////////////////////// /// @brief abstract base class for http request handlers /// /// @file /// /// DISCLAIMER /// /// Copyright 2014 ArangoDB GmbH, Cologne, Germany /// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany /// /// Licensed under the Apache License, Version 2.0 (the "License"); /// you may not use this file except in compliance with the License. /// You may obtain a copy of the License at /// /// http://www.apache.org/licenses/LICENSE-2.0 /// /// Unless required by applicable law or agreed to in writing, software /// distributed under the License is distributed on an "AS IS" BASIS, /// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. /// See the License for the specific language governing permissions and /// limitations under the License. /// /// Copyright holder is ArangoDB GmbH, Cologne, Germany /// /// @author Richard Bruch /// @author Copyright 2014, ArangoDB GmbH, Cologne, Germany /// @author Copyright 2011-2013, triAGENS GmbH, Cologne, Germany //////////////////////////////////////////////////////////////////////////////// #ifndef ARANGODB_BASICS_FUNCTOR_H #define ARANGODB_BASICS_FUNCTOR_H 1 #include "Basics/Common.h" namespace triagens { namespace basics { //////////////////////////////////////////////////////////////////////////////// /// @brief null type //////////////////////////////////////////////////////////////////////////////// enum NullType { }; //////////////////////////////////////////////////////////////////////////////// /// @brief abstract base class for functors //////////////////////////////////////////////////////////////////////////////// template struct FunctionBase { virtual R call (P1 p1, P2 p2) = 0; virtual FunctionBase* clone () = 0; virtual ~FunctionBase () {} }; //////////////////////////////////////////////////////////////////////////////// /// @brief abstract base class for functors //////////////////////////////////////////////////////////////////////////////// template struct FunctionBase { virtual R call () = 0; virtual FunctionBase* clone () = 0; virtual ~FunctionBase () {} }; //////////////////////////////////////////////////////////////////////////////// /// @brief abstract base class for functors //////////////////////////////////////////////////////////////////////////////// template struct FunctionBase { virtual R call (P1 p1) = 0; virtual FunctionBase* clone () = 0; virtual ~FunctionBase () {} }; //////////////////////////////////////////////////////////////////////////////// /// @brief internal function //////////////////////////////////////////////////////////////////////////////// template struct InternalFunction : public FunctionBase { typedef FunctionBase BaseType; T& _ref; F _ptr; InternalFunction (T& ref, F ptr) : _ref(ref), _ptr(ptr) { } }; //////////////////////////////////////////////////////////////////////////////// /// @brief call with 0 arguments //////////////////////////////////////////////////////////////////////////////// template struct Call0 { typedef R (T::*CALL) (); }; //////////////////////////////////////////////////////////////////////////////// /// @brief call with 1 argument //////////////////////////////////////////////////////////////////////////////// template struct Call1 { typedef R (T::*CALL) (P1); }; //////////////////////////////////////////////////////////////////////////////// /// @brief call with 2 arguments //////////////////////////////////////////////////////////////////////////////// template struct Call2 { typedef R (T::*CALL) (P1, P2); }; //////////////////////////////////////////////////////////////////////////////// /// @brief function with 0 arguments //////////////////////////////////////////////////////////////////////////////// template struct Function0 : public InternalFunction::CALL, R> { typedef typename Call0::CALL CallType; typedef InternalFunction BaseType; Function0 (T& ref, CallType call) : BaseType(ref, call) { } virtual typename BaseType::BaseType* clone () { return new Function0(BaseType::_ref, BaseType::_ptr); } virtual R call () { return (BaseType::_ref.*BaseType::_ptr)(); } }; //////////////////////////////////////////////////////////////////////////////// /// @brief function with 1 argument //////////////////////////////////////////////////////////////////////////////// template struct Function1 : public InternalFunction::CALL, R, P1> { typedef typename Call1::CALL CallType; typedef InternalFunction BaseType; Function1 (T& ref, CallType call) : BaseType(ref, call) { } virtual typename BaseType::BaseType* clone () { return new Function1(BaseType::_ref, BaseType::_ptr); } virtual R call (P1 p1) { return (BaseType::_ref.*BaseType::_ptr)(p1); } }; //////////////////////////////////////////////////////////////////////////////// /// @brief function with 2 arguments //////////////////////////////////////////////////////////////////////////////// template struct Function2 : public InternalFunction::CALL, R, P1, P2> { typedef typename Call2::CALL CallType; typedef InternalFunction BaseType; Function2 (T& ref, CallType call) : BaseType(ref, call) { } virtual typename BaseType::BaseType* clone () { return new Function2(BaseType::_ref, BaseType::_ptr); } virtual R call (P1 p1, P2 p2) { return (BaseType::_ref.*BaseType::_ptr)(p1, p2); } }; //////////////////////////////////////////////////////////////////////////////// /// @brief functor //////////////////////////////////////////////////////////////////////////////// template class Functor { public: //////////////////////////////////////////////////////////////////////////////// /// @brief constructor //////////////////////////////////////////////////////////////////////////////// Functor () : _function(0) { } //////////////////////////////////////////////////////////////////////////////// /// @brief constructor //////////////////////////////////////////////////////////////////////////////// template Functor (T& obj, R(T::*fptr) ()) { _function = new Function0(obj, fptr); } //////////////////////////////////////////////////////////////////////////////// /// @brief constructor //////////////////////////////////////////////////////////////////////////////// template Functor (T& obj, R(T::*fptr) (P1)) { _function = new Function1(obj, fptr); } //////////////////////////////////////////////////////////////////////////////// /// @brief constructor //////////////////////////////////////////////////////////////////////////////// template Functor (T& obj, R(T::*fptr) (P1, P2)) { _function = new Function2(obj, fptr); } //////////////////////////////////////////////////////////////////////////////// /// @brief copy constructor //////////////////////////////////////////////////////////////////////////////// Functor (const Functor& src) : _function(0) { copy(*this, src); } //////////////////////////////////////////////////////////////////////////////// /// @brief destructor //////////////////////////////////////////////////////////////////////////////// ~Functor () { if (_function) { delete _function; } } //////////////////////////////////////////////////////////////////////////////// /// @brief assigment //////////////////////////////////////////////////////////////////////////////// Functor& operator= (const Functor& src) { copy(*this, src); return *this; } public: //////////////////////////////////////////////////////////////////////////////// /// @brief call with no arguments //////////////////////////////////////////////////////////////////////////////// void operator() () { if (_function) _function->call(); } //////////////////////////////////////////////////////////////////////////////// /// @brief call with 1 argument //////////////////////////////////////////////////////////////////////////////// void operator() (P1 p1) { if (_function) { _function->call(p1); } } //////////////////////////////////////////////////////////////////////////////// /// @brief call with 2 argument //////////////////////////////////////////////////////////////////////////////// void operator() (P1 p1, P2 p2) { if (_function) { _function->call(p1, p2); } } private: FunctionBase* _function; friend void copy (Functor& dst, const Functor& src) { if (dst._function) { delete dst._function; } if (src._function) { dst._function = src._function->clone(); } else { dst._function = 0; } } }; typedef Functor Command; } } #endif // ----------------------------------------------------------------------------- // --SECTION-- END-OF-FILE // ----------------------------------------------------------------------------- // Local Variables: // mode: outline-minor // outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @page\\|// --SECTION--\\|/// @\\}" // End: