FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ansys-pythonnet/src/runtime/Codecs/DecoderGroup.cs at update-python · ansys/ansys-pythonnet · GitHub
ansys
ansys-pythonnet
Repository navigation
Code
Issues
3
(3)
Pull requests
4
(4)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ansys-pythonnet
/
src
/
runtime
/
Codecs
/
DecoderGroup.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
84 lines (74 loc) · 2.86 KB
Breadcrumbs
ansys-pythonnet
/
src
/
runtime
/
Codecs
/
DecoderGroup.cs
Copy path
File metadata and controls
84 lines (74 loc) · 2.86 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
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
DecoderGroup
:
IPyObjectDecoder
,
IEnumerable
<
IPyObjectDecoder
>
,
IDisposable
{
readonly
List
<
IPyObjectDecoder
>
decoders
=
new
(
)
;
/// <summary>
/// Add specified decoder to the group
/// </summary>
public
void
Add
(
IPyObjectDecoder
item
)
{
if
(
item
is
null
)
throw
new
ArgumentNullException
(
nameof
(
item
)
)
;
this
.
decoders
.
Add
(
item
)
;
}
/// <summary>
/// Remove all decoders from the group
/// </summary>
public
void
Clear
(
)
=>
this
.
decoders
.
Clear
(
)
;
/// <inheritdoc />
public
bool
CanDecode
(
PyType
objectType
,
Type
targetType
)
=>
this
.
decoders
.
Any
(
decoder
=>
decoder
.
CanDecode
(
objectType
,
targetType
)
)
;
/// <inheritdoc />
public
bool
TryDecode
<
T
>
(
PyObject
pyObj
,
out
T
?
value
)
{
if
(
pyObj
is
null
)
throw
new
ArgumentNullException
(
nameof
(
pyObj
)
)
;
var
decoder
=
this
.
GetDecoder
(
pyObj
.
GetPythonType
(
)
,
typeof
(
T
)
)
;
if
(
decoder
is
null
)
{
value
=
default
;
return
false
;
}
return
decoder
.
TryDecode
(
pyObj
,
out
value
)
;
}
/// <inheritdoc />
public
IEnumerator
<
IPyObjectDecoder
>
GetEnumerator
(
)
=>
this
.
decoders
.
GetEnumerator
(
)
;
IEnumerator
IEnumerable
.
GetEnumerator
(
)
=>
this
.
decoders
.
GetEnumerator
(
)
;
public
void
Dispose
(
)
{
foreach
(
var
decoder
in
this
.
decoders
.
OfType
<
IDisposable
>
(
)
)
{
decoder
.
Dispose
(
)
;
}
this
.
decoders
.
Clear
(
)
;
}
}
public
static
class
DecoderGroupExtensions
{
/// <summary>
/// Gets a concrete instance of <see cref="IPyObjectDecoder"/>
/// (potentially selecting one from a collection),
/// that can decode from <paramref name="objectType"/> to <paramref name="targetType"/>,
/// or <c>null</c> if a matching decoder can not be found.
/// </summary>
public
static
IPyObjectDecoder
?
GetDecoder
(
this
IPyObjectDecoder
decoder
,
PyType
objectType
,
Type
targetType
)
{
if
(
decoder
is
null
)
throw
new
ArgumentNullException
(
nameof
(
decoder
)
)
;
if
(
decoder
is
IEnumerable
<
IPyObjectDecoder
>
composite
)
{
return
composite
.
Select
(
nestedDecoder
=>
nestedDecoder
.
GetDecoder
(
objectType
,
targetType
)
)
.
FirstOrDefault
(
d
=>
d
!=
null
)
;
}
return
decoder
.
CanDecode
(
objectType
,
targetType
)
?
decoder
:
null
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL