componenthandshake.cxx 5.41 KB
Newer Older
1
2
#include <iostream>

3
// test case: Assume there are two (slightly) incompatible codebases (i.e. 3rd party and 4th! party), each with an optimizer object and a metric object. The example classes of 3rd and 4th party code bases cannot be changed, but the wrapping is in our control.
4
5
// goal: make elastix components of all objects and define a handshake that checks if connections can be made.

6
7
8
9
#include "SSDMetric3rdPartyComponent.h"
#include "GDOptimizer3rdPartyComponent.h"
#include "SSDMetric4thPartyComponent.h"
#include "GDOptimizer4thPartyComponent.h"
10

11
12


13
14
using namespace elx;
  int main() {
15
16
17
18
19
20
21
22
23
24
    {
      std::cout << InterfaceName<MetricValueInterface>::Get() << std::endl;

      std::cout << AcceptorInterfaceName<InterfaceAcceptor<MetricValueInterface>>::Get() << std::endl;

      std::cout << InterfaceName<InterfaceAcceptor<MetricValueInterface>>::Get() << std::endl;
      std::cout << InterfaceName<InterfaceProvider<MetricValueInterface>>::Get() << std::endl;

      
    }
25
    {
26
27
28
29
30
      /************ testing interface casts ***********
      * expected: ok
      */
      SSDMetric3rdPartyComponent* tempmetric3p = new SSDMetric3rdPartyComponent();
      ComponentBase* metric3p = tempmetric3p;
31

32
33
      MetricValueInterface* valIF = dynamic_cast<MetricValueInterface*> (metric3p);
      std::cout << valIF->GetValue() << std::endl;
34

35
36
37
38
      MetricDerivativeInterface* derIF = dynamic_cast<MetricDerivativeInterface*> (metric3p);
      std::cout << derIF->GetDerivative() << std::endl;
    }
    /************ Connect metric4p to optimizer4p ***********
39
40
    * expected: ok
    */
41
  {
42
    SSDMetric4thPartyComponent* tempmetric4p = new SSDMetric4thPartyComponent();
43
    ComponentBase* metric4p = tempmetric4p; // type returned by our component factory
44

45
    GDOptimizer4thPartyComponent* tempOptimizer4p = new GDOptimizer4thPartyComponent();
46
    ComponentBase* optimizer4p = tempOptimizer4p; // type returned by our component factory
47

48
    interfaceStatus IFstatus = optimizer4p->ConnectFrom("MetricValueInterface", metric4p);
49

50
    InterfaceAcceptor<MetricValueInterface>* opValIF = dynamic_cast<InterfaceAcceptor<MetricValueInterface>*> (optimizer4p);
51
52
53
54
55
56
    if (!opValIF)
    {
      std::cout << "optimizer4p has no OptimizerValueInterface" << std::endl;
    }

    // connect value interfaces
57
58
    opValIF->Connect(metric4p);

59
60
61
62
63
    OptimizerUpdateInterface* opUpdIF = dynamic_cast<OptimizerUpdateInterface*> (optimizer4p);
    if (!opValIF)
    {
      std::cout << "optimizer4p has no OptimizerUpdateInterface" << std::endl;
    }
64

65
66
67
    // Update the optimizer component 
    opUpdIF->Update();
  }
68
69
  /************ Connect metric3p to optimizer4p ***********
  * expected: ok
70
  * optimizer4p will only use/have access to the GetValue interface of metric3p
71
  */
72
73
  {
    SSDMetric3rdPartyComponent* tempmetric3p = new SSDMetric3rdPartyComponent();
74
    ComponentBase* metric3p = tempmetric3p; // type returned by our component factory
75

76
    GDOptimizer4thPartyComponent* tempOptimizer4p = new GDOptimizer4thPartyComponent();
77
    ComponentBase* optimizer4p = tempOptimizer4p; // type returned by our component factory
78

79
    InterfaceAcceptor<MetricValueInterface>* opValIF = dynamic_cast<InterfaceAcceptor<MetricValueInterface>*> (optimizer4p);
80
81
82
83
84
85
    if (!opValIF)
    {
      std::cout << "optimizer4p has no OptimizerValueInterface" << std::endl;
    }

    // connect value interfaces
86
87
88
89
    if (!opValIF->Connect(metric3p))
    {
      std::cout << "metric3p cannot connect to optimizer4p by ValueInterface" << std::endl;
    }
90

91
92
93
94
95
96
97
98
99
    OptimizerUpdateInterface* opUpdIF = dynamic_cast<OptimizerUpdateInterface*> (optimizer4p);
    if (!opValIF)
    {
      std::cout << "optimizer4p has no OptimizerUpdateInterface" << std::endl;
    }

    // Update the optimizer component 
    opUpdIF->Update();
  }
100
101
102
103
104
  /************ Connect metric4p to optimizer3p ***********
  * expected: fail
  * optimizer3p needs a metric with GetDerivative which metric4p doesn't have
  */
  {
105
    SSDMetric4thPartyComponent* tempmetric4p = new SSDMetric4thPartyComponent();
106
107
108
109
110
    ComponentBase* metric4p = tempmetric4p; // type returned by our component factory

    GDOptimizer3rdPartyComponent* tempOptimizer3p = new GDOptimizer3rdPartyComponent();
    ComponentBase* optimizer3p = tempOptimizer3p; // type returned by our component factory

111
    InterfaceAcceptor<MetricValueInterface>* opValIF = dynamic_cast<InterfaceAcceptor<MetricValueInterface>*> (optimizer3p);
112
113
    if (!opValIF)
    {
114
      std::cout << "optimizer3p has no OptimizerValueInterface" << std::endl;
115
116
117
    }

    // connect value interfaces
118
    if (!opValIF->Connect(metric4p))
119
    {
120
      std::cout << "metric4p cannot connect to optimizer3p by ValueInterface" << std::endl;
121
    }
122
123
124
125

    //opValIF->Set(tempmetric4p);

    InterfaceAcceptor<MetricDerivativeInterface>* opDerivIF = dynamic_cast<InterfaceAcceptor<MetricDerivativeInterface>*> (optimizer3p);
126
127
    if (!opDerivIF)
    {
128
      std::cout << "optimizer3p has no OptimizerDerivativeInterface" << std::endl;
129
130
    }
    // connect derivative interfaces
131
132
133
134
    if (!opDerivIF->Connect(metric4p))
    {
      std::cout << "metric4p cannot connect to optimizer3p by DerivativeInterface" << std::endl;
    }
135

136
137
138
139
140
141
142
143
144
145

    OptimizerUpdateInterface* opUpdIF = dynamic_cast<OptimizerUpdateInterface*> (optimizer3p);
    if (!opValIF)
    {
      std::cout << "optimizer3p has no OptimizerUpdateInterface" << std::endl;
    }

    // Update the optimizer component 
    // opUpdIF->Update(); // will fail since the metric does'nt have GetDerivative()
  }
146
  return 0;
147
  }