-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathlandscape_cxx_client.cpp
93 lines (74 loc) · 2.95 KB
/
landscape_cxx_client.cpp
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
85
86
87
88
89
90
91
92
93
#include <iostream>
#include "examples/xplatform/swift_from_cxx/landscape-Swift.h"
// The Swift module "SwiftLandscape" is imported as the C++ namespace
// "SwiftLandscape".
SwiftLandscape::VolcanoStatus invertVolcanoStatus(
SwiftLandscape::VolcanoStatus status) {
switch (status) {
case SwiftLandscape::VolcanoStatus::dormant:
// Returns `VolcanoStatus.active` case.
return SwiftLandscape::VolcanoStatus::active();
case SwiftLandscape::VolcanoStatus::active:
// Returns `VolcanoStatus.dormant` case.
return SwiftLandscape::VolcanoStatus::dormant();
}
}
void printLandmarkIdentifier(SwiftLandscape::LandmarkIdentifier identifier) {
switch (identifier) {
case SwiftLandscape::LandmarkIdentifier::name:
std::cout << (std::string)identifier.getName() << std::endl;
break;
case SwiftLandscape::LandmarkIdentifier::id:
std::cout << "unnamed landmark #" << identifier.getId() << std::endl;
break;
}
}
void structsAndClasses() {
std::cout << "----- Swift structs and classes in C++ -----" << std::endl;
// SwiftLandscape::MountainPeak is a Swift struct. But you must still use
// init(), because Swift initializers are not the same as C++
// constructors.
SwiftLandscape::MountainPeak mount_everest =
SwiftLandscape::MountainPeak::init("Mount Everest", 8848.0f);
// You can call a Swift struct member function from C++.
mount_everest.printDescription();
// Swift arrays are mapped to swift::Array<T> in C++.
auto peaks = swift::Array<SwiftLandscape::MountainPeak>::init();
{
SwiftLandscape::MountainPeak k2 =
SwiftLandscape::MountainPeak::init("K2", 8611.0f);
peaks.append(mount_everest);
peaks.append(k2);
}
SwiftLandscape::MountainRange himalayas =
SwiftLandscape::MountainRange::init(peaks);
himalayas.printPeaks();
}
void simpleEnums() {
std::cout << "----- Simple Swift enums -----" << std::endl;
auto dormant = SwiftLandscape::VolcanoStatus::dormant();
auto active = SwiftLandscape::VolcanoStatus::active();
std::cout << "dormant has C++ int value: "
<< static_cast<int>(SwiftLandscape::VolcanoStatus::cases::dormant)
<< std::endl;
std::cout << "active has C++ int value: "
<< static_cast<int>(SwiftLandscape::VolcanoStatus::cases::active)
<< std::endl;
auto inverted = invertVolcanoStatus(dormant);
std::cout << "inverted(dormant) == active? -> "
<< ((inverted == active) ? "true" : "false") << std::endl;
}
void enumsWithAssociatedTypes() {
std::cout << "----- Swift enums with associated types -----" << std::endl;
auto new_landmark_id = SwiftLandscape::LandmarkIdentifier::id(1234);
printLandmarkIdentifier(new_landmark_id);
auto new_landmark_name =
SwiftLandscape::LandmarkIdentifier::name("Eiffel Tower");
printLandmarkIdentifier(new_landmark_name);
}
int main(int argc, char* argv[]) {
structsAndClasses();
simpleEnums();
enumsWithAssociatedTypes();
return 0;
}