cutego/internal/binding/parser/variable.go

194 lines
4.5 KiB
Go
Raw Normal View History

package parser
import (
"fmt"
"strings"
)
type Variable struct {
2016-12-11 23:03:06 +03:00
Name string `xml:"name,attr"`
Fullname string `xml:"fullname,attr"`
Href string `xml:"href,attr"`
Status string `xml:"status,attr"`
Access string `xml:"access,attr"`
Filepath string `xml:"filepath,attr"`
Static bool `xml:"static,attr"`
Output string `xml:"type,attr"`
Brief string `xml:"brief,attr"`
Getter []struct{} `xml:"getter"`
Setter []struct{} `xml:"setter"`
IsMocSynthetic bool
PureGoType string
Connect int
ConnectGet bool
ConnectSet bool
ConnectChanged bool
Target string
Inbound bool
}
2016-12-11 23:03:06 +03:00
func (v *Variable) Class() (*Class, bool) {
var class, ok = State.ClassMap[v.ClassName()]
return class, ok
2016-12-11 23:03:06 +03:00
}
2017-01-02 19:01:18 +03:00
2016-12-11 23:03:06 +03:00
func (v *Variable) ClassName() string {
2016-12-01 20:35:01 +03:00
var s = strings.Split(v.Fullname, "::")
if len(s) == 3 {
return s[1]
}
return s[0]
}
2016-12-11 23:03:06 +03:00
func (v *Variable) varToFunc() []*Function {
var funcs = make([]*Function, 0)
var class, ok = v.Class()
if !ok || class.HasFunctionWithName(v.Name) {
2016-12-11 23:03:06 +03:00
return funcs
}
2016-12-11 23:03:06 +03:00
funcs = append(funcs, &Function{
Name: v.Name,
Fullname: v.Fullname,
Href: v.Href,
Status: v.Status,
Access: v.Access,
Filepath: v.Filepath,
Static: v.Static,
Output: v.Output,
Meta: GETTER,
Brief: v.Brief,
})
if strings.Contains(v.Output, "const") {
return funcs
}
funcs = append(funcs, &Function{
Name: fmt.Sprintf("set%v", strings.Title(v.Name)),
2016-12-11 23:03:06 +03:00
Fullname: fmt.Sprintf("%v::set%v", v.ClassName(), strings.Title(v.Name)),
Href: v.Href,
Status: v.Status,
Access: v.Access,
Filepath: v.Filepath,
Static: v.Static,
Output: "void",
2016-12-11 23:03:06 +03:00
Meta: SETTER,
2016-10-22 01:50:26 +03:00
Parameters: []*Parameter{{Value: v.Output}},
TmpName: v.Name,
2016-11-01 21:52:59 +03:00
Brief: v.Brief,
2016-12-11 23:03:06 +03:00
})
return funcs
}
2016-11-01 21:52:59 +03:00
2016-12-11 23:03:06 +03:00
func (v *Variable) propToFunc(c *Class) []*Function {
var funcs = make([]*Function, 0)
2016-11-01 21:52:59 +03:00
2016-12-11 23:03:06 +03:00
if len(v.Getter) != 0 {
2016-11-01 21:52:59 +03:00
return funcs
}
2016-12-11 23:03:06 +03:00
if !(c.HasFunctionWithName(v.Name) ||
c.HasFunctionWithName(fmt.Sprintf("is%v", strings.Title(v.Name))) ||
c.HasFunctionWithName(fmt.Sprintf("has%v", strings.Title(v.Name)))) {
tmpF := &Function{
Name: v.Name,
Fullname: v.Fullname,
Href: v.Href,
Status: v.Status,
Access: v.Access,
Filepath: v.Filepath,
Static: v.Static,
Output: v.Output,
Meta: PLAIN,
Virtual: func() string {
if c.Module == MOC && !v.IsMocSynthetic {
return IMPURE
}
return ""
}(),
Signature: "()",
IsMocFunction: c.Module == MOC,
IsMocProperty: c.Module == MOC,
2016-12-11 23:03:06 +03:00
}
2016-11-01 21:52:59 +03:00
2016-12-11 23:03:06 +03:00
if tmpF.Output == "bool" {
if !strings.HasPrefix(strings.ToLower(v.Name), "is") {
tmpF.Name = fmt.Sprintf("is%v", strings.Title(tmpF.Name))
}
2016-12-11 23:03:06 +03:00
tmpF.Fullname = fmt.Sprintf("%v::%v", tmpF.ClassName(), tmpF.Name)
2016-11-01 21:52:59 +03:00
}
2016-12-11 23:03:06 +03:00
funcs = append(funcs, tmpF)
2016-11-01 21:52:59 +03:00
}
2016-12-11 23:03:06 +03:00
if len(v.Setter) != 0 || c.HasFunctionWithName(fmt.Sprintf("set%v", strings.Title(v.Name))) {
return funcs
}
2016-11-01 21:52:59 +03:00
2016-12-11 23:03:06 +03:00
funcs = append(funcs, &Function{
Name: fmt.Sprintf("set%v", strings.Title(v.Name)),
Fullname: fmt.Sprintf("%v::set%v", v.ClassName(), strings.Title(v.Name)),
Href: v.Href,
Status: v.Status,
Access: v.Access,
Filepath: v.Filepath,
Static: v.Static,
Output: "void",
Meta: PLAIN,
Virtual: func() string {
if c.Module == MOC && !v.IsMocSynthetic {
return IMPURE
}
return ""
}(),
Parameters: []*Parameter{{Name: v.Name, Value: v.Output}},
Signature: "()",
IsMocFunction: c.Module == MOC,
IsMocProperty: c.Module == MOC,
2016-12-11 23:03:06 +03:00
})
2016-11-01 21:52:59 +03:00
2017-01-18 21:28:40 +03:00
if c.Module == MOC {
funcs = append(funcs, &Function{
Name: fmt.Sprintf("%vChanged", v.Name),
Fullname: fmt.Sprintf("%v::%vChanged", v.ClassName(), v.Name),
Status: v.Status,
Access: v.Access,
Output: "void",
Meta: SIGNAL,
Parameters: []*Parameter{{Name: v.Name, Value: v.Output}},
Signature: "()",
IsMocFunction: true,
})
}
2016-12-11 23:03:06 +03:00
//add all overloaded property functions from base classes
//TODO: move rest into seperate function, as this func is called multiple times
2016-12-11 23:03:06 +03:00
for _, bc := range c.GetAllBases() {
var bclass, ok = State.ClassMap[bc]
if !ok {
2016-12-11 23:03:06 +03:00
continue
2016-11-01 21:52:59 +03:00
}
2016-12-11 23:03:06 +03:00
for _, bcf := range bclass.Functions {
if bcf.Name != fmt.Sprintf("set%v", strings.Title(v.Name)) || !bcf.Overload {
continue
2016-11-01 21:52:59 +03:00
}
2016-12-11 23:03:06 +03:00
var tmpF = *bcf
tmpF.Name = fmt.Sprintf("set%v", strings.Title(v.Name))
tmpF.Fullname = fmt.Sprintf("%v::%v", v.ClassName(), tmpF.Name)
funcs = append(funcs, &tmpF)
2016-11-01 21:52:59 +03:00
}
}
return funcs
}