00001 //---------------------------------------------------------------------------- 00002 /** @file SgWrite.h 00003 Utility write functions for SmartGo data types. 00004 @todo Write functions logically belong to the files of the classes they 00005 are outputting; should be moved. */ 00006 //---------------------------------------------------------------------------- 00007 00008 #ifndef SG_WRITE_H 00009 #define SG_WRITE_H 00010 00011 #include <iomanip> 00012 #include <iosfwd> 00013 #include <sstream> 00014 #include <string> 00015 #include <vector> 00016 #include "SgArrayList.h" 00017 #include "SgBoardColor.h" 00018 #include "SgPoint.h" 00019 #include "SgVector.h" 00020 00021 //---------------------------------------------------------------------------- 00022 00023 /** Write left aligned text with a minimum width of 15 characters. 00024 Also appends a space to the label text. */ 00025 class SgWriteLabel 00026 { 00027 public: 00028 SgWriteLabel(const std::string& label) 00029 : m_label(label) 00030 { } 00031 00032 friend std::ostream& operator<<(std::ostream& out, 00033 const SgWriteLabel& w); 00034 00035 private: 00036 std::string m_label; 00037 }; 00038 00039 //---------------------------------------------------------------------------- 00040 00041 /** Write all points in list. 00042 Splits long lists into multiple lines. */ 00043 class SgWritePointList 00044 { 00045 public: 00046 SgWritePointList(const std::vector<SgPoint>& pointList, 00047 std::string label = "", 00048 bool writeSize = true); 00049 00050 SgWritePointList(const SgVector<SgPoint>& pointList, 00051 std::string label = "", 00052 bool writeSize = true); 00053 00054 std::ostream& Write(std::ostream& out) const; 00055 00056 private: 00057 bool m_writeSize; 00058 00059 std::vector<SgPoint> m_pointList; 00060 00061 std::string m_label; 00062 }; 00063 00064 /** @relatesalso SgWritePointList */ 00065 std::ostream& operator<<(std::ostream& out, const SgWritePointList& write); 00066 00067 //---------------------------------------------------------------------------- 00068 00069 /** Output a SgSList with SgPoint elements to a stream. */ 00070 template<int SIZE> 00071 class SgWriteSPointList 00072 { 00073 public: 00074 SgWriteSPointList(const SgArrayList<SgPoint,SIZE>& list, 00075 std::string label = "", bool writeSize = true); 00076 00077 std::ostream& Write(std::ostream& out) const; 00078 00079 private: 00080 bool m_writeSize; 00081 00082 const SgArrayList<SgPoint,SIZE>& m_list; 00083 00084 std::string m_label; 00085 }; 00086 00087 /** @relatesalso SgWriteSPointList */ 00088 template<int SIZE> 00089 std::ostream& operator<<(std::ostream& out, 00090 const SgWriteSPointList<SIZE>& write); 00091 00092 template<int SIZE> 00093 SgWriteSPointList<SIZE>::SgWriteSPointList( 00094 const SgArrayList<SgPoint,SIZE>& list, 00095 std::string label, bool writeSize) 00096 : m_writeSize(writeSize), 00097 m_list(list), 00098 m_label(label) 00099 { 00100 } 00101 00102 template<int SIZE> 00103 std::ostream& SgWriteSPointList<SIZE>::Write(std::ostream& out) const 00104 { 00105 std::vector<SgPoint> list; 00106 for (typename SgArrayList<SgPoint,SIZE>::Iterator it(m_list); it; ++it) 00107 list.push_back(*it); 00108 return (out << SgWritePointList(list, m_label, m_writeSize)); 00109 } 00110 00111 template<int SIZE> 00112 std::ostream& operator<<(std::ostream& out, 00113 const SgWriteSPointList<SIZE>& write) 00114 { 00115 return write.Write(out); 00116 } 00117 00118 //---------------------------------------------------------------------------- 00119 00120 /** Write player color and move for games in which a move is a SgPoint. 00121 @todo Move to SgPoint, merge class with SgWritePoint. */ 00122 class SgWriteMove 00123 { 00124 public: 00125 SgWriteMove(SgPoint point, SgBlackWhite color) 00126 : m_point(point), 00127 m_color(color) 00128 { } 00129 00130 private: 00131 friend std::ostream& operator<<(std::ostream& out, const SgWriteMove &w); 00132 00133 SgPoint m_point; 00134 00135 SgBlackWhite m_color; 00136 }; 00137 00138 //---------------------------------------------------------------------------- 00139 00140 /** Write line of dashes. */ 00141 class SgWriteLine 00142 { 00143 public: 00144 SgWriteLine() 00145 { } 00146 00147 friend std::ostream& operator<<(std::ostream& out, 00148 const SgWriteLine &w); 00149 }; 00150 00151 //---------------------------------------------------------------------------- 00152 00153 /** Only writes "not" if value is false */ 00154 class SgWriteBoolean 00155 { 00156 public: 00157 explicit SgWriteBoolean(bool value) : m_value(value) {} 00158 00159 private: 00160 friend std::ostream& operator<<(std::ostream& out, 00161 const SgWriteBoolean &w); 00162 bool m_value; 00163 }; 00164 00165 //---------------------------------------------------------------------------- 00166 00167 /** Writes boolean as 0/1. */ 00168 class SgWriteBoolAsInt 00169 { 00170 public: 00171 SgWriteBoolAsInt(bool value); 00172 00173 std::ostream& Write(std::ostream& out) const; 00174 00175 private: 00176 bool m_value; 00177 }; 00178 00179 /** @relatesalso SgWriteBoolAsInt */ 00180 std::ostream& operator<<(std::ostream& out, const SgWriteBoolAsInt& write); 00181 00182 //---------------------------------------------------------------------------- 00183 00184 #endif // SG_WRITE_H