blob: 3a85ee51ed3d221d55edcefc83fa245aa7f167a3 (
plain)
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
|
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Paul Buetow
package rpn
import (
"sync"
)
// Operations provides operator implementations and stack manipulation.
type Operations struct {
vars VariableStore
consts ConstantsProvider
mode CalculationMode
prefixMode PrefixMode
metricRegistry MetricReader
mu sync.RWMutex
}
// Ensure Operations implements all operator sub-interfaces at compile time.
var (
_ ArithmeticOperator = (*Operations)(nil)
_ LogarithmicOperator = (*Operations)(nil)
_ MetricOperator = (*Operations)(nil)
_ BooleanOperator = (*Operations)(nil)
_ HyperOperator = (*Operations)(nil)
_ StackOperator = (*Operations)(nil)
_ VariableOperator = (*Operations)(nil)
_ ConstantOperator = (*Operations)(nil)
_ PowerIntOperator = (*Operations)(nil)
_ ModeController = (*Operations)(nil)
_ MetricCommander = (*Operations)(nil)
_ CustomMetricManager = (*Operations)(nil)
_ OperationsProvider = (*Operations)(nil)
_ OperatorProvider = (*Operations)(nil)
)
// NewOperations creates a new Operations instance with the given variable store.
// Does not create a ConstantsProvider internally; caller must use SetConstants.
// If no registry is provided, defaults to the global MetricRegistry.
func NewOperations(vars VariableStore, reg MetricReader) *Operations {
r := MetricReader(GetMetricRegistry())
if reg != nil {
r = reg
}
return &Operations{
vars: vars,
mode: FloatMode, // default
prefixMode: SI, // default
metricRegistry: r,
}
}
// SetConstants sets the constants provider for the Operations instance.
// This allows sharing a single ConstantsProvider between RPN and Operations.
func (o *Operations) SetConstants(c ConstantsProvider) {
o.consts = c
}
// SetMode sets the calculation mode for the Operations instance.
// This method is thread-safe for writes.
func (o *Operations) SetMode(mode CalculationMode) {
o.mu.Lock()
defer o.mu.Unlock()
o.mode = mode
}
// GetMode returns the current calculation mode.
// This method is thread-safe for reads.
func (o *Operations) GetMode() CalculationMode {
o.mu.RLock()
defer o.mu.RUnlock()
return o.mode
}
// GetPrefixMode returns the current prefix mode.
// This method is thread-safe for reads.
func (o *Operations) GetPrefixMode() PrefixMode {
o.mu.RLock()
defer o.mu.RUnlock()
return o.prefixMode
}
// SetPrefixMode sets the prefix mode for data size calculations.
// This method is thread-safe for writes.
func (o *Operations) SetPrefixMode(mode PrefixMode) {
o.mu.Lock()
defer o.mu.Unlock()
o.prefixMode = mode
}
// MetricRegistry returns the metric registry used by this Operations instance.
func (o *Operations) MetricRegistry() MetricReader {
return o.metricRegistry
}
|