LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
versionactionmapper.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2013 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #ifndef UTIL_VERSIONACTIONMAPPER_H
31 #define UTIL_VERSIONACTIONMAPPER_H
32 #include <functional>
33 #include <QDataStream>
34 #include <QMap>
35 
36 namespace LeechCraft
37 {
38  namespace Util
39  {
40  template<typename ActionType, typename VerType = quint8>
42  {
43  public:
44  typedef std::function<bool (QDataStream&)> Functor_t;
45  private:
46  typedef QMap<ActionType, Functor_t> Action2Functor_t;
47  typedef QMap<VerType, Action2Functor_t> Version2Functors_t;
48  Version2Functors_t Functors_;
49  public:
50  struct Simple
51  {
53 
55  : F_ (f)
56  {
57  }
58 
59  bool operator() (QDataStream& ds)
60  {
61  F_ (ds);
62  return true;
63  }
64  };
65 
67  {
68  }
69 
70  void AddFunctor (VerType version, ActionType action,
71  Functor_t functor)
72  {
73  Functors_ [version] [action] = functor;
74  }
75 
76  bool Process (const QByteArray& ba)
77  {
78  QDataStream in (ba);
79  return Process (in);
80  }
81 
82  bool Process (QDataStream& in)
83  {
84  quint8 version = 0;
85  in >> version;
86  if (!Functors_.contains (version))
87  {
88  qWarning () << Q_FUNC_INFO
89  << "unknown version"
90  << version;
91  return false;
92  }
93 
94  quint16 action = 0;
95  in >> action;
96  if (in.status () != QDataStream::Ok)
97  {
98  qWarning () << Q_FUNC_INFO
99  << "bad status"
100  << in.status ()
101  << "for version"
102  << version;
103  return false;
104  }
105 
106  ActionType act = static_cast<ActionType> (action);
107  if (!Functors_ [version].contains (act))
108  {
109  qWarning () << Q_FUNC_INFO
110  << "unknown action"
111  << act
112  << "for version"
113  << version;
114  return false;
115  }
116 
117  return Functors_ [version] [act] (in);
118  }
119  };
120  }
121 }
122 
123 #endif
void AddFunctor(VerType version, ActionType action, Functor_t functor)
std::function< bool(QDataStream &)> Functor_t