EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PyServiceCD.h
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Zhur
24 */
25 
26 #ifndef __PYSERVICECD_H_INCL__
27 #define __PYSERVICECD_H_INCL__
28 
29 #include "Client.h"
30 #include "PyCallable.h"
31 
32 /*
33  * This whole concept exists to allow the generic PyService to make a
34  * call to the specific `Svc` object with ->*
35  */
36 template <class Svc>
39 {
40  typedef PyResult (Svc::*CallProc)(PyCallArgs &call);
41  typedef typename std::map<std::string, CallProc>::iterator mapitr;
42 public:
43  PyCallableDispatcher(Svc *parent)
44  : m_parent(parent) {
45  }
46 
48  }
49 
50  void RegisterCall(const char *call_name, CallProc p) {
51  m_serviceCalls[call_name] = p;
52  }
53 
54  //CallDispatcher interface:
55  virtual PyResult Dispatch(const std::string &method_name, PyCallArgs &call) {
56  //this could be done a lot more efficiently with a custom data structure IF NEEDED
57  mapitr res = m_serviceCalls.find(method_name);
58  if (res == m_serviceCalls.end()) {
59  sLog.Error("Server","Unknown call to '%s' by '%s'", method_name.c_str(), call.client->GetName());
60  // list registered calls for named service
61  //if (is_log_enabled(SERVICE__WARNING))
62  // for (auto cur : m_serviceCalls)
63  // _log(SERVICE__WARNING, " %s", cur.first.c_str());
64  return nullptr;
65  }
66 
67  CallProc p = res->second;
68  return (m_parent->*p)(call);
69  }
70 
71 protected: //_MAY_ consume args
72  std::map<std::string, CallProc> m_serviceCalls;
73 
74  Svc *const m_parent; //we do not own this pointer
75 };
76 
77 //convenience macro, you do not HAVE to use this
78 #define PyCallable_REG_CALL(c,m) m_dispatch->RegisterCall(#m, &c::Handle_##m);
79 
80 //macro of a template... nice.
81 #define PyCallable_Make_Dispatcher(objname) \
82  class Dispatcher : public PyCallableDispatcher<objname> { \
83  public: \
84  Dispatcher(objname *c) : PyCallableDispatcher<objname>(c) {} \
85  };
86 
87 #define PyCallable_Make_InnerDispatcher(objname) \
88  class objname::Dispatcher \
89  : public PyCallableDispatcher<objname> { \
90  public: \
91  Dispatcher(objname *c) \
92  : PyCallableDispatcher<objname>(c) {} \
93  };
94 
95 #endif // __PYSERVICECD_H_INCL__
PyResult(Svc::* CallProc)(PyCallArgs &call)
Definition: PyServiceCD.h:40
PyCallableDispatcher(Svc *parent)
Definition: PyServiceCD.h:43
virtual PyResult Dispatch(const std::string &method_name, PyCallArgs &call)
Definition: PyServiceCD.h:55
virtual ~PyCallableDispatcher()
Definition: PyServiceCD.h:47
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
Svc *const m_parent
Definition: PyServiceCD.h:74
const char * GetName() const
Definition: Client.h:94
Client *const client
Definition: PyCallable.h:49
void RegisterCall(const char *call_name, CallProc p)
Definition: PyServiceCD.h:50
std::map< std::string, CallProc >::iterator mapitr
Definition: PyServiceCD.h:41
std::map< std::string, CallProc > m_serviceCalls
Definition: PyServiceCD.h:72