This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfieldtypeset.go
107 lines (94 loc) · 2.14 KB
/
fieldtypeset.go
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
100
101
102
103
104
105
106
107
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tiff
import (
"encoding/json"
"fmt"
"sort"
"sync"
)
// FieldTypeSet represents a set of field types that may be in use within a file
// that uses a TIFF file structure. This can be customized for custom file
// formats and private IFDs.
type FieldTypeSet interface {
GetFieldType(id uint16) (FieldType, bool)
ListFieldTypes() []uint16
ListFieldTypeNames() []string
Name() string
Register(ft FieldType) bool
Lock()
}
func NewFieldTypeSet(name string) FieldTypeSet {
return &fieldTypeSet{
name: name,
types: make(map[uint16]FieldType, 1),
}
}
type fieldTypeSet struct {
mu sync.RWMutex
locked bool
name string
types map[uint16]FieldType
}
func (fts *fieldTypeSet) GetFieldType(id uint16) (FieldType, bool) {
fts.mu.RLock()
ft, ok := fts.types[id]
fts.mu.RUnlock()
return ft, ok
}
func (fts *fieldTypeSet) ListFieldTypes() []uint16 {
fts.mu.RLock()
ids := make([]uint16, len(fts.types))
i := 0
for id := range fts.types {
ids[i] = id
i++
}
sort.Sort(uint16Slice(ids))
fts.mu.RUnlock()
return ids
}
func (fts *fieldTypeSet) ListFieldTypeNames() []string {
fts.mu.RLock()
names := make([]string, len(fts.types))
i := 0
for _, ft := range fts.types {
names[i] = ft.Name()
i++
}
sort.Strings(names)
fts.mu.RUnlock()
return names
}
func (fts *fieldTypeSet) Name() string {
return fts.name
}
func (fts *fieldTypeSet) Register(ft FieldType) bool {
fts.mu.Lock()
defer fts.mu.Unlock()
// Disallow registration if the set is locked.
if fts.locked {
return false
}
// Just overwrite field types if they already exist. Users doing this
// should be generally aware of what they are doing.
fts.types[ft.ID()] = ft
return true
}
func (fts *fieldTypeSet) Lock() {
fts.mu.Lock()
fts.locked = true
fts.mu.Unlock()
}
func (fts *fieldTypeSet) MarshalJSON() ([]byte, error) {
tmp := struct {
Name string
}{
Name: fts.name,
}
return json.Marshal(tmp)
}
func (fts *fieldTypeSet) String() string {
return fmt.Sprintf("<FieldTypeSet: %q>", fts.name)
}