bool.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package binding
  2. type not struct {
  3. Bool
  4. }
  5. var _ Bool = (*not)(nil)
  6. // Not returns a Bool binding that invert the value of the given data binding.
  7. // This is providing the logical Not boolean operation as a data binding.
  8. //
  9. // Since 2.4
  10. func Not(data Bool) Bool {
  11. return &not{Bool: data}
  12. }
  13. func (n *not) Get() (bool, error) {
  14. v, err := n.Bool.Get()
  15. return !v, err
  16. }
  17. func (n *not) Set(value bool) error {
  18. return n.Bool.Set(!value)
  19. }
  20. type and struct {
  21. booleans
  22. }
  23. var _ Bool = (*and)(nil)
  24. // And returns a Bool binding that return true when all the passed Bool binding are
  25. // true and false otherwise. It does apply a logical and boolean operation on all passed
  26. // Bool bindings. This binding is two way. In case of a Set, it will propagate the value
  27. // identically to all the Bool bindings used for its construction.
  28. //
  29. // Since 2.4
  30. func And(data ...Bool) Bool {
  31. return &and{booleans: booleans{data: data}}
  32. }
  33. func (a *and) Get() (bool, error) {
  34. for _, d := range a.data {
  35. v, err := d.Get()
  36. if err != nil {
  37. return false, err
  38. }
  39. if !v {
  40. return false, nil
  41. }
  42. }
  43. return true, nil
  44. }
  45. func (a *and) Set(value bool) error {
  46. for _, d := range a.data {
  47. err := d.Set(value)
  48. if err != nil {
  49. return err
  50. }
  51. }
  52. return nil
  53. }
  54. type or struct {
  55. booleans
  56. }
  57. var _ Bool = (*or)(nil)
  58. // Or returns a Bool binding that return true when at least one of the passed Bool binding
  59. // is true and false otherwise. It does apply a logical or boolean operation on all passed
  60. // Bool bindings. This binding is two way. In case of a Set, it will propagate the value
  61. // identically to all the Bool bindings used for its construction.
  62. //
  63. // Since 2.4
  64. func Or(data ...Bool) Bool {
  65. return &or{booleans: booleans{data: data}}
  66. }
  67. func (o *or) Get() (bool, error) {
  68. for _, d := range o.data {
  69. v, err := d.Get()
  70. if err != nil {
  71. return false, err
  72. }
  73. if v {
  74. return true, nil
  75. }
  76. }
  77. return false, nil
  78. }
  79. func (o *or) Set(value bool) error {
  80. for _, d := range o.data {
  81. err := d.Set(value)
  82. if err != nil {
  83. return err
  84. }
  85. }
  86. return nil
  87. }
  88. type booleans struct {
  89. data []Bool
  90. }
  91. func (g *booleans) AddListener(listener DataListener) {
  92. for _, d := range g.data {
  93. d.AddListener(listener)
  94. }
  95. }
  96. func (g *booleans) RemoveListener(listener DataListener) {
  97. for _, d := range g.data {
  98. d.RemoveListener(listener)
  99. }
  100. }