Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. //HOMO HOMO HOO PEE PEE//
  2. #ifndef ELEC_CPP_PRINTERS_HH
  3. #define ELEC_CPP_PRINTERS_HH
  4.  
  5. #include <string>
  6. #include <map>
  7. #include <iostream>
  8. #include <stdexcept>
  9.  
  10.  
  11. /* Implement the abstract base class StringPrinter here
  12. *
  13. * The class has a constructor that takes one parameter:
  14. * std::ostream& with the default value std::cout
  15. *
  16. * The class contains the following pure virtual member functions:
  17. * - A clone() function that copies the object and returns StringPrinter*
  18. *
  19. * - Function call operator overload, i.e. this base class is a function object.
  20. * - Takes one std::string argument (the string should not be modified).
  21. * - Returns a reference to the StringPrinter object meaning that this
  22. * function call operation can be "chained", e.g.
  23. * object("First")("Second")("Third");
  24. *
  25. * The base class contains the following protected member:
  26. * An std::ostream reference which is the output stream
  27. *
  28. * Implement the class accordingly and do not forget the virtual destructor..
  29. */
  30.  
  31.  
  32. /* Class StandardPrinter
  33. * A minimal StringPrinter implementation with no special functionality
  34. * apart from the std::ostream& stream parameter
  35. * Note:
  36. * The Constructor
  37. * The implementation of clone()
  38. */
  39. class StringPrinter{
  40. public:
  41. StringPrinter(std::ostream& o = std::cout) : os(o) { }
  42.  
  43. virtual ~StringPrinter() { }
  44.  
  45. virtual StringPrinter* clone() const = 0;
  46.  
  47. virtual StringPrinter& operator()(const std::string& str) = 0;
  48.  
  49. protected:
  50. std::ostream& os;
  51. };
  52.  
  53.  
  54.  
  55. class StandardPrinter : public StringPrinter
  56. {
  57. public:
  58. /* Construct a new object - pass the std::ostream& to the base class */
  59. StandardPrinter(std::ostream &os = std::cout) : StringPrinter(os)
  60. { }
  61. /* Copy this object */
  62. StandardPrinter* clone() const
  63. {
  64. return new StandardPrinter(os);
  65. }
  66. virtual StringPrinter& operator()(const std::string& str)
  67. {
  68. /* os is a protected member of the base class */
  69. os << str << std::endl;
  70. return *this;
  71. }
  72. private:
  73. };
  74.  
  75. class DiagonalPrinter : public StringPrinter{
  76. public:
  77. /* Construct a new object - pass the std::ostream& to the base class */
  78. DiagonalPrinter(std::string first = "" , std::string last = "", std::ostream &os = std::cout) : StringPrinter(os) , first_(first), last_(last)
  79. { }
  80. /* Copy this object */
  81. DiagonalPrinter* clone() const
  82. {
  83. return new DiagonalPrinter(first_, last_, os);
  84. }
  85. virtual StringPrinter& operator()(const std::string& str)
  86. {
  87. size_t a = 0;
  88. size_t b = str.length() - 1;
  89. if (first_ != ""){
  90. os << first_ << std::endl;
  91. }
  92.  
  93. for (auto i : str){
  94. if (a > 0){
  95. for (size_t c = a; c > 0; c--){
  96. os << " ";
  97. }
  98. }
  99. os << i;
  100.  
  101. if (b > 0){
  102. for (size_t d = b; d > 0; d--){
  103. os << " ";
  104. }
  105. os << std::endl;
  106. }
  107. a++;
  108. b--;
  109. }
  110.  
  111. if (last_ != ""){
  112. os << std::endl << last_ << std::endl;
  113. }
  114. else{
  115. os << std::endl;
  116. }
  117. return *this;
  118. }
  119. private:
  120. std::string first_;
  121. std::string last_;
  122. };
  123.  
  124. /* Implement the class DiagonalPrinter here
  125. * The class inherits (public inheritance) class StringPrinter
  126. *
  127. * DiagonalPrinter prints a string diagonally
  128. * l -
  129. * i -
  130. * k -
  131. * e -
  132. * -
  133. * t -
  134. * h -
  135. * i -
  136. * s-
  137. * Where the dash (-) indicates a newline character.
  138. * Additionally and optionally, this printer can print a special first and
  139. * last line for its output. This functionality is disabled by default.
  140. *
  141. * The DiagonalPrinter is constructed with three parameters
  142. * The first line - "" by default, i.e. it is not printed at all
  143. * The last line - "" by default, i.e. it is not printed at all either
  144. * The output stream
  145. *
  146. * Note that the default print functionality does not suffice for this class
  147. * and you will have to implement the print overload yourself.
  148. *
  149. * Implement the class accordingly.
  150. */
  151.  
  152.  
  153. /* Class Printers
  154. * A storage for StringPrinters pointers - has ownership of the objects
  155. * Maintains an association of name to StringPrinter pointer
  156. * Note:
  157. * Some functions can throw exceptions - this is specified for each function
  158. */
  159. class Printers
  160. {
  161. public:
  162. /* Construct an empty Printers object */
  163. Printers() { }
  164.  
  165. /* Copy construct a Printers object */
  166. Printers(const Printers& a){
  167. index = a.index;
  168. }
  169.  
  170. /* Free all memory associated with the Printers object */
  171. ~Printers();
  172.  
  173. /* Adds a new StringPrinter to the Printers object
  174. * Creates the association of name to StringPrinter*
  175. * Throws:
  176. * std::invalid_argument if an association for the name already exists ("Duplicate index")
  177. * std::invalid_argument if the pointer is null ("Invalid printer")
  178. * Note: THe Printers object takes ownership of the pointer and is
  179. * therefore responsible for eventually calling delete on the pointer
  180. */
  181. void add(const std::string& name, StringPrinter* ptr);
  182.  
  183. /* Retrieves a StringPrinter from the Printers object
  184. * The StringPrinter is obtained based on the association on the name
  185. * Returns:
  186. * a reference to the StringPrinter object
  187. * Throws:
  188. * std::invalid_argument if an association does not exist ("Unknown index")
  189. */
  190. StringPrinter& operator[](const std::string& t);
  191.  
  192. /* Assign a Printers object */
  193. void operator=(const Printers& a){
  194. index = a.index;
  195. }
  196.  
  197. private:
  198. /* Internal representation of Printers
  199. * Associates std::string to StringPrinter*
  200. * Note: Due to the pointer content, this does not copy nicely
  201. */
  202. std::map<std::string, StringPrinter*> index;
  203. };
  204.  
  205. #endif
  206.  
  207. //CP CP CEE PEE PEE//
  208. #include "printers.hpp"
  209.  
  210. #include <stdexcept>
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217. Printers::~Printers(){
  218. for(auto it = index.begin(); it != index.end(); it++){
  219. index.erase(it);
  220. }
  221. }
  222.  
  223. void Printers::add(const std::string& name, StringPrinter* ptr){
  224. for (auto i : index){
  225. if (name == i.first){
  226. throw std::invalid_argument("Duplicate index");
  227. }
  228. }
  229. if (ptr == nullptr){
  230. throw std::invalid_argument("Invalid printer");
  231. }
  232.  
  233. index.insert( std::pair<std::string, StringPrinter*>(name, ptr) );
  234.  
  235. }
  236.  
  237. StringPrinter& Printers::operator[](const std::string& t){
  238.  
  239. for (auto it = index.begin(); it != index.end(); it++){
  240. if (t == it->first){
  241. return *(it->second);
  242. }
  243. }
  244. throw std::invalid_argument("Unknown index");
  245.  
  246.  
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement