<commit_before># cppgo

This library allows methods on C++ objects to be called directly from the
Go runtime without requiring cgo compilation.

**NOTE**: This library currently only works on Windows due to lack of `dlopen()`
support in the Go stdlib on Linux/Mac. It is possible to support other platforms,
but the work has not been done.

## Example Usage

The following example will call into a C++ class `Library` with a
`GetString(char *name)` object method prototype. See the "STEP X" comments
for usage guides:

```go
package main

var (
  dll = syscall.MustLoadLibrary("mylib.dll")
  create = dll.MustFindProc("new_object")
)

// STEP 1. define our C++ proxy struct type with function pointers.
// The functions in this struct will be filled in by the `cpp.ConvertRef()`
// function, at which point this struct will proxy all method calls to the
// C++ object.
type Library struct {
  GetString(name string) string
}

func main() {
  // STEP 2. get an address for the C++ object
  // NOTE: you may need to free this later depending on call semantics.
  o, _, _ := create.Call()

  // STEP 3. point our proxy structure at the functions located in the object
  // that we got from step 2.
  if err := cpp.ConvertRef(o, &l); err != nil {
    panic(err)
  }

  // STEP 4. call the function with arguments
  fmt.Println(l.GetString("Loren"))
}

// Prints:
// Hello, Loren!
```

The C++ class for the above program could look something like:

```cpp
#include <stdio.h>

class Library {
public:
  virtual char __stdcall *GetString(char *name) {
    return sprintf("Hello, %s!", name);
  }
}

extern "C" __declspec(dllexport) Library* __stdcall new_object() {
  return new Library();
}
```

## Caveats & Gotchas

### Limited Type Conversion

You can define arbitrary function arguments and return types, but the internal
translation does not support a full range of types. Currently, only the
following types are supported:

* `uint*`, `int*`, `string`, `[]byte`, `uintptr`

Any other type of pointer will be passed as a pointer directly, which may be
what you want, but may not be. For any type that isn't well converted by
the library, use `uintptr` to send its address. 

### Passing Objects as Arguments
<commit_msg>Slices are not supported yet<commit_after># cppgo

This library allows methods on C++ objects to be called directly from the
Go runtime without requiring cgo compilation.

**NOTE**: This library currently only works on Windows due to lack of `dlopen()`
support in the Go stdlib on Linux/Mac. It is possible to support other platforms,
but the work has not been done.

## Example Usage

The following example will call into a C++ class `Library` with a
`GetString(char *name)` object method prototype. See the "STEP X" comments
for usage guides:

```go
package main

var (
  dll = syscall.MustLoadLibrary("mylib.dll")
  create = dll.MustFindProc("new_object")
)

// STEP 1. define our C++ proxy struct type with function pointers.
// The functions in this struct will be filled in by the `cpp.ConvertRef()`
// function, at which point this struct will proxy all method calls to the
// C++ object.
type Library struct {
  GetString(name string) string
}

func main() {
  // STEP 2. get an address for the C++ object
  // NOTE: you may need to free this later depending on call semantics.
  o, _, _ := create.Call()

  // STEP 3. point our proxy structure at the functions located in the object
  // that we got from step 2.
  if err := cpp.ConvertRef(o, &l); err != nil {
    panic(err)
  }

  // STEP 4. call the function with arguments
  fmt.Println(l.GetString("Loren"))
}

// Prints:
// Hello, Loren!
```

The C++ class for the above program could look something like:

```cpp
#include <stdio.h>

class Library {
public:
  virtual char __stdcall *GetString(char *name) {
    return sprintf("Hello, %s!", name);
  }
}

extern "C" __declspec(dllexport) Library* __stdcall new_object() {
  return new Library();
}
```

## Caveats & Gotchas

### Limited Type Conversion

You can define arbitrary function arguments and return types, but the internal
translation does not support a full range of types. Currently, only the
following types are supported:

* `uint*`, `int*`, `string`, `uintptr`

Any other type of pointer will be passed as a pointer directly, which may be
what you want, but may not be. For any type that isn't well converted by
the library, use `uintptr` to send its address.

Note that slices are not well supported due to the extra information
encoded in a Go slice.

### Passing Objects as Arguments

{'fraction_non_alphanumeric': 0.09698038351333481, 'fraction_numerical': 0.002204099625303064, 'mean_word_length': 3.837953091684435, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '841940', 'n_tokens_mistral': 1356, 'n_tokens_neox': 1307, 'n_words': 642}