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

small refactoring and cosmetic changes on *.csv

parent c0228cdb
......@@ -64,11 +64,14 @@ public:
Q_ENABLE_DEFAULT_MOVE(Val)
Val(T_&& out) { std::swap(m_out, out); }
operator bool() { return bool(m_out); } //!< to drive QBind<TResult,T>() traversal
using TResult = typename T_::TResult; //!< the start and end point of the traversal as well as the associated processing (see QJsonWriter for an example)
using TImpl = typename TResult::TImpl;
operator bool() { return m_out.operator bool(); } //!< to drive QBind<TResult,T>() traversal
TImpl* operator->() { return m_out.operator ->(); }
/**/ Seq<T_> sequence() { if (Q_LIKELY(m_out) && m_out->sequence()) return Seq<T_>(std::move(m_out)); return Seq<T_>(); }
/**/ T_ null() { if (Q_LIKELY(m_out) && m_out-> null()) return std::move(m_out) ; return T_ (); }
template<typename T> T_ bind(const T& t) { if (Q_LIKELY(m_out) && m_out-> bind(t)) return std::move(m_out) ; return T_ (); }
template<typename T> T_ bind( T& t) { if (Q_LIKELY(m_out) && m_out-> bind(t)) return std::move(m_out) ; return T_ (); }
template<typename T> T_ bind( T&& t) { if (Q_LIKELY(m_out) && m_out-> bind(t)) return std::move(m_out) ; return T_ (); }
......@@ -83,12 +86,13 @@ public:
Q_ENABLE_DEFAULT_MOVE(Seq)
// T_ can be either a TResult or any combination of nested Seq or Rec like T_ = Rec<Seq<TResult>
using TResult = typename T_::TResult; //!< the start and end point of the traversal as well as the associated processing (see QJsonWriter below for an example)
using TImpl = typename TResult::TImpl;
using TResult = typename T_::TResult; //!< the start and end point of the traversal as well as the associated processing (see QJsonWriter for an example)
using TImpl = typename TResult::TImpl;
Seq(T_&& out) { std::swap(m_out, out); }
operator bool() { return m_out; } //!< to drive QBind<TResult,T>() traversal
operator bool() { return m_out.operator bool(); } //!< to drive QBind<TResult,T>() traversal
TImpl* operator->() { return m_out.operator ->(); }
Val<Seq<T_>> item() { if (Q_LIKELY(m_out) && m_out->item()) return Val<Seq<T_>>(std::move(*this)); return Seq<T_>(); }
T_ out() { if (Q_LIKELY(m_out) && m_out-> out()) return std::move(m_out) ; return T_ (); }
......@@ -100,37 +104,42 @@ public:
template<typename T> Seq<T_> bind(const T& t) { return item().bind(t); }
template<typename T> Seq<T_> bind( T& t) { return item().bind(t); }
template<typename T> Seq<T_> bind( T&& t) { return item().bind(t); }
TImpl* operator->() { return m_out.operator->(); }
private:
T_ m_out = T_();
};
template<class TResult_, class TImpl_, BindMode Mode_>
class Top // : public Val<TImpl_>
class QScopedResult
{
Q_DISABLE_COPY(Top)
Q_DISABLE_COPY(QScopedResult)
public:
Q_ENABLE_SWAP(Top, std::swap(m, o.m); std::swap(m_owned, o.m_owned); )
~Top() { if (m && m_owned) delete m; }
Q_ENABLE_SWAP(QScopedResult, std::swap(m, o.m); std::swap(m_owned, o.m_owned); )
~QScopedResult() { if (m && m_owned) delete m; }
using TResult = TResult_;
using TImpl = TImpl_;
static const BindMode Mode = Mode_;
static constexpr BindMode Mode = Mode_;
operator bool() { return m; } //!< to drive QBind<TResult,T>() traversal
operator bool() { return m; } //!< to drive QBind<TResult,T>() traversal
TImpl* operator->() { Q_ASSERT_X(m,Q_FUNC_INFO,"check operator bool() before calling operator->()"); return m; }
// Shortcuts
/**/ Seq<TResult> sequence() { return static_cast<TResult*>(this)->begin().sequence(); }
template<typename T> TResult bind( T& t) { return static_cast<TResult*>(this)->begin(). bind(t); } // TODO only for readers ?
template<typename T> TResult bind(const T& t) { return static_cast<TResult*>(this)->begin(). bind(t); }
template<typename T> TResult bind( T&& t) { return static_cast<TResult*>(this)->begin(). bind(t); }
TImpl* operator->() { Q_ASSERT_X(m,Q_FUNC_INFO,"check operator bool() before calling operator->()"); return m; }
/**/ Seq<TResult> sequence() { return value().sequence(); }
/**/ TResult null() { return value(). null(); }
template<typename T> TResult bind( T& t) { return value(). bind(t); } // TODO only for readers ?
template<typename T> TResult bind(const T& t) { return value(). bind(t); }
template<typename T> TResult bind( T&& t) { return value(). bind(t); }
protected:
Top(TImpl_* m, bool owned = true) : m(m), m_owned(owned) {}
//friend class TImpl; // uses method below
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) {}
TImpl* m = nullptr; //!< gives semantic to the traversal (nullptr if already moved)
TImpl* m = nullptr;
bool m_owned = true; //!< for nested QBind only
};
......
......@@ -155,15 +155,13 @@ private:
}
}
};
class QCborWriter : public Top<QCborWriter, QCborWriterShared, BindMode::Write>
class QCborWriter : public QScopedResult<QCborWriter, QCborWriterShared, BindMode::Write>
{
Q_DISABLE_COPY(QCborWriter) // but not swap
public:
Q_ENABLE_DEFAULT_MOVE(QCborWriter)
QCborWriter(QIODevice* io) : Top(new QCborWriterShared(io)) {}
Val<QCborWriter> begin() { return std::move(Val<QCborWriter>(std::move(*this))); }
QCborWriter(QIODevice* io) : QScopedResult(new QCborWriterShared(io), true) {}
private:
friend class QCborWriterShared; // uses method below
QCborWriter(QCborWriterShared* outer) : Top(outer, false) {}
QCborWriter(QCborWriterShared* outer) : QScopedResult(outer, false) {}
};
......@@ -94,17 +94,15 @@ private:
}
}
};
class QJsonBuilder : public Top<QJsonBuilder, QJsonBuilderShared, BindMode::Write>
class QJsonBuilder : public QScopedResult<QJsonBuilder, QJsonBuilderShared, BindMode::Write>
{
Q_DISABLE_COPY(QJsonBuilder)
public:
Q_ENABLE_DEFAULT_MOVE(QJsonBuilder)
QJsonBuilder(QJsonValue* v) : Top(new QJsonBuilderShared(v)) {}
Val<TResult> begin() { return std::move(Val<TResult>(std::move(*this))); }
QJsonBuilder(QJsonValue* v) : QScopedResult(new QJsonBuilderShared(v), true) {}
private:
friend class QJsonBuilderShared; // uses method below
QJsonBuilder(QJsonBuilderShared* outer) : Top(outer, false) {}
QJsonBuilder(QJsonBuilderShared* outer) : QScopedResult(outer, false) {}
};
class QJsonWriter;
......@@ -147,17 +145,15 @@ 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 Top<QJsonWriter, QJsonWriterShared, BindMode::Write>
class QJsonWriter : public QScopedResult<QJsonWriter, QJsonWriterShared, BindMode::Write>
{
Q_DISABLE_COPY(QJsonWriter)
public:
Q_ENABLE_DEFAULT_MOVE(QJsonWriter)
QJsonWriter(QIODevice* io) : Top(new QJsonWriterShared(io)) {}
Val<QJsonWriter> begin() { return std::move(Val<QJsonWriter>(std::move(*this))); }
QJsonWriter(QIODevice* io) : QScopedResult(new QJsonWriterShared(io), true) {}
private:
friend class QJsonWriterShared; // uses method below
QJsonWriter(QJsonWriterShared* outer) : Top(outer, false) {}
QJsonWriter(QJsonWriterShared* outer) : QScopedResult(outer, false) {}
};
class QJsonReader;
......@@ -341,17 +337,15 @@ private:
;
}
};
class QJsonReader : public Top<QJsonReader, QJsonReaderShared, BindMode::Read>
class QJsonReader : public QScopedResult<QJsonReader, QJsonReaderShared, BindMode::Read>
{
Q_DISABLE_COPY(QJsonReader)
public:
Q_ENABLE_DEFAULT_MOVE(QJsonReader)
QJsonReader(QIODevice* io) : Top(new QJsonReaderShared(io)) {}
Val<QJsonReader> begin() { return std::move(Val<QJsonReader>(std::move(*this))); }
QJsonReader(QIODevice* io) : QScopedResult(new QJsonReaderShared(io), true) {}
private:
friend class QJsonReaderShared; // uses method below
QJsonReader(QJsonReaderShared* outer) : Top(outer, false) {}
QJsonReader(QJsonReaderShared* outer) : QScopedResult(outer, false) {}
};
// //////////////////////////////////////////////////////////////////////////
......
......@@ -97,17 +97,15 @@ protected:
bool item() { io->write(" "); return true; }
bool out() { io->write("]"); return true; }
};
class TextWriter : public Top<TextWriter, TextWriterShared, BindMode::Write>
class TextWriter : public QScopedResult<TextWriter, TextWriterShared, BindMode::Write>
{
Q_DISABLE_COPY(TextWriter)
public:
Q_ENABLE_DEFAULT_MOVE(TextWriter)
TextWriter(QIODevice* io) : Top(new TextWriterShared(io)) {}
Val<TextWriter> begin() { return std::move(Val<TResult>(std::move(*this))); }
TextWriter(QIODevice* io) : QScopedResult(new TextWriterShared(io)) {}
private:
friend class TextWriterShared; // uses method below
TextWriter(TextWriterShared* outer) : Top(outer, false) {}
TextWriter(TextWriterShared* outer) : QScopedResult(outer, false) {}
};
// //////////////////////////////////////////////////////////////////////////
......@@ -129,17 +127,17 @@ private:
groupWarmup=(previousTotal==0 || abs(groupTotal-previousTotal)*100/previousTotal > 1); \
previousTotal=groupTotal; \
groupTotal=0; \
if (!previousTotal) { fprintf(results,"group"); fprintf(samples, "=== " group " ========================================\n"); } else fprintf(results,group);
if (!previousTotal) { fprintf(results,"group "); fprintf(samples, group " --------------------\n"); } else fprintf(results,"%-10s",group);
#define GROUP_STOP \
if (!previousTotal) fprintf(results,",total(usecs),variation(%%)\n"); else fprintf(results,",%10lld,%3lld\n", groupTotal, previousTotal ? abs(groupTotal-previousTotal)*100/previousTotal : 0); \
if (!previousTotal) fprintf(results,"|total(usecs)|variation(%%)\n"); else fprintf(results,"|%12lld|%3lld\n", groupTotal, previousTotal ? abs(groupTotal-previousTotal)*100/previousTotal : 0); \
} while (groupWarmup); }
QElapsedTimer item;
#define START item.start(); for (int i=0; i<123; ++i)
#define STOP(label,result) { \
auto usecs=item.nsecsElapsed()/1000/123; groupTotal+=usecs; \
if (!previousTotal) { fprintf(results,"," label); fprintf(samples, "--- " label " ---------------------------------------\n%s\n", qPrintable(result)); } else fprintf(results,",%10lld", usecs); }
if (!previousTotal) { fprintf(results,"|%16s", label); fprintf(samples, "%-16s|%16s\n", label, qPrintable(result)); } else fprintf(results,"|%16lld", usecs); }
int main()
{
......@@ -172,7 +170,7 @@ int main()
QBuffer b;
b.open(QIODevice::ReadWrite);
GROUP("Format << atoms")
GROUP("<<atoms")
{
START {
s.clear();
......@@ -185,7 +183,7 @@ int main()
STOP("QDebug",s);
START {
b.seek(0); b.buffer().clear();
TextWriter(&b).begin()
TextWriter(&b)
.sequence()
.bind(123)
.bind(text)
......@@ -200,6 +198,7 @@ int main()
.bind(text)
;
}
STOP("Json",QString::fromUtf8(b.buffer()));
START {
b.seek(0); b.buffer().clear();
QCborWriter(&b)
......@@ -223,7 +222,7 @@ int main()
STOP("QWritable>Cbor",QString::fromUtf8(b.buffer().toHex()));
}
GROUP_STOP;
GROUP("Format << doubles")
GROUP("<<doubles")
{
START {
s.clear();
......@@ -236,9 +235,7 @@ int main()
STOP("QDebug",s);
START {
b.seek(0); b.buffer().clear();
TextWriter(&b).begin()
.bind(transform)
;
TextWriter(&b).bind(transform);
}
STOP("Text",QString::fromUtf8(b.buffer()));
START {
......@@ -263,7 +260,7 @@ int main()
STOP("QWritable>Cbor",QString::fromUtf8(b.buffer().toHex()));
}
GROUP_STOP
GROUP("Format << struct")
GROUP("<<struct")
{
START {
s.clear();
......@@ -277,7 +274,7 @@ int main()
STOP("QDebug",s);
START {
b.seek(0); b.buffer().clear();
TextWriter(&b).begin().bind(person);
TextWriter(&b).bind(person);
}
STOP("Text",QString::fromUtf8(b.buffer()));
START {
......
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