Skip to content

OpenToonz - An open-source full-featured 2D animation creation software

C

Modernize TIStream/TOStream safer ownership and leak fixes

andeon930d277b351f
diff --git a/toonz/sources/common/tstream/tstream.cpp b/toonz/sources/common/tstream/tstream.cpp
index 9133bf42f..cef39a4e6 100644
--- a/toonz/sources/common/tstream/tstream.cpp
+++ b/toonz/sources/common/tstream/tstream.cpp
@@ -16,12 +16,10 @@
#include <sstream>
#include <memory>
-using namespace std;
-
//===============================================================
namespace {
-string escape(string v) {
+std::string escape(std::string v) {
int i = 0;
for (;;) {
// Removing escaping of apostrophe from Windows and OSX as it's not needed and
@@ -31,8 +29,9 @@ string escape(string v) {
#else
i = v.find_first_of("\\\"", i);
#endif
- if (i == (int)string::npos) break;
- string h = "\\" + v[i];
+ if (i == (int)std::string::npos) break;
+ std::string h = "\\";
+ h += v[i];
v.insert(i, "\\");
i = i + 2;
}
@@ -45,7 +44,7 @@ void writeCompressedFile(TFilePath dst, const std::string &str) {}
//===================================================================
-void readCompressedFile(string &str, TFilePath src) {
+void readCompressedFile(std::string &str, TFilePath src) {
TFileStatus status(src);
if (!status.doesExist()) return;
@@ -120,8 +119,8 @@ bool lz4decompress(LZ4F_decompressionContext_t lz4dctx, char *out,
class StreamTag {
public:
- string m_name;
- std::map<std::string, string> m_attributes;
+ std::string m_name;
+ std::map<std::string, std::string> m_attributes;
enum Type { BeginTag, EndTag, BeginEndTag };
Type m_type;
StreamTag() : m_type(BeginTag) {}
@@ -129,26 +128,27 @@ public:
operator bool() const { return m_name != ""; }
void dump() {
- cout << "name = '" << m_name << "'" << endl;
- cout << "type = ";
+ std::cout << "name = '" << m_name << "'" << std::endl;
+ std::cout << "type = ";
switch (m_type) {
case BeginTag:
- cout << "begin Tag";
+ std::cout << "begin Tag";
break;
case EndTag:
- cout << "end Tag";
+ std::cout << "end Tag";
break;
case BeginEndTag:
- cout << "begin/end Tag";
+ std::cout << "begin/end Tag";
break;
default:
- cout << "**bad Tag**";
+ std::cout << "**bad Tag**";
break;
}
- cout << endl;
- std::map<std::string, string>::iterator it;
+ std::cout << std::endl;
+ std::map<std::string, std::string>::iterator it;
for (it = m_attributes.begin(); it != m_attributes.end(); ++it) {
- cout << " '" << it->first << "' = '" << it->second << "'" << endl;
+ std::cout << " '" << it->first << "' = '" << it->second << "'"
+ << std::endl;
}
}
};
@@ -166,8 +166,10 @@ public:
if (!m_factory) m_factory = new TPersistFactory;
return m_factory;
}
- void add(string name, TPersistDeclaration *decl) { m_table[name] = decl; }
- TPersist *create(string name) {
+ void add(std::string name, TPersistDeclaration *decl) {
+ m_table[name] = decl;
+ }
+ TPersist *create(std::string name) {
Table::iterator it = m_table.find(name);
if (it != m_table.end())
return (it->second)->create();
@@ -198,15 +200,15 @@ TPersist *TPersist::create(const std::string &name) {
class TOStream::Imp {
public:
- ostream *m_os;
+ std::ostream *m_os;
bool m_chanOwner;
bool m_compressed;
- ostringstream m_ostringstream;
+ std::ostringstream m_ostringstream;
- vector<std::string> m_tagStack;
+ std::vector<std::string> m_tagStack;
int m_tab;
bool m_justStarted;
- typedef map<TPersist *, int> PersistTable;
+ typedef std::map<TPersist *, int> PersistTable;
PersistTable m_table;
int m_maxId;
TFilePath m_filepath;
@@ -242,7 +244,7 @@ TOStream::TOStream(const TFilePath &fp, bool compressed) : m_imp(new Imp) {
TOStream::TOStream(std::shared_ptr<Imp> imp) : m_imp(std::move(imp)) {
assert(!m_imp->m_tagStack.empty());
- ostream &os = *m_imp->m_os;
+ std::ostream &os = *m_imp->m_os;
if (!m_imp->m_justStarted) cr();
os << "<" << m_imp->m_tagStack.back() << ">";
m_imp->m_tab++;
@@ -267,10 +269,10 @@ TOStream::~TOStream() {
}
try {
if (!m_imp->m_tagStack.empty()) {
- string tagName = m_imp->m_tagStack.back();
+ std::string tagName = m_imp->m_tagStack.back();
m_imp->m_tagStack.pop_back();
assert(tagName != "");
- ostream &os = *m_imp->m_os;
+ std::ostream &os = *m_imp->m_os;
m_imp->m_tab--;
if (!m_imp->m_justStarted) cr();
os << "</" << tagName << ">";
@@ -289,8 +291,8 @@ TOStream::~TOStream() {
out_len = LZ4F_compressFrame(out, out_len, in, in_len, NULL);
if (!LZ4F_isError(out_len)) {
Tofstream os(m_imp->m_filepath);
- // TNZC <lunghezza dati decompress> <lunghezza dati compresso> <dati
- // compressi>
+ // TNZC <decompressed data length> <compressed data length>
+ // <compressed data>
os.write("TABc", 4);
TINT32 v;
v = 0x0A0B0C0D;
@@ -332,8 +334,8 @@ TOStream &TOStream::operator<<(int v) {
//---------------------------------------------------------------
TOStream &TOStream::operator<<(double v) {
- if (areAlmostEqual(v, 0)) // con valori molto piccoli (es. 1.4e-310) non
- // riesce a rileggerli!
+ if (areAlmostEqual(v, 0)) // with very small values (e.g. 1.4e-310) it cannot
+ // read them back!
v = 0;
*(m_imp->m_os) << v << " ";
@@ -343,12 +345,11 @@ TOStream &TOStream::operator<<(double v) {
//---------------------------------------------------------------
-TOStream &TOStream::operator<<(string v) {
- ostream &os = *(m_imp->m_os);
- int len = v.length();
+TOStream &TOStream::operator<<(std::string v) {
+ std::ostream &os = *(m_imp->m_os);
+ int len = v.length();
if (len == 0) {
- os << "\"\""
- << " ";
+ os << "\"\"" << " ";
m_imp->m_justStarted = false;
return *this;
}
@@ -357,7 +358,7 @@ TOStream &TOStream::operator<<(string v) {
if ((!iswalnum(v[i]) && v[i] != '_' && v[i] != '%') ||
v[i] < 32 // Less than ASCII for SPACE
|| v[i] > 126 // Greater than ASCII for ~
- )
+ )
break;
if (i == len)
os << v << " ";
@@ -371,13 +372,12 @@ TOStream &TOStream::operator<<(string v) {
//---------------------------------------------------------------
TOStream &TOStream::operator<<(QString _v) {
- string v = _v.toStdString();
+ std::string v = _v.toStdString();
- ostream &os = *(m_imp->m_os);
- int len = v.length();
+ std::ostream &os = *(m_imp->m_os);
+ int len = v.length();
if (len == 0) {
- os << "\"\""
- << " ";
+ os << "\"\"" << " ";
m_imp->m_justStarted = false;
return *this;
}
@@ -386,7 +386,7 @@ TOStream &TOStream::operator<<(QString _v) {
if ((!iswalnum(v[i]) && v[i] != '_' && v[i] != '%') ||
v[i] < 32 // Less than ASCII for SPACE
|| v[i] > 126 // Greater than ASCII for ~
- )
+ )
break;
if (i == len)
os << v << " ";
@@ -402,7 +402,7 @@ TOStream &TOStream::operator<<(QString _v) {
TOStream &TOStream::operator<<(std::wstring v) {
return operator<<(::to_string(v));
/*
-ostream &os = *(m_imp->m_os);
+std::ostream &os = *(m_imp->m_os);
int len = v.length();
if(len==0)
{
@@ -449,7 +449,7 @@ TOStream &TOStream::operator<<(const TFilePath &v) {
//---------------------------------------------------------------
TOStream &TOStream::operator<<(const TPixel32 &v) {
- ostream &os = *(m_imp->m_os);
+ std::ostream &os = *(m_imp->m_os);
os << (int)v.r << " " << (int)v.g << " " << (int)v.b << " " << (int)v.m
<< " ";
m_imp->m_justStarted = false;
@@ -459,7 +459,7 @@ TOStream &TOStream::operator<<(const TPixel32 &v) {
//---------------------------------------------------------------
TOStream &TOStream::operator<<(const TPixel64 &v) {
- ostream &os = *(m_imp->m_os);
+ std::ostream &os = *(m_imp->m_os);
os << (int)v.r << " " << (int)v.g << " " << (int)v.b << " " << (int)v.m
<< " ";
m_imp->m_justStarted = false;
@@ -469,7 +469,7 @@ TOStream &TOStream::operator<<(const TPixel64 &v) {
//---------------------------------------------------------------
void TOStream::cr() {
- *(m_imp->m_os) << endl;
+ *(m_imp->m_os) << std::endl;
for (int i = 0; i < m_imp->m_tab; i++) *(m_imp->m_os) << " ";
m_imp->m_justStarted = false;
}
@@ -479,7 +479,7 @@ void TOStream::cr() {
TOStream::operator bool() const { return (m_imp->m_os && *m_imp->m_os); }
//---------------------------------------------------------------
-TOStream TOStream::child(string tagName) {
+TOStream TOStream::child(std::string tagName) {
assert(tagName != "");
m_imp->m_tagStack.push_back(tagName);
return TOStream(m_imp);
@@ -487,7 +487,7 @@ TOStream TOStream::child(string tagName) {
//---------------------------------------------------------------
-void TOStream::openChild(string tagName) {
+void TOStream::openChild(std::string tagName) {
assert(tagName != "");
m_imp->m_tagStack.push_back(tagName);
if (m_imp->m_justStarted == false) cr();
@@ -499,13 +499,14 @@ void TOStream::openChild(string tagName) {
//---------------------------------------------------------------
-void TOStream::openChild(string tagName,
- const map<std::string, string> &attributes) {
+void TOStream::openChild(std::string tagName,
+ const std::map<std::string, std::string> &attributes) {
assert(tagName != "");
m_imp->m_tagStack.push_back(tagName);
if (m_imp->m_justStarted == false) cr();
*(m_imp->m_os) << "<" << m_imp->m_tagStack.back();
- for (std::map<std::string, string>::const_iterator it = attributes.begin();
+ for (std::map<std::string, std::string>::const_iterator it =
+ attributes.begin();
it != attributes.end(); ++it) {
*(m_imp->m_os) << " " << it->first << "=\"" << escape(it->second) << "\"";
}
@@ -519,10 +520,10 @@ void TOStream::openChild(string tagName,
void TOStream::closeChild() {
assert(!m_imp->m_tagStack.empty());
- string tagName = m_imp->m_tagStack.back();
+ std::string tagName = m_imp->m_tagStack.back();
m_imp->m_tagStack.pop_back();
assert(tagName != "");
- // ostream &os = *m_imp->m_os; //os non e' usato
+ // std::ostream &os = *m_imp->m_os; // os is not used
m_imp->m_tab--;
if (!m_imp->m_justStarted) cr();
*(m_imp->m_os) << "</" << tagName << ">";
@@ -532,13 +533,14 @@ void TOStream::closeChild() {
//---------------------------------------------------------------
-void TOStream::openCloseChild(string tagName,
- const map<std::string, string> &attributes) {
+void TOStream::openCloseChild(
+ std::string tagName, const std::map<std::string, std::string> &attributes) {
assert(tagName != "");
// m_imp->m_tagStack.push_back(tagName);
if (m_imp->m_justStarted == false) cr();
*(m_imp->m_os) << "<" << tagName;
- for (std::map<std::string, string>::const_iterator it = attributes.begin();
+ for (std::map<std::string, std::string>::const_iterator it =
+ attributes.begin();
it != attributes.end(); ++it) {
*(m_imp->m_os) << " " << it->first << "=\"" << escape(it->second) << "\"";
}
@@ -584,7 +586,7 @@ bool TOStream::checkStatus() const {
if (!m_imp->m_os) return false;
m_imp->m_os->flush();
- return m_imp->m_os->rdstate() == ios_base::goodbit;
+ return m_imp->m_os->rdstate() == std::ios_base::goodbit;
}
std::string TOStream::getCurrentTagName() {
@@ -598,15 +600,15 @@ std::string TOStream::getCurrentTagName() {
*/
class TIStream::Imp {
public:
- istream *m_is;
+ std::istream *m_is;
bool m_chanOwner;
int m_line;
- string m_strbuffer;
+ std::string m_strbuffer;
bool m_compressed;
- vector<std::string> m_tagStack;
+ std::vector<std::string> m_tagStack;
- typedef map<int, TPersist *> PersistTable;
+ typedef std::map<int, TPersist *> PersistTable;
PersistTable m_table;
StreamTag m_currentTag;
@@ -628,8 +630,8 @@ public:
inline void skipBlanks();
bool matchTag();
inline bool match(char c);
- bool matchIdent(string &ident);
- bool matchValue(string &value);
+ bool matchIdent(std::string &ident);
+ bool matchValue(std::string &value);
void skipCurrentTag();
};
@@ -658,8 +660,8 @@ int TIStream::Imp::getNextChar() {
//---------------------------------------------------------------
void TIStream::Imp::skipBlanks() {
- istream &is = *m_is;
- istream::int_type c;
+ std::istream &is = *m_is;
+ std::istream::int_type c;
while (c = is.peek(), (isspace(c) || c == '\r')) getNextChar();
}
@@ -675,8 +677,8 @@ bool TIStream::Imp::match(char c) {
//---------------------------------------------------------------
-bool TIStream::Imp::matchIdent(string &ident) {
- istream &is = *m_is;
+bool TIStream::Imp::matchIdent(std::string &ident) {
+ std::istream &is = *m_is;
if (!isalnum(is.peek())) return false;
ident = "";
char c;
@@ -691,9 +693,9 @@ bool TIStream::Imp::matchIdent(string &ident) {
//---------------------------------------------------------------
-bool TIStream::Imp::matchValue(string &str) {
- istream &is = *m_is;
- char quote = is.peek();
+bool TIStream::Imp::matchValue(std::string &str) {
+ std::istream &is = *m_is;
+ char quote = is.peek();
char c;
if (!is || (quote != '\'' && quote != '\"')) return false;
is.get(c);
@@ -726,7 +728,7 @@ bool TIStream::Imp::matchTag() {
if (match('!')) {
skipBlanks();
if (!match('-') || !match('-')) throw TException("expected '<!--' tag");
- istream &is = *m_is;
+ std::istream &is = *m_is;
char c;
int status = 1;
while (status != 0 && is.get(c)) switch (status) {
@@ -764,11 +766,11 @@ bool TIStream::Imp::matchTag() {
if (match('>')) break;
throw TException("expected '>'");
}
- string name;
+ std::string name;
if (!matchIdent(name)) throw TException("expected identifier");
skipBlanks();
if (match('=')) {
- string value;
+ std::string value;
skipBlanks();
if (!matchValue(value)) throw TException("expected value");
tag.m_attributes[name] = value;
@@ -782,8 +784,8 @@ bool TIStream::Imp::matchTag() {
void TIStream::Imp::skipCurrentTag() {
if (m_currentTag.m_type == StreamTag::BeginEndTag) return;
- istream &is = *m_is;
- int level = 1;
+ std::istream &is = *m_is;
+ int level = 1;
int c;
for (;;) {
if (is.eof()) break; // unexpected eof
@@ -802,8 +804,7 @@ void TIStream::Imp::skipCurrentTag() {
if (c == '/') {
// end tag
- do
- c = getNextChar();
+ do c = getNextChar();
while (c >= 0 && c != '>');
if (c < 0) break; // unexpected eof
if (--level <= 0) {
@@ -831,16 +832,16 @@ TIStream::TIStream(const TFilePath &fp) : m_imp(new Imp) {
m_imp->m_filepath = fp;
m_imp->m_is = new Tifstream(fp);
- if (m_imp->m_is->peek() == 'T') // non comincia con '<' dev'essere compresso
+ if (m_imp->m_is->peek() == 'T') // does not start with '<' must be compressed
{
bool swapForEndianness = false;
- unique_ptr<std::istream> is(m_imp->m_is);
+ std::unique_ptr<std::istream> is(m_imp->m_is);
m_imp->m_is = 0;
char magicBuffer[4];
is->read(magicBuffer, 4);
- string magic(magicBuffer, 4);
+ std::string magic(magicBuffer, 4);
size_t in_len, out_len;
if (magic == "TNZC") {
@@ -868,8 +869,9 @@ TIStream::TIStream(const TFilePath &fp) : m_imp(new Imp) {
} else
throw TException("Bad magic number");
- if (in_len <= 0 || in_len > 100000000) // 100M di tnzfile (compresso)
- // sembrano proprio esagerati
+ if (in_len <= 0 ||
+ in_len >
+ 100000000) // 100M of tnzfile (compressed) seems really excessive
throw TException("Corrupted file");
LZ4F_decompressionContext_t lz4dctx;
@@ -881,7 +883,7 @@ TIStream::TIStream(const TFilePath &fp) : m_imp(new Imp) {
char *in = (char *)malloc(in_len);
is->read((char *)in, in_len);
- m_imp->m_strbuffer.resize(out_len + 1000); // per prudenza
+ m_imp->m_strbuffer.resize(out_len + 1000); // for safety
char *out = (char *)m_imp->m_strbuffer.c_str();
size_t check_len = out_len;
@@ -897,7 +899,7 @@ TIStream::TIStream(const TFilePath &fp) : m_imp(new Imp) {
if (check_len != out_len) throw TException("corrupted file");
- m_imp->m_is = new istringstream(std::string(out, out_len));
+ m_imp->m_is = new std::istringstream(std::string(out, out_len));
}
m_imp->m_chanOwner = true;
@@ -906,7 +908,7 @@ TIStream::TIStream(const TFilePath &fp) : m_imp(new Imp) {
//---------------------------------------------------------------
/*
-TIStream::TIStream(istream &is)
+TIStream::TIStream(std::istream &is)
: m_imp(new Imp)
{
m_imp->m_is = &is;
@@ -935,7 +937,7 @@ TIStream &TIStream::operator>>(double &v) {
//---------------------------------------------------------------
TIStream &TIStream::operator>>(std::wstring &v) {
- string s;
+ std::string s;
operator>>(s);
v = ::to_wstring(s);
return *this;
@@ -943,9 +945,9 @@ TIStream &TIStream::operator>>(std::wstring &v) {
//---------------------------------------------------------------
-TIStream &TIStream::operator>>(string &v) {
- istream &is = *(m_imp->m_is);
- v = "";
+TIStream &TIStream::operator>>(std::string &v) {
+ std::istream &is = *(m_imp->m_is);
+ v = "";
m_imp->skipBlanks();
char c;
is.get(c);
@@ -984,8 +986,8 @@ TIStream &TIStream::operator>>(string &v) {
//---------------------------------------------------------------
TIStream &TIStream::operator>>(QString &v) {
- istream &is = *(m_imp->m_is);
- v = "";
+ std::istream &is = *(m_imp->m_is);
+ v = "";
m_imp->skipBlanks();
char c;
is.get(c);
@@ -1023,9 +1025,9 @@ TIStream &TIStream::operator>>(QString &v) {
//---------------------------------------------------------------
-string TIStream::getString() {
- istream &is = *(m_imp->m_is);
- string v = "";
+std::string TIStream::getString() {
+ std::istream &is = *(m_imp->m_is);
+ std::string v = "";
m_imp->skipBlanks();
char c = is.peek();
while (c != '<') {
@@ -1040,7 +1042,7 @@ string TIStream::getString() {
//---------------------------------------------------------------
TIStream &TIStream::operator>>(TPixel32 &v) {
- istream &is = *(m_imp->m_is);
+ std::istream &is = *(m_imp->m_is);
int r, g, b, m;
is >> r;
is >> g;
@@ -1055,7 +1057,7 @@ TIStream &TIStream::operator>>(TPixel32 &v) {
//---------------------------------------------------------------
TIStream &TIStream::operator>>(TPixel64 &v) {
- istream &is = *(m_imp->m_is);
+ std::istream &is = *(m_imp->m_is);
int r, g, b, m;
is >> r;
is >> g;
@@ -1071,8 +1073,8 @@ TIStream &TIStream::operator>>(TPixel64 &v) {
//---------------------------------------------------------------
TIStream &TIStream::operator>>(TFilePath &v) {
- istream &is = *(m_imp->m_is);
- string s;
+ std::istream &is = *(m_imp->m_is);
+ std::string s;
char c;
m_imp->skipBlanks();
is.get(c);
@@ -1091,8 +1093,8 @@ TIStream &TIStream::operator>>(TFilePath &v) {
is.get(c);
}
} else {
- // il filepath non e' fra virgolette:
- // puo' contenere solo caratteri alfanumerici, % e _
+ // the filepath is not in quotes: it can only contain alphanumeric
+ // characters, % and _
s.append(1, c);
while (is) {
c = is.peek();
@@ -1120,12 +1122,12 @@ TIStream &TIStream::operator>>(TPersist *&v) {
}
StreamTag tag = m_imp->m_currentTag;
m_imp->m_currentTag = StreamTag();
- string tagName = tag.m_name;
- std::map<std::string, string>::iterator it;
+ std::string tagName = tag.m_name;
+ std::map<std::string, std::string>::iterator it;
int id = -1;
it = tag.m_attributes.find("id");
if (it != tag.m_attributes.end()) id = atoi(it->second.c_str());
- // cout << "tagname = " << tagName << " id = " << id << endl;
+ // std::cout << "tagname = " << tagName << " id = " << id << std::endl;
Imp::PersistTable::iterator pit = m_imp->m_table.find(id);
if (pit == m_imp->m_table.end()) {
@@ -1175,7 +1177,7 @@ bool TIStream::eos() {
//---------------------------------------------------------------
-bool TIStream::matchTag(string &tagName) {
+bool TIStream::matchTag(std::string &tagName) {
if (!m_imp->matchTag()) return false;
if (m_imp->m_currentTag.m_type == StreamTag::EndTag) return false;
tagName = m_imp->m_currentTag.m_name;
@@ -1187,9 +1189,9 @@ bool TIStream::matchTag(string &tagName) {
//---------------------------------------------------------------
-string TIStream::getTagAttribute(string name) const {
+std::string TIStream::getTagAttribute(std::string name) const {
StreamTag &tag = m_imp->m_currentTag;
- std::map<std::string, string>::const_iterator it =
+ std::map<std::string, std::string>::const_iterator it =
tag.m_attributes.find(name);
if (it == tag.m_attributes.end())
return "";
@@ -1199,10 +1201,10 @@ string TIStream::getTagAttribute(string name) const {
//---------------------------------------------------------------
-bool TIStream::getTagParam(string paramName, string &value) {
+bool TIStream::getTagParam(std::string paramName, std::string &value) {
if (m_imp->m_tagStack.empty()) return false;
StreamTag &tag = m_imp->m_currentTag;
- std::map<std::string, string>::const_iterator it =
+ std::map<std::string, std::string>::const_iterator it =
tag.m_attributes.find(paramName);
if (it == tag.m_attributes.end()) return false;
value = it->second;
@@ -1211,10 +1213,10 @@ bool TIStream::getTagParam(string paramName, string &value) {
//---------------------------------------------------------------
-bool TIStream::getTagParam(string paramName, int &value) {
- string svalue;
+bool TIStream::getTagParam(std::string paramName, int &value) {
+ std::string svalue;
if (!getTagParam(paramName, svalue)) return false;
- istringstream is(svalue);
+ std::istringstream is(svalue);
value = 0;
is >> value;
return true;
@@ -1228,7 +1230,7 @@ bool TIStream::isBeginEndTag() {
//---------------------------------------------------------------
-bool TIStream::openChild(string &tagName) {
+bool TIStream::openChild(std::string &tagName) {
if (!m_imp->matchTag()) return false;
if (m_imp->m_currentTag.m_type != StreamTag::BeginTag) return false;
tagName = m_imp->m_currentTag.m_name;
@@ -1241,7 +1243,7 @@ bool TIStream::openChild(string &tagName) {
void TIStream::closeChild() {
if (!matchEndTag()) {
- string tagName;
+ std::string tagName;
if (!m_imp->m_tagStack.empty()) tagName = m_imp->m_tagStack.back();
if (tagName != "")
throw TException("Expected \"" + tagName + "\" end tag");
@@ -1284,4 +1286,17 @@ void TIStream::skipCurrentTag() { m_imp->skipCurrentTag(); }
//---------------------------------------------------------------
-std::string TIStream::getCurrentTagName() { return m_imp->m_tagStack.back(); }
\ No newline at end of file
+std::string TIStream::getCurrentTagName() { return m_imp->m_tagStack.back(); }
+
+//===============================================================
+// MOVE CONSTRUCTORS AND ASSIGNMENTS FOR TIStream
+//===============================================================
+
+TIStream::TIStream(TIStream &&other) noexcept : m_imp(std::move(other.m_imp)) {}
+
+TIStream &TIStream::operator=(TIStream &&other) noexcept {
+ if (this != &other) {
+ m_imp = std::move(other.m_imp);
+ }
+ return *this;
+}
diff --git a/toonz/sources/include/tstream.h b/toonz/sources/include/tstream.h
index 354514777..64946ca18 100644
--- a/toonz/sources/include/tstream.h
+++ b/toonz/sources/include/tstream.h
@@ -4,6 +4,9 @@
#define TSTREAM_H
#include <memory>
+#include <utility>
+#include <map>
+#include <string>
// TnzCore includes
#include "tpixel.h"
@@ -30,9 +33,10 @@
class TPersist;
class TFilePath;
-typedef std::pair<int, int>
- VersionNumber; //!< Integer pair storing the major and minor
- //! application version numbers.
+// use using instead of typedef
+using VersionNumber =
+ std::pair<int, int>; //!< Integer pair storing the major and minor
+ //! application version numbers.
//===================================================================
@@ -60,8 +64,13 @@ public:
TIStream(const TFilePath &is); //!< Opens the document at the specified path
~TIStream(); //!< Destroys the stream
- //! \sa std::basic_istream::operator void*().
- operator bool() const; //!< Returns whether the stream has a valid status.
+ // Add move operations
+ TIStream(TIStream &&other) noexcept;
+ TIStream &operator=(TIStream &&other) noexcept;
+
+ // Explicit bool conversion
+ explicit operator bool()
+ const; //!< Returns whether the stream has a valid status.
TIStream &operator>>(int &v); //!< Reads an integer from the stream
TIStream &operator>>(double &v); //!< Reads a double from the stream
@@ -72,14 +81,14 @@ public:
TIStream &operator>>(TPixel64 &v); //!< Reads a TPixel64 from the stream
#ifndef TNZCORE_LIGHT
- TIStream &operator>>(QString &v); //!< Reads an integer from the stream
+ TIStream &operator>>(QString &v); //!< Reads a QString from the stream
#endif
/*! \detail
This function dispatches the loading process to the derived class'
reimplementation of the TPersist::loadData() function.
-\note Unlinke operator>>(TPersist*&), this function \a requires that
+\note Unlike operator>>(TPersist*&), this function \a requires that
the passed object is of the \b correct type to read the current tag.
*/
TIStream &operator>>(
@@ -88,11 +97,11 @@ reimplementation of the TPersist::loadData() function.
//! derived object data from the stream.
//! \sa operator>>(TPersist&)
- //! \deprecated
+ //! \deprecated Use string extraction operators instead
std::string
getString(); //!< Returns the stream content as a string, up to the next tag.
- //! \deprecated
+ //! \deprecated Use matchEndTag() instead
bool eos(); //!< \brief Returns \e true in case of end of string (a
//! StreamTag::EndTag
//! is encountered or the string is empty).
@@ -116,25 +125,25 @@ reimplementation of the TPersist::loadData() function.
\return Whether the tag attribute was found.
*/
bool getTagParam(std::string paramName, std::string &value);
- bool getTagParam(std::string paramName,
- int &value); //!< \sa getTagParam(string, string&)
+ bool getTagParam(std::string paramName, int &value);
bool isBeginEndTag(); //!< Returns whether current tag is of type
//! StreamTag::BeginEndTag.
- bool openChild(
- std::string &tagName); //!< \deprecated Use matchTag(string&) instead.
- void closeChild(); //!< \deprecated Use matchEndTag() instead.
+ //! \deprecated Use matchTag() instead
+ bool openChild(std::string &tagName);
+ //! \deprecated Use matchEndTag() instead
+ void closeChild();
- bool match(char c) const; //! \deprecated
+ //! \deprecated
+ bool match(char c) const;
- std::string getTagAttribute(
- std::string name) const; //!< \sa getTagParam(string, string&),
- //! TOStream::openChild(string, const map<std::string, string>&).
+ std::string getTagAttribute(std::string name) const;
TFilePath getFilePath(); //!< Returns the stream's path (i.e. the opened
//! filename associated to the input stream).
- TFilePath getRepositoryPath(); //!< \deprecated
+ //! \deprecated
+ TFilePath getRepositoryPath();
int getLine()
const; //!< Returns the line number of the stream <TT><B>+1</B></TT>.
@@ -144,10 +153,10 @@ reimplementation of the TPersist::loadData() function.
const; //!< Returns the currently stored version of the opened document.
//! \sa setVersion()
- void setVersion(const VersionNumber &version); //!< Returns the currently
+ void setVersion(const VersionNumber &version); //!< Sets the currently
//! stored version of the
//! opened document.
- //! \sa setVersion()
+ //! \sa getVersion()
/*!
\note After skipping the tag content, the stream is positioned immediately
after the end tag.
@@ -158,18 +167,35 @@ reimplementation of the TPersist::loadData() function.
std::string getCurrentTagName();
private:
- // Not copyable
- TIStream(const TIStream &); //!< Not implemented
- TIStream &operator=(const TIStream &); //!< Not implemented
+ // Delete copy operations instead of declaring without implementation
+ TIStream(const TIStream &) = delete;
+ TIStream &operator=(const TIStream &) = delete;
};
//-------------------------------------------------------------------
+// Fix memory leak in template function
template <class T>
TIStream &operator>>(TIStream &is, T *&v) {
- TPersist *persist = 0;
+ TPersist *persist = nullptr;
is >> persist;
- v = persist ? dynamic_cast<T *>(persist) : 0;
+
+ // Use unique_ptr for automatic cleanup if cast fails
+ std::unique_ptr<TPersist> persistGuard(persist);
+
+ if (persistGuard) {
+ v = dynamic_cast<T *>(persistGuard.get());
+ if (v) {
+ // Cast succeeded, release ownership
+ persistGuard.release();
+ } else {
+ // Cast failed, unique_ptr will automatically delete
+ v = nullptr;
+ }
+ } else {
+ v = nullptr;
+ }
+
return is;
}
@@ -187,12 +213,6 @@ class DVAPI TOStream {
class Imp;
std::shared_ptr<Imp> m_imp;
-private:
- explicit TOStream(std::shared_ptr<Imp> imp); //!< deprecated
-
- TOStream(TOStream &&);
- TOStream &operator=(TOStream &&);
-
public:
/*!
\param fp Output file path
@@ -209,8 +229,12 @@ public:
bool compressed = false); //!< Opens the specified file for write
~TOStream(); //!< Closes the file and destroys the stream
- //! \sa std::basic_ostream::operator void*().
- operator bool() const; //!< Returns whether the stream has a valid status.
+ TOStream(TOStream &&other);
+ TOStream &operator=(TOStream &&other);
+
+ // Explicit bool conversion
+ explicit operator bool()
+ const; //!< Returns whether the stream has a valid status.
TOStream &operator<<(int v); //!< Writes an int to the stream.
TOStream &operator<<(double v); //!< Writes a double to the stream.
@@ -227,38 +251,32 @@ public:
TOStream &operator<<(QString v); //!< Writes a QString to the stream.
#endif
- TOStream &operator<<(
- TPersist *v); //!< deprecated Use operator<<(TPersist&) instead.
+ //! \deprecated Use operator<<(TPersist&) instead
+ TOStream &operator<<(TPersist *v);
+
TOStream &operator<<(TPersist &v); //!< Saves data to the stream according
//! to the reimplemented TPersist::saveData.
//! \deprecated Use openChild(string) instead
TOStream child(std::string tagName);
- void openChild(std::string tagName); //!< Writes a <tagName> to the stream,
- //! opening a tag.
- void openChild(
- std::string tagName,
- const std::map<std::string, std::string>
- &attributes); //!< \brief Writes a <tagName attribute1="value1" ..>
- //! to the stream, opening a tag with embedded attributes.
+ void openChild(std::string tagName);
+ void openChild(std::string tagName,
+ const std::map<std::string, std::string> &attributes);
void openCloseChild(std::string tagName,
- const std::map<std::string, std::string>
- &attributes); //!< \brief Writes a tag <tagName
- //! attribute1="value1" ../>
- //! to the stream, opening a tag with embedded attributes
- //! which is immediately closed.
+ const std::map<std::string, std::string> &attributes);
void closeChild(); //!< Closes current tag, writing </currentTagName> to the
//! stream.
- void cr(); //!< Writes carriage return to the stream. \deprecated
+ void cr(); //!< Writes carriage return to the stream.
- void tab(int dt); //!< \deprecated
+ void tab(int dt); //!< Adjusts indentation.
TFilePath getFilePath(); //!< Returns the file path of the file associated to
//! this output stream.
- TFilePath getRepositoryPath(); //!< \deprecated
+ //! \deprecated
+ TFilePath getRepositoryPath();
/*! \detail
This function is similar to operator bool(), but \b flushes the stream before
@@ -271,9 +289,12 @@ checking the status.
std::string getCurrentTagName();
private:
+ // Keep deprecated constructor private
+ explicit TOStream(std::shared_ptr<Imp> imp);
+
// Not copyable
- TOStream(const TOStream &) = delete; //!< Not implemented
- TOStream &operator=(const TOStream &) = delete; //!< Not implemented
+ TOStream(const TOStream &) = delete;
+ TOStream &operator=(const TOStream &) = delete;
};
#endif // TSTREAM_H