From a3724718862ea28a70f2c6c330cdc729451d6510 Mon Sep 17 00:00:00 2001 From: Gert van Valkenhoef Date: Wed, 9 Nov 2011 19:15:32 +0000 Subject: [PATCH] Document fold.h and textmodel.h, and add a destructor --- fold.h | 14 ++++++++++++++ textmodel.cc | 20 +++++++++----------- textmodel.h | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/fold.h b/fold.h index 79ee735..e8c78b7 100644 --- a/fold.h +++ b/fold.h @@ -7,6 +7,10 @@ namespace odc { + /** + * Supertype for views (in MVC framework). + * Included because a Fold is a View and not a Model, for some reason. + */ class View : public Store { private: static const TypeProxy PROXY; @@ -16,9 +20,15 @@ namespace odc { virtual const std::string &getTypeName() const; View(INTEGER id); + /** + * Just calls super and reads the version and checks that its in the allowed range. + */ virtual void internalize(Reader &reader); }; + /** + * Folds are collapsible components in a text document. + */ class Fold : public View { private: static const TypeProxy PROXY; @@ -32,6 +42,10 @@ namespace odc { virtual const std::string &getTypeName() const; Fold(INTEGER id); + /** + * Calls super and reads the version and checks that its in the allowed range. + * Then reads the state of the Fold, including the hidden part. + */ virtual void internalize(Reader &reader); virtual std::string toString(); diff --git a/textmodel.cc b/textmodel.cc index c02620c..cfd7730 100644 --- a/textmodel.cc +++ b/textmodel.cc @@ -151,15 +151,16 @@ void StdTextModel::accept(Visitor &visitor) const { TextPiece::TextPiece(size_t len): d_len(len) {} -LongPiece::LongPiece(size_t len): TextPiece(len) {} +LongPiece::LongPiece(size_t len): TextPiece(len * 2) {} LongPiece::~LongPiece() { delete d_buf; } void LongPiece::read(Reader &reader) { - d_buf = new CHAR[d_len]; - reader.readLChar(d_buf, d_len); + d_buf = new CHAR[d_len / 2 + 1]; + reader.readLChar(d_buf, d_len / 2); + d_buf[d_len / 2] = 0; } std::string LongPiece::toString() const { @@ -181,16 +182,9 @@ ShortPiece::~ShortPiece() { } void ShortPiece::read(Reader &reader) { -// static char piece[] = "pieceA"; d_buf = new SHORTCHAR[d_len + 1]; reader.readSChar(d_buf, d_len); d_buf[d_len] = 0; -// std::cout.write(buf, d_len); -// std::ofstream ofs(piece, std::ios::out); -// ofs.write(buf, d_len); -// ofs.close(); -// ++piece[5]; -// delete buf; } std::string ShortPiece::toString() const { @@ -209,7 +203,11 @@ void ShortPiece::accept(Visitor &visitor) const { visitor.textShortPiece(this); } -ViewPiece::ViewPiece(Store *view): TextPiece(0), d_view(view) {} +ViewPiece::ViewPiece(Store *view): TextPiece(1), d_view(view) {} + +ViewPiece::~ViewPiece() { + delete d_view; +} void ViewPiece::read(Reader &reader) { reader.readByte(); diff --git a/textmodel.h b/textmodel.h index abfe13d..8eb26eb 100644 --- a/textmodel.h +++ b/textmodel.h @@ -16,11 +16,23 @@ namespace odc { virtual const std::string &getTypeName() const; TextModel(INTEGER id); + /** + * Just calls super and reads the version and checks that its in the allowed range. + */ virtual void internalize(Reader &reader); }; + /** + * A TextPiece is just a component of an StdTextModel. + * It has a certain length in bytes, which is known construction time (due + * to meta-data in the StdTextModel header) and contents which are read + * later. + */ class TextPiece { public: + /** + * The number of bytes that will be read. + */ const size_t d_len; TextPiece(size_t len); virtual void read(Reader &reader) = 0; @@ -28,6 +40,10 @@ namespace odc { virtual void accept(Visitor &visitor) const = 0; }; + /** + * TextPiece consisting of 16-bit characters. + * Not sure of the encoding. + */ class LongPiece : public TextPiece { private: CHAR *d_buf; @@ -36,10 +52,17 @@ namespace odc { ~LongPiece(); virtual void read(Reader &reader); virtual std::string toString() const; + /** + * Return the text contained in this piece. + * Currently just casting the buffer to wchar_t* and hoping for the best. + */ virtual std::wstring getText() const; virtual void accept(Visitor &visitor) const; }; + /** + * TextPiece consisting of 8-bit characters. + */ class ShortPiece : public TextPiece { private: SHORTCHAR *d_buf; @@ -52,15 +75,23 @@ namespace odc { virtual void accept(Visitor &visitor) const; }; + /** + * TextPiece that embeds a View. + */ class ViewPiece : public TextPiece { Store *d_view; public: ViewPiece(Store *view); + ~ViewPiece(); virtual void read(Reader &reader); virtual std::string toString() const; virtual void accept(Visitor &visitor) const; }; + /** + * Default implementation of a TextModel. + * Essentially it is a series of TextPieces. + */ class StdTextModel : public TextModel { private: static const TypeProxy PROXY; @@ -71,6 +102,11 @@ namespace odc { virtual const std::string &getTypeName() const; StdTextModel(INTEGER id); + /** + * Calls super and reads the version and checks that its in the allowed range. + * Then the text model meta-data is read, including information on all the pieces. + * After reading the meta-data, the pieces themselves are read. + */ virtual void internalize(Reader &reader); virtual std::string toString(); -- 2.29.2