record.h
Go to the documentation of this file.
00001 #ifndef _RECORD_H
00002 #define _RECORD_H
00003 #include "osl/record/searchInfo.h"
00004 #include "osl/move.h"
00005 #include "osl/state/numEffectState.h"
00006 #include "osl/misc/carray.h"
00007 #include "osl/misc/align16New.h"
00008 #include <boost/date_time/gregorian/gregorian_types.hpp>
00009 #include <boost/ptr_container/ptr_vector.hpp>
00010 #include <iosfwd>
00011 
00012 namespace osl
00013 {
00014   namespace record
00015   {
00016     class Record;
00017     class IRecordStream{
00018     public:
00019       virtual void load(Record*)=0;
00020       virtual ~IRecordStream();
00021     private:
00022     };
00023     class ORecordStream{
00024     public:
00025       virtual void save(Record*)=0;
00026       virtual ~ORecordStream();
00027     private:
00028     };
00029   
00030     enum NodeType{
00031       MOVE,
00032       TORYO,
00033       MATTA,
00034       CHUDAN,
00035       SENNICHITE,
00036       JISHOGI,
00037       TSUMI,
00038       FUZUMI,
00039       ND_ERROR, // ERROR conflicts with ERROR macro on windows
00040       KACHI,
00041       HIKIWAKE,
00042     };
00043 
00049     class MoveRecord{
00050     private:
00051       Move move;
00052       int nodeIndex;
00053       int time;
00054       std::string comment;
00055     public:
00056       SearchInfo info;
00057 
00058       MoveRecord(const Move& mv, int ni); 
00059       const Move getMove() const;
00060       int getNodeIndex() const;
00061       void setTime(int t);
00062       int getTime() const{ return time; }
00063       void setComment(const std::string& com){ comment=com; }
00064       void addComment(const std::string& com)
00065       {
00066         if (! comment.empty())
00067           comment += "\n";
00068         comment += com; 
00069       }
00070       const std::string& getComment() const{ return comment; }
00071     };
00072 
00073     class NodeRecord{
00074     private:
00075       NodeType type;
00076       vector<int> moves;
00077       std::string comment;
00078     public:
00079       NodeRecord():type(MOVE){}
00080       NodeType getType() const{ return type; }
00081       int size() const { return moves.size(); }
00082       int at(int index) const{ return moves.at(index); }
00083       void setComment(const std::string& com){ comment=com; }
00084       const std::string& getComment() const{ return comment; }
00085       void addMoveRecord(int moveIndex);
00086     };
00087   
00088     class Record
00089 #if OSL_WORDSIZE == 32
00090       : public misc::Align16New
00091 #endif
00092     {
00093     public:
00094       enum ResultType {
00095         UNKNOWN=0,
00096         BLACK_WIN=1,
00097         WHITE_WIN=2,
00098         SENNNICHITE=3,
00099         JISHOGI=4,
00100       };
00101     private:
00102       SimpleState initialState;
00103       std::string version, initial_comment, tounament_name;
00104       CArray<std::string,2> playerNames;
00105       vector<NodeRecord> nrs;
00106       vector<MoveRecord> mrs;
00107       ResultType result;
00108       boost::gregorian::date start_date; // default : not_a_date_time
00109     public:
00110       Record();
00111       Record(const SimpleState& initial, const vector<Move>& moves);
00112       void init();
00113       void setVersion(const std::string& str);
00114       const std::string getVersion() const { return version; }
00115       void addInitialComment(const std::string& comment)
00116       {
00117         if (! initial_comment.empty())
00118           initial_comment += "\n";
00119         initial_comment += comment;
00120       }
00121       const std::string getInitialComment() const 
00122       {
00123         return initial_comment; 
00124       }
00125       void setPlayer(Player player,const std::string& str);
00126       const std::string& getPlayer(Player player) const;
00127       void setInitialState(const SimpleState& state);
00128       const NumEffectState getInitialState() const;
00129       int addNodeRecord();
00130       int addMoveRecord(const MoveRecord& moveRecord);
00131       NodeRecord* nodeOf(int index);
00132       NodeRecord& operator[](int index);
00133       MoveRecord* moveOf(int index);
00134       void load(IRecordStream&); 
00135       void save(ORecordStream&); 
00136       const vector<Move> getMoves() const;
00137       void getMoves(vector<Move>&, vector<int>&) const;
00138       void getMoves(vector<Move>&, vector<int>&, vector<std::string>&,
00139                     vector<SearchInfo>&) const;
00140       const NodeRecord* nodeOf(int index) const;
00141       const MoveRecord* moveOf(int index) const;
00142       size_t moveRecordSize() const {return mrs.size();}
00143       void setResult(ResultType new_result) { result = new_result; }
00144       ResultType getResult() const { return result; }
00145       void setTounamentName(const std::string& name) { tounament_name = name; }
00146       const std::string& tounamentName() const { return tounament_name; }
00152       void setDate(const std::string& date_str);
00153       void setDate(const boost::gregorian::date& date);
00154       boost::gregorian::date getDate() const;
00155     };
00156 
00157     class RecordVisitor;
00158 
00159     class RecordVisitorObserver {
00160     public:
00161         virtual ~RecordVisitorObserver() {}
00162         virtual void update(RecordVisitor* rv) = 0;
00163     };
00164   
00165     class RecordVisitor{
00166     private:
00167       Record* rec;
00168       SimpleState* state;
00169       int lastMoveIndex;
00170       int nodeIndex;
00171       boost::ptr_vector<RecordVisitorObserver> observers;
00172     public:
00173       RecordVisitor():rec(NULL),state(NULL),lastMoveIndex(0),nodeIndex(0){}
00174       RecordVisitor(Record& r);
00175       ~RecordVisitor();
00176 
00177       SimpleState *getState() const{ return state; }
00178       void setState(SimpleState *s){ state=s;}
00179       Record *getRecord() { return rec; }
00180       void setRecord(Record *r){ rec=r;}
00181       MoveRecord *getLastMove(){ return rec->moveOf(lastMoveIndex); }
00182       void addMoveAndAdvance(Move move);
00183       NodeRecord *getNode(){ return rec->nodeOf(nodeIndex); }
00184       void addObserver(RecordVisitorObserver *observer)
00185       { observers.push_back(observer); }
00186     };
00187   
00188     std::ostream& operator<<(std::ostream&,const MoveRecord &);
00189     std::ostream& operator<<(std::ostream&,Record &);
00190 
00191     int readInt(std::istream& is);
00192     void writeInt(std::ostream& os, int n);
00193   } // namespace record
00194   using record::Record;
00195 } // namespace osl
00196 #endif /* _RECORD_H */
00197 // ;;; Local Variables:
00198 // ;;; mode:c++
00199 // ;;; c-basic-offset:2
00200 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines