From 12b7a51ea78bb162572e5b58b65fd240d70ed440 Mon Sep 17 00:00:00 2001 From: Matej Sekoranja Date: Sun, 16 Jan 2011 23:50:56 +0100 Subject: [PATCH] bitSet serialization --- pvDataApp/factory/BasePVStructure.h | 1 + pvDataApp/misc/bitSet.cpp | 99 +++++++++++++++-------------- pvDataApp/misc/bitSet.h | 11 +++- pvDataApp/misc/serialize.h | 2 +- pvDataApp/pvMisc/bitSetUtil.h | 1 + 5 files changed, 61 insertions(+), 53 deletions(-) diff --git a/pvDataApp/factory/BasePVStructure.h b/pvDataApp/factory/BasePVStructure.h index 5256e83..fa814c9 100644 --- a/pvDataApp/factory/BasePVStructure.h +++ b/pvDataApp/factory/BasePVStructure.h @@ -12,6 +12,7 @@ #include #include "pvData.h" #include "factory.h" +#include "bitSet.h" namespace epics { namespace pvData { diff --git a/pvDataApp/misc/bitSet.cpp b/pvDataApp/misc/bitSet.cpp index fe0b620..4b4f3b9 100644 --- a/pvDataApp/misc/bitSet.cpp +++ b/pvDataApp/misc/bitSet.cpp @@ -9,6 +9,7 @@ #include "bitSet.h" #include "lock.h" #include "showConstructDestruct.h" +#include "serializeHelper.h" namespace epics { namespace pvData { @@ -374,55 +375,55 @@ namespace epics { namespace pvData { *buffer += '}'; } - - /* - -void serialize(ByteBuffer buffer, SerializableControl flusher) { - - final int n = wordsInUse; - if (n == 0) { - SerializeHelper.writeSize(0, buffer, flusher); - return; + void BitSet::serialize(ByteBuffer* buffer, SerializableControl* flusher) { + + uint32 n = wordsInUse; + if (n == 0) { + SerializeHelper::writeSize(0, buffer, flusher); + return; + } + uint32 len = 8 * (n-1); + for (uint64 x = words[n - 1]; x != 0; x >>= 8) + len++; + + SerializeHelper::writeSize(len, buffer, flusher); + flusher->ensureBuffer(len); + + for (uint32 i = 0; i < n - 1; i++) + buffer->putLong(words[i]); + + for (uint64 x = words[n - 1]; x != 0; x >>= 8) + buffer->putByte((int8) (x & 0xff)); + } + + void BitSet::deserialize(ByteBuffer* buffer, DeserializableControl* control) { + + uint32 bytes = SerializeHelper::readSize(buffer, control); // in bytes + + wordsInUse = (bytes + 7) / 8; + if (wordsInUse > wordsLength) + { + if (words) delete[] words; + words = new uint64[wordsInUse]; + wordsLength = wordsInUse; + } + + if (wordsInUse == 0) + return; + + control->ensureData(bytes); + + uint32 i = 0; + uint32 longs = bytes / 8; + while (i < longs) + words[i++] = buffer->getLong(); + + for (uint32 j = i; j < wordsInUse; j++) + words[j] = 0; + + for (uint32 remaining = (bytes - longs * 8), j = 0; j < remaining; j++) + words[i] |= (buffer->getByte() & 0xffL) << (8 * j); + } - int len = 8 * (n-1); - for (long x = words[n - 1]; x != 0; x >>>= 8) - len++; - - SerializeHelper.writeSize(len, buffer, flusher); - flusher.ensureBuffer(len); - - for (int i = 0; i < n - 1; i++) - buffer.putLong(words[i]); - - for (long x = words[n - 1]; x != 0; x >>>= 8) - buffer.put((byte) (x & 0xff)); -} - -public void deserialize(ByteBuffer buffer, DeserializableControl control) { - - final int bytes = SerializeHelper.readSize(buffer, control); // in bytes - - wordsInUse = (bytes + 7) / 8; - if (wordsInUse > words.length) - words = new long[wordsInUse]; - - if (wordsInUse == 0) - return; - - control.ensureData(bytes); - - int i = 0; - final int longs = bytes / 8; - while (i < longs) - words[i++] = buffer.getLong(); - - for (int j = i; j < wordsInUse; j++) - words[j] = 0; - - for (int remaining = (bytes - longs * 8), j = 0; j < remaining; j++) - words[i] |= (buffer.get() & 0xffL) << (8 * j); - -} - */ }}; diff --git a/pvDataApp/misc/bitSet.h b/pvDataApp/misc/bitSet.h index 8d5bebb..525e111 100644 --- a/pvDataApp/misc/bitSet.h +++ b/pvDataApp/misc/bitSet.h @@ -9,8 +9,8 @@ #include #include #include "factory.h" -//#include "byteBuffer.h" -//#include "serialize.h" +#include "serialize.h" + namespace epics { namespace pvData { /** @@ -36,7 +36,7 @@ namespace epics { namespace pvData { * * Based on Java implementation. */ - class BitSet /*: public Serializable*/ { + class BitSet : public Serializable { public: /** @@ -216,6 +216,11 @@ namespace epics { namespace pvData { void toString(StringBuilder buffer, int indentLevel = 0) const; + virtual void serialize(ByteBuffer *buffer, + SerializableControl *flusher); + virtual void deserialize(ByteBuffer *buffer, + DeserializableControl *flusher); + private: /* diff --git a/pvDataApp/misc/serialize.h b/pvDataApp/misc/serialize.h index 45e65f8..effc763 100644 --- a/pvDataApp/misc/serialize.h +++ b/pvDataApp/misc/serialize.h @@ -6,7 +6,6 @@ */ #ifndef SERIALIZE_H #define SERIALIZE_H -#include "bitSet.h" #include "byteBuffer.h" namespace epics { namespace pvData { @@ -15,6 +14,7 @@ namespace epics { namespace pvData { class Serializable; class BitSetSerializable; class SerializableArray; + class BitSet; class SerializableControl { public: diff --git a/pvDataApp/pvMisc/bitSetUtil.h b/pvDataApp/pvMisc/bitSetUtil.h index 1c7c7f4..f6f52ce 100644 --- a/pvDataApp/pvMisc/bitSetUtil.h +++ b/pvDataApp/pvMisc/bitSetUtil.h @@ -8,6 +8,7 @@ #define BITSETUTIL_H #include "noDefaultMethods.h" #include "pvData.h" +#include "bitSet.h" namespace epics { namespace pvData {