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

Added CborWriter:QMovedResult for writing Cbor without heap allocation...

Added CborWriter:QMovedResult for writing Cbor without heap allocation (QCborWriter can be used when a pimpled class is needed)
parent 360672e4
......@@ -94,25 +94,26 @@ enum tag{
// //////////////////////////////////////////////////////////////////////////
// QBind<QCborWriter,T> support
class QCborWriterImpl;
class QCborWriter : public QScopedResult<QCborWriter, QCborWriterImpl, BindMode::Write>
class CborWriter;
class QCborWriter : public QScopedResult<QCborWriter, CborWriter, BindMode::Write>
{
Q_DISABLE_COPY(QCborWriter)
public:
Q_ENABLE_MOVE_DEFAULT(QCborWriter)
QCborWriter(QIODevice* io);
private:
friend class QCborWriterImpl; // uses method below
QCborWriter(QCborWriterImpl* outer) : QScopedResult(outer, false) {}
friend class CborWriter; // uses method below
QCborWriter(CborWriter* outer) : QScopedResult(outer, false) {}
};
class QCborWriterImpl
class CborWriter : public QMovedResult<CborWriter, BindMode::Write>
{
QIODevice* io;
int levels = 0; //!< minimal dynamic context to ensure well-formedness in case TResult is abandoned
Q_DISABLE_COPY(CborWriter)
public:
QCborWriterImpl() = default;
QCborWriterImpl(QIODevice* io) : io(io) { Q_ASSERT(io); }
~QCborWriterImpl() { while (levels) out(); }
Q_ENABLE_MOVE_DEFAULT(CborWriter)
CborWriter(QIODevice* io) : io(io) { Q_ASSERT(io); }
~CborWriter() { while (levels) out(); }
operator bool() { return io; } // for QMovedResult
protected:
template<class T_> friend class Val; // calls methods below
bool sequence() { levels++;
......@@ -141,9 +142,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(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<typename T> bool bind(const T& t) { return QBind<CborWriter,T>::bind(CborWriter(io).value(), const_cast<T&>(t)); } // t will not be modified anyway
template<typename T> bool bind( T& t) { return QBind<CborWriter,T>::bind(CborWriter(io).value(), t ); }
template<typename T> bool bind( T&& t) { return QBind<CborWriter,T>::bind(CborWriter(io).value(), t ); }
template<class T_> friend class Seq; // calls methods below
bool item() { return true; }
......@@ -162,5 +163,8 @@ private:
else { io->putChar((majorType << cbor::MajorTypeShift) | cbor::Value64Bit); qToBigEndian( n ,bytes); io->write(bytes,sizeof(quint64)); }
}
}
QIODevice* io = nullptr;
int levels = 0; //!< minimal dynamic context to ensure well-formedness in case TResult is abandoned
};
QCborWriter::QCborWriter(QIODevice* io) : QScopedResult(new QCborWriterImpl(io), true) {}
QCborWriter::QCborWriter(QIODevice* io) : QScopedResult(new CborWriter(io), true) {}
......@@ -78,10 +78,10 @@ class TextWriter : public QMovedResult<TextWriter, BindMode::Write>
{
Q_DISABLE_COPY(TextWriter)
public:
Q_ENABLE_MOVE_DEFAULT(TextWriter)
Q_ENABLE_MOVE_DEFAULT(TextWriter) // for QMovedResult
TextWriter(QIODevice* io) : io(io) { Q_ASSERT(io); }
operator bool() { return io; }
operator bool() { return io; } // for QMovedResult
protected:
template<class T_> friend class Val; // enables calling methods below through operator->()
......@@ -99,7 +99,7 @@ protected:
bool item() { io->write(" "); return true; }
bool out() { io->write("]"); return true; }
private:
QIODevice* io = nullptr;
QIODevice* io = nullptr; // for QMovedResult
};
// //////////////////////////////////////////////////////////////////////////
......@@ -195,14 +195,14 @@ int main()
STOP("Json",QString::fromUtf8(b.buffer()));
START {
b.seek(0); b.buffer().clear();
QCborWriter(&b)
CborWriter(&b).value()
.sequence()
.bind(1.333333333333f)
.bind(text)
;
}
STOP("Cbor",QString::fromUtf8(b.buffer().toHex()));
QVector<QWritable<QCborWriter>> writables;
QVector<QWritable<CborWriter>> writables;
START {
writables.clear();
writables.append(1.333333333333f);
......@@ -211,7 +211,7 @@ int main()
STOP("QWritable","");
START {
b.seek(0); b.buffer().clear();
QCborWriter(&b).bind(writables);
CborWriter(&b).value().bind(writables);
}
STOP("QWritable>Cbor",QString::fromUtf8(b.buffer().toHex()));
}
......@@ -239,17 +239,17 @@ int main()
STOP("Json",b.buffer())
START {
b.seek(0); b.buffer().clear();
QCborWriter(&b).bind(transform);
CborWriter(&b).value().bind(transform);
}
STOP("Cbor",QString::fromUtf8(b.buffer().toHex()));
QWritable<QCborWriter> writable;
QWritable<CborWriter> writable;
START {
writable = transform;
}
STOP("QWritable","");
START {
b.seek(0); b.buffer().clear();
QCborWriter(&b).bind(writable);
CborWriter(&b).value().bind(writable);
}
STOP("QWritable>Cbor",QString::fromUtf8(b.buffer().toHex()));
}
......@@ -278,17 +278,17 @@ int main()
STOP("Json",b.buffer())
START {
b.seek(0); b.buffer().clear();
QCborWriter(&b).bind(person);
CborWriter(&b).value().bind(person);
}
STOP("Cbor",QString::fromUtf8(b.buffer().toHex()));
QWritable<QCborWriter> writable;
QWritable<CborWriter> writable;
START {
writable = person;
}
STOP("QWritable","");
START {
b.seek(0); b.buffer().clear();
QCborWriter(&b).bind(writable);
CborWriter(&b).value().bind(writable);
}
STOP("QWritable>Cbor",QString::fromUtf8(b.buffer().toHex()));
}
......@@ -320,7 +320,7 @@ int main()
STOP("Json>JsonValue",QString::fromUtf8(QJsonDocument(v.toArray()).toJson())+QString(" errors:%1").arg(errors.size()));
START {
b.seek(0); b.buffer().clear();
QCborWriter(&b).bind(v);
CborWriter(&b).value().bind(v);
}
STOP("JsonValue>Cbor",QString::fromUtf8(b.buffer().toHex()));
}
......
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