Skip to content

Commit

Permalink
add for tm_ostream
Browse files Browse the repository at this point in the history
  • Loading branch information
jingkaimori committed Jun 2, 2024
1 parent 8b1d80f commit e79e91f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 54 deletions.
55 changes: 13 additions & 42 deletions System/IO/tm_ostream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Routines for abstract base class
******************************************************************************/

tm_ostream_rep::tm_ostream_rep () : ref_count (0) {}
tm_ostream_rep::tm_ostream_rep () {}
tm_ostream_rep::~tm_ostream_rep () {}
void
tm_ostream_rep::flush () {}
Expand Down Expand Up @@ -116,21 +116,21 @@ std_ostream_rep::flush () {

class buffered_ostream_rep : public tm_ostream_rep {
public:
tm_ostream_rep* master;
string buf;
tm_ostream master;
string buf;

public:
buffered_ostream_rep (tm_ostream_rep* master);
buffered_ostream_rep (tm_ostream master);
~buffered_ostream_rep ();

bool is_writable () const;
void write (const char*);
};

buffered_ostream_rep::buffered_ostream_rep (tm_ostream_rep* master2)
buffered_ostream_rep::buffered_ostream_rep (tm_ostream master2)
: master (master2) {}

buffered_ostream_rep::~buffered_ostream_rep () { DEC_COUNT (master); }
buffered_ostream_rep::~buffered_ostream_rep () {}

bool
buffered_ostream_rep::is_writable () const {
Expand All @@ -146,33 +146,9 @@ buffered_ostream_rep::write (const char* s) {
* Abstract user interface
******************************************************************************/

tm_ostream::tm_ostream () : rep (tm_new<std_ostream_rep> ()) {
INC_COUNT (this->rep);
}
tm_ostream::tm_ostream (char* s) : rep (tm_new<std_ostream_rep> (s)) {
INC_COUNT (this->rep);
}
tm_ostream::tm_ostream (FILE* f) : rep (tm_new<std_ostream_rep> (f)) {
INC_COUNT (this->rep);
}
tm_ostream::tm_ostream (const tm_ostream& x) : rep (x.rep) {
INC_COUNT (this->rep);
}
tm_ostream::tm_ostream (tm_ostream_rep* rep2) : rep (rep2) {
INC_COUNT (this->rep);
}
tm_ostream::~tm_ostream () { DEC_COUNT (this->rep); }
tm_ostream_rep*
tm_ostream::operator->() {
return rep;
}
tm_ostream&
tm_ostream::operator= (tm_ostream x) {
INC_COUNT (x.rep);
DEC_COUNT (this->rep);
this->rep= x.rep;
return *this;
}
tm_ostream::tm_ostream () : base (make<std_ostream_rep> ()) {}
tm_ostream::tm_ostream (char* s) : base (make<std_ostream_rep> (s)) {}
tm_ostream::tm_ostream (FILE* f) : base (make<std_ostream_rep> (f)) {}
bool
tm_ostream::operator== (tm_ostream& out) {
return (&out == this);
Expand All @@ -190,25 +166,20 @@ tm_ostream::flush () {

void
tm_ostream::buffer () {
rep= tm_new<buffered_ostream_rep> (rep);
INC_COUNT (rep);
*this= tm_ostream (make<buffered_ostream_rep> (*this));
}

string
tm_ostream::unbuffer () {
buffered_ostream_rep* ptr= (buffered_ostream_rep*) rep;
rep = ptr->master;
string r = ptr->buf;
INC_COUNT (rep);
DEC_COUNT (ptr);
string r = ptr->buf;
*this = ptr->master;
return r;
}

void
tm_ostream::redirect (tm_ostream x) {
INC_COUNT (x.rep);
DEC_COUNT (this->rep);
this->rep= x.rep;
*this= x;
}

/******************************************************************************
Expand Down
16 changes: 4 additions & 12 deletions System/IO/tm_ostream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
#define OUT_STREAM_HPP

// #include "url.hpp"
#include "sharedptr.hpp"
#include "string.hpp"
#include <cstdio>
class tm_ostream;

class tm_ostream_rep {
public:
int ref_count;

public:
tm_ostream_rep ();
virtual ~tm_ostream_rep ();
Expand All @@ -33,9 +31,8 @@ class tm_ostream_rep {
friend class tm_ostream;
};

class tm_ostream {
public:
tm_ostream_rep* rep;
class tm_ostream : public counted_ptr<tm_ostream_rep> {
using base::counted_ptr;

public:
static tm_ostream private_cout;
Expand All @@ -47,12 +44,7 @@ class tm_ostream {
tm_ostream ();
tm_ostream (char*);
tm_ostream (FILE*);
tm_ostream (const tm_ostream&);
tm_ostream (tm_ostream_rep*);
~tm_ostream ();
tm_ostream_rep* operator->();
tm_ostream& operator= (tm_ostream x);
bool operator== (tm_ostream&);
bool operator== (tm_ostream&);

void clear ();
void flush ();
Expand Down

0 comments on commit e79e91f

Please sign in to comment.