Skip to content

Commit 64c3dbf

Browse files
committed
refactoring
1 parent f790820 commit 64c3dbf

File tree

14 files changed

+195
-100
lines changed

14 files changed

+195
-100
lines changed

autoit-opencv-com/patches/001-opencv-src.patch

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ index 32b2392904..c5ed2dc30d 100644
8383
}
8484

8585
diff --git a/modules/python/src2/hdr_parser.py b/modules/python/src2/hdr_parser.py
86-
index fa2d0077d9..f7fe0a8b4b 100755
86+
index fa2d0077d9..1d9e5a57f8 100755
8787
--- a/modules/python/src2/hdr_parser.py
8888
+++ b/modules/python/src2/hdr_parser.py
8989
@@ -70,6 +70,29 @@ class CppHeaderParser(object):
@@ -175,7 +175,15 @@ index fa2d0077d9..f7fe0a8b4b 100755
175175
func_modlist.append("="+arg)
176176
decl_str = decl_str[:npos] + decl_str[npos3+1:]
177177
npos = decl_str.find("CV_WRAP_PHANTOM")
178-
@@ -598,7 +630,7 @@ class CppHeaderParser(object):
178+
@@ -448,6 +480,7 @@ class CppHeaderParser(object):
179+
decl_str = self.batch_replace(decl_str, [("static inline", ""),
180+
("inline", ""),
181+
("explicit ", ""),
182+
+ ("[[nodiscard]] ", ""),
183+
("CV_EXPORTS_W", ""),
184+
("CV_EXPORTS", ""),
185+
("CV_CDECL", ""),
186+
@@ -598,7 +631,7 @@ class CppHeaderParser(object):
179187
if eqpos >= 0:
180188
a = a[:eqpos].strip()
181189
arg_type, arg_name, modlist, argno = self.parse_arg(a, argno)
@@ -184,7 +192,7 @@ index fa2d0077d9..f7fe0a8b4b 100755
184192
# TODO: Vectors should contain UMat, but this is not very easy to support and not very needed
185193
vector_mat = "vector_{}".format(mat)
186194
vector_mat_template = "vector<{}>".format(mat)
187-
@@ -659,10 +691,13 @@ class CppHeaderParser(object):
195+
@@ -659,10 +692,13 @@ class CppHeaderParser(object):
188196

189197
the function will convert "A" to "cv.A" and "f" to "cv.A.f".
190198
"""
@@ -201,7 +209,7 @@ index fa2d0077d9..f7fe0a8b4b 100755
201209
qualified_name = (("." in name) or ("::" in name))
202210
n = ""
203211
for b in self.block_stack:
204-
@@ -754,7 +789,10 @@ class CppHeaderParser(object):
212+
@@ -754,7 +790,10 @@ class CppHeaderParser(object):
205213

206214
if stmt.startswith("enum") or stmt.startswith("namespace"):
207215
# NB: Drop inheritance syntax for enum
@@ -213,7 +221,7 @@ index fa2d0077d9..f7fe0a8b4b 100755
213221
stmt_list = stmt.rsplit(" ", 1)
214222
if len(stmt_list) < 2:
215223
stmt_list.append("<unnamed>")
216-
@@ -790,8 +828,18 @@ class CppHeaderParser(object):
224+
@@ -790,8 +829,18 @@ class CppHeaderParser(object):
217225
var_modlist = []
218226
if "CV_PROP_RW" in stmt:
219227
var_modlist.append("/RW")

autoit-opencv-com/src/autoit_bridge_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace fs = std::filesystem;
55

66
namespace {
7-
template <typename char_type>
7+
template<typename char_type>
88
inline bool null_or_empty(const char_type* s) {
99
return s == nullptr || *s == 0;
1010
}

autoit-opencv-com/src/autoit_bridge_common.h

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
#define AUTOIT_INFO( _message ) do { \
7373
std::ostringstream _out; _out << _message; \
7474
fflush(stdout); fflush(stderr); \
75-
fprintf(stderr, AUTOIT_QUOTE_STRING(AUTOIT_LIB_NAME) "(%s) Info: %s (%s) in %s, file %s, line %d\n", AUTOIT_QUOTE_STRING(AUTOIT_LIB_VERSION), _out.str().c_str(), "", AutoIt_Func, __FILE__, __LINE__); \
75+
fprintf(stderr, AUTOIT_QUOTE_STRING(AUTOIT_LIB_NAME) "(%s) Info: %s in %s, file %s, line %d\n", AUTOIT_QUOTE_STRING(AUTOIT_LIB_VERSION), _out.str().c_str(), AutoIt_Func, __FILE__, __LINE__); \
7676
fflush(stdout); fflush(stderr); \
7777
} while(0)
7878
#endif
@@ -81,7 +81,7 @@
8181
#define AUTOIT_WARN( _message ) do { \
8282
std::ostringstream _out; _out << _message; \
8383
fflush(stdout); fflush(stderr); \
84-
fprintf(stderr, AUTOIT_QUOTE_STRING(AUTOIT_LIB_NAME) "(%s) Warning: %s (%s) in %s, file %s, line %d\n", AUTOIT_QUOTE_STRING(AUTOIT_LIB_VERSION), _out.str().c_str(), "", AutoIt_Func, __FILE__, __LINE__); \
84+
fprintf(stderr, AUTOIT_QUOTE_STRING(AUTOIT_LIB_NAME) "(%s) Warning: %s in %s, file %s, line %d\n", AUTOIT_QUOTE_STRING(AUTOIT_LIB_VERSION), _out.str().c_str(), AutoIt_Func, __FILE__, __LINE__); \
8585
fflush(stdout); fflush(stderr); \
8686
} while(0)
8787
#endif
@@ -90,7 +90,7 @@
9090
#define AUTOIT_ERROR( _message ) do { \
9191
std::ostringstream _out; _out << _message; \
9292
fflush(stdout); fflush(stderr); \
93-
fprintf(stderr, AUTOIT_QUOTE_STRING(AUTOIT_LIB_NAME) "(%s) Error: %s (%s) in %s, file %s, line %d\n", AUTOIT_QUOTE_STRING(AUTOIT_LIB_VERSION), _out.str().c_str(), "", AutoIt_Func, __FILE__, __LINE__); \
93+
fprintf(stderr, AUTOIT_QUOTE_STRING(AUTOIT_LIB_NAME) "(%s) Error: %s in %s, file %s, line %d\n", AUTOIT_QUOTE_STRING(AUTOIT_LIB_VERSION), _out.str().c_str(), AutoIt_Func, __FILE__, __LINE__); \
9494
fflush(stdout); fflush(stderr); \
9595
} while(0)
9696
#endif
@@ -99,7 +99,7 @@
9999
#define AUTOIT_THROW( _message ) do { \
100100
std::ostringstream _out; _out << _message; \
101101
fflush(stdout); fflush(stderr); \
102-
fprintf(stderr, AUTOIT_QUOTE_STRING(AUTOIT_LIB_NAME) "(%s) Error: %s (%s) in %s, file %s, line %d\n", AUTOIT_QUOTE_STRING(AUTOIT_LIB_VERSION), _out.str().c_str(), "", AutoIt_Func, __FILE__, __LINE__); \
102+
fprintf(stderr, AUTOIT_QUOTE_STRING(AUTOIT_LIB_NAME) "(%s) Error: %s in %s, file %s, line %d\n", AUTOIT_QUOTE_STRING(AUTOIT_LIB_VERSION), _out.str().c_str(), AutoIt_Func, __FILE__, __LINE__); \
103103
fflush(stdout); fflush(stderr); \
104104
throw std::exception(_out.str().c_str()); \
105105
} while(0)
@@ -117,7 +117,9 @@
117117

118118
#ifndef AUTOIT_ASSERT_SET_HR
119119
#define AUTOIT_ASSERT_SET_HR( expr ) do { if(!!(expr)) { hr = S_OK; } else { \
120+
fflush(stdout); fflush(stderr); \
120121
fprintf(stderr, AUTOIT_QUOTE_STRING(AUTOIT_LIB_NAME) "(%s) Error: (%s) in %s, file %s, line %d\n", AUTOIT_QUOTE_STRING(AUTOIT_LIB_VERSION), #expr, AutoIt_Func, __FILE__, __LINE__); \
122+
fflush(stdout); fflush(stderr); \
121123
hr = E_FAIL; } \
122124
} while(0)
123125
#endif
@@ -808,8 +810,8 @@ namespace autoit {
808810
/**
809811
* https://github.com/ThePhD/sol2/blob/v3.3.0/include/sol/stack_core.hpp#L1352
810812
*/
811-
template <typename T>
812-
std::string adl_default_to_string(const T& obj) {
813+
template<typename T>
814+
inline std::string adl_default_to_string(const T& obj) {
813815
return std::to_string(obj);
814816
}
815817

@@ -878,7 +880,7 @@ namespace autoit {
878880
bool __eq__(const std::vector<T>& v1, const std::vector<T>& v2);
879881

880882
template<typename T>
881-
bool __eq__(const T& o1, const T& o2) {
883+
inline bool __eq__(const T& o1, const T& o2) {
882884
if constexpr (requires(const T & a, const T & b) { static_cast<bool>(a == b); }) {
883885
return static_cast<bool>(o1 == o2);
884886
}
@@ -888,15 +890,15 @@ namespace autoit {
888890
}
889891

890892
template<typename T>
891-
bool __eq__(const std::shared_ptr<T>& p1, const std::shared_ptr<T>& p2) {
893+
inline bool __eq__(const std::shared_ptr<T>& p1, const std::shared_ptr<T>& p2) {
892894
if (static_cast<bool>(p1) && static_cast<bool>(p2)) {
893895
return __eq__(*p1, *p2);
894896
}
895897
return !static_cast<bool>(p1) && !static_cast<bool>(p2);
896898
}
897899

898900
template<typename K, typename V>
899-
bool __eq__(const std::map<K, V>& m1, const std::map<K, V>& m2) {
901+
inline bool __eq__(const std::map<K, V>& m1, const std::map<K, V>& m2) {
900902
if (m1.size() != m2.size()) {
901903
return false;
902904
}
@@ -911,48 +913,48 @@ namespace autoit {
911913
}
912914

913915
template<typename T1, typename T2>
914-
bool __eq__(const std::pair<T1, T2>& p1, const std::pair<T1, T2>& p2) {
916+
inline bool __eq__(const std::pair<T1, T2>& p1, const std::pair<T1, T2>& p2) {
915917
return __eq__(p1.first, p2.first) && __eq__(p1.second, p2.second);
916918
}
917919

918920
template<typename T>
919-
bool __eq__(const std::vector<T>& v1, const std::vector<T>& v2) {
921+
inline bool __eq__(const std::vector<T>& v1, const std::vector<T>& v2) {
920922
if (v1.size() != v2.size()) {
921923
return false;
922924
}
923925
const auto mismatched = std::mismatch(v1.begin(), v1.end(), v2.begin(), static_cast<bool(*)(const T&, const T&)>(__eq__));
924926
return mismatched.first == v1.end();
925927
}
926928

927-
template <typename _Tp>
929+
template<typename _Tp>
928930
AUTOIT_PTR<typename _Tp> cast(IDispatch* element);
929931

930-
template <typename _Tp>
932+
template<typename _Tp>
931933
const AUTOIT_PTR<typename _Tp> cast(const IDispatch* element);
932934

933935
template<typename _Tp>
934-
_Tp cast(VARIANT const* const& in_val) {
936+
inline _Tp cast(VARIANT const* const& in_val) {
935937
_Tp value;
936938
AUTOIT_ASSERT_THROW(SUCCEEDED(autoit_to(in_val, value)), "Invalid argument");
937939
return value;
938940
}
939941

940-
template <typename _Tp>
941-
const AUTOIT_PTR<typename _Tp> reference_internal(_Tp* element) {
942+
template<typename _Tp>
943+
inline const AUTOIT_PTR<typename _Tp> reference_internal(_Tp* element) {
942944
return AUTOIT_PTR<_Tp>(AUTOIT_PTR<_Tp>{}, element);
943945
}
944946

945-
template <typename _Tp>
946-
const AUTOIT_PTR<typename _Tp> reference_internal(const _Tp* element) {
947+
template<typename _Tp>
948+
inline const AUTOIT_PTR<typename _Tp> reference_internal(const _Tp* element) {
947949
return AUTOIT_PTR<_Tp>(AUTOIT_PTR<_Tp>{}, const_cast<_Tp*>(element));
948950
}
949951

950-
template <typename _Tp>
952+
template<typename _Tp>
951953
const AUTOIT_PTR<typename _Tp> reference_internal(_Tp& element) {
952954
return AUTOIT_PTR<_Tp>(AUTOIT_PTR<_Tp>{}, & element);
953955
}
954956

955-
template <typename _Tp>
957+
template<typename _Tp>
956958
const AUTOIT_PTR<typename _Tp> reference_internal(const _Tp& element) {
957959
return AUTOIT_PTR<_Tp>(AUTOIT_PTR<_Tp>{}, const_cast<_Tp*>(&element));
958960
}
@@ -979,7 +981,7 @@ namespace autoit {
979981
}
980982
};
981983

982-
template <typename destination_type, typename _Ty1, typename _Ty2>
984+
template<typename destination_type, typename _Ty1, typename _Ty2>
983985
struct _GenericCopy<destination_type, std::pair<_Ty1, _Ty2>> {
984986
inline static HRESULT copy(destination_type* pTo, const std::pair<_Ty1, _Ty2>* pFrom) {
985987
return autoit_from(*pFrom, pTo);

autoit-opencv-com/src/impl_mat.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ namespace autoit
152152
}
153153

154154
template<typename _Tp>
155-
const bool is_assignable_from(cv::Point3_<_Tp>& out_val, VARIANT const* const& in_val, bool is_optional) {
155+
inline const bool is_assignable_from(cv::Point3_<_Tp>& out_val, VARIANT const* const& in_val, bool is_optional) {
156156
static cv::Vec<_Tp, 3> tmp;
157157
return is_assignable_from(tmp, in_val, is_optional);
158158
}
@@ -164,7 +164,7 @@ const bool is_assignable_from(AUTOIT_PTR<cv::Point3_<_Tp>>& out_val, VARIANT con
164164
}
165165

166166
template<typename _Tp>
167-
const HRESULT autoit_to(VARIANT const* const& in_val, cv::Point3_<_Tp>& out_val) {
167+
inline const HRESULT autoit_to(VARIANT const* const& in_val, cv::Point3_<_Tp>& out_val) {
168168
cv::Vec<_Tp, 3> tmp;
169169
HRESULT hr = autoit_to(in_val, tmp);
170170
if (SUCCEEDED(hr)) {
@@ -174,12 +174,12 @@ const HRESULT autoit_to(VARIANT const* const& in_val, cv::Point3_<_Tp>& out_val)
174174
}
175175

176176
template<typename _Tp>
177-
const HRESULT autoit_to(VARIANT const* const& in_val, AUTOIT_PTR<cv::Point3_<_Tp>>& out_val) {
177+
inline const HRESULT autoit_to(VARIANT const* const& in_val, AUTOIT_PTR<cv::Point3_<_Tp>>& out_val) {
178178
out_val = std::make_shared<cv::Point3_<_Tp>>();
179179
return autoit_to(in_val, *out_val.get());
180180
}
181181

182182
template<typename _Tp>
183-
const HRESULT autoit_from(const cv::Point3_<_Tp>& in_val, VARIANT*& out_val) {
183+
inline const HRESULT autoit_from(const cv::Point3_<_Tp>& in_val, VARIANT*& out_val) {
184184
return autoit_from(cv::Vec<_Tp, 3>(in_val), out_val);
185185
}

autoit-opencv-com/src/wgc/direct3d11.interop.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ inline auto CreateDirect3DSurface(IDXGISurface* dxgi_surface)
3030
return d3d_surface.as<winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DSurface>();
3131
}
3232

33-
template <typename T>
33+
template<typename T>
3434
auto GetDXGIInterfaceFromObject(winrt::Windows::Foundation::IInspectable const& object)
3535
{
3636
auto access = object.as<IDirect3DDxgiInterfaceAccess>();

src/COMGenerator.js

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,37 @@ const escapeHTML = str => {
4343
};
4444

4545
const proto = {
46+
setReturn(returns, idltype, argname) {
47+
if (returns.length === 0) {
48+
returns.push(idltype, argname);
49+
return;
50+
}
51+
52+
if (returns[0] === idltype) {
53+
return;
54+
}
55+
56+
if (returns[0][0] === "I" && idltype[0] === "I") {
57+
returns[0] = "IDispatch*";
58+
return;
59+
}
60+
61+
returns[0] = "VARIANT";
62+
},
63+
4664
getIDLType(type, coclass, options = {}) {
4765
if (type.includes("(") || type.includes(")") || type.includes("<") && !type.endsWith(">") || countInstances(type, "<") !== countInstances(type, ">")) {
4866
// invalid type, most likely comming from defval
4967
return type;
5068
}
5169

70+
if (options.getIDLType) {
71+
const idltype = options.getIDLType(this, type, coclass, options);
72+
if (idltype) {
73+
return idltype;
74+
}
75+
}
76+
5277
type = getAlias(type);
5378

5479
if (!IDL_TYPES.has(type) && CPP_TYPES.has(type)) {
@@ -168,6 +193,10 @@ const proto = {
168193
return this.getIDLType(type.slice(0, -1), coclass, options);
169194
}
170195

196+
if (type === "void") {
197+
return type;
198+
}
199+
171200
return type.toUpperCase();
172201
},
173202

@@ -246,15 +275,15 @@ const proto = {
246275
"=get__NewEnum",
247276
"/IDL"
248277
], [], "", ""], options);
249-
}
278+
},
250279
};
251280

252281
class COMGenerator {
253282
static proto = proto;
254283

255284
generate(processor, configuration, options, cb) {
256285
const { generated_include } = configuration;
257-
const { APP_NAME, LIB_UID, LIBRARY, shared_ptr } = options;
286+
const { APP_NAME, LIB_UID, LIBRARY, make_shared, shared_ptr } = options;
258287

259288
const files = new Map();
260289
const libs = [];
@@ -283,7 +312,7 @@ class COMGenerator {
283312
constructor.push(`__self = new ${ shared_ptr }<${ coclass.fqn }>();`);
284313

285314
if (coclass.has_default_constructor) {
286-
constructor.push(`__self->reset(new ${ coclass.fqn }());`);
315+
constructor.push(`*__self = ${ make_shared }<${ coclass.fqn }>();`);
287316
}
288317

289318
destructor.push("delete __self;");
@@ -393,14 +422,52 @@ class COMGenerator {
393422

394423
if (set_dispid_value) {
395424
fname = "get_create";
425+
const {has_default_constructor} = coclass;
426+
let has_default_create = false;
427+
let maxargc = 0;
396428

397429
for (const decl of overrides) {
398-
const [, , func_modifiers] = decl;
430+
const [, return_value_type, func_modifiers, list_of_arguments] = decl;
431+
399432
for (const modifier of ["/attr=propget", "=get_create", "/idlname=create", "/id=DISPID_VALUE"]) {
400433
if (!func_modifiers.includes(modifier)) {
401434
func_modifiers.push(modifier);
402435
}
403436
}
437+
438+
if (!has_default_constructor) {
439+
const has_default_arguments = list_of_arguments.length === 0 || !list_of_arguments.some(([argtype, argname, defval]) => defval === "");
440+
const is_static = func_modifiers.includes("/S");
441+
maxargc = Math.max(maxargc, list_of_arguments.length);
442+
443+
if (has_default_arguments && is_static && processor.getReturnCppType(return_value_type, coclass, options) === `${ shared_ptr }<${ fqn }>`) {
444+
has_default_create = true;
445+
}
446+
}
447+
}
448+
449+
if (has_default_create) {
450+
constructor.push("");
451+
452+
if (maxargc !== 0) {
453+
constructor.push(`
454+
_variant_t _vtDefault;
455+
VariantClear(&_vtDefault);
456+
V_VT(&_vtDefault) = VT_ERROR;
457+
V_ERROR(&_vtDefault) = DISP_E_PARAMNOTFOUND;
458+
`.replace(/^ {36}/mg, "").trim(), "");
459+
}
460+
461+
const {interface: iface} = coclass;
462+
const wtype = iface === "IDispatch" ? "DISPATCH" : "UNKNOWN";
463+
464+
constructor.push(`
465+
_variant_t _retval(this);
466+
HRESULT hr = get_create(${ Array.from(new Array(maxargc).keys()).map(_ => "&_vtDefault").concat("&_retval").join(", ") });
467+
if (FAILED(hr)) {
468+
return hr;
469+
}
470+
`.replace(/^ {32}/mg, "").trim(), "");
404471
}
405472
}
406473
}
@@ -608,7 +675,7 @@ class COMGenerator {
608675
if (!coclass.is_cexternal) {
609676
impl.unshift(`
610677
HRESULT C${ cotype }::FinalConstruct() {
611-
${ constructor.join(`\n${ " ".repeat(28) }`) }
678+
${ constructor.join("\n").split("\n").join(`\n${ " ".repeat(28) }`) }
612679
}
613680
`.replace(/^ {24}/mg, ""));
614681
}

0 commit comments

Comments
 (0)