From af54a22d16f17e51193020b44a984577640c1d67 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sat, 13 Jul 2024 18:11:34 +0800 Subject: [PATCH] inih: INIReaderDispose --- cpp/inih/_demo/inihreader/reader_demo.go | 4 ++++ cpp/inih/_wrap/reader.cpp | 13 +++++++++++++ cpp/inih/inih.go | 3 ++- cpp/inih/reader.go | 3 +++ cpp/std/{_cppstd => _wrap}/string.cpp | 2 +- cpp/std/std.go | 2 +- 6 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 cpp/inih/_wrap/reader.cpp rename cpp/std/{_cppstd => _wrap}/string.cpp (95%) diff --git a/cpp/inih/_demo/inihreader/reader_demo.go b/cpp/inih/_demo/inihreader/reader_demo.go index 13fbbcf6..3fe5d607 100644 --- a/cpp/inih/_demo/inihreader/reader_demo.go +++ b/cpp/inih/_demo/inihreader/reader_demo.go @@ -12,6 +12,8 @@ username=admin timeout=100 ` reader := inih.NewReader(c.Str(buf), uintptr(len(buf))) + defer reader.Dispose() + println(reader.ParseError()) sec := std.Str("settings") name := std.Str("timeout") @@ -19,6 +21,8 @@ timeout=100 println("value:", value) reader = inih.NewReaderFile(std.Str("config.ini")) + defer reader.Dispose() + if ret := reader.ParseError(); ret != 0 { println("Error parsing config file:", ret) return diff --git a/cpp/inih/_wrap/reader.cpp b/cpp/inih/_wrap/reader.cpp new file mode 100644 index 00000000..f7d2c099 --- /dev/null +++ b/cpp/inih/_wrap/reader.cpp @@ -0,0 +1,13 @@ +#include + +extern "C" { + +// ----------------------------------------------------------------------------- + +void INIReaderDispose(INIReader* r) { + r->~INIReader(); +} + +// ----------------------------------------------------------------------------- + +} // extern "C" diff --git a/cpp/inih/inih.go b/cpp/inih/inih.go index 08f96fd9..fe8a5214 100644 --- a/cpp/inih/inih.go +++ b/cpp/inih/inih.go @@ -23,7 +23,8 @@ import ( ) const ( - LLGoPackage = "link: $(pkg-config --libs inih INIReader); -linih -lINIReader" + LLGoFiles = "_wrap/reader.cpp" + LLGoPackage = "link: $(pkg-config --cflags --libs inih INIReader); -linih -lINIReader" ) //go:linkname Parse C.ini_parse diff --git a/cpp/inih/reader.go b/cpp/inih/reader.go index 083edbd5..987f3f48 100644 --- a/cpp/inih/reader.go +++ b/cpp/inih/reader.go @@ -36,6 +36,9 @@ func (r *Reader) InitFromBuffer(buffer *c.Char, bufferSize uintptr) {} // llgo:link (*Reader).InitFromFile C._ZN9INIReaderC1ERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE func (r *Reader) InitFromFile(fileName *std.String) {} +// llgo:link (*Reader).Dispose C.INIReaderDispose +func (s *Reader) Dispose() {} + // ----------------------------------------------------------------------------- func NewReader(buffer *c.Char, bufferSize uintptr) (ret Reader) { diff --git a/cpp/std/_cppstd/string.cpp b/cpp/std/_wrap/string.cpp similarity index 95% rename from cpp/std/_cppstd/string.cpp rename to cpp/std/_wrap/string.cpp index f48415e7..94f73508 100644 --- a/cpp/std/_cppstd/string.cpp +++ b/cpp/std/_wrap/string.cpp @@ -20,7 +20,7 @@ void stdStringInitFromCStrLen(std::string* s, const char* cstr, size_t len) { new(s) std::string(cstr, len); } -void stdStringDispose(const std::string* s) { +void stdStringDispose(std::string* s) { s->~basic_string(); } diff --git a/cpp/std/std.go b/cpp/std/std.go index 89d6b720..d36a2e09 100644 --- a/cpp/std/std.go +++ b/cpp/std/std.go @@ -17,7 +17,7 @@ package std const ( - LLGoFiles = "_cppstd/string.cpp" + LLGoFiles = "_wrap/string.cpp" LLGoPackage = "link: c++" )