-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.mm
More file actions
98 lines (77 loc) · 2.84 KB
/
Copy pathinterface.mm
File metadata and controls
98 lines (77 loc) · 2.84 KB
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
94
95
96
97
98
#import <Foundation/Foundation.h>
#include <iostream>
#include <vector>
#include <string>
// ==========================================
// 1. PURE C++ ENGINE CLASS
// ==========================================
class CPPDataEngine {
private:
std::vector<std::string> internalLog;
public:
CPPDataEngine() {
internalLog.push_back("C++ Engine Initialized.");
}
void processDataStream(const std::string& input) {
internalLog.push_back("Processed: " + input);
std::cout << "[C++ stdout] Engine logged string: " << input << std::endl;
}
size_t getLogCount() const {
return internalLog.size();
}
};
// ==========================================
// 2. OBJECTIVE-C INTERFACE
// ==========================================
@interface BridgeManager : NSObject {
// Objective-C instance variables can be C++ objects or pointers
CPPDataEngine *engineInstance;
}
@property (nonatomic, strong) NSString *managerName;
- (instancetype)initWithName:(NSString *)name;
- (void)executePipelineWithPayload:(NSString *)payload;
@end
// ==========================================
// 3. OBJECTIVE-C++ IMPLEMENTATION
// ==========================================
@implementation BridgeManager
- (instancetype)initWithName:(NSString *)name {
self = [super init];
if (self) {
_managerName = name;
// Allocating the heap-based C++ object smoothly
engineInstance = new CPPDataEngine();
}
return self;
}
- (void)executePipelineWithPayload:(NSString *)payload {
NSLog(@"[Obj-C] %@ is converting payload...", self.managerName);
// Converting an Objective-C NSString to a standard C++ std::string
std::string cppString = [payload UTF8String];
// Direct call to the C++ member function
engineInstance->processDataStream(cppString);
// Mixing native collections and logs
size_t count = engineInstance->getLogCount();
NSLog(@"[Obj-C] Total engine logs tracked: %lu", count);
}
- (void)dealloc {
// Explicitly clean up C++ heap objects since ARC does not manage them
delete engineInstance;
NSLog(@"[Obj-C] Manager deallocated, C++ memory freed safely.");
}
@end
// ==========================================
// 4. MAIN ENTRY POINT (MIXED EXECUTION)
// ==========================================
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@"--- Starting Objective-C++ Execution ---");
// Allocate and initialize the hybrid object
BridgeManager *manager = [[BridgeManager alloc] initWithName:@"CoreBridge"];
// Pass a native Objective-C string payload into the pipeline
[manager executePipelineWithPayload:@"Data_Packet_01A"];
[manager executePipelineWithPayload:@"Data_Packet_02B"];
NSLog(@"--- Execution Finished ---");
}
return 0;
}