Skip to content

Commit

Permalink
Made it possible to configure types/names of "trivial" arguments that…
Browse files Browse the repository at this point in the history
… will not get default helpers generated (#24)

Changed order of Ex functions relative to the original (#23)
  • Loading branch information
ShironekoBen committed Sep 19, 2022
1 parent cc47cb8 commit 5c953e6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
4 changes: 3 additions & 1 deletion docs/Changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ Fixed incorrect disambiguation of function pairs that appear in the #if/#else bl
Fixed incorrect generation of conditionals around code in #else blocks
Added support for (read: removal of) constexpr
Fixed disambiguation of default argument functions resulting in Ex functions with names that don't match their argument list (#11)
Reduced number of Ex functions (when trailing arguments are trivial to fill, e.g. flags = 0, an Ex functions is not waranted). (#10)
Reduced number of Ex functions (when trailing arguments are trivial to fill, e.g. flags = 0, an Ex functions is not warranted). (#10)
Split code_dom into multiple files for tidiness and refactored the modules a bit
Improved the header template system somewhat and added a header to all files (#18, #22)
Made it possible to configure types/names of "trivial" arguments that will not get default helpers generated (#24)
Changed order of Ex functions relative to the original (#23)

--- v0.03

Expand Down
7 changes: 7 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,13 @@ def convert_header(src_file, dest_file_no_ext, template_dir):
function_prefixes_to_ignore=[
'ImGuiStorage_',
'ImFontAtlas_'
],
trivial_argument_types=[
'ImGuiCond'
],
trivial_argument_names=[
'flags',
'popup_flags'
])

# Make all functions use CIMGUI_API
Expand Down
2 changes: 1 addition & 1 deletion src/code_dom/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ def parse_basic(context, stream):
# Attach preceding comments
def attach_preceding_comments(self, comments):
for comment in comments:
self.pre_comments.append(comment)
if comment.parent:
comment.parent.remove_child(comment)
self.pre_comments.append(comment)
comment.parent = self
comment.is_preceding_comment = True

Expand Down
54 changes: 39 additions & 15 deletions src/modifiers/mod_generate_default_argument_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@
# (for ease of programming, since C doesn't support default arguments)
# functions_to_ignore contains a list of functions that should not have variants generated
# function_prefixes_to_ignore contains a list of function prefixes that should not have variants generated
def apply(dom_root, functions_to_ignore=[], function_prefixes_to_ignore=[]):
# trivial_argument_types is a list of argument types that will not get defaulted where possible if the default is 0/null
# trivial_argument_names is a list of argument names that will not get defaulted where possible if the default is 0/null
def apply(dom_root,
functions_to_ignore=[],
function_prefixes_to_ignore=[],
trivial_argument_types=[],
trivial_argument_names=[]):

# Should functions with default arguments appear after the no-defaults ("ex") version of the function or before it?
add_defaulted_functions_before_ex = True

for function in dom_root.list_all_children_of_type(code_dom.DOMFunctionDeclaration):
# Ignore based on exact name match
if function.name in functions_to_ignore:
Expand Down Expand Up @@ -43,20 +53,27 @@ def apply(dom_root, functions_to_ignore=[], function_prefixes_to_ignore=[]):
if function.has_imstr_helper and not has_non_defaulted_imstr_args:
continue

# If a function only has a zero-default flags argument, don't generate helpers for it
# (as passing an extra zero is trivial, and they clutter up the API)
if (num_args_with_defaults == 1) and \
((function.arguments[num_args - 1].name == "flags") or
(function.arguments[num_args - 1].name == "popup_flags")) and \
(function.arguments[num_args - 1].get_default_value() == '0'):
continue
# Check how many of the first default arguments are trivial, and leave those without a default even in the
# with-defaults function (rationale: there are many cases where flags/etc can be trivially specified without
# cluttering the API, and conversely having to use the Ex function with all arguments is overkill if only
# they need to be changed).

# Similarly, ignore any functions that take a single "ImGuiCond cond = 0" default argument
# (as passing an extra zero is trivial, and they clutter up the API)
if (num_args_with_defaults == 1) and \
(function.arguments[num_args - 1].arg_type.get_fully_qualified_name() == "ImGuiCond") and \
(function.arguments[num_args - 1].get_default_value() == '0'):
continue
for i in range(num_args - num_args_with_defaults, num_args):
# Only consider leaving arguments if they have a zero or null default
if (function.arguments[i].get_default_value() == '0') or \
(function.arguments[i].get_default_value() == 'NULL'):
if (function.arguments[i].name in trivial_argument_names) or \
(function.arguments[i].arg_type.get_fully_qualified_name() in trivial_argument_types):
# Reduce the number of arguments we will apply defaults to
num_args_with_defaults -= 1
else:
# Once a single argument we do intend to default has been found, we need to stop
break
else:
break

if num_args_with_defaults == 0:
continue # Nothing useful left to do

# Clone the function and set the defaulted arguments

Expand Down Expand Up @@ -94,4 +111,11 @@ def apply(dom_root, functions_to_ignore=[], function_prefixes_to_ignore=[]):

# Insert new function

function.parent.insert_after_child(function, [new_function])
if add_defaulted_functions_before_ex:
# When inserting before the Ex function, we need to move any pre-comments up onto ourselves
comments_to_move = function.pre_comments.copy() # Copy to avoid altering the list as we iterate
new_function.attach_preceding_comments(comments_to_move)

function.parent.insert_before_child(function, [new_function])
else:
function.parent.insert_after_child(function, [new_function])

0 comments on commit 5c953e6

Please sign in to comment.