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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
/*!
* Hello world! application
*
* \file hello.cpp
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cassert>
/*!
* Dispay message.
*/
void displayMessage();
/*!
* Sentence type
*
* Type of sentence, used to decide how to terminate sentence.
*/
enum ESentenceType {
eStatement,
eExclamation,
sQuestion,
eCommand
};
/*!
* Utility class to prevent unintended copying of class instances.
*/
class nonCopyable {
protected:
nonCopyable() {
}
~nonCopyable() {
}
private:
nonCopyable(const nonCopyable&);
const nonCopyable& operator=(const nonCopyable&);
};
/*!
* Utility function to obtain punctuation mark to end sentence
* of specified type.
*/
inline char getPunctionMark(ESentenceType sentenceType) {
char puncMark = '.';
switch(sentenceType) {
case eStatement : puncMark = '.'; break;
case eExclamation: puncMark = '!'; break;
case sQuestion : puncMark = '?'; break;
case eCommand : puncMark = '.'; break;
default: {
// should never get here
assert(false);
}
}
return puncMark;
}
/*!
* Utility class for creation of instances.
*/
template<typename TElem>
class Creatable {
protected:
Creatable() {
}
virtual ~Creatable() {
clear();
}
public:
static TElem* create() {
TElem* e = new TElem;
return e;
}
void free() {
delete this;
}
virtual void clear() {
}
};
template<typename TElem, typename TParam>
class CreatableParam {
protected:
CreatableParam() {
}
virtual ~CreatableParam() {
}
public:
static TElem* create(TParam p) {
TElem* e = new TElem;
e->initialize(p);
return e;
}
void free() {
finalize();
delete this;
}
virtual void initialize(TParam /*p*/) {
}
virtual void finalize() {
clear();
}
virtual void clear() {
}
};
/*!
* Base class for displayable content
*/
class DisplayElem
: public nonCopyable {
protected:
DisplayElem() {
}
virtual ~DisplayElem() {
}
public:
virtual void display(std::ostream& os) const = 0;
};
/*!
* STL algorithm for displaying elements
*/
class Displayer
: public std::unary_function<void, const DisplayElem*> {
private:
std::ostream& m_os;
char m_sep;
size_t m_count;
public:
Displayer(std::ostream& os, char sep = '\0')
: m_os(os)
, m_sep(sep)
, m_count(0) {
}
~Displayer() {
}
void operator()(const DisplayElem* e) {
if(('\0' != m_sep) && (0 < m_count)) {
m_os << m_sep;
}
e->display(m_os);
++m_count;
}
};
/*!
* STL algorithm for freeing display elements
*/
template <typename TElem>
class Freer
: public std::unary_function<void, TElem*> {
public:
void operator()(TElem* e) {
e->free();
}
};
/*!
* Display element for letter.
*
* The letter is the fundamental element: it has no substructure.
*/
class Letter
: public DisplayElem
, public CreatableParam<Letter, char> {
private:
char m_ch;
protected:
/*virtual*/ ~Letter() {
}
public:
Letter() : m_ch('\0') {
}
void initialize(char ch) {
m_ch = ch;
}
void finalize() {
m_ch = '\0';
}
void display(std::ostream& os) const {
os << m_ch;
// no endLetter()
}
};
/*!
* Display element for word.
*
* A word is a sequence of letters.
*/
class Word
: public DisplayElem
, public Creatable<Word> {
private:
std::vector<Letter*> m_letters;
protected:
/*virtual*/ ~Word() {
clear();
}
public:
Word() {
}
void clear() {
std::for_each(m_letters.begin(), m_letters.end(), Freer<Letter>());
m_letters.clear();
}
void addLetter(Letter* s) {
m_letters.push_back(s);
}
/*virtual*/ void display(std::ostream& os) const {
std::for_each(m_letters.begin(), m_letters.end(), Displayer(os));
// no endLetter()
}
};
/*!
* Display element for sentence.
*
* A sentence is a sequence of words.
*/
class Sentence
: public DisplayElem
, public CreatableParam<Sentence, ESentenceType> {
private:
std::vector<Word*> m_words;
ESentenceType m_sentenceType;
protected:
/*virtual*/ ~Sentence() {
clear();
}
void endSentence(std::ostream& os) const {
const char puncMark = getPunctionMark(m_sentenceType);
os << puncMark;
}
public:
Sentence()
: m_sentenceType(eStatement) {
}
void initialize(ESentenceType sentenceType) {
m_sentenceType = sentenceType;
}
void finalize() {
m_sentenceType = eStatement;
}
void clear() {
std::for_each(m_words.begin(), m_words.end(), Freer<Word>());
m_words.clear();
}
void addWord(Word* w) {
m_words.push_back(w);
}
void display(std::ostream& os) const {
std::for_each(m_words.begin(), m_words.end(), Displayer(os, ' '));
endSentence(os);
}
};
/*!
* Display element for message.
*
* A message is a sequence of sentences.
*/
class Message
: public DisplayElem
, public Creatable<Message> {
private:
std::vector<Sentence*> m_sentences;
protected:
/*virtual*/ ~Message() {
clear();
}
void endMessage(std::ostream& os) const {
os << std::endl;
}
public:
Message() {
}
void clear() {
std::for_each(m_sentences.begin(), m_sentences.end(), Freer<Sentence>());
m_sentences.clear();
}
void addSentence(Sentence* s) {
m_sentences.push_back(s);
}
void display(std::ostream& os) const {
std::for_each(m_sentences.begin(), m_sentences.end(), Displayer(os, ' '));
endMessage(os);
}
};
/*!
* Main entrance point.
*/
int main() {
displayMessage();
return 0;
}
/*!
* Display message.
*/
void displayMessage() {
Word* first_word = Word::create();
first_word->addLetter(Letter::create('H'));
first_word->addLetter(Letter::create('e'));
first_word->addLetter(Letter::create('l'));
first_word->addLetter(Letter::create('l'));
first_word->addLetter(Letter::create('o'));
Word* second_word = Word::create();
second_word->addLetter(Letter::create('w'));
second_word->addLetter(Letter::create('o'));
second_word->addLetter(Letter::create('r'));
second_word->addLetter(Letter::create('l'));
second_word->addLetter(Letter::create('d'));
Sentence* sentence = Sentence::create(eExclamation);
sentence->addWord(first_word);
sentence->addWord(second_word);
Message* message = Message::create();
message->addSentence(sentence);
message->display(std::cout);
message->free();
// sentences, etc freed by parent
}
| |