FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/Codecs/EncoderGroup.cs at master · generalloki/pythonnet · GitHub
generalloki
pythonnet
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
Codecs
/
EncoderGroup.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
85 lines (76 loc) · 2.73 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
Codecs
/
EncoderGroup.cs
Copy path
File metadata and controls
85 lines (76 loc) · 2.73 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
namespace
Python
.
Runtime
.
Codecs
{
using
System
;
using
System
.
Collections
;
using
System
.
Collections
.
Generic
;
using
System
.
Linq
;
/// <summary>
/// Represents a group of <see cref="IPyObjectDecoder"/>s. Useful to group them by priority.
/// </summary>
public
sealed
class
EncoderGroup
:
IPyObjectEncoder
,
IEnumerable
<
IPyObjectEncoder
>
,
IDisposable
{
readonly
List
<
IPyObjectEncoder
>
encoders
=
new
(
)
;
/// <summary>
/// Add specified encoder to the group
/// </summary>
public
void
Add
(
IPyObjectEncoder
item
)
{
if
(
item
is
null
)
throw
new
ArgumentNullException
(
nameof
(
item
)
)
;
this
.
encoders
.
Add
(
item
)
;
}
/// <summary>
/// Remove all encoders from the group
/// </summary>
public
void
Clear
(
)
=>
this
.
encoders
.
Clear
(
)
;
/// <inheritdoc />
public
bool
CanEncode
(
Type
type
)
=>
this
.
encoders
.
Any
(
encoder
=>
encoder
.
CanEncode
(
type
)
)
;
/// <inheritdoc />
public
PyObject
?
TryEncode
(
object
value
)
{
if
(
value
is
null
)
throw
new
ArgumentNullException
(
nameof
(
value
)
)
;
foreach
(
var
encoder
in
this
.
GetEncoders
(
value
.
GetType
(
)
)
)
{
var
result
=
encoder
.
TryEncode
(
value
)
;
if
(
result
!=
null
)
{
return
result
;
}
}
return
null
;
}
/// <inheritdoc />
public
IEnumerator
<
IPyObjectEncoder
>
GetEnumerator
(
)
=>
this
.
encoders
.
GetEnumerator
(
)
;
IEnumerator
IEnumerable
.
GetEnumerator
(
)
=>
this
.
encoders
.
GetEnumerator
(
)
;
public
void
Dispose
(
)
{
foreach
(
var
encoder
in
this
.
encoders
.
OfType
<
IDisposable
>
(
)
)
{
encoder
.
Dispose
(
)
;
}
this
.
encoders
.
Clear
(
)
;
}
}
public
static
class
EncoderGroupExtensions
{
/// <summary>
/// Gets specific instances of <see cref="IPyObjectEncoder"/>
/// (potentially selecting one from a collection),
/// that can encode the specified <paramref name="type"/>.
/// </summary>
public
static
IEnumerable
<
IPyObjectEncoder
>
GetEncoders
(
this
IPyObjectEncoder
decoder
,
Type
type
)
{
if
(
decoder
is
null
)
throw
new
ArgumentNullException
(
nameof
(
decoder
)
)
;
if
(
decoder
is
IEnumerable
<
IPyObjectEncoder
>
composite
)
{
foreach
(
var
nestedEncoder
in
composite
)
foreach
(
var
match
in
nestedEncoder
.
GetEncoders
(
type
)
)
{
yield
return
match
;
}
}
else
if
(
decoder
.
CanEncode
(
type
)
)
{
yield
return
decoder
;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL