Skip to content

Commit f02e81c

Browse files
author
Fredrik Orderud
committed
Update C++ client to use new INumberCruncher2 interface if available.
1 parent a67ba74 commit f02e81c

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

MyClientCpp/Main.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,16 @@ int main() {
7272

7373
try {
7474
auto cruncher = server->GetNumberCruncher();
75-
double pi = cruncher->ComputePi();
75+
// try to cast to INumberCruncher2 to check if the new interface is implemented
76+
MyInterfaces::INumberCruncher2Ptr cruncher2 = cruncher;
77+
double pi = 0;
78+
if (cruncher2) {
79+
// use new interface
80+
pi = cruncher2->ComputePi2();
81+
} else {
82+
// fallback to old interface
83+
pi = cruncher->ComputePi();
84+
}
7685
std::wcout << L"pi = " << pi << std::endl;
7786

7887
auto callback = CreateLocalInstance<MyClient>();

MyClientCs/Program.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ static void CommunicateWithServer()
1313

1414
{
1515
var cruncher = server.GetNumberCruncher();
16-
double pi = cruncher.ComputePi();
16+
double pi = 0;
17+
try {
18+
// try to cast to INumberCruncher2 to check if the new interface is implemented
19+
var cruncher2 = (MyInterfaces.INumberCruncher2)cruncher;
20+
// use new interface
21+
pi = cruncher2.ComputePi2();
22+
} catch (System.InvalidCastException) {
23+
// fallback to old interface
24+
pi = cruncher.ComputePi();
25+
}
1726
Console.WriteLine($"pi = {pi}");
1827

1928
// release reference to help GC clean up (not strctly needed)

0 commit comments

Comments
 (0)