Commit b208801c authored by EXT Arnaud Clère's avatar EXT Arnaud Clère
Browse files

small refactoring

parent c65a73fe
......@@ -130,14 +130,9 @@ public:
template<typename T> TResult bind(const T& t) { return value(). bind(t); }
template<typename T> TResult bind( T&& t) { return value(). bind(t); }
protected:
//friend class TImpl; // uses method below
QScopedResult(TImpl* result, bool owned) : m(result), m_owned(owned) {}
QScopedResult(TImpl* result, bool owned = false) : m(result), m_owned(owned) {}
Val<TResult> value() { return std::move(Val<TResult>(std::move(*static_cast<TResult*>(this)))); }
Val<TResult> innerValue() { TResult inner; inner.m = m; inner.m_owned = false; return std::move(Val<TResult>(std::move(*inner ))); }
//QJsonBuilder(TImpl* outer) : QScopedResult(outer, false) {}
Val<TResult> value() { return std::move(Val<TResult>(std::move(*static_cast<TResult*>(this)))); }
TImpl* m = nullptr;
bool m_owned = true; //!< for nested QBind only
......
......@@ -94,15 +94,25 @@ enum tag{
// //////////////////////////////////////////////////////////////////////////
// QBind<QCborWriter,T> support
class QCborWriter;
class QCborWriterShared
class QCborWriterImpl;
class QCborWriter : public QScopedResult<QCborWriter, QCborWriterImpl, BindMode::Write>
{
Q_DISABLE_COPY(QCborWriter) // but not swap
public:
Q_ENABLE_DEFAULT_MOVE(QCborWriter)
QCborWriter(QIODevice* io);
private:
friend class QCborWriterImpl; // uses method below
QCborWriter(QCborWriterImpl* outer) : QScopedResult(outer, false) {}
};
class QCborWriterImpl
{
QIODevice* io;
int levels = 0; //!< minimal dynamic context to ensure well-formedness in case TResult is abandoned
public:
QCborWriterShared() = default;
QCborWriterShared(QIODevice* io) : io(io) { Q_ASSERT(io); }
~QCborWriterShared() { while (levels) out(); }
QCborWriterImpl() = default;
QCborWriterImpl(QIODevice* io) : io(io) { Q_ASSERT(io); }
~QCborWriterImpl() { while (levels) out(); }
operator bool() { return io; } //!< to drive QBind<TResult,T>() traversal
protected:
......@@ -133,9 +143,9 @@ protected:
return true; }
// Val<TResult> prevents QBind from getting out() of an outer Seq for instance
template<typename T> bool bind(const T& t) { return QBind<QCborWriter,T>::bind(Val<QCborWriter>(QCborWriter(this)), const_cast<T&>(t)); } // t will not be modified anyway
template<typename T> bool bind( T& t) { return QBind<QCborWriter,T>::bind(Val<QCborWriter>(QCborWriter(this)), t ); }
template<typename T> bool bind( T&& t) { return QBind<QCborWriter,T>::bind(Val<QCborWriter>(QCborWriter(this)), t ); }
template<typename T> bool bind(const T& t) { return QBind<QCborWriter,T>::bind(QCborWriter(this).value(), const_cast<T&>(t)); } // t will not be modified anyway
template<typename T> bool bind( T& t) { return QBind<QCborWriter,T>::bind(QCborWriter(this).value(), t ); }
template<typename T> bool bind( T&& t) { return QBind<QCborWriter,T>::bind(QCborWriter(this).value(), t ); }
template<class T_> friend class Seq; // calls methods below
bool item() { return true; }
......@@ -155,13 +165,4 @@ private:
}
}
};
class QCborWriter : public QScopedResult<QCborWriter, QCborWriterShared, BindMode::Write>
{
Q_DISABLE_COPY(QCborWriter) // but not swap
public:
Q_ENABLE_DEFAULT_MOVE(QCborWriter)
QCborWriter(QIODevice* io) : QScopedResult(new QCborWriterShared(io), true) {}
private:
friend class QCborWriterShared; // uses method below
QCborWriter(QCborWriterShared* outer) : QScopedResult(outer, false) {}
};
QCborWriter::QCborWriter(QIODevice* io) : QScopedResult(new QCborWriterImpl(io), true) {}
......@@ -52,14 +52,24 @@
// //////////////////////////////////////////////////////////////////////////
// QBind<QJson*,T> support
class QJsonBuilder;
class QJsonBuilderShared
class QJsonBuilderImpl;
class QJsonBuilder : public QScopedResult<QJsonBuilder, QJsonBuilderImpl, BindMode::Write>
{
Q_DISABLE_COPY(QJsonBuilder)
public:
Q_ENABLE_DEFAULT_MOVE(QJsonBuilder)
QJsonBuilder(QJsonValue* v);
private:
friend class QJsonBuilderImpl; // uses method below
QJsonBuilder(QJsonBuilderImpl* outer) : QScopedResult(outer, false) {}
};
class QJsonBuilderImpl
{
QJsonValue* result;
struct Level { const char* key; /* TODO union */ QJsonObject object; QJsonArray array; Level(const char* k=nullptr) : key(k) {}};
QStack<Level> levels = QStack<Level>(); //!< minimal dynamic context to implement out() and ensure actual building in case QJsonBuilderShared is abandoned
QStack<Level> levels = QStack<Level>(); //!< minimal dynamic context to implement out() and ensure actual building in case QJsonBuilderImpl is abandoned
public:
QJsonBuilderShared(QJsonValue* v) : result(v) { Q_ASSERT(v); }
QJsonBuilderImpl(QJsonValue* v) : result(v) { Q_ASSERT(v); }
protected:
template<class T_> friend class Val; // calls methods below
......@@ -73,9 +83,9 @@ protected:
template<typename T> bool bind(T t, std::enable_if_t<std::is_arithmetic<T>::value>* = nullptr) { return bind(double(t)); }
// Val<TResult> prevents QBind from getting out() of an outer Seq for instance
template<typename T> bool bind(const T& t) { return QBind<QJsonBuilder,T>::bind(Val<QJsonBuilder>(QJsonBuilder(this)), const_cast<T&>(t)); } // t will not be modified anyway
template<typename T> bool bind( T& t) { return QBind<QJsonBuilder,T>::bind(Val<QJsonBuilder>(QJsonBuilder(this)), t ); }
template<typename T> bool bind( T&& t) { return QBind<QJsonBuilder,T>::bind(Val<QJsonBuilder>(QJsonBuilder(this)), t ); }
template<typename T> bool bind(const T& t) { return QBind<QJsonBuilder,T>::bind(QJsonBuilder(this).value(), const_cast<T&>(t)); } // t will not be modified anyway
template<typename T> bool bind( T& t) { return QBind<QJsonBuilder,T>::bind(QJsonBuilder(this).value(), t ); }
template<typename T> bool bind( T&& t) { return QBind<QJsonBuilder,T>::bind(QJsonBuilder(this).value(), t ); }
template<class T_> friend class Seq; // calls methods below
......@@ -94,27 +104,28 @@ private:
}
}
};
class QJsonBuilder : public QScopedResult<QJsonBuilder, QJsonBuilderShared, BindMode::Write>
QJsonBuilder::QJsonBuilder(QJsonValue* v) : QScopedResult(new QJsonBuilderImpl(v), true) {}
class QJsonWriterImpl;
class QJsonWriter : public QScopedResult<QJsonWriter, QJsonWriterImpl, BindMode::Write>
{
Q_DISABLE_COPY(QJsonBuilder)
Q_DISABLE_COPY(QJsonWriter)
public:
Q_ENABLE_DEFAULT_MOVE(QJsonBuilder)
QJsonBuilder(QJsonValue* v) : QScopedResult(new QJsonBuilderShared(v), true) {}
Q_ENABLE_DEFAULT_MOVE(QJsonWriter)
QJsonWriter(QIODevice* io);
private:
friend class QJsonBuilderShared; // uses method below
QJsonBuilder(QJsonBuilderShared* outer) : QScopedResult(outer, false) {}
friend class QJsonWriterImpl; // uses method below
QJsonWriter(QJsonWriterImpl* outer) : QScopedResult(outer, false) {}
};
class QJsonWriter;
class QJsonWriterShared
class QJsonWriterImpl
{
private:
struct Level { const char* sep; const char* end; };
QIODevice* io;
QStack<Level> levels = QStack<Level>(); //!< minimal dynamic context to implement out() and ensure well-formedness in case TResult is abandoned
public:
QJsonWriterShared(QIODevice* io) : io(io) { Q_ASSERT(io); }
~QJsonWriterShared() { for (auto&& level : levels) io->write(level.end); }
QJsonWriterImpl(QIODevice* io) : io(io) { Q_ASSERT(io); }
~QJsonWriterImpl() { for (auto&& level : levels) io->write(level.end); }
protected:
template<class T_> friend class Val; // calls methods below
......@@ -145,26 +156,27 @@ protected:
bool item() { io->write(levels.last().sep); levels.last().sep = ","; return this; }
bool out() { io->write(levels.pop() .end) ; return this; }
};
class QJsonWriter : public QScopedResult<QJsonWriter, QJsonWriterShared, BindMode::Write>
QJsonWriter::QJsonWriter(QIODevice* io) : QScopedResult(new QJsonWriterImpl(io), true) {}
class QJsonReaderImpl;
class QJsonReader : public QScopedResult<QJsonReader, QJsonReaderImpl, BindMode::Read>
{
Q_DISABLE_COPY(QJsonWriter)
Q_DISABLE_COPY(QJsonReader)
public:
Q_ENABLE_DEFAULT_MOVE(QJsonWriter)
QJsonWriter(QIODevice* io) : QScopedResult(new QJsonWriterShared(io), true) {}
Q_ENABLE_DEFAULT_MOVE(QJsonReader)
QJsonReader(QIODevice* io);
private:
friend class QJsonWriterShared; // uses method below
QJsonWriter(QJsonWriterShared* outer) : QScopedResult(outer, false) {}
friend class QJsonReaderImpl; // uses method below
QJsonReader(QJsonReaderImpl* outer) : QScopedResult(outer, false) {}
};
class QJsonReader;
class QJsonReaderShared
class QJsonReaderImpl
{
struct Level { bool isFirst; const char* end; };
QIODevice* io;
QStack<Level> levels = QStack<Level>(); //!< minimal dynamic context to implement out()
char nextChar = '\0'; //!< minimal dynamic context to parse
public:
QJsonReaderShared(QIODevice* io) : io(io) { Q_ASSERT(io); getChar(); }
QJsonReaderImpl(QIODevice* io) : io(io) { Q_ASSERT(io); getChar(); }
protected:
template<class T_> friend class Val; // calls methods below
......@@ -246,7 +258,7 @@ protected:
return true; }
// Val<TResult> prevents QBind from getting out() of an outer Seq for instance
template<typename T> bool bind(T& t) { return QBind<QJsonReader,T>::bind(Val<QJsonReader>(QJsonReader(this)), t); }
template<typename T> bool bind(T& t) { return QBind<QJsonReader,T>::bind(QJsonReader(this).value(), t); }
// TODO static_assert on const T& and T&& ?
template<class T_> friend class Seq; // calls methods below
......@@ -337,16 +349,7 @@ private:
;
}
};
class QJsonReader : public QScopedResult<QJsonReader, QJsonReaderShared, BindMode::Read>
{
Q_DISABLE_COPY(QJsonReader)
public:
Q_ENABLE_DEFAULT_MOVE(QJsonReader)
QJsonReader(QIODevice* io) : QScopedResult(new QJsonReaderShared(io), true) {}
private:
friend class QJsonReaderShared; // uses method below
QJsonReader(QJsonReaderShared* outer) : QScopedResult(outer, false) {}
};
QJsonReader::QJsonReader(QIODevice* io) : QScopedResult(new QJsonReaderImpl(io), true) {}
// //////////////////////////////////////////////////////////////////////////
// QBind<TResult,QJson*> support
......
......@@ -74,12 +74,23 @@ struct Person
#include <type_traits>
#include <QtCore/qiodevice.h>
class TextWriter;
class TextWriterShared
class TextWriterImpl;
class TextWriter : public QScopedResult<TextWriter, TextWriterImpl, BindMode::Write>
{
QIODevice* io;
Q_DISABLE_COPY(TextWriter)
public:
Q_ENABLE_DEFAULT_MOVE(TextWriter)
TextWriter(QIODevice* io);
private:
friend class TextWriterImpl; // uses method below
TextWriter(TextWriterImpl* outer) : QScopedResult(outer, false) {}
};
class TextWriterImpl
{
Q_DISABLE_COPY(TextWriterImpl)
public:
TextWriterShared(QIODevice* io) : io(io) { Q_ASSERT(io); }
TextWriterImpl(QIODevice* io) : io(io) { Q_ASSERT(io); }
protected:
template<class T_> friend class Val; // enables calling methods below through operator->()
......@@ -88,26 +99,20 @@ protected:
bool bind(const char* s) { io->write( s ); return true; }
// Val<TResult> prevents QBind from getting out() of an outer Seq for instance
template<typename T> bool bind(const T& t) { return QBind<TextWriter,T>::bind(Val<TextWriter>(TextWriter(this)), const_cast<T&>(t)); } // t will not be modified anyway
template<typename T> bool bind( T& t) { return QBind<TextWriter,T>::bind(Val<TextWriter>(TextWriter(this)), t ); }
template<typename T> bool bind( T&& t) { return QBind<TextWriter,T>::bind(Val<TextWriter>(TextWriter(this)), t ); }
template<typename T> bool bind(const T& t) { return QBind<TextWriter,T>::bind(TextWriter(this).value(), const_cast<T&>(t)); } // t will not be modified anyway
template<typename T> bool bind( T& t) { return QBind<TextWriter,T>::bind(TextWriter(this).value(), t ); }
template<typename T> bool bind( T&& t) { return QBind<TextWriter,T>::bind(TextWriter(this).value(), t ); }
template<class T_> friend class Seq; // enables calling methods below through operator->()
bool item() { io->write(" "); return true; }
bool out() { io->write("]"); return true; }
};
class TextWriter : public QScopedResult<TextWriter, TextWriterShared, BindMode::Write>
{
Q_DISABLE_COPY(TextWriter)
public:
Q_ENABLE_DEFAULT_MOVE(TextWriter)
TextWriter(QIODevice* io) : QScopedResult(new TextWriterShared(io)) {}
private:
friend class TextWriterShared; // uses method below
TextWriter(TextWriterShared* outer) : QScopedResult(outer, false) {}
QIODevice* io;
};
TextWriter::TextWriter(QIODevice* io) : QScopedResult(new TextWriterImpl(io), true) {}
// //////////////////////////////////////////////////////////////////////////
// Tests and Benchmarks
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment