-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp.cpp
More file actions
148 lines (116 loc) · 3.97 KB
/
Cpp.cpp
File metadata and controls
148 lines (116 loc) · 3.97 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <vector>
#include <string>
#include <memory>
#include <algorithm>
#include <map>
#include <thread>
#include <chrono>
// Namespace for the viewer application
namespace GitCodeViewer {
// Enum for connection status
enum class ConnectionStatus {
Disconnected,
Connecting,
Connected,
Error
};
// Base class for a File
class FileNode {
protected:
std::string name;
size_t size;
public:
FileNode(std::string n, size_t s) : name(n), size(s) {}
virtual ~FileNode() = default;
virtual void render() const = 0;
std::string getName() const { return name; }
size_t getSize() const { return size; }
};
// Derived class for Source Code
class SourceFile : public FileNode {
std::string language;
public:
SourceFile(std::string n, size_t s, std::string lang)
: FileNode(n, s), language(lang) {}
void render() const override {
std::cout << "[CODE] " << name << " (" << language
<< ") - " << size << " bytes" << std::endl;
highlightSyntax();
}
void highlightSyntax() const {
// Simulation of syntax highlighting logic
std::cout << " >> Applying colors for " << language << " syntax..." << std::endl;
}
};
// Derived class for Image
class ImageFile : public FileNode {
int width, height;
public:
ImageFile(std::string n, size_t s, int w, int h)
: FileNode(n, s), width(w), height(h) {}
void render() const override {
std::cout << "[IMG] " << name << " [" << width << "x" << height
<< "]" << std::endl;
}
};
// Repository Manager Class
class RepositoryManager {
private:
std::vector<std::shared_ptr<FileNode>> files;
std::map<std::string, std::string> config;
public:
RepositoryManager() {
config["theme"] = "dark";
config["auto_fetch"] = "true";
}
void addFile(std::shared_ptr<FileNode> file) {
files.push_back(file);
}
void listFiles() {
std::cout << "\n--- Repository Content ---\n";
for (const auto& file : files) {
file->render();
}
std::cout << "--------------------------\n";
}
void sync() {
std::cout << "\nSyncing with remote..." << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
// Sort files by size using lambda
std::sort(files.begin(), files.end(),
[](const std::shared_ptr<FileNode>& a, const std::shared_ptr<FileNode>& b) {
return a->getSize() > b->getSize();
});
std::cout << "Sync complete. Files sorted by size.\n";
}
};
}
// Template function example
template<typename T>
T add(T a, T b) {
return a + b;
}
int main() {
using namespace GitCodeViewer;
std::cout << "Starting C++ Engine for GitCodeViewer...\n";
RepositoryManager repo;
// Adding various files using smart pointers
repo.addFile(std::make_shared<SourceFile>("main.cpp", 1024, "C++"));
repo.addFile(std::make_shared<SourceFile>("utils.ts", 512, "TypeScript"));
repo.addFile(std::make_shared<ImageFile>("logo.png", 20480, 512, 512));
repo.addFile(std::make_shared<SourceFile>("styles.css", 200, "CSS"));
repo.listFiles();
// Multithreading simulation
std::thread syncThread(&RepositoryManager::sync, &repo);
std::cout << "Main thread doing other work...\n";
for(int i=0; i<3; i++) {
std::cout << "Working " << i << "...\n";
}
if (syncThread.joinable()) {
syncThread.join();
}
repo.listFiles();
std::cout << "\nEngine Shutdown. Memory cleaned up automatically via smart pointers.\n";
return 0;
}