diff options
author | Frediano Ziglio <fziglio@redhat.com> | 2018-05-14 23:09:53 +0100 |
---|---|---|
committer | Frediano Ziglio <fziglio@redhat.com> | 2018-05-23 10:30:44 +0100 |
commit | abdef4fd2a4be5c5d363cda2b2838a1f7cc4899d (patch) | |
tree | 33c5124fc67fc99b947c2844c57c5dd04eec6105 /python_modules | |
parent | 78a17ba00a0a321cba58b650b3827dc66a855acf (diff) |
codegen: Remove duplicate client and server code from ChannelType::resolve
Code that handled client and server messages check was the same, just
changed some variable names.
Instead use a class to store same information and reuse the code.
This allows easier extension of the 2 path of code.
Signed-off-by: Frediano Ziglio <fziglio@redhat.com>
Acked-by: Lukáš Hrázký <lhrazky@redhat.com>
Diffstat (limited to 'python_modules')
-rw-r--r-- | python_modules/ptypes.py | 68 |
1 files changed, 29 insertions, 39 deletions
diff --git a/python_modules/ptypes.py b/python_modules/ptypes.py index 0f6d8d6..631739e 100644 --- a/python_modules/ptypes.py +++ b/python_modules/ptypes.py @@ -1021,56 +1021,46 @@ class ChannelType(Type): return self.server_messages_byname[name] def resolve(self): - if self.base != None: + class MessagesInfo: + def __init__(self, is_server, messages=[], messages_byname={}): + self.is_server = is_server + self.messages = messages[:] + self.messages_byname = messages_byname.copy() + self.count = 1 + + if self.base is None: + server_info = MessagesInfo(True) + client_info = MessagesInfo(False) + else: self.base = self.base.resolve() - server_messages = self.base.server_messages[:] - server_messages_byname = self.base.server_messages_byname.copy() - client_messages = self.base.client_messages[:] - client_messages_byname = self.base.client_messages_byname.copy() + server_info = MessagesInfo(True, self.base.server_messages, + self.base.server_messages_byname) + client_info = MessagesInfo(False, self.base.client_messages, + self.base.client_messages_byname) # Set default member_name, FooChannel -> foo self.member_name = self.name[:-7].lower() - else: - server_messages = [] - server_messages_byname = {} - client_messages = [] - client_messages_byname = {} - server_count = 1 - client_count = 1 - - server = True + info = server_info for m in self.members: if m == "server": - server = True + info = server_info elif m == "client": - server = False - elif server: - m.is_server = True - m = m.resolve(self) - if m.value: - server_count = m.value + 1 - else: - m.value = server_count - server_count = server_count + 1 - server_messages.append(m) - server_messages_byname[m.name] = m + info = client_info else: - m.is_server = False + m.is_server = info.is_server m = m.resolve(self) - if m.value: - client_count = m.value + 1 - else: - m.value = client_count - client_count = client_count + 1 - client_messages.append(m) - client_messages_byname[m.name] = m - - self.server_messages = server_messages - self.server_messages_byname = server_messages_byname - self.client_messages = client_messages - self.client_messages_byname = client_messages_byname + if not m.value: + m.value = info.count + info.count = m.value + 1 + info.messages.append(m) + info.messages_byname[m.name] = m + + self.server_messages = server_info.messages + self.server_messages_byname = server_info.messages_byname + self.client_messages = client_info.messages + self.client_messages_byname = client_info.messages_byname return self |