-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateClass.sh
More file actions
executable file
·146 lines (122 loc) · 2.71 KB
/
createClass.sh
File metadata and controls
executable file
·146 lines (122 loc) · 2.71 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
#!/bin/bash
# author: danil.rempel <danil.rempel@gmail.com>
VERSION="0.9"
# 0.2: added uppercasing in "ifndef HAVE_CLASSNAME_HPP" statement
# 0.3: added --relative. it isn't enough useful, but i hope, someone will like it:)
# note: it isn't sensitive to arguments order
# but any non-"--relative" text it will treat as class name
# 0.4: checks if class definition or implementation files exist and dies if any
# 0.5: added ';' after class definiton ^_^'', (changed CPP to HPP in include lock)
# 0.6: added "class CLASSNAME;" statement after include lock. I use it
# to prevent recursive includes when classes are not found
# added "public:" and "private:" lines to class definition body
# 0.7: return to ordinary way: only one src dir, all sources have relative (within one directory paths)
# 0.8: rewritten, simplified
# 0.9: add a key to create a singleton class
function create () {
HEADER_FILE=$3.hpp
HEADER=$2/${HEADER_FILE}
SOURCE=$2/$3.cpp
CLASS=$3
UCLASS=`echo $3 | awk '{print toupper($0)}'`
if [ -e "${HEADER}" -o -e "${SOURCE}" ];
then
echo "File(s) already exist: ${HEADER}, ${SOURCE}"
exit 1;
fi
touch ${HEADER}
cat >> $HEADER <<EOF
#ifndef __HAVE_${UCLASS}_HPP__
#define __HAVE_${UCLASS}_HPP__
class ${CLASS};
class ${CLASS}
{
EOF
if [ "$1" == "1" ];
then
cat >> $HEADER <<EOF
public: // Singleton stuff
static ${CLASS}& getInstance();
${CLASS}(${CLASS} const&) = delete;
void operator=(${CLASS} const&) = delete;
EOF
fi
cat >> $HEADER <<EOF
public:
${CLASS}();
virtual ~${CLASS}();
protected:
private:
};
#endif // __HAVE_${UCLASS}_HPP__
EOF
touch ${SOURCE}
cat >> $SOURCE <<EOF
#include "${HEADER_FILE}"
EOF
if [ "$1" == "1" ];
then
cat >> $SOURCE <<EOF
${CLASS}& ${CLASS}::getInstance()
{
// according to loki-astari@stackoverflow,
// this way the singleton is guaranteed to
// be destroyed
static ${CLASS} instance;
return instance;
}
EOF
fi
cat >> $SOURCE <<EOF
${CLASS}::${CLASS}()
{
}
${CLASS}::~${CLASS}()
{
}
EOF
}
function parse () {
if [ "$1" == "--help" ];
then
# show help text
echo "Class creation tool v${VERSION}"
echo "Creates C++ classes with base headers in given directory"
echo "Usage: $0 <dir> <classname> | $0 <classname>"
exit 0;
fi
if [ "$1" == "-s" ]
then
# create a singleton class
SINGLETON=1
if [ -z "$3" ];
then
# createclass <classname>
DIR=.
CLASS=$2
else
# createclass <dir> <classname>
DIR=$2
CLASS=$3
fi
else
SINGLETON=0
if [ -z "$2" ];
then
# createclass <classname>
DIR=.
CLASS=$1
else
# createclass <dir> <classname>
DIR=$1
CLASS=$2
fi
fi
create $SINGLETON $DIR $CLASS
}
if [ -z $1 ];
then
echo "Usage: $0 [-s] [<dir>] <classname> | $0 --help"
else
parse $@
fi