Skip to content

Commit fad0971

Browse files
committed
Add cpp-demo
1 parent 738a441 commit fad0971

File tree

11 files changed

+106
-29
lines changed

11 files changed

+106
-29
lines changed

android-fetchurl/.gitignore

Lines changed: 0 additions & 8 deletions
This file was deleted.

cpp-demo/Package.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// swift-tools-version:5.9
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "ATMProject",
6+
platforms: [.iOS(.v17), .macOS(.v14)],
7+
products: [
8+
.library(name: "ATMPackage", targets: ["ATMPackage"]),
9+
.executable(name: "ATMCLI", targets: ["ATMCLI"])
10+
],
11+
targets: [
12+
// C++ library target
13+
.target(
14+
name: "ATMWithdrawCpp",
15+
path: "Sources/ATMWithdrawCpp",
16+
publicHeadersPath: "include",
17+
cxxSettings: [
18+
.headerSearchPath("include"),
19+
.define("SWIFT_PACKAGE")
20+
]
21+
),
22+
// Swift wrapper target
23+
.target(
24+
name: "ATMPackage",
25+
dependencies: ["ATMWithdrawCpp"],
26+
path: "Sources/ATMPackage",
27+
swiftSettings: [
28+
// enable C++ interop
29+
.interoperabilityMode(.Cxx)
30+
]
31+
),
32+
// CLI executable target
33+
.executableTarget(
34+
name: "ATMCLI",
35+
dependencies: ["ATMPackage"],
36+
path: "Sources/ATMCLI",
37+
swiftSettings: [
38+
.interoperabilityMode(.Cxx)
39+
]
40+
)
41+
]
42+
)

cpp-demo/Sources/ATMCLI/main.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import ATMPackage
2+
import Foundation
3+
4+
print("🏧 ATM CLI 🏧")
5+
var atm = ATMWrapper(initialBalance: 1000)
6+
print("Current balance: \(atm.getBalance())")
7+
8+
for arg in CommandLine.arguments.dropFirst() {
9+
guard let amount = Int32(arg) else {
10+
fatalError("argument not a number: \(arg)")
11+
}
12+
if atm.withdraw(amount: amount) {
13+
print("✅ Withdrawn \(amount). New balance: \(atm.getBalance())")
14+
} else {
15+
print("❌ Insufficient funds.")
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import ATMWithdrawCpp
2+
3+
public struct ATMWrapper {
4+
private var underlying: ATM
5+
6+
public init(initialBalance: Int32) {
7+
underlying = ATM(initialBalance)
8+
}
9+
10+
public mutating func withdraw(amount: Int32) -> Bool {
11+
return underlying.withdraw(amount)
12+
}
13+
14+
public func getBalance() -> Int32 {
15+
return underlying.getBalance()
16+
}
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
class ATM {
4+
public:
5+
ATM(int initialBalance);
6+
bool withdraw(int amount);
7+
int getBalance() const;
8+
private:
9+
int balance;
10+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
#include "ATM.h"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "ATMWithdrawCpp/ATM.h"
2+
3+
ATM::ATM(int initialBalance) : balance(initialBalance) {}
4+
5+
bool ATM::withdraw(int amount) {
6+
if (balance >= amount) {
7+
balance -= amount;
8+
return true;
9+
}
10+
return false;
11+
}
12+
13+
int ATM::getBalance() const {
14+
return balance;
15+
}

cpp-demo/run.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh -ex
2+
skip android run ATMCLI 100 200 300 400 500

macro-sample/.gitignore

Lines changed: 0 additions & 8 deletions
This file was deleted.

macro-sample/Package.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
// swift-tools-version: 6.1
2-
// The swift-tools-version declares the minimum version of Swift required to build this package.
3-
42
import PackageDescription
53
import CompilerPluginSupport
64

75
let package = Package(
86
name: "MacrosDemo",
97
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .macCatalyst(.v13)],
108
products: [
11-
// Products define the executables and libraries a package produces, making them visible to other packages.
129
.library(
1310
name: "MacrosDemo",
1411
targets: ["MacrosDemo"]
@@ -22,8 +19,6 @@ let package = Package(
2219
.package(url: "https://github.com/swiftlang/swift-syntax.git", from: "600.0.0-latest"),
2320
],
2421
targets: [
25-
// Targets are the basic building blocks of a package, defining a module or a test suite.
26-
// Targets can depend on other targets in this package and products from dependencies.
2722
// Macro implementation that performs the source transformation of a macro.
2823
.macro(
2924
name: "MacrosDemoMacros",

0 commit comments

Comments
 (0)