Skip to content

Commit

Permalink
changed structs to type alias, addded documentation to high level fun…
Browse files Browse the repository at this point in the history
…ctions (#5 #4)
  • Loading branch information
ear7h committed Aug 19, 2018
1 parent b61f0b3 commit 4ff4d3c
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 85 deletions.
4 changes: 3 additions & 1 deletion mbgl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ This directory contains direct go bindings against the Mapbox GL Native C++ API.
* in the higher level package, `runtime.SetFinalizer` is used for memory management, note that this cannot be done for classes (which are aliased as empty structs) as:

> It is not guaranteed that a finalizer will run if the size of *obj is zero bytes.
> https://golang.org/pkg/runtime/#SetFinalizer
> https://golang.org/pkg/runtime/#SetFinalizer
* after calling `(T).Destruct()` make sure to not reuse the variable, this will result in strange errors; another `(T).Destruct()` call might yeild something like "freed block must be allocated with malloc".
14 changes: 8 additions & 6 deletions mbgl/camera_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import "C"
*/
import "C"

type EdgeInsets struct {
ptr *C.MbglEdgeInsets
type EdgeInsets C.MbglEdgeInsets

func (ei *EdgeInsets) edgeInsets() *C.MbglEdgeInsets {
return (*C.MbglEdgeInsets)(ei)
}

func NewEdgeInsets(top, left, bottom, right float64) *EdgeInsets {
Expand All @@ -18,11 +20,11 @@ func NewEdgeInsets(top, left, bottom, right float64) *EdgeInsets {
C.double(right),
)

return &EdgeInsets{ptr:ptr}
return (*EdgeInsets)(ptr)
}

func (ei *EdgeInsets) Destruct() {
C.mbgl_edge_insets_destruct(ei.ptr)
C.mbgl_edge_insets_destruct(ei.edgeInsets())
}

type Point struct {
Expand Down Expand Up @@ -69,12 +71,12 @@ func (opt *CameraOptions) update() {
// todo (@ear7h): change structs to wrapped types
var center *C.MbglLatLng
if opt.Center != nil {
center = opt.Center.ptr
center = opt.Center.latLng()
}

var padding *C.MbglEdgeInsets
if opt.Padding != nil {
padding = opt.Padding.ptr
padding = opt.Padding.edgeInsets()
}

if opt.ptr == nil {
Expand Down
18 changes: 9 additions & 9 deletions mbgl/default_file_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ package mbgl
*/
import "C"

type DefaultFileSource struct {
ptr *C.MbglDefaultFileSource
type DefaultFileSource C.MbglDefaultFileSource

func (fs *DefaultFileSource) defaultFileSource() *C.MbglDefaultFileSource {
return (*C.MbglDefaultFileSource)(fs)
}

// This instantiates a new file source which can handle online and offline sources. It will create a chache file
func NewDefaultFileSource(cachePath, assetRoot string, maxCache *uint64) *DefaultFileSource {
ptr := C.mbgl_default_file_source_new(C.CString(cachePath),
C.CString(assetRoot),
(*C.uint64_t)(maxCache))

return &DefaultFileSource{
ptr: ptr,
}
return (*DefaultFileSource)(ptr)
}

func (fs DefaultFileSource) fileSource() *C.MbglFileSource {
return (*C.MbglFileSource)(fs.ptr)
func (fs *DefaultFileSource) fileSource() *C.MbglFileSource {
return (*C.MbglFileSource)(fs.defaultFileSource())
}

func (fs *DefaultFileSource) Destruct() {
C.mbgl_default_file_source_destruct(fs.ptr)
fs.ptr = nil
C.mbgl_default_file_source_destruct(fs.defaultFileSource())
}
28 changes: 15 additions & 13 deletions mbgl/lat_lng.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,29 @@ import "C"
import "github.com/go-spatial/geom/slippy"

//TODO (@ear7h): use github.com/go-spatial/geom type
type LatLng struct {
ptr *C.MbglLatLng
}
type LatLng C.MbglLatLng

func NewLatLng(lat, lng float64) *LatLng {
ptr := C.mbgl_lat_lng_new(C.double(lat), C.double(lng))

return &LatLng{ptr:ptr}
return (*LatLng)(ptr)
}

func (ll *LatLng) latLng() *C.MbglLatLng {
return (*C.MbglLatLng)(ll)
}

func (ll *LatLng) Destruct() {
C.mbgl_lat_lng_destruct(ll.ptr)
ll.ptr = nil
C.mbgl_lat_lng_destruct(ll.latLng())
}


// bounds

type LatLngBounds struct {
ptr *C.MbglLatLngBounds
}
type LatLngBounds C.MbglLatLngBounds

func NewLatLngBounds(a, b *LatLng) *LatLngBounds {
ptr := C.mbgl_lat_lng_bounds_hull(a.ptr, b.ptr)
return &LatLngBounds{ptr:ptr}
ptr := C.mbgl_lat_lng_bounds_hull(a.latLng(), b.latLng())
return (*LatLngBounds)(ptr)
}

func NewLatLngBoundsFromTile(tile *slippy.Tile) *LatLngBounds {
Expand All @@ -48,6 +46,10 @@ func NewLatLngBoundsFromTile(tile *slippy.Tile) *LatLngBounds {
return ret
}

func (bb *LatLngBounds) latLngBounds() *C.MbglLatLngBounds {
return (*C.MbglLatLngBounds)(bb)
}

func (bb *LatLngBounds) Destruct() {
C.mbgl_lat_lng_bounds_destruct(bb.ptr)
C.mbgl_lat_lng_bounds_destruct(bb.latLngBounds())
}
50 changes: 26 additions & 24 deletions mbgl/map_snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import (
"fmt"
)

type MapSnapshotter struct {
ptr *C.MbglMapSnapshotter
}
type MapSnapshotter C.MbglMapSnapshotter


func NewMapSnapshotter(src FileSource,
Expand All @@ -38,7 +36,7 @@ func NewMapSnapshotter(src FileSource,

var _region *C.MbglLatLngBounds
if region != nil {
_region = region.ptr
_region = region.latLngBounds()
}

var _cacheDir *C.char
Expand All @@ -57,43 +55,48 @@ func NewMapSnapshotter(src FileSource,
_region,
_cacheDir)

return &MapSnapshotter{ptr:ptr}
return (*MapSnapshotter)(ptr)
}

func (ms *MapSnapshotter) mapSnapshotter() *C.MbglMapSnapshotter {
return (*C.MbglMapSnapshotter)(ms)
}

func (ms MapSnapshotter) Snapshot() *PremultipliedImage {
ptr := C.mbgl_map_snapshotter_snapshot(ms.ptr)
return &PremultipliedImage{ptr:ptr}
func (ms *MapSnapshotter) Snapshot() *PremultipliedImage {
ptr := C.mbgl_map_snapshotter_snapshot(ms.mapSnapshotter())
return (*PremultipliedImage)(ptr)
}

func (ms MapSnapshotter) SetCameraOptions(camOpts CameraOptions) {
C.mbgl_map_snapshotter_set_camera_options(ms.ptr, camOpts.cPtr())
func (ms *MapSnapshotter) SetCameraOptions(camOpts CameraOptions) {
C.mbgl_map_snapshotter_set_camera_options(ms.mapSnapshotter(), camOpts.cPtr())
}

func (ms MapSnapshotter) SetRegion(region *LatLngBounds) {
C.mbgl_map_snapshotter_set_region(ms.ptr, region.ptr)
func (ms *MapSnapshotter) SetRegion(region *LatLngBounds) {
C.mbgl_map_snapshotter_set_region(ms.mapSnapshotter(), region.latLngBounds())
}

func (ms MapSnapshotter) SetStyleURL(style string) {
func (ms *MapSnapshotter) SetStyleURL(style string) {
fmt.Println("setting style, ", style)
C.mbgl_map_snapshotter_set_style_url(ms.ptr, C.CString(style))
C.mbgl_map_snapshotter_set_style_url(ms.mapSnapshotter(), C.CString(style))
fmt.Println("style set")
}

func (ms MapSnapshotter) SetSize(size Size) {
C.mbgl_map_snapshotter_set_size(ms.ptr, size.cSize())
func (ms *MapSnapshotter) SetSize(size Size) {
C.mbgl_map_snapshotter_set_size(ms.mapSnapshotter(), size.cSize())
}

func (ms *MapSnapshotter) Destruct() {
C.mbgl_map_snapshotter_destruct(ms.ptr)
ms.ptr = nil
C.mbgl_map_snapshotter_destruct(ms.mapSnapshotter())
}

type PremultipliedImage struct {
ptr *C.MbglPremultipliedImage
type PremultipliedImage C.MbglPremultipliedImage

func (im *PremultipliedImage) premultipliedImage() *C.MbglPremultipliedImage {
return (*C.MbglPremultipliedImage)(im)
}

func (im PremultipliedImage) Image() image.Image {
raw := C.mbgl_premultiplied_image_raw(im.ptr)
func (im *PremultipliedImage) Image() image.Image {
raw := C.mbgl_premultiplied_image_raw(im.premultipliedImage())

bytes := int(raw.width) * int(raw.height) * 4

Expand All @@ -106,8 +109,7 @@ func (im PremultipliedImage) Image() image.Image {
}

func (im *PremultipliedImage) Destruct() {
C.mbgl_premultiplied_image_destruct(im.ptr)
im.ptr = nil
C.mbgl_premultiplied_image_destruct(im.premultipliedImage())
}

type img struct {
Expand Down
9 changes: 5 additions & 4 deletions mbgl/map_snapshotter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/go-spatial/geom/slippy"
"path/filepath"
"strings"
"github.com/arolek/p"
)

func TestNewMapSnapshotter(t *testing.T) {
Expand Down Expand Up @@ -38,7 +39,7 @@ func TestNewMapSnapshotter(t *testing.T) {

testcases := map[string]tcase{
"1": {
src: NewDefaultFileSource("", "", nil),
src: NewDefaultFileSource("", "", p.Uint64(0)),
sched: NewThreadPool(4),
style: "https://osm.tegola.io/maps/osm/style.json",
size: Size{Width: 100, Height: 100},
Expand Down Expand Up @@ -94,7 +95,7 @@ func TestSnapshotterSnapshot(t *testing.T) {
testcases := map[string]tcase{
"1": {
ms: NewMapSnapshotter(
NewDefaultFileSource("", "", nil),
NewDefaultFileSource("", "", p.Uint64(0)),
tpool,
"https://osm.tegola.io/maps/osm/style.json",
Size{Height: 100, Width: 100},
Expand Down Expand Up @@ -122,7 +123,7 @@ func TestSnapshotterSetCamOpts(t *testing.T) {
SchedulerSetCurrent(tpool)

ms := NewMapSnapshotter(
NewDefaultFileSource("", "", nil),
NewDefaultFileSource("", "", p.Uint64(0)),
tpool,
"https://osm.tegola.io/maps/osm/style.json",
Size{Height: 100, Width: 100},
Expand Down Expand Up @@ -171,7 +172,7 @@ func TestSnapshotterSetRegion(t *testing.T) {
SchedulerSetCurrent(tpool)

ms := NewMapSnapshotter(
NewDefaultFileSource("","", nil),
NewDefaultFileSource("","", p.Uint64(0)),
tpool,
"https://osm.tegola.io/maps/osm/style.json",
Size{Height: 100, Width: 100},
Expand Down
12 changes: 4 additions & 8 deletions mbgl/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ type Scheduler interface {
}

// Satisfies the schduler inteface
type scheduler struct {
ptr *C.MbglScheduler
}
type scheduler C.MbglScheduler

func (s *scheduler) scheduler() *C.MbglScheduler {
return s.ptr
return (*C.MbglScheduler)(s)
}

func (s *scheduler) Destruct() {
C.mbgl_scheduler_destruct(s.ptr)
C.mbgl_scheduler_destruct(s.scheduler())
}

func SchedulerGetCurrent() Scheduler {
Expand All @@ -30,9 +28,7 @@ func SchedulerGetCurrent() Scheduler {
return nil
}

return &scheduler{
ptr: ptr,
}
return (*scheduler)(ptr)
}

func SchedulerSetCurrent(sched Scheduler) {
Expand Down
19 changes: 7 additions & 12 deletions mbgl/thread_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,23 @@ package mbgl
*/
import "C"

type ThreadPool struct {
ptr *C.MbglThreadPool
}
type ThreadPool C.MbglThreadPool

func NewThreadPool(threads int) (*ThreadPool) {
ptr := C.mbgl_thread_pool_new(C.int(threads))

return &ThreadPool{
ptr: ptr,
}
return (*ThreadPool)(ptr)
}

func (t ThreadPool) threadPool() *C.MbglThreadPool {
return t.ptr
func (t *ThreadPool) threadPool() *C.MbglThreadPool {
return (*C.MbglThreadPool)(t)
}

// Scheduler is a prarent class
func (t ThreadPool) scheduler() *C.MbglScheduler {
return (*C.MbglScheduler)(t.ptr)
func (t *ThreadPool) scheduler() *C.MbglScheduler {
return (*C.MbglScheduler)(t)
}

func (t *ThreadPool) Destruct() {
C.mbgl_thread_pool_destruct(t.ptr)
t.ptr = nil
C.mbgl_thread_pool_destruct(t.threadPool())
}
Loading

0 comments on commit 4ff4d3c

Please sign in to comment.