fork download
  1. // based on ex7.32
  2. #include <string>
  3. #include <vector>
  4. #include <iostream>
  5.  
  6. template <std::string::size_type height, std::string::size_type width>
  7. class Screen;
  8.  
  9. class Window_mgr {
  10. public:
  11. using FixedScreen = Screen<24, 80>;
  12. using screen_index = std::vector<FixedScreen>::size_type;
  13. void clear(screen_index);
  14. Window_mgr();
  15.  
  16. private:
  17. std::vector<FixedScreen> screens;
  18. };
  19.  
  20. // the Screen size is fixed at run time
  21. template <std::string::size_type height, std::string::size_type width>
  22. class Screen {
  23. friend void Window_mgr::clear(screen_index);
  24.  
  25. public:
  26. using size_type = std::string::size_type;
  27. using content_type = char;
  28.  
  29. explicit Screen(content_type c = ' ')
  30. : cursor(0), contents(height * width, c) { }
  31.  
  32. const content_type &get() const { return contents[cursor]; }
  33. const content_type &get(size_type row, size_type col) const;
  34.  
  35. Screen &set(content_type c);
  36. Screen &set(size_type row, size_type col, content_type c);
  37.  
  38. Screen &move(size_type row, size_type col);
  39.  
  40. const Screen &display(std::ostream &os) const;
  41. Screen &display(std::ostream &os);
  42.  
  43. private:
  44. void do_display(std::ostream &os) const;
  45.  
  46. size_type cursor = 0;
  47. std::string contents;
  48. };
  49.  
  50. template <std::string::size_type height, std::string::size_type width>
  51. inline const typename Screen<height, width>::content_type &
  52. Screen<height, width>::get(size_type row, size_type col) const {
  53. return contents[row * width + col];
  54. }
  55.  
  56. template <std::string::size_type height, std::string::size_type width>
  57. inline Screen<height, width> &Screen<height, width>::set(content_type c) {
  58. contents[cursor] = c;
  59. return *this;
  60. }
  61.  
  62. template <std::string::size_type height, std::string::size_type width>
  63. inline Screen<height, width> &
  64. Screen<height, width>::set(size_type row, size_type col, content_type c) {
  65. contents[row * width + col] = c;
  66. return *this;
  67. }
  68.  
  69. template <std::string::size_type height, std::string::size_type width>
  70. inline Screen<height, width> &
  71. Screen<height, width>::move(size_type row, size_type col) {
  72. cursor = row * width + col;
  73. return *this;
  74. }
  75.  
  76. template <std::string::size_type height, std::string::size_type width>
  77. inline const Screen<height, width> &
  78. Screen<height, width>::display(std::ostream &os) const {
  79. do_display(os);
  80. return *this;
  81. }
  82.  
  83. template <std::string::size_type height, std::string::size_type width>
  84. inline Screen<height, width> &
  85. Screen<height, width>::display(std::ostream &os) {
  86. do_display(os);
  87. return *this;
  88. }
  89.  
  90. template <std::string::size_type height, std::string::size_type width>
  91. inline void Screen<height, width>::do_display(std::ostream &os) const {
  92. for (size_type i = 0; i != contents.size(); ++i) {
  93. os << contents[i];
  94. if ((i + 1) % width == 0 && i + 1 != contents.size())
  95. os << "\n";
  96. }
  97. }
  98.  
  99. Window_mgr::Window_mgr() : screens{FixedScreen()} {}
  100.  
  101. //void Window_mgr::clear(screen_index i) {
  102. // FixedScreen &s = screens[i];
  103. // s.contents = std::string(s.height * s.width, ' '); // error in template
  104. //}
  105.  
  106. int main() {
  107. Screen<5, 5> myScreen('X');
  108. myScreen.display(std::cout);
  109. std::cout << "\n-----\n";
  110. myScreen.move(4,0).set('#').display(std::cout);
  111. std::cout << "\n-----\n";
  112. myScreen.display(std::cout);
  113. std::cout << "\n-----\n";
  114.  
  115. Screen<2, 3> myScreen2;
  116. const Screen<2, 3> blank;
  117. myScreen2.set('#').display(std::cout); // calls nonconst version
  118. std::cout << "\n-----\n";
  119. blank.display(std::cout); // calls const version
  120. std::cout << "\n-----\n";
  121.  
  122. Window_mgr window;
  123. //window.clear(0);
  124.  
  125. return 0;
  126. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
-----
XXXXX
XXXXX
XXXXX
XXXXX
#XXXX
-----
XXXXX
XXXXX
XXXXX
XXXXX
#XXXX
-----
#  
   
-----
   
   
-----