generic_utils: changed semantics of find to ap to std library
std library functions take the predicate as first argument
diff --git a/generic_utils.py b/generic_utils.py
index 0bf289e..ea9c589 100644
--- a/generic_utils.py
+++ b/generic_utils.py
@@ -216,12 +216,22 @@
collections.OrderedDict.__repr__(self))
-def find(iterable, func):
+def find(func, iterable):
"""
find the first item in iterable for which func returns something true'ish.
- @raise KeyError if no item in iterable fulfills the condition
+ @returns None if no item in iterable fulfills the condition
"""
for i in iterable:
if func(i):
return i
- raise KeyError("Couldn't find value that matches: %s" % repr(func))
+ return None
+
+def count(func, iteratable):
+ """
+ count how the number of items in iterable for which func returns something true'ish.
+ """
+ c = 0
+ for i in iterable:
+ if func(i):
+ c +=1
+ return c
diff --git a/java_gen/java_model.py b/java_gen/java_model.py
index a894179..a118fdf 100644
--- a/java_gen/java_model.py
+++ b/java_gen/java_model.py
@@ -95,10 +95,10 @@
@memoize
def enum_by_name(self, name):
- try:
- return find(self.enums, lambda e: e.name == name)
- except KeyError:
+ res = find(lambda e: e.name == name, self.enums)
+ if not res:
raise KeyError("Could not find enum with name %s" % name)
+ return res
@property
@memoize
@@ -348,7 +348,11 @@
@property
def is_virtual(self):
- return type_maps.class_is_virtual(self.c_name)
+ return self.ir_class.virtual # type_maps.class_is_virtual(self.c_name) or self.ir_class.virtual
+
+ @property
+ def discriminator(self):
+ return find(lambda m: isinstance(m, OFDiscriminatorMember), self.ir_class.members)
@property
def is_extension(self):
@@ -358,6 +362,16 @@
def align(self):
return int(self.ir_class.params['align']) if 'align' in self.ir_class.params else 0
+ @property
+ @memoize
+ def superclass(self):
+ return find(lambda c: c.version == self.version and c.c_name == self.ir_class.superclass, model.all_classes)
+
+ @property
+ @memoize
+ def subclasses(self):
+ return [ c for c in model.all_classes if c.version == self.version and c.ir_class.superclass == self.c_name ]
+
#######################################################################
### Member
#######################################################################
@@ -580,23 +594,26 @@
@memoize
def entry_by_name(self, name):
- try:
- return find(self.entries, lambda e: e.name == name)
- except KeyError:
+ res = find(lambda e: e.name == name, self.entries)
+ if res:
+ return res
+ else:
raise KeyError("Enum %s: no entry with name %s" % (self.name, name))
@memoize
def entry_by_c_name(self, name):
- try:
- return find(self.entries, lambda e: e.c_name == name)
- except KeyError:
+ res = find(lambda e: e.c_name == name, self.entries)
+ if res:
+ return res
+ else:
raise KeyError("Enum %s: no entry with c_name %s" % (self.name, name))
@memoize
def entry_by_version_value(self, version, value):
- try:
- return find(self.entries, lambda e: e.values[version] == value if version in e.values else False )
- except KeyError:
+ res = find(lambda e: e.values[version] == value if version in e.values else False, self.entries)
+ if res:
+ return res
+ else:
raise KeyError("Enum %s: no entry with version %s, value %s" % (self.name, version, value))
# values: Map JavaVersion->Value