Procházet zdrojové kódy

Use generics for fifo buffer.

Steve Thielemann před 2 roky
rodič
revize
c748cac29e
4 změnil soubory, kde provedl 48 přidání a 27 odebrání
  1. 21 0
      door/LICENSE
  2. 12 12
      door/door.go
  3. 7 7
      door/fifobuffer.go
  4. 8 8
      door/fifobuffer_test.go

+ 21 - 0
door/LICENSE

@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2022 Steve Thielemann
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.

+ 12 - 12
door/door.go

@@ -115,17 +115,17 @@ type Door struct {
 	Config         DropfileConfig
 	READFD         int
 	WRITEFD        int
-	Disconnected   bool            // int32         // atomic bool      // Has User disconnected/Hung up?
-	TimeOut        time.Time       // Fixed point in time, when time expires
-	StartTime      time.Time       // Time when User started door
-	Pushback       FIFOBuffer      // Key buffer
-	LastColor      []int           // Track the last color sent for restore color
-	ReaderClosed   bool            // Reader close
-	readerChannel  chan ReaderData // Reading from the User
-	readerMutex    sync.Mutex      // Reader close mutex
-	ReaderCanClose bool            // We can close the reader (in tests)
-	WriterClosed   bool            // Writer closed
-	writerChannel  chan string     // Writing to the User
+	Disconnected   bool             // int32         // atomic bool      // Has User disconnected/Hung up?
+	TimeOut        time.Time        // Fixed point in time, when time expires
+	StartTime      time.Time        // Time when User started door
+	Pushback       FIFOBuffer[rune] // Key buffer
+	LastColor      []int            // Track the last color sent for restore color
+	ReaderClosed   bool             // Reader close
+	readerChannel  chan ReaderData  // Reading from the User
+	readerMutex    sync.Mutex       // Reader close mutex
+	ReaderCanClose bool             // We can close the reader (in tests)
+	WriterClosed   bool             // Writer closed
+	writerChannel  chan string      // Writing to the User
 	writerMutex    sync.RWMutex
 	LastMouse      []Mouse     // Store Mouse information
 	LastCursor     []CursorPos // Store Cursor pos information
@@ -358,7 +358,7 @@ func (d *Door) Detect() bool {
 // detect terminal capabilities.
 func (d *Door) Init(doorname string) {
 	var dropfile string
-	d.Pushback = NewFIFOBuffer(5)
+	d.Pushback = NewFIFOBuffer[rune](5)
 
 	// Get path to binary, and chdir to it.
 	var binaryPath string

+ 7 - 7
door/fifobuffer.go

@@ -2,20 +2,20 @@ package door
 
 import "log"
 
-type FIFOBuffer struct {
-	data  []rune
+type FIFOBuffer[T any] struct {
+	data  []T
 	index int
 }
 
-func NewFIFOBuffer(maxsize int) FIFOBuffer {
-	return FIFOBuffer{data: make([]rune, maxsize)}
+func NewFIFOBuffer[T any](maxsize int) FIFOBuffer[T] {
+	return FIFOBuffer[T]{data: make([]T, maxsize)}
 }
 
-func (f *FIFOBuffer) Empty() bool {
+func (f *FIFOBuffer[T]) Empty() bool {
 	return f.index == 0
 }
 
-func (f *FIFOBuffer) Push(value rune) {
+func (f *FIFOBuffer[T]) Push(value T) {
 	if f.index >= len(f.data) {
 		log.Panicf("Exceeded FIFOBuffer index %d size %d %#v", f.index, len(f.data), f.data)
 	}
@@ -23,7 +23,7 @@ func (f *FIFOBuffer) Push(value rune) {
 	f.index++
 }
 
-func (f *FIFOBuffer) Pop() rune {
+func (f *FIFOBuffer[T]) Pop() T {
 	if f.index == 0 {
 		log.Panicf("Pop from empty FIFOBuffer (size %d).", len(f.data))
 	}

+ 8 - 8
door/fifobuffer_test.go

@@ -6,7 +6,7 @@ import (
 )
 
 func TestFIFOEmpty(t *testing.T) {
-	buffer := NewFIFOBuffer(3)
+	buffer := NewFIFOBuffer[rune](3)
 
 	defer func() {
 		if r := recover(); r == nil {
@@ -14,9 +14,9 @@ func TestFIFOEmpty(t *testing.T) {
 		}
 	}()
 
-	buffer.Push(1)
+	buffer.Push(rune(1))
 	x := buffer.Pop()
-	if x != 1 {
+	if x != rune(1) {
 		t.Errorf("Buffer did not return expected value 1: %d", x)
 	}
 
@@ -24,7 +24,7 @@ func TestFIFOEmpty(t *testing.T) {
 }
 
 func TestFIFOOverflow(t *testing.T) {
-	buffer := NewFIFOBuffer(3)
+	buffer := NewFIFOBuffer[rune](3)
 
 	defer func() {
 		if r := recover(); r == nil {
@@ -32,9 +32,9 @@ func TestFIFOOverflow(t *testing.T) {
 		}
 	}()
 
-	buffer.Push(1)
-	buffer.Push(2)
-	buffer.Push(3)
-	buffer.Push(4)
+	buffer.Push(rune(1))
+	buffer.Push(rune(2))
+	buffer.Push(rune(3))
+	buffer.Push(rune(4))
 
 }