Skip to content

Commit 32a0407

Browse files
committed
Add is_struct() function (#63)
The documentation of is_struct and is_union was updated A test for the is_struct function was added
1 parent f253ece commit 32a0407

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Changes
22
=======
33

4+
Version 1.8.1 (not yet released)
5+
--------------------------------
6+
7+
* Added ```is_struct``` function to declarations package. It returns true if
8+
a declaration is a struct.
9+
410
Version 1.8.0
511
-------------
612

pygccxml/declarations/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
from .type_traits_classes import is_noncopyable
168168
from .type_traits_classes import is_copy_constructor
169169
from .type_traits_classes import is_trivial_constructor
170+
from .type_traits_classes import is_struct
170171
from .type_traits_classes import is_union
171172

172173
from .type_traits_classes import is_unary_operator

pygccxml/declarations/type_traits_classes.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,38 @@
1515
from .. import utils
1616

1717

18-
def is_union(type_):
19-
"""returns True if type represents a C++ union"""
20-
if not is_class(type_):
18+
def is_union(declaration):
19+
"""
20+
Returns True if declaration represents a C++ union
21+
22+
Args:
23+
declaration (declaration_t): the declaration to be checked.
24+
25+
Returns:
26+
bool: True if declaration represents a C++ union
27+
"""
28+
if not is_class(declaration):
2129
return False
22-
decl = class_traits.get_declaration(type_)
30+
decl = class_traits.get_declaration(declaration)
2331
return decl.class_type == class_declaration.CLASS_TYPES.UNION
2432

2533

34+
def is_struct(declaration):
35+
"""
36+
Returns True if declaration represents a C++ struct
37+
38+
Args:
39+
declaration (declaration_t): the declaration to be checked.
40+
41+
Returns:
42+
bool: True if declaration represents a C++ struct
43+
"""
44+
if not is_class(declaration):
45+
return False
46+
decl = class_traits.get_declaration(declaration)
47+
return decl.class_type == class_declaration.CLASS_TYPES.STRUCT
48+
49+
2650
class declaration_xxx_traits(object):
2751
"""this class implements the functionality needed for convenient work with
2852
declaration classes

unittests/unnamed_classes_tester.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ def validate_bitfields(self, parent, bitfields):
3232
def do_union_test(self, union_name, bitfields):
3333
s2 = self.global_ns.class_('S2')
3434
self.assertFalse(declarations.is_union(s2))
35+
self.assertTrue(declarations.is_struct(s2))
3536
self.assertEqual(s2.parent.name, 'S1')
3637
self.assertFalse(declarations.is_union(s2.parent))
3738

3839
union = s2.variable(union_name)
3940
self.assertTrue(declarations.is_union(union.decl_type))
41+
self.assertFalse(declarations.is_struct(union.decl_type))
4042

4143
union_type = type_traits.remove_declarated(union.decl_type)
4244
self.validate_bitfields(union_type, bitfields)

0 commit comments

Comments
 (0)