Source file src/cmd/cgo/internal/swig/testdata/callback/main.go

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  )
    11  
    12  func main() {
    13  	if len(os.Args) != 2 {
    14  		fatal("usage: callback testname")
    15  	}
    16  	switch os.Args[1] {
    17  	default:
    18  		fatal("unknown test %q", os.Args[1])
    19  	case "Call":
    20  		testCall()
    21  	case "Callback":
    22  		testCallback()
    23  	}
    24  	println("OK")
    25  }
    26  
    27  func fatal(f string, args ...any) {
    28  	fmt.Fprintln(os.Stderr, fmt.Sprintf(f, args...))
    29  	os.Exit(1)
    30  }
    31  
    32  type GoCallback struct{}
    33  
    34  func (p *GoCallback) Run() string {
    35  	return "GoCallback.Run"
    36  }
    37  
    38  func testCall() {
    39  	c := NewCaller()
    40  	cb := NewCallback()
    41  
    42  	c.SetCallback(cb)
    43  	s := c.Call()
    44  	if s != "Callback::run" {
    45  		fatal("unexpected string from Call: %q", s)
    46  	}
    47  	c.DelCallback()
    48  }
    49  
    50  func testCallback() {
    51  	c := NewCaller()
    52  	cb := NewDirectorCallback(&GoCallback{})
    53  	c.SetCallback(cb)
    54  	s := c.Call()
    55  	if s != "GoCallback.Run" {
    56  		fatal("unexpected string from Call with callback: %q", s)
    57  	}
    58  	c.DelCallback()
    59  	DeleteDirectorCallback(cb)
    60  }
    61  

View as plain text