Skip to content
This repository was archived by the owner on Jun 26, 2025. It is now read-only.

Commit ece0e96

Browse files
committed
improve numbering of overloaded functions
1 parent 2b4d0e1 commit ece0e96

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

generator.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ def save(self):
235235
f.write(self.buf.getvalue())
236236

237237

238-
def convert_name(name):
238+
def convert_name(name, is_function=True, create_ocaml_overload_counts=False,
239+
parent_module=None):
239240
# strip 'cv.' prefix
240241
if name.startswith('cv.'):
241242
name = name[3:]
@@ -251,14 +252,26 @@ def convert_name(name):
251252
overload_counts[c_name] += 1
252253
suffix = str(overload_counts[c_name])
253254
c_name = c_name + suffix
254-
ocaml_name = name + suffix
255+
ocaml_name = name if is_function else name + suffix
255256
else:
256257
overload_counts[c_name] = 0
257258
ocaml_name = name
258259

259260
# convert to camel case
260261
ocaml_name = snake_case(ocaml_name)
261262

263+
# in this case we detect a class method and re-run this function anyway,
264+
# so don't screw up the overload counts
265+
if not ('.' in ocaml_name and is_function):
266+
overload_key = ocaml_name if parent_module is None \
267+
else parent_module + '.' + ocaml_name
268+
if overload_key in ocaml_overload_counts:
269+
ocaml_overload_counts[overload_key] += 1
270+
suffix = str(ocaml_overload_counts[overload_key])
271+
ocaml_name += suffix
272+
elif create_ocaml_overload_counts:
273+
ocaml_overload_counts[overload_key] = 0
274+
262275
if ocaml_name in ocaml_reserved:
263276
ocaml_name = ocaml_reserved[ocaml_name]
264277

@@ -344,6 +357,7 @@ def param_sub(match):
344357
defined_enum_consts = set()
345358
classes = {}
346359
overload_counts = {}
360+
ocaml_overload_counts = {}
347361
draw_functions = []
348362

349363
def add_struct(struct):
@@ -377,7 +391,7 @@ def build_enum_constr(arg):
377391

378392
def add_class(decl):
379393
full_class_name = decl[0].rsplit(' ', 1)[1]
380-
name, c_name, ocaml_name = convert_name(full_class_name)
394+
name, c_name, ocaml_name = convert_name(full_class_name, is_function=False)
381395
inherits = decl[1].rsplit('::', 1)[1] if len(decl[1]) > 0 else None
382396

383397
cpp_name = name
@@ -432,7 +446,7 @@ def sanitize_param(param):
432446
if cls in classes:
433447
name = name.rsplit('.', 1)[1]
434448
c_name = c_name.replace('.', '_')
435-
ocaml_name = convert_name(name)[2]
449+
ocaml_name = convert_name(name, parent_module=cls)[2]
436450

437451
c_params = params[:]
438452

@@ -466,8 +480,8 @@ def sanitize_param(param):
466480
# first pass to collect classes and add types
467481
for decl in decls:
468482
if decl[0].startswith('enum'):
469-
# there's something screwy that happens where sometimes classes show up as enums
470-
# this is a really hacky way to make it work
483+
# there's something screwy that happens where sometimes classes
484+
# show up as enums, this is a really hacky way to make it work
471485
if decl[0].endswith('.<unnamed>'):
472486
decl[0] = decl[0][:-10]
473487
full_class_name = decl[0].rsplit(' ', 1)[1]
@@ -481,7 +495,26 @@ def sanitize_param(param):
481495
else:
482496
pass
483497

484-
# second pass to collect functions and methods
498+
# second pass to identify overloaded OCaml functions
499+
for decl in decls:
500+
if decl[0].startswith('enum') or decl[0].startswith('class'):
501+
pass
502+
else:
503+
name, _, _ = convert_name(decl[0], create_ocaml_overload_counts=True)
504+
505+
if '.' in name:
506+
# class method
507+
cls = name.rsplit('.', 1)[0]
508+
if cls in classes:
509+
name = name.rsplit('.', 1)[1]
510+
convert_name(name, create_ocaml_overload_counts=True,
511+
parent_module=cls)
512+
513+
# only add numbers for functions that are actually overloaded
514+
ocaml_overload_counts = \
515+
{name: 0 for name, count in ocaml_overload_counts.items() if count > 0}
516+
517+
# third pass to collect functions and methods
485518
for decl in decls:
486519
if decl[0].startswith('enum'):
487520
pass

tests/demo/demo.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ let () =
77
let vid = Video_capture.video_capture2 "test.mp4" in
88
(* let _ = get_text_size "hello" 10 10.0 10 10 in *)
99
let rec loop () =
10-
let mat, _ = Video_capture.read1 vid in
10+
let mat, _ = Video_capture.read vid in
1111
let lab = cvt_color mat ~~`COLOR_BGR2Lab in
1212
let lab_l = extract_channel lab 0 in
1313
let blurred = gaussian_blur lab_l {width=21; height=21} 10. in
1414
let threshed, _ = threshold blurred 100. 200. ~~`THRESH_BINARY in
1515
let contours, _ = find_contours threshed ~~`RETR_EXTERNAL ~~`CHAIN_APPROX_SIMPLE in
1616
let rect = bounding_rect threshed in
1717
let drawn = Draw.draw [
18-
Draw.rectangle1 rect (color1 255.) ~thickness:2;
18+
Draw.rectangle2 rect (color1 255.) ~thickness:2;
1919
Draw.draw_contours contours (-1) (color1 0.) ~thickness:4;
2020
] blurred in
2121
(* let _ = calc_hist [lab_l] [0] threshed [10] [0.; 255.] in *)

tests/demo_reuse/demo_reuse.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ let () =
99
let chan = Cvdata.Mat (Mat.create ()) in
1010
let threshed = Cvdata.Mat (Mat.create ()) in
1111
let rec loop () =
12-
let _ = Video_capture.read1 vid ~image:mat in
12+
let _ = Video_capture.read vid ~image:mat in
1313
let _ = cvt_color mat ~dst:mat ~~`COLOR_BGR2Lab in
1414
let _ = extract_channel mat ~dst:chan 0 in
1515
let _ = gaussian_blur chan ~dst:chan {width=21; height=21} 10. in
1616
let _ = threshold chan ~dst:threshed 100. 200. ~~`THRESH_BINARY in
1717
let rect = bounding_rect threshed in
18-
let _ = rectangle1 chan rect (color1 255.) ~thickness:2 in
18+
let _ = rectangle2 chan rect (color1 255.) ~thickness:2 in
1919
let b = 30 in
2020
let tiled = Owl.Dense.Ndarray.Generic.concatenate ~axis:1
2121
[|chan |> Cvdata.to_mat; threshed |> Cvdata.to_mat|] in

0 commit comments

Comments
 (0)