forked from AsunaU2/GoCGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT02_Param.go
More file actions
99 lines (77 loc) · 2.01 KB
/
T02_Param.go
File metadata and controls
99 lines (77 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//go:build T02
// +build T02
package main
import (
"fmt"
)
const ParamKey = "param_key"
// MyParam implements the GParam interface for parameter sharing
type MyParam struct {
BaseGParam
Val int `json:"val"`
Loop int `json:"loop"`
}
// Reset resets the parameter values
func (p *MyParam) Reset(curStatus *CStatus) {
p.Val = 0
}
// MyReadParamNode reads shared parameters
type MyReadParamNode struct {
BaseGElement
}
// Init initializes the node and creates shared parameters
func (n *MyReadParamNode) Init() *CStatus {
return n.CreateGParam(ParamKey, &MyParam{})
}
// Run executes the read logic
func (n *MyReadParamNode) Run() *CStatus {
param := n.GetGParam(ParamKey)
if param == nil {
return NewCStatusWithError("param not found")
}
myParam, ok := param.(*MyParam)
if !ok {
return NewCStatusWithError("param type mismatch")
}
// Lock for thread safety
myParam.GetLock().RLock()
fmt.Printf("%s: val = %d, loop = %d\n", n.GetName(), myParam.Val, myParam.Loop)
myParam.GetLock().RUnlock()
return NewCStatus()
}
// MyWriteParamNode writes to shared parameters
type MyWriteParamNode struct {
BaseGElement
}
// Run executes the write logic
func (n *MyWriteParamNode) Run() *CStatus {
param := n.GetGParam(ParamKey)
if param == nil {
return NewCStatusWithError("param not found")
}
myParam, ok := param.(*MyParam)
if !ok {
return NewCStatusWithError("param type mismatch")
}
// Lock for thread safety
myParam.GetLock().Lock()
myParam.Val++
myParam.Loop++
fmt.Printf("%s: val = %d, loop = %d\n", n.GetName(), myParam.Val, myParam.Loop)
myParam.GetLock().Unlock()
return NewCStatus()
}
// tutorialParam demonstrates parameter sharing between nodes
func tutorialParam() {
var w, r GElement
pipeline := Factory.Create()
w = &MyWriteParamNode{}
pipeline.RegisterGElement(w, []GElement{}, "WriteNode")
r = &MyReadParamNode{}
pipeline.RegisterGElement(r, []GElement{w}, "ReadNode")
pipeline.Process(5) // Run 5 times like the C++ version
Factory.Remove(pipeline)
}
func main() {
tutorialParam()
}