Skip to content

Commit eb83510

Browse files
committed
refactoring, better names
1 parent 9621f1a commit eb83510

File tree

2 files changed

+76
-74
lines changed

2 files changed

+76
-74
lines changed

CppAsScript.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
int main()
66
{
7-
CLangProject PR;
7+
cppProject PR;
88
std::string download = PR.checkIfCompilerInstalled();
99
if(download.length()) {
1010
std::cout << "LLVM-CL not installed, please download and install at:\n" << download << "\n";

cppAsScript.h

Lines changed: 75 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -9,56 +9,24 @@
99
#include <cstdio>
1010
#include <functional>
1111

12-
class LibReference {
13-
public:
14-
virtual ~LibReference() {};
15-
virtual bool valid() const = 0;
16-
virtual bool loadModule(const char* path) = 0;
17-
virtual void unLoadModule() = 0;
18-
virtual void* raw_ptr(const char* functionName) = 0;
19-
static LibReference* create();
20-
};
21-
22-
class CLangProject;
2312

24-
class CLangBuilder {
25-
friend class CLangProject;
26-
public:
27-
28-
virtual ~CLangBuilder() {};
29-
30-
/// returns the module extension (dll for windows)
31-
virtual const char* getModuleExtension() const = 0;
32-
33-
/// compile the project
34-
virtual void compile(CLangProject& project) = 0;
13+
class cppModule;
14+
class cppBuilder;
3515

36-
/// converts relative path to file to the full path
37-
virtual std::string pathInHeap(const std::string& relative) = 0;
38-
39-
/// returns LLVM-CL download path for the current OS
40-
virtual std::string downloadPath() = 0;
41-
42-
/// returns true if the LLVM-CL exists in system and available to be used
43-
virtual bool valid() = 0;
44-
45-
static CLangBuilder* create();
46-
};
47-
48-
class CLangProject {
16+
class cppProject {
4917
protected:
5018
std::vector<std::string> files;
5119
std::string modulePath;
5220
std::string log;
5321
std::string _options;
5422
std::string _includes;
55-
LibReference* ref;
56-
CLangBuilder* builder;
23+
cppModule* ref;
24+
cppBuilder* builder;
5725
void _add(const std::string& opt);
5826
void _remove(const std::string& opt);
5927
public:
60-
CLangProject();
61-
virtual ~CLangProject();
28+
cppProject();
29+
virtual ~cppProject();
6230

6331
/// Compile the project (if need).
6432
void recompileIfNeed();
@@ -67,25 +35,25 @@ class CLangProject {
6735
bool valid();
6836

6937
/// Add the cpp file into the project, provide the full path
70-
CLangProject& addFile(const std::string& path);
38+
cppProject& addFile(const std::string& path);
7139

7240
/// Add the include file into the project
73-
CLangProject& addIncludeFolder(const std::string& path);
41+
cppProject& addIncludeFolder(const std::string& path);
7442

7543
/// Add the text of cpp file to be compiled into the project
76-
CLangProject& addSource(const char* cpp_text);
44+
cppProject& addSource(const char* cpp_text);
7745

7846
/// Compile (in future) with the speed optimization
79-
CLangProject& speedOptimization();
47+
cppProject& speedOptimization();
8048

8149
/// Compile (in future) with the size optimization
82-
CLangProject& sizeOptimization();
50+
cppProject& sizeOptimization();
8351

8452
/// Compile debug version (this is just option setting, not compilation itself)
85-
CLangProject& debug();
53+
cppProject& debug();
8654

8755
/// Compile release version (this is just option setting, not compilation itself)
88-
CLangProject& release();
56+
cppProject& release();
8957

9058
/// Returns "" if the LLVM-CL installed correctly, othervice it returns the path to download, you may display the message to offer the download.
9159
std::string checkIfCompilerInstalled();
@@ -111,7 +79,7 @@ class CLangProject {
11179
};
11280

11381
template <class F>
114-
std::function<F> CLangProject::bind(const char* functionName) {
82+
std::function<F> cppProject::bind(const char* functionName) {
11583
std::function<F> f1 = nullptr;
11684
if (!ref) {
11785
recompileIfNeed();
@@ -122,6 +90,40 @@ std::function<F> CLangProject::bind(const char* functionName) {
12290
return f1;
12391
}
12492

93+
class cppModule {
94+
public:
95+
virtual ~cppModule() {};
96+
virtual bool valid() const = 0;
97+
virtual bool loadModule(const char* path) = 0;
98+
virtual void unLoadModule() = 0;
99+
virtual void* raw_ptr(const char* functionName) = 0;
100+
static cppModule* create();
101+
};
102+
103+
class cppBuilder {
104+
friend class cppProject;
105+
public:
106+
107+
virtual ~cppBuilder() {};
108+
109+
/// returns the module extension (dll for windows)
110+
virtual const char* getModuleExtension() const = 0;
111+
112+
/// compile the project
113+
virtual void compile(cppProject& project) = 0;
114+
115+
/// converts relative path to file to the full path
116+
virtual std::string pathInHeap(const std::string& relative) = 0;
117+
118+
/// returns LLVM-CL download path for the current OS
119+
virtual std::string downloadPath() = 0;
120+
121+
/// returns true if the LLVM-CL exists in system and available to be used
122+
virtual bool valid() = 0;
123+
124+
static cppBuilder* create();
125+
};
126+
125127

126128
/// implementation
127129

@@ -262,65 +264,65 @@ inline static std::string _replace(const std::string& str, const std::string& su
262264
return tmp;
263265
}
264266

265-
inline void CLangProject::_add(const std::string& opt) {
267+
inline void cppProject::_add(const std::string& opt) {
266268
_remove(opt);
267269
_options += " " + opt;
268270
_options = _replace(_options, " ", " ");
269271
}
270272

271-
inline void CLangProject::_remove(const std::string& opt) {
273+
inline void cppProject::_remove(const std::string& opt) {
272274
_options = _replace(_options, opt, "");
273275
_options = _replace(_options, " ", " ");
274276
}
275277

276-
inline CLangProject::CLangProject() {
277-
builder = CLangBuilder::create();
278+
inline cppProject::cppProject() {
279+
builder = cppBuilder::create();
278280
ref = nullptr;
279281
_options = "/std:c++latest /LD";
280282
}
281283

282-
inline CLangProject::~CLangProject() {
284+
inline cppProject::~cppProject() {
283285
if (builder)delete(builder);
284286
builder = nullptr;
285287
if (ref)delete(ref);
286288
ref = nullptr;
287289
}
288290

289-
inline bool CLangProject::valid() {
291+
inline bool cppProject::valid() {
290292
return ref != nullptr;
291293
}
292294

293-
inline std::string& CLangProject::compileLog() {
295+
inline std::string& cppProject::compileLog() {
294296
return log;
295297
}
296298

297-
inline std::string& CLangProject::module() {
299+
inline std::string& cppProject::module() {
298300
return modulePath;
299301
}
300302

301-
inline std::vector<std::string>& CLangProject::filesList() {
303+
inline std::vector<std::string>& cppProject::filesList() {
302304
return files;
303305
}
304306

305-
inline std::string& CLangProject::options() {
307+
inline std::string& cppProject::options() {
306308
return _options;
307309
}
308310

309-
inline std::string& CLangProject::includes() {
311+
inline std::string& cppProject::includes() {
310312
return _includes;
311313
}
312314

313-
inline CLangProject& CLangProject::addFile(const std::string& path) {
315+
inline cppProject& cppProject::addFile(const std::string& path) {
314316
filesList().push_back(path);
315317
return *this;
316318
}
317319

318-
inline CLangProject& CLangProject::addIncludeFolder(const std::string& path) {
320+
inline cppProject& cppProject::addIncludeFolder(const std::string& path) {
319321
includes() += " /I " + path;
320322
return *this;
321323
}
322324

323-
inline CLangProject& CLangProject::addSource(const char* cpp_text) {
325+
inline cppProject& cppProject::addSource(const char* cpp_text) {
324326
std::string fn = "temp_" + md5::hash(cpp_text) + ".cpp";
325327
std::string res = builder->pathInHeap(fn);
326328
std::ofstream f(res);
@@ -332,30 +334,30 @@ inline CLangProject& CLangProject::addSource(const char* cpp_text) {
332334
return *this;
333335
}
334336

335-
inline CLangProject& CLangProject::speedOptimization() {
337+
inline cppProject& cppProject::speedOptimization() {
336338
_add("/Ot");
337339
return *this;
338340
}
339341

340-
inline CLangProject& CLangProject::sizeOptimization() {
342+
inline cppProject& cppProject::sizeOptimization() {
341343
_add("/Os");
342344
return *this;
343345
}
344346

345-
inline CLangProject& CLangProject::debug() {
347+
inline cppProject& cppProject::debug() {
346348
_remove("/LD");
347349
_add("/LDd");
348350
_add("-fuse-ld=lld -Z7");
349351
return *this;
350352
}
351353

352-
inline CLangProject& CLangProject::release() {
354+
inline cppProject& cppProject::release() {
353355
_remove("/LDd");
354356
_add("/LD");
355357
return *this;
356358
}
357359

358-
inline std::string CLangProject::checkIfCompilerInstalled() {
360+
inline std::string cppProject::checkIfCompilerInstalled() {
359361
if (builder) {
360362
if (!builder->valid()) {
361363
return builder->downloadPath();
@@ -364,11 +366,11 @@ inline std::string CLangProject::checkIfCompilerInstalled() {
364366
return "";
365367
}
366368

367-
inline void CLangProject::recompileIfNeed() {
369+
inline void cppProject::recompileIfNeed() {
368370
if (builder) {
369371
builder->compile(*this);
370372
if (ref)delete(ref);
371-
ref = LibReference::create();
373+
ref = cppModule::create();
372374
ref->loadModule(module().c_str());
373375
}
374376
}
@@ -398,7 +400,7 @@ struct funcRef {
398400
FARPROC address;
399401
};
400402

401-
class LibReferenceWin : public LibReference {
403+
class LibReferenceWin : public cppModule {
402404
HMODULE h;
403405
std::vector<funcRef> refs;
404406
public:
@@ -507,7 +509,7 @@ inline std::string exec(const char* cmd) {
507509
return strResult;
508510
}
509511

510-
class CLangBuilderWin : public CLangBuilder {
512+
class CLangBuilderWin : public cppBuilder {
511513
std::string clangPath;
512514
std::string tempPath;
513515
public:
@@ -532,7 +534,7 @@ class CLangBuilderWin : public CLangBuilder {
532534
const char* getModuleExtension() const override {
533535
return "dll";
534536
}
535-
void compile(CLangProject& project) override {
537+
void compile(cppProject& project) override {
536538
std::filesystem::path p = _getexepath();
537539
p.replace_extension("lib");
538540
std::string libpath = p.string();
@@ -581,11 +583,11 @@ class CLangBuilderWin : public CLangBuilder {
581583
}
582584
};
583585

584-
inline LibReference* LibReference::create() {
586+
inline cppModule* cppModule::create() {
585587
return new LibReferenceWin;
586588
}
587589

588-
inline CLangBuilder* CLangBuilder::create() {
590+
inline cppBuilder* cppBuilder::create() {
589591
return new CLangBuilderWin;
590592
}
591593

0 commit comments

Comments
 (0)