-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathya_code_generator.cpp
More file actions
executable file
·78 lines (67 loc) · 1.88 KB
/
Copy pathya_code_generator.cpp
File metadata and controls
executable file
·78 lines (67 loc) · 1.88 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
#include <string>
#include <memory>
#include <stdexcept>
#include <iostream>
#include <boost/shared_ptr.hpp>
// #include <boost/noncopyable.hpp>
class CodeGenerator
{
public:
virtual std::string code() = 0;
virtual std::string thing() = 0;
virtual ~CodeGenerator() { };
};
class JavaCodeGenerator : public CodeGenerator
{
public:
std::string code() { return std::string("Java Code") + thing();}
std::string thing() { return std::string("Java thing"); }
};
class CppCodeGenerator : public CodeGenerator
{
public:
std::string code() { return std::string("Cpp Code") + thing() + thing(); }
std::string thing() { return std::string("Cpp thing"); }
};
class PhpCodeGenerator : public CodeGenerator
{
public:
std::string code() { return std::string("Php Code") + thing() + thing() + thing(); }
std::string thing() { return std::string("Php thing"); }
};
typedef boost::shared_ptr<CodeGenerator> CodeGeneratorPtr;
class CodeGenratorFactory
{
public:
enum Lang {JAVA, C_PLUS_PLUS, PHP};
static CodeGeneratorPtr create(Lang lang)
{
switch (lang)
{
case JAVA:
return CodeGeneratorPtr(new JavaCodeGenerator());
case C_PLUS_PLUS:
return CodeGeneratorPtr(new CppCodeGenerator());
// case PHP:
// return CodeGeneratorPtr(new PhpCodeGenerator());
default:
throw std::logic_error("Bad language");
}
}
};
int
main()
{
try
{
CodeGeneratorPtr cg = CodeGenratorFactory::create(CodeGenratorFactory::C_PLUS_PLUS);
std::cout << cg->code() << std::endl;
CodeGeneratorPtr ec = CodeGenratorFactory::create(CodeGenratorFactory::PHP);
std::cout << ec->code() << std::endl;
}
catch (const std::logic_error &e)
{
std::cout << "ERROR: " << e.what() << std::endl;
}
return 0;
}