cgoemitter

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 3, 2018 License: MIT Imports: 6 Imported by: 0

README

CGOEmitter (golang package)

Logo

GoReport Coverage GoDoc License

What is it for?

The CGOEmitter package serves to obtain asynchronous C code response returns, facilitating the process of returning the data returned to the program in GO.

How to use?

1. Create the C header file

You must include the cgoemitter.h file to obtain the required methods.

File: (github.com/user/packagename/x/x.h)

#ifndef X_H_
#define X_H_

#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include "../../cgoemitter.h"

void say_message();

#endif
2. Create the C file with your logic

File: (github.com/user/packagename/x/x.c)

#include "x.h"

void check_err_cgoemitter_args_halloc_arg(void* value) {
  if (value == NULL) puts("Failed on cgoemitter_args_halloc_arg()");
}

void check_err_cgoemitter_args_add_arg(int code) {
  if (code == EXIT_FAILURE) puts("Failed on cgoemitter_args_add_arg()");
}

void say_message() {
  char* message = "Parameter sent from C language";

  cgoemitter_args_t cgoemitter_args = cgoemitter_new_args(1);
  
  void* message_arg = cgoemitter_args_halloc_arg(&message, (strlen(message)+1) * sizeof(char));
  check_err_cgoemitter_args_halloc_arg(message_arg);
  check_err_cgoemitter_args_add_arg(cgoemitter_args_add_arg(&cgoemitter_args, &message_arg));

  emit("message", &cgoemitter_args);
}
3. Create the package that will call this method

It is extremely important to add the flags below so that your code is compiled.

#cgo darwin LDFLAGS: -Wl,-undefined -Wl,dynamic_lookup
#cgo !darwin LDFLAGS: -Wl,-unresolved-symbols=ignore-all

File: (github.com/user/packagename/x/x.go)

package x

// The LDFLAGS lines below are needed to prevent linker errors
// since not all packages are present while building intermediate
// packages. The darwin build tag is used as a proxy for clang
// versus gcc because there doesn't seem to be a better way
// to detect this.

/*
#cgo darwin LDFLAGS: -Wl,-undefined -Wl,dynamic_lookup
#cgo !darwin LDFLAGS: -Wl,-unresolved-symbols=ignore-all
#include "x.h"
*/
import "C"

//SayMessage | Execute C function
func SayMessage() {
  C.say_message()
}
4. Using the CGOEmitter to receive the returned data

File: (github.com/user/packagename/main.go)

package main

import (
  "fmt"
  "sync"
  
  "github.com/supermock/cgoemitter"
  "github.com/user/packagename/x"
)

func main() {
  var wg sync.WaitGroup
  wg.Add(1)

  cgoemitter.On("message", cgoemitter.NewListener(func(args cgoemitter.Arguments) {
    fmt.Printf("Receveid message: %s\n", args.String(0))
    wg.Done()
  }))

  x.SayMessage()

  wg.Wait()
}
5. Just run your program and see the magic happening
$ go run main.go

Warnings Event

These warnings help you identify issues with your implementation, such as events triggered without manipulators.

Example:

cgoemitter.On("cgoemitter-warnings", cgoemitter.NewListener(func(args cgoemitter.Arguments) {
  fmt.Println("[WARNING]", args.String(0))
}))

Supported Methods:

  • On() => Add a new listener to the event.
  • Off() => Removes an existing listener in the event.
  • NewListener() => Creates a new listener.
  • GetListeners() => Returns all listeners to an event.
  • parser/CStructToGoStruct() => Transports data from a structure received from C to a structure in the GO.

Project with example of package usage:

Go documentation

Roadmap

  • Middleware to parse the listener's Arguments so that it returns in golang typing.
  • Add new conversions of types from C to GO in Arguments.
  • Add new treatments from C types to the GO in the CStructToGoStruct method

Read in another language

Clique aqui e leia em português

Contributions

Just download the code make your change and send a pull request explaining the purpose if it is a bug or an improvement and etc... After this will be analyzed to be approved. Note: If it is a major change, open a issue explaining what will be done so you do not waste your precious time developing something that will not be used. Make yourself at home!

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrUnknownEvent | This event does not exist
	ErrUnknownEvent = errors.New("This event does not exist")
)

Functions

func Off

func Off(eventName string, listener *ListenerFunc)

Off | Removes a listener from the event or the event itself if the listener number equals zero

func On

func On(eventName string, listener *ListenerFunc)

On | Adds a new event if it does not exist, and also adds a new listener

Types

type Arguments

type Arguments []unsafe.Pointer

Arguments | Custom arguments received from C language

func (Arguments) Arg

func (args Arguments) Arg(index int) unsafe.Pointer

Arg | Get the index argument

func (Arguments) Count

func (args Arguments) Count() int

Count | Returns the number of arguments

func (Arguments) Double

func (args Arguments) Double(index int) float64

Double | Converts the received pointer to the float64 type

func (Arguments) Float

func (args Arguments) Float(index int) float32

Float | Converts the received pointer to the float32 type

func (Arguments) Int

func (args Arguments) Int(index int) int

Int | Converts the received pointer to integer type

func (Arguments) String

func (args Arguments) String(index int) string

String | Converts the received pointer to string type

type Events

type Events map[string]*Listeners

Events | List of events

func (*Events) AddEvent

func (events *Events) AddEvent(eventName string) *Listeners

AddEvent | Add a new event

func (Events) Has

func (events Events) Has(eventName string) (*Listeners, bool)

Has | Returns the value and if the event already exists it returns true

func (*Events) RemoveEvent

func (events *Events) RemoveEvent(eventName string)

RemoveEvent | Remove an event

type ListenerFunc

type ListenerFunc func(Arguments)

ListenerFunc | Listener function

func NewListener

func NewListener(listener ListenerFunc) *ListenerFunc

NewListener | Instance a new listener

type Listeners

type Listeners []*ListenerFunc

Listeners | List of listeners

func GetListeners

func GetListeners(eventName string) (Listeners, error)

GetListeners | Return all listeners for an event

func (*Listeners) AddListener

func (listeners *Listeners) AddListener(listener *ListenerFunc)

AddListener | Adds a new listener

func (*Listeners) RemoveListener

func (listeners *Listeners) RemoveListener(listener *ListenerFunc)

RemoveListener | Remove a listener

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL