Galactic Bloodshed
namegen.h
Go to the documentation of this file.
1 // Copyright 2014 The Galactic Bloodshed Authors. All rights reserved.
2 // Use of this source code is governed by a license that can be
3 // found in the COPYING file.
4 
5 #ifndef NAMEGEN_H
6 #define NAMEGEN_H
7 
8 #include <string>
9 
11  public:
12  virtual ~NameGenerator(){};
13  class Iterator {
14  private:
16 
17  public:
18  /*
19  typedef std::string value_type;
20  typedef void difference_type;
21  typedef std::string * pointer;
22  typedef std::string & reference;
23  typedef std::input_iterator_tag iterator_category;
24  */
25 
26  explicit Iterator(NameGenerator *parent) : namegen(parent) {}
27 
28  const std::string operator*() const {
29  return namegen ? namegen->current() : "";
30  }
31  Iterator &operator++() { // preincrement
32  if (namegen != nullptr) {
33  if (!namegen->next()) {
34  namegen = nullptr;
35  }
36  }
37  return *this;
38  }
39  friend bool operator==(Iterator const &lhs, Iterator const &rhs) {
40  return lhs.namegen == rhs.namegen;
41  }
42  friend bool operator!=(Iterator const &lhs, Iterator const &rhs) {
43  return !(lhs == rhs);
44  }
45 
46  class PostIncResult {
47  std::string value;
48 
49  public:
50  PostIncResult(const std::string &val) : value(val) {}
51  const std::string &operator*() { return value; }
52  };
53 
55  PostIncResult ret(namegen ? namegen->current() : "");
56  ++*this;
57  return ret;
58  }
59  };
60 
61  protected:
62  virtual bool next() = 0;
63  virtual const std::string &current() = 0;
64 
65  auto begin() { return new Iterator(this); }
66  auto end() { return new Iterator(nullptr); }
67 };
68 
70  public:
71  SequentialNameGenerator(int startval = 1,
72  const std::string &pref = std::string(""),
73  const std::string &suff = std::string(""),
74  const std::string &nf = std::string(""))
75  : currval(startval), prefix(pref), suffix(suff), numberformat(nf) {}
76  virtual bool next();
77  virtual const std::string &current() { return current_value; }
78 
79  private:
80  std::string current_value;
81  int currval;
82  std::string prefix;
83  std::string suffix;
84  std::string numberformat;
85 };
86 
88  public:
89  IterativeNameGenerator(std::string::iterator start, std::string::iterator end)
90  : head(start), tail(end) {}
91  virtual bool next();
92  virtual const std::string &current() { return current_value; }
93 
94  private:
97  std::string current_value;
98 };
99 
100 #endif // gameobj_h
const std::string & operator*()
Definition: namegen.h:51
std::string suffix
Definition: namegen.h:83
NameGenerator * namegen
Definition: namegen.h:15
friend bool operator==(Iterator const &lhs, Iterator const &rhs)
Definition: namegen.h:39
auto end()
Definition: namegen.h:66
virtual const std::string & current()=0
std::string::iterator head
Definition: namegen.h:95
virtual const std::string & current()
Definition: namegen.h:77
auto begin()
Definition: namegen.h:65
std::string numberformat
Definition: namegen.h:84
Iterator & operator++()
Definition: namegen.h:31
std::string prefix
Definition: namegen.h:82
virtual bool next()
Definition: namegen.cc:12
std::string current_value
Definition: namegen.h:97
PostIncResult(const std::string &val)
Definition: namegen.h:50
virtual ~NameGenerator()
Definition: namegen.h:12
virtual bool next()=0
SequentialNameGenerator(int startval=1, const std::string &pref=std::string(""), const std::string &suff=std::string(""), const std::string &nf=std::string(""))
Definition: namegen.h:71
std::string::iterator tail
Definition: namegen.h:96
PostIncResult operator++(int)
Definition: namegen.h:54
IterativeNameGenerator(std::string::iterator start, std::string::iterator end)
Definition: namegen.h:89
virtual bool next()
Definition: namegen.cc:20
friend bool operator!=(Iterator const &lhs, Iterator const &rhs)
Definition: namegen.h:42
const std::string operator*() const
Definition: namegen.h:28
std::string current_value
Definition: namegen.h:80
virtual const std::string & current()
Definition: namegen.h:92
Iterator(NameGenerator *parent)
Definition: namegen.h:26