Skip to content

Commit 3458f23

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

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
@@ -80,7 +80,16 @@ int main() {
8080

8181
try {
8282
auto cruncher = server->GetNumberCruncher();
83-
double pi = cruncher->ComputePi();
83+
// try to cast to INumberCruncher2 to check if the new interface is implemented
84+
MyInterfaces::INumberCruncher2Ptr cruncher2 = cruncher;
85+
double pi = 0;
86+
if (cruncher2) {
87+
// use new interface
88+
pi = cruncher2->ComputePi2();
89+
} else {
90+
// fallback to old interface
91+
pi = cruncher->ComputePi();
92+
}
8493
std::wcout << L"pi = " << pi << std::endl;
8594

8695
auto callback = MyClient::Create();

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)