Debian Codegraph @

src/internal/action-method.cpp

raw symbols SCIP
1 /*
2 * %injeqt copyright begin%
3 * Copyright 2014 Rafał Malinowski ([email protected])
4 * %injeqt copyright end%
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "action-method.h"
22
23 #include <injeqt/exception/invalid-action.h>
24 #include <injeqt/type.h>
25
26 #include "internal/interfaces-utils.h"
27
28 #include <cassert>
29
30 namespace injeqt { namespace internal {
31
32 bool action_method::is_action_init_tag(const std::string& tag)
33 {
34 return tag == "INJEQT_INIT";
35 }
36
37 bool action_method::is_action_done_tag(const std::string& tag)
38 {
39 return tag == "INJEQT_DONE";
40 }
41
42 bool action_method::validate_action_method(const QMetaMethod &meta_method)
43 {
44 auto meta_object = meta_method.enclosingMetaObject();
45 if (!meta_object)
46 throw exception::invalid_action{std::string{"action does not have enclosing meta object: "} + "?::" + meta_method.methodSignature().data()};
47 if (meta_method.methodType() == QMetaMethod::Signal)
48 throw exception::invalid_action{std::string{"action is signal: "} + meta_object->className() + "::" + meta_method.methodSignature().data()};
49 if (meta_method.methodType() == QMetaMethod::Constructor)
50 throw exception::invalid_action{std::string{"action is constructor: "} + meta_object->className() + "::" + meta_method.methodSignature().data()};
51 if (!is_action_init_tag(meta_method.tag()) && !is_action_done_tag(meta_method.tag()))
52 throw exception::invalid_action{std::string{"action does not have valid tag: "} + meta_object->className() + "::" + meta_method.methodSignature().data()};
53 if (meta_method.parameterCount() != 0)
54 throw exception::invalid_action{std::string{"invalid parameter count: "} + meta_object->className() + "::" + meta_method.methodSignature().data()};
55
56 return true;
57 }
58
59 action_method::action_method()
60 {
61 }
62
63 action_method::action_method(QMetaMethod meta_method) :
64 _object_type{meta_method.enclosingMetaObject()},
65 _meta_method{std::move(meta_method)}
66 {
67 assert(validate_action_method(meta_method));
68 }
69
70 bool action_method::is_empty() const
71 {
72 return !_meta_method.isValid();
73 }
74
75 const type & action_method::object_type() const
76 {
77 return _object_type;
78 }
79
80 bool action_method::invoke(QObject *on) const
81 {
82 assert(!is_empty());
83 assert(on != nullptr);
84 assert(implements(type{on->metaObject()}, _object_type));
85
86 return _meta_method.invoke(on);
87 }
88
89 action_method make_action_method(const QMetaMethod &meta_method)
90 {
91 action_method::validate_action_method(meta_method);
92
93 return action_method{meta_method};
94 }
95
96 std::vector<action_method> extract_actions(const std::string &action_tag, const type &for_type)
97 {
98 assert(!for_type.is_empty());
99
100 auto result = std::vector<action_method>{};
101
102 auto meta_object = for_type.meta_object();
103 auto method_count = meta_object->methodCount();
104 for (decltype(method_count) i = 0; i < method_count; i++)
105 {
106 auto probably_action = meta_object->method(i);
107 auto method_tag = std::string{probably_action.tag()};
108 if (action_tag == method_tag)
109 result.emplace_back(make_action_method(probably_action));
110 }
111
112 return result;
113 }
114
115 }}