gentype 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env ruby
  2. TEMPLATE=<<EOT
  3. type XXX struct {
  4. handle * C.YYY
  5. }
  6. // Converts a zzz to it's underlying C pointer
  7. func (self * XXX) toC() *C.YYY {
  8. return (*C.YYY)(self.handle)
  9. }
  10. // Destroys the zzz.
  11. func (self *XXX) Destroy() {
  12. if self.handle != nil {
  13. C.al_destroy_zzz(self.toC())
  14. }
  15. self.handle = nil
  16. }
  17. // Wraps a C zzz into a go zzz
  18. func wrapXXXRaw(data *C.YYY) *XXX {
  19. if data == nil {
  20. return nil
  21. }
  22. return &XXX{data}
  23. }
  24. // Sets up a finalizer for this XXX that calls Destroy()
  25. func (self *XXX) SetDestroyFinalizer() *XXX {
  26. if self != nil {
  27. runtime.SetFinalizer(self, func(me *XXX) { me.Destroy() })
  28. }
  29. return self
  30. }
  31. // Wraps a C zzz into a go zzz and sets up a finalizer that calls Destroy()
  32. func wrapXXX(data *C.YYY) *XXX {
  33. self := wrapXXXRaw(data)
  34. return self.SetDestroyFinalizer()
  35. }
  36. EOT
  37. gotype = ARGV[0] || 'XXX'
  38. ctype = ARGV[1] || 'YYY'
  39. funcname = ARGV[2] || 'zzz'
  40. res = TEMPLATE.gsub('XXX', gotype).gsub('YYY',ctype).gsub('zzz', funcname)
  41. puts res