Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SuperElastix
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Analyze
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mirrors
SuperElastix
Commits
882e0853
Commit
882e0853
authored
9 years ago
by
Floris Berendsen
Browse files
Options
Downloads
Patches
Plain Diff
ENH: added acceptorinterface templated on providing interface. Code for dynamic
casts for the handshape are put in the acceptor generalized.
parent
b12e9100
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Modules/Core/ComponentInterface/componenthandshake.cxx
+62
-49
62 additions, 49 deletions
Modules/Core/ComponentInterface/componenthandshake.cxx
with
62 additions
and
49 deletions
Modules/Core/ComponentInterface/componenthandshake.cxx
+
62
−
49
View file @
882e0853
// multiple inheritance
// multiple inheritance
#include
<iostream>
#include
<iostream>
// test case: there are two (slightly) incompatible codebases (i.e. 3rd party and 4th party!), each with an optimizer object and a metric object.
// goal: make elastix components of all objects and define a handshake that checks if connections can be made.
/*************** below: example implementations of 3rd and 4th party code base (assume we cannot change that) *********************/
class
Metric3rdPartyBase
{
class
Metric3rdPartyBase
{
public:
public:
virtual
int
GetValue
()
=
0
;
virtual
int
GetValue
()
=
0
;
...
@@ -101,7 +105,8 @@ int GDOptimizer4rdParty::DoOptimization()
...
@@ -101,7 +105,8 @@ int GDOptimizer4rdParty::DoOptimization()
}
}
return
0
;
return
0
;
}
}
/*************** above: example implementations of 3rd and 4th party code base (assume we cannot change that) *********************/
/*************** below: our elastix wrappers to connect varoius components (we can redesign that) *********************/
class
ComponentBase
{
class
ComponentBase
{
public:
public:
virtual
~
ComponentBase
(){};
virtual
~
ComponentBase
(){};
...
@@ -116,22 +121,36 @@ class MetricValueInterface {
...
@@ -116,22 +121,36 @@ class MetricValueInterface {
public:
public:
virtual
int
GetValue
()
=
0
;
virtual
int
GetValue
()
=
0
;
};
};
template
<
class
InterfaceT
>
class
OptimizerValue
Interface
{
class
Interface
Acceptor
{
public:
public:
virtual
int
SetMetricValueComponentInterface
(
MetricValueInterface
*
)
=
0
;
virtual
int
Set
(
InterfaceT
*
)
=
0
;
int
Connect
(
ComponentBase
*
);
};
};
class
OptimizerDerivativeInterface
{
template
<
class
InterfaceT
>
public:
int
InterfaceAcceptor
<
InterfaceT
>::
Connect
(
ComponentBase
*
providerComponent
){
virtual
int
SetMetricDerivativeComponentInterface
(
MetricDerivativeInterface
*
)
=
0
;
};
InterfaceT
*
providerInterface
=
dynamic_cast
<
InterfaceT
*>
(
providerComponent
);
if
(
!
providerInterface
)
{
std
::
cout
<<
"providerComponent does not have required interface"
<<
std
::
endl
;
return
0
;
}
// connect value interfaces
this
->
Set
(
providerInterface
);
return
1
;
}
class
OptimizerUpdateInterface
{
class
OptimizerUpdateInterface
{
public:
public:
virtual
int
Update
()
=
0
;
virtual
int
Update
()
=
0
;
};
};
// wrapping into components:
// SSDMetric3rdPartyComponent provides a value and a derivative
class
SSDMetric3rdPartyComponent
:
public
ComponentBase
,
public
MetricDerivativeInterface
,
public
MetricValueInterface
{
class
SSDMetric3rdPartyComponent
:
public
ComponentBase
,
public
MetricDerivativeInterface
,
public
MetricValueInterface
{
public:
public:
SSDMetric3rdPartyComponent
();
SSDMetric3rdPartyComponent
();
...
@@ -160,6 +179,7 @@ int SSDMetric3rdPartyComponent::GetValue()
...
@@ -160,6 +179,7 @@ int SSDMetric3rdPartyComponent::GetValue()
return
this
->
theImplementation
->
GetValue
();
return
this
->
theImplementation
->
GetValue
();
};
};
// the GDOptimizer3rdParty expects that Metric3rdParty is will be set as input. All required interfaces will be delegated to a Metric3rdParty object.
class
Metric3rdPartyWrapper
:
public
Metric3rdPartyBase
{
class
Metric3rdPartyWrapper
:
public
Metric3rdPartyBase
{
public:
public:
void
SetMetricValueComponent
(
MetricValueInterface
*
);
void
SetMetricValueComponent
(
MetricValueInterface
*
);
...
@@ -191,15 +211,15 @@ int Metric3rdPartyWrapper::GetDerivative()
...
@@ -191,15 +211,15 @@ int Metric3rdPartyWrapper::GetDerivative()
return
this
->
metricderiv
->
GetDerivative
();
return
this
->
metricderiv
->
GetDerivative
();
}
}
class
GDOptimizer3rdPartyComponent
:
public
ComponentBase
,
public
Optimizer
ValueInterface
,
public
Optimizer
DerivativeInterface
,
public
OptimizerUpdateInterface
class
GDOptimizer3rdPartyComponent
:
public
ComponentBase
,
public
InterfaceAcceptor
<
Metric
ValueInterface
>
,
public
InterfaceAcceptor
<
Metric
DerivativeInterface
>
,
public
OptimizerUpdateInterface
{
{
public:
public:
GDOptimizer3rdPartyComponent
();
GDOptimizer3rdPartyComponent
();
~
GDOptimizer3rdPartyComponent
();
~
GDOptimizer3rdPartyComponent
();
GDOptimizer3rdParty
*
theImplementation
;
GDOptimizer3rdParty
*
theImplementation
;
Metric3rdPartyWrapper
*
MetricObject
;
Metric3rdPartyWrapper
*
MetricObject
;
int
Set
MetricValueComponentInterface
(
MetricValueInterface
*
);
int
Set
(
MetricValueInterface
*
);
int
Set
MetricDerivativeComponentInterface
(
MetricDerivativeInterface
*
);
int
Set
(
MetricDerivativeInterface
*
);
int
Update
();
int
Update
();
};
};
...
@@ -214,12 +234,12 @@ GDOptimizer3rdPartyComponent::~GDOptimizer3rdPartyComponent()
...
@@ -214,12 +234,12 @@ GDOptimizer3rdPartyComponent::~GDOptimizer3rdPartyComponent()
delete
this
->
MetricObject
;
delete
this
->
MetricObject
;
}
}
int
GDOptimizer3rdPartyComponent
::
Set
MetricValueComponentInterface
(
MetricValueInterface
*
component
)
int
GDOptimizer3rdPartyComponent
::
Set
(
MetricValueInterface
*
component
)
{
{
this
->
MetricObject
->
SetMetricValueComponent
(
component
);
this
->
MetricObject
->
SetMetricValueComponent
(
component
);
return
0
;
return
0
;
}
}
int
GDOptimizer3rdPartyComponent
::
Set
MetricDerivativeComponentInterface
(
MetricDerivativeInterface
*
component
)
int
GDOptimizer3rdPartyComponent
::
Set
(
MetricDerivativeInterface
*
component
)
{
{
this
->
MetricObject
->
SetMetricDerivativeComponent
(
component
);
this
->
MetricObject
->
SetMetricDerivativeComponent
(
component
);
return
0
;
return
0
;
...
@@ -271,14 +291,14 @@ int Metric4rdPartyWrapper::GetCost()
...
@@ -271,14 +291,14 @@ int Metric4rdPartyWrapper::GetCost()
return
this
->
metricval
->
GetValue
();
return
this
->
metricval
->
GetValue
();
}
}
class
GDOptimizer4rdPartyComponent
:
public
ComponentBase
,
public
Optimizer
ValueInterface
,
public
OptimizerUpdateInterface
class
GDOptimizer4rdPartyComponent
:
public
ComponentBase
,
public
InterfaceAcceptor
<
Metric
ValueInterface
>
,
public
OptimizerUpdateInterface
{
{
public:
public:
GDOptimizer4rdPartyComponent
();
GDOptimizer4rdPartyComponent
();
~
GDOptimizer4rdPartyComponent
();
~
GDOptimizer4rdPartyComponent
();
GDOptimizer4rdParty
*
theImplementation
;
GDOptimizer4rdParty
*
theImplementation
;
Metric4rdPartyWrapper
*
MetricObject
;
Metric4rdPartyWrapper
*
MetricObject
;
int
Set
MetricValueComponentInterface
(
MetricValueInterface
*
);
int
Set
(
MetricValueInterface
*
);
int
Update
();
int
Update
();
};
};
...
@@ -293,7 +313,7 @@ GDOptimizer4rdPartyComponent::~GDOptimizer4rdPartyComponent()
...
@@ -293,7 +313,7 @@ GDOptimizer4rdPartyComponent::~GDOptimizer4rdPartyComponent()
delete
this
->
MetricObject
;
delete
this
->
MetricObject
;
}
}
int
GDOptimizer4rdPartyComponent
::
Set
MetricValueComponentInterface
(
MetricValueInterface
*
component
)
int
GDOptimizer4rdPartyComponent
::
Set
(
MetricValueInterface
*
component
)
{
{
this
->
MetricObject
->
SetMetricValueComponent
(
component
);
this
->
MetricObject
->
SetMetricValueComponent
(
component
);
return
0
;
return
0
;
...
@@ -306,6 +326,9 @@ int GDOptimizer4rdPartyComponent::Update()
...
@@ -306,6 +326,9 @@ int GDOptimizer4rdPartyComponent::Update()
int
main
()
{
int
main
()
{
{
{
/************ testing interface casts ***********
* expected: ok
*/
SSDMetric3rdPartyComponent
*
tempmetric3p
=
new
SSDMetric3rdPartyComponent
();
SSDMetric3rdPartyComponent
*
tempmetric3p
=
new
SSDMetric3rdPartyComponent
();
ComponentBase
*
metric3p
=
tempmetric3p
;
ComponentBase
*
metric3p
=
tempmetric3p
;
...
@@ -325,20 +348,15 @@ int main () {
...
@@ -325,20 +348,15 @@ int main () {
GDOptimizer4rdPartyComponent
*
tempOptimizer4p
=
new
GDOptimizer4rdPartyComponent
();
GDOptimizer4rdPartyComponent
*
tempOptimizer4p
=
new
GDOptimizer4rdPartyComponent
();
ComponentBase
*
optimizer4p
=
tempOptimizer4p
;
// type returned by our component factory
ComponentBase
*
optimizer4p
=
tempOptimizer4p
;
// type returned by our component factory
MetricValueInterface
*
metvalIF
=
dynamic_cast
<
MetricValueInterface
*>
(
metric4p
);
InterfaceAcceptor
<
MetricValueInterface
>*
opValIF
=
dynamic_cast
<
InterfaceAcceptor
<
MetricValueInterface
>*>
(
optimizer4p
);
if
(
!
metvalIF
)
{
std
::
cout
<<
"metric4p has no MetricValueInterface"
<<
std
::
endl
;
}
OptimizerValueInterface
*
opValIF
=
dynamic_cast
<
OptimizerValueInterface
*>
(
optimizer4p
);
if
(
!
opValIF
)
if
(
!
opValIF
)
{
{
std
::
cout
<<
"optimizer4p has no OptimizerValueInterface"
<<
std
::
endl
;
std
::
cout
<<
"optimizer4p has no OptimizerValueInterface"
<<
std
::
endl
;
}
}
// connect value interfaces
// connect value interfaces
opValIF
->
SetMetricValueComponentInterface
(
metvalIF
);
opValIF
->
Connect
(
metric4p
);
OptimizerUpdateInterface
*
opUpdIF
=
dynamic_cast
<
OptimizerUpdateInterface
*>
(
optimizer4p
);
OptimizerUpdateInterface
*
opUpdIF
=
dynamic_cast
<
OptimizerUpdateInterface
*>
(
optimizer4p
);
if
(
!
opValIF
)
if
(
!
opValIF
)
{
{
...
@@ -359,20 +377,18 @@ int main () {
...
@@ -359,20 +377,18 @@ int main () {
GDOptimizer4rdPartyComponent
*
tempOptimizer4p
=
new
GDOptimizer4rdPartyComponent
();
GDOptimizer4rdPartyComponent
*
tempOptimizer4p
=
new
GDOptimizer4rdPartyComponent
();
ComponentBase
*
optimizer4p
=
tempOptimizer4p
;
// type returned by our component factory
ComponentBase
*
optimizer4p
=
tempOptimizer4p
;
// type returned by our component factory
MetricValueInterface
*
metvalIF
=
dynamic_cast
<
MetricValueInterface
*>
(
metric3p
);
InterfaceAcceptor
<
MetricValueInterface
>*
opValIF
=
dynamic_cast
<
InterfaceAcceptor
<
MetricValueInterface
>*>
(
optimizer4p
);
if
(
!
metvalIF
)
{
std
::
cout
<<
"metric3p has no MetricValueInterface"
<<
std
::
endl
;
}
OptimizerValueInterface
*
opValIF
=
dynamic_cast
<
OptimizerValueInterface
*>
(
optimizer4p
);
if
(
!
opValIF
)
if
(
!
opValIF
)
{
{
std
::
cout
<<
"optimizer4p has no OptimizerValueInterface"
<<
std
::
endl
;
std
::
cout
<<
"optimizer4p has no OptimizerValueInterface"
<<
std
::
endl
;
}
}
// connect value interfaces
// connect value interfaces
opValIF
->
SetMetricValueComponentInterface
(
metvalIF
);
if
(
!
opValIF
->
Connect
(
metric3p
))
{
std
::
cout
<<
"metric3p cannot connect to optimizer4p by ValueInterface"
<<
std
::
endl
;
}
OptimizerUpdateInterface
*
opUpdIF
=
dynamic_cast
<
OptimizerUpdateInterface
*>
(
optimizer4p
);
OptimizerUpdateInterface
*
opUpdIF
=
dynamic_cast
<
OptimizerUpdateInterface
*>
(
optimizer4p
);
if
(
!
opValIF
)
if
(
!
opValIF
)
{
{
...
@@ -393,34 +409,31 @@ int main () {
...
@@ -393,34 +409,31 @@ int main () {
GDOptimizer3rdPartyComponent
*
tempOptimizer3p
=
new
GDOptimizer3rdPartyComponent
();
GDOptimizer3rdPartyComponent
*
tempOptimizer3p
=
new
GDOptimizer3rdPartyComponent
();
ComponentBase
*
optimizer3p
=
tempOptimizer3p
;
// type returned by our component factory
ComponentBase
*
optimizer3p
=
tempOptimizer3p
;
// type returned by our component factory
MetricValueInterface
*
metvalIF
=
dynamic_cast
<
MetricValueInterface
*>
(
metric4p
);
InterfaceAcceptor
<
MetricValueInterface
>*
opValIF
=
dynamic_cast
<
InterfaceAcceptor
<
MetricValueInterface
>*>
(
optimizer3p
);
if
(
!
metvalIF
)
{
std
::
cout
<<
"metric4p has no MetricValueInterface"
<<
std
::
endl
;
}
OptimizerValueInterface
*
opValIF
=
dynamic_cast
<
OptimizerValueInterface
*>
(
optimizer3p
);
if
(
!
opValIF
)
if
(
!
opValIF
)
{
{
std
::
cout
<<
"optimizer
4
p has no OptimizerValueInterface"
<<
std
::
endl
;
std
::
cout
<<
"optimizer
3
p has no OptimizerValueInterface"
<<
std
::
endl
;
}
}
// connect value interfaces
// connect value interfaces
opValIF
->
SetMetricValueComponentInterface
(
metvalIF
);
if
(
!
opValIF
->
Connect
(
metric4p
))
MetricDerivativeInterface
*
metderivIF
=
dynamic_cast
<
MetricDerivativeInterface
*>
(
metric4p
);
if
(
!
metderivIF
)
{
{
std
::
cout
<<
"metric4p
has no MetricDerivativ
eInterface"
<<
std
::
endl
;
std
::
cout
<<
"metric4p
cannot connect to optimizer3p by Valu
eInterface"
<<
std
::
endl
;
}
}
OptimizerDerivativeInterface
*
opDerivIF
=
dynamic_cast
<
OptimizerDerivativeInterface
*>
(
optimizer3p
);
//opValIF->Set(tempmetric4p);
InterfaceAcceptor
<
MetricDerivativeInterface
>*
opDerivIF
=
dynamic_cast
<
InterfaceAcceptor
<
MetricDerivativeInterface
>*>
(
optimizer3p
);
if
(
!
opDerivIF
)
if
(
!
opDerivIF
)
{
{
std
::
cout
<<
"optimizer
4
p has no OptimizerDerivativeInterface"
<<
std
::
endl
;
std
::
cout
<<
"optimizer
3
p has no OptimizerDerivativeInterface"
<<
std
::
endl
;
}
}
// connect derivative interfaces
// connect derivative interfaces
opDerivIF
->
SetMetricDerivativeComponentInterface
(
metderivIF
);
if
(
!
opDerivIF
->
Connect
(
metric4p
))
{
std
::
cout
<<
"metric4p cannot connect to optimizer3p by DerivativeInterface"
<<
std
::
endl
;
}
OptimizerUpdateInterface
*
opUpdIF
=
dynamic_cast
<
OptimizerUpdateInterface
*>
(
optimizer3p
);
OptimizerUpdateInterface
*
opUpdIF
=
dynamic_cast
<
OptimizerUpdateInterface
*>
(
optimizer3p
);
if
(
!
opValIF
)
if
(
!
opValIF
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment