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
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <initializer_list>
#include <list>
#include <sstream>
#include <numeric>
#include <typeinfo>
template <typename T>
const std::string toString (T t) {
std::ostringstream os;
os << t;
return os.str();
}
template <typename I>
struct SentenceAppender {
std::string sentence;
template <typename T>
SentenceAppender& initialize (const T& t) { // base case function
if (typeid (T) == typeid (std::string))
sentence = t;
else
sentence = toString (t);
return *this;
}
template <typename FIRST, typename...REST>
SentenceAppender& initialize (const FIRST& first, const REST&...rest) { // recursive case function
const std::size_t N = sizeof...(rest) + 1;
if (std::rand() % N == 0)
return initialize (first);
else
return initialize (rest...);
return *this;
}
template <typename T>
SentenceAppender& append (const T& t) {
if (typeid (T) == typeid (std::string))
sentence += t;
else
sentence += toString (t);
return *this;
}
template <typename FIRST, typename...REST>
SentenceAppender& append (const FIRST& first, const REST&...rest) {
const std::size_t N = sizeof...(rest) + 1;
if (std::rand() % N == 0)
return append (first);
else
return append (rest...);
return *this;
}
template <typename... ARGS>
SentenceAppender& operator() (const ARGS&... args) {
append (args...);
return *this; // returning SentenceAppender& is required to allow for concatenation
}
// overloading the above to allow for probability weights
template <typename T>
SentenceAppender& initialize (std::list<I>& weights, const T& t) {
if (typeid (T) == typeid (std::string))
sentence = t;
else
sentence = toString (t);
return *this;
}
template <typename FIRST, typename...REST>
SentenceAppender& initialize (std::list<I>& weights, const FIRST& first, const REST&...rest) { // weights cannot be const
const int N = std::accumulate (weights.begin(), weights.end(), 0);
if (std::rand() % N < weights.front())
return initialize (weights, first);
else
{
weights.pop_front();
return initialize (weights, rest...);
}
return *this;
}
template <typename...ARGS>
SentenceAppender& initialize (const std::initializer_list<I>& weights, const ARGS&... args) {
std::list<I> listWeights = weights; // cannot be const
initialize (listWeights, args...);
return *this;
}
template <typename T>
SentenceAppender& append (std::list<I>& weights, const T& t) {
if (typeid (T) == typeid (std::string))
sentence += t;
else
sentence += toString (t);
return *this;
}
template <typename FIRST, typename...REST>
SentenceAppender& append (std::list<I>& weights, const FIRST& first, const REST&...rest) {
const int N = std::accumulate (weights.begin(), weights.end(), 0);
if (std::rand() % N < weights.front())
return append (weights, first);
else
{
weights.pop_front();
return append (weights, rest...);
}
return *this;
}
template <typename...ARGS>
SentenceAppender& operator() (const std::initializer_list<I>& weights, const ARGS&... args) {
std::list<I> listWeights = weights; // cannot be const
append (listWeights, args...);
return *this;
}
};
const char space = ' ' ;
const std::string INDENT = " ";
const std::string NEW = " ";
std::string wordWrap (std::string str, size_t width = 70) {
size_t currentWidth = width;
const int newSentenceSpaceLength = NEW.length();
while (currentWidth < str.length ()) {
std::string::size_type spacePos = str.rfind (' ', currentWidth);
if (spacePos == std::string::npos)
spacePos = str.find (' ', currentWidth);
if (spacePos != std::string::npos)
{
str[spacePos] = '\n';
for (int i = 0; i < newSentenceSpaceLength - 1; i++)
{
if (str[spacePos + 1] == ' ')
{
str.erase (spacePos + 1, 1);
spacePos++;
}
}
currentWidth = spacePos + width + 1;
}
}
return str;
}
struct Person {
std::string name;
int salary;
Person (const std::string& thisName, int thisSalary): name (thisName), salary (thisSalary) {}
};
SentenceAppender<int> sapp;
int main () { // Showing how SentenceAppender<I> works
std::srand (std::time (nullptr));
Person mark ("Mark", 15000), sam ("Sam", 40000);
sapp.initialize (INDENT)("Wow!", "Hey!", "My god!")(space)(mark.name)(", You earn a", ", You make a")(space);
if (mark.salary < 20000)
{
sapp ({1,2,1}, "measly $", "lousy $", "crummy $")(mark.salary)(space)("per year?", "per year???")(NEW);
if (2 * mark.salary < sam.salary)
sapp (sam.name)(", whom I thought was poor,", ", whom I've made fun of,")(space)("earns more than", "makes more than")(space)('$')
(2 * mark.salary)(", which is double your salary.", ", i.e. twice your salary.")(NEW)("Are you sure about your career?", "Ever consider getting a new career?");
else
sapp ("Well, at least you have your health.", "But hey, life goes on, right?");
}
else
sapp ("whopping", "cool")(space)(mark.salary)(space)("per year!", "per year!!!")(NEW)("That is so awesome!");
std::cout << wordWrap (sapp.sentence) << std::endl;
sapp.initialize ({3,1}, INDENT, NEW)({20,3}, "Now hear this...", "Now I have this to say...");
std::cout << sapp.sentence << std::endl;
std::cin.get();
return 0;
}
|