From 9cc762d85afffea42de3e1d156a6b8838d88a00c Mon Sep 17 00:00:00 2001 From: Stefan Radomski Date: Sun, 6 Jul 2014 18:04:18 +0200 Subject: Made Blob into Pimpl to better support language bindings --- embedding/csharp/uSCXMLEmbedding.suo | Bin 37376 -> 40448 bytes embedding/csharp/uSCXMLEmbedding/RunTests.cs | 29 +++++++ embedding/java/src/org/uscxml/tests/TestData.java | 30 ++++++- src/bindings/swig/csharp/uscxml.i | 12 ++- src/bindings/swig/java/uscxml.i | 5 +- src/bindings/swig/uscxml_ignores.i | 2 + src/uscxml/messages/Blob.cpp | 16 ++-- src/uscxml/messages/Blob.h | 93 ++++++++++++++++++--- src/uscxml/messages/Data.cpp | 4 +- src/uscxml/messages/Data.h | 13 ++- .../ecmascript/JavaScriptCore/JSCDataModel.cpp | 3 +- .../plugins/datamodel/ecmascript/TypedArray.cpp | 48 +++++------ .../plugins/datamodel/ecmascript/TypedArray.h | 82 +++++++++--------- .../datamodel/ecmascript/v8/V8DataModel.cpp | 2 +- src/uscxml/plugins/element/file/FileElement.cpp | 4 +- .../plugins/element/respond/RespondElement.cpp | 4 +- .../plugins/invoker/ffmpeg/FFMPEGInvoker.cpp | 14 ++-- src/uscxml/plugins/invoker/ffmpeg/FFMPEGInvoker.h | 2 +- src/uscxml/plugins/invoker/smtp/SMTPInvoker.cpp | 10 +-- .../ioprocessor/basichttp/BasicHTTPIOProcessor.cpp | 2 +- src/uscxml/server/InterpreterServlet.cpp | 8 +- test/src/test-datamodel.cpp | 17 ++++ test/src/test-predicates.cpp | 20 ++--- 23 files changed, 289 insertions(+), 131 deletions(-) diff --git a/embedding/csharp/uSCXMLEmbedding.suo b/embedding/csharp/uSCXMLEmbedding.suo index 823eecc..3d75994 100644 Binary files a/embedding/csharp/uSCXMLEmbedding.suo and b/embedding/csharp/uSCXMLEmbedding.suo differ diff --git a/embedding/csharp/uSCXMLEmbedding/RunTests.cs b/embedding/csharp/uSCXMLEmbedding/RunTests.cs index 38e3736..d2b188d 100644 --- a/embedding/csharp/uSCXMLEmbedding/RunTests.cs +++ b/embedding/csharp/uSCXMLEmbedding/RunTests.cs @@ -32,6 +32,7 @@ namespace embedding int i = 1; while (i-- > 0) { + testData(); testLifeCycle(); testExecutableContent(); testIOProcessor(); @@ -40,6 +41,34 @@ namespace embedding Console.ReadKey(); } + public static void testData() { + byte[] origData = new byte[1024]; + for (int i = 0; i < origData.Length; i++) { + origData[i] = (byte)i; + } + + { + + Blob blob = new Blob(origData, "application/octet-stream"); + Debug.Assert(origData.Length == blob.getSize()); + + for (int i = 0; i < origData.Length; i++) { + Debug.Assert(origData[i] == blob.getData()[i]); + } + } + + Data data = new Data(origData, "application/octet-stream"); + Blob blob2 = data.getBinary(); + + byte[] newData = blob2.getData(); + + if (newData.Length == origData.Length); + for (int i = 0; i < origData.Length; i++) { + Debug.Assert(newData[i] == origData[i]); + } + + } + public static void testInvoker() { Console.WriteLine("testInvoker"); CustomInvoker invoker = new CustomInvoker(); diff --git a/embedding/java/src/org/uscxml/tests/TestData.java b/embedding/java/src/org/uscxml/tests/TestData.java index ca1da00..d225dec 100644 --- a/embedding/java/src/org/uscxml/tests/TestData.java +++ b/embedding/java/src/org/uscxml/tests/TestData.java @@ -1,6 +1,7 @@ package org.uscxml.tests; import org.uscxml.Blob; +import org.uscxml.BlobImpl; import org.uscxml.Data; public class TestData { @@ -17,8 +18,33 @@ public class TestData { } { - byte binData[] = new byte[1024]; - Data data = new Data(binData, "application/octet-stream"); + byte origData[] = new byte[1024]; + for (int i = 0; i < origData.length; i++) { + origData[i] = (byte)i; + } + + { + Blob blob = new Blob(origData, "application/octet-stream"); + if (origData.length != blob.getSize()) throw new RuntimeException("Blob does not match"); + + for (int i = 0; i < origData.length; i++) { + if (origData[i] != blob.getData()[i]) + throw new RuntimeException("Blob mismatch at " + i); + } + } + + Data data = new Data(origData, "application/octet-stream"); + Blob blob = data.getBinary(); + System.out.println(blob.getSize()); + + byte newData[] = blob.getData(); + + if (newData.length != origData.length) throw new RuntimeException("Arrays length does not match"); + for (int i = 0; i < origData.length; i++) { + if (newData[i] != origData[i]) + throw new RuntimeException("Mismatch at " + i); + } + } } diff --git a/src/bindings/swig/csharp/uscxml.i b/src/bindings/swig/csharp/uscxml.i index bd8c669..7625c8d 100644 --- a/src/bindings/swig/csharp/uscxml.i +++ b/src/bindings/swig/csharp/uscxml.i @@ -13,6 +13,7 @@ %include // these are needed at least for the templates to work +typedef uscxml::Blob Blob; typedef uscxml::Data Data; typedef uscxml::Event Event; typedef uscxml::InvokeRequest InvokeRequest; @@ -161,11 +162,16 @@ WRAP_TO_STRING(uscxml::InvokeRequest); // byte[] signature for Blob get/setData // see http://permalink.gmane.org/gmane.comp.programming.swig/5804 +%csmethodmodifiers uscxml::Blob::setData(const char* data, size_t length) "private"; +%csmethodmodifiers uscxml::Blob::setMimeType(const std::string& mimeType) "private"; %csmethodmodifiers uscxml::Blob::Blob(const char* data, size_t size, const std::string& mimeType) "private"; +%csmethodmodifiers uscxml::Blob::Blob(const char* data, size_t size) "private"; + %typemap(cscode) uscxml::Blob %{ - public Blob(byte[] data, string mimeType) : this(uscxmlNativeCSharpPINVOKE.new_Blob(data, (uint)data.Length, mimeType), true) { + public Blob(byte[] data, string mimeType) : this(uscxmlNativeCSharpPINVOKE.new_Blob__SWIG_2(data, (uint)data.Length, mimeType), true) { if (uscxmlNativeCSharpPINVOKE.SWIGPendingException.Pending) throw uscxmlNativeCSharpPINVOKE.SWIGPendingException.Retrieve(); } + %} %typemap(imtype, out="System.IntPtr") const char *data "byte[]" @@ -251,6 +257,10 @@ using System.Runtime.InteropServices; %} %typemap(cscode) uscxml::Data %{ + public Data(byte[] data, String mimeType) : this() { + setBinary(new Blob(data, mimeType)); + } + public Data(List arr) : this() { setArray(arr); } diff --git a/src/bindings/swig/java/uscxml.i b/src/bindings/swig/java/uscxml.i index 9610e77..654ea91 100644 --- a/src/bindings/swig/java/uscxml.i +++ b/src/bindings/swig/java/uscxml.i @@ -13,6 +13,7 @@ %include // these are needed at least for the templates to work +typedef uscxml::Blob Blob; typedef uscxml::Data Data; typedef uscxml::Event Event; typedef uscxml::InvokeRequest InvokeRequest; @@ -184,7 +185,6 @@ BEAUTIFY_NATIVE(uscxml::InvokeRequest, InvokeRequest, InvokeRequestNative); JCALL4(SetByteArrayRegion, jenv, $result, 0, ((uscxml::Blob const *)arg1)->getSize(), (jbyte *)$1); } - //*********************************************** // Beautify important classes //*********************************************** @@ -254,7 +254,8 @@ import java.util.LinkedList; %typemap(javacode) uscxml::Data %{ public Data(byte[] data, String mimeType) { - + this(uscxmlNativeJavaJNI.new_Data__SWIG_0(), true); + setBinary(new Blob(data, mimeType)); } public Data(Map compound) { diff --git a/src/bindings/swig/uscxml_ignores.i b/src/bindings/swig/uscxml_ignores.i index 5e99416..9a0dbe4 100644 --- a/src/bindings/swig/uscxml_ignores.i +++ b/src/bindings/swig/uscxml_ignores.i @@ -1,6 +1,7 @@ %ignore uscxml::NumAttr; %ignore uscxml::SCXMLParser; %ignore uscxml::InterpreterImpl; +%ignore uscxml::BlobImpl; #if 0 %ignore uscxml::EventHandlerImpl; #endif @@ -196,6 +197,7 @@ %ignore uscxml::Blob::Blob(size_t size); %ignore uscxml::Blob::Blob(const char* data, size_t size, const std::string& mimeType, bool adopt); +%ignore uscxml::Blob::Blob(const boost::shared_ptr); %ignore operator!=; diff --git a/src/uscxml/messages/Blob.cpp b/src/uscxml/messages/Blob.cpp index 2e68e98..b8a263e 100644 --- a/src/uscxml/messages/Blob.cpp +++ b/src/uscxml/messages/Blob.cpp @@ -24,27 +24,27 @@ namespace uscxml { -Blob::~Blob() { +BlobImpl::~BlobImpl() { free(data); } -std::string Blob::md5() { +std::string BlobImpl::md5() const { return uscxml::md5(data, size); } -Blob* Blob::fromBase64(const std::string base64) { +BlobImpl* BlobImpl::fromBase64(const std::string base64, const std::string& mimeType) { std::string decoded = base64Decode(base64); - return new Blob(decoded.c_str(), decoded.length(), mimeType); + return new BlobImpl(decoded.c_str(), decoded.length(), mimeType); } -Blob::Blob(size_t _size) { +BlobImpl::BlobImpl(size_t _size) { data = (char*)malloc(_size); memset(data, 0, _size); size = _size; mimeType = "application/octet-stream"; } -Blob::Blob(const char* _data, size_t _size, const std::string& _mimeType, bool adopt) { +BlobImpl::BlobImpl(const char* _data, size_t _size, const std::string& _mimeType, bool adopt) { if (adopt) { data = (char*)_data; } else { @@ -54,8 +54,8 @@ Blob::Blob(const char* _data, size_t _size, const std::string& _mimeType, bool a mimeType = _mimeType; size = _size; } - -std::string Blob::base64() { + +std::string BlobImpl::base64() const { return base64Encode((char* const)data, size); } diff --git a/src/uscxml/messages/Blob.h b/src/uscxml/messages/Blob.h index b805da1..0c20f95 100644 --- a/src/uscxml/messages/Blob.h +++ b/src/uscxml/messages/Blob.h @@ -21,33 +21,38 @@ #define BLOB_H_E1B6D2C3 #include +#include #include "uscxml/Common.h" namespace uscxml { -class USCXML_API Blob { +class USCXML_API BlobImpl { public: - ~Blob(); - Blob(size_t size); - Blob(const char* data, size_t size, const std::string& mimeType, bool adopt = false); - - std::string base64(); - std::string md5(); - Blob* fromBase64(const std::string base64); - + BlobImpl(size_t size); + BlobImpl(const char* data, size_t size, const std::string& mimeType, bool adopt = false); + virtual ~BlobImpl(); + + std::string base64() const; + std::string md5() const; + static BlobImpl* fromBase64(const std::string base64, const std::string& mimeType); + char* getData() const { return data; } - + size_t getSize() const { return size; } - + std::string getMimeType() const { return mimeType; } - + + void setMimeType(const std::string& mimeType) { + this->mimeType = mimeType; + } + #ifdef SWIGIMPORTED protected: #endif @@ -55,7 +60,71 @@ protected: char* data; size_t size; std::string mimeType; +}; + +class USCXML_API Blob { +public: + + Blob() : _impl() {} + Blob(const boost::shared_ptr impl) : _impl(impl) { } + Blob(const Blob& other) : _impl(other._impl) { } + Blob(size_t size) : _impl(boost::shared_ptr(new BlobImpl(size))) {} + Blob(const char* data, + size_t size, + const std::string& mimeType = "application/octet-stream", + bool adopt = false) : + _impl(boost::shared_ptr(new BlobImpl(data, size, mimeType, adopt))) {} + virtual ~Blob() {}; + + operator bool() const { + return _impl; + } + bool operator< (const Blob& other) const { + return _impl < other._impl; + } + bool operator==(const Blob& other) const { + return _impl == other._impl; + } + bool operator!=(const Blob& other) const { + return _impl != other._impl; + } + Blob& operator= (const Blob& other) { + _impl = other._impl; + return *this; + } + static Blob fromBase64(const std::string base64, const std::string& mimeType = "application/octet-stream") { + return Blob(boost::shared_ptr(BlobImpl::fromBase64(base64, mimeType))); + } + + std::string base64() const { + return _impl->base64(); + } + + std::string md5() const { + return _impl->md5(); + } + + char* getData() const { + return _impl->getData(); + } + + size_t getSize() const { + return _impl->getSize(); + } + + std::string getMimeType() const { + return _impl->getMimeType(); + } + + void setMimeType(const std::string& mimeType) { + _impl->setMimeType(mimeType); + } + +#ifdef SWIGIMPORTED +protected: +#endif + boost::shared_ptr _impl; }; diff --git a/src/uscxml/messages/Data.cpp b/src/uscxml/messages/Data.cpp index 1ee946a..911730a 100644 --- a/src/uscxml/messages/Data.cpp +++ b/src/uscxml/messages/Data.cpp @@ -35,9 +35,7 @@ extern "C" { namespace uscxml { -Data::Data(const char* data, size_t size, const std::string& mimeType, bool adopt) { - binary = boost::shared_ptr(new Blob(data, size, mimeType, adopt)); -} +Data::Data(const char* data, size_t size, const std::string& mimeType, bool adopt) : binary(data, size, mimeType, adopt) {} void Data::merge(const Data& other) { if (other.compound.size() > 0) { diff --git a/src/uscxml/messages/Data.h b/src/uscxml/messages/Data.h index 44ce1d7..11b46fb 100644 --- a/src/uscxml/messages/Data.h +++ b/src/uscxml/messages/Data.h @@ -27,12 +27,11 @@ #include "uscxml/Common.h" #include "uscxml/Convenience.h" +#include "uscxml/messages/Blob.h" #include namespace uscxml { -class Blob; - static int _dataIndentation = 1; class USCXML_API Data { @@ -220,6 +219,13 @@ public: this->atom = atom; } + Blob getBinary() { + return this->binary; + } + void setBinary(const Blob& binary) { + this->binary = binary; + } + Type getType() { return type; } @@ -227,7 +233,6 @@ public: this->type = type; } - #ifdef SWIGIMPORTED protected: #endif @@ -236,7 +241,7 @@ protected: std::map compound; std::list array; std::string atom; - boost::shared_ptr binary; + Blob binary; Type type; protected: diff --git a/src/uscxml/plugins/datamodel/ecmascript/JavaScriptCore/JSCDataModel.cpp b/src/uscxml/plugins/datamodel/ecmascript/JavaScriptCore/JSCDataModel.cpp index 73a3744..64b61af 100644 --- a/src/uscxml/plugins/datamodel/ecmascript/JavaScriptCore/JSCDataModel.cpp +++ b/src/uscxml/plugins/datamodel/ecmascript/JavaScriptCore/JSCDataModel.cpp @@ -394,7 +394,7 @@ Data JSCDataModel::getValueAsData(const JSValueRef value) { if (JSValueIsObjectOfClass(_ctx, value, JSCArrayBuffer::getTmpl())) { // binary data JSCArrayBuffer::JSCArrayBufferPrivate* privObj = (JSCArrayBuffer::JSCArrayBufferPrivate*)JSObjectGetPrivate(objValue); - data.binary = privObj->nativeObj->_buffer; + data.binary = privObj->nativeObj->_blob; return data; } else if (JSValueIsObjectOfClass(_ctx, value, JSCNode::getTmpl())) { // dom node @@ -403,6 +403,7 @@ Data JSCDataModel::getValueAsData(const JSValueRef value) { return data; } std::set propertySet; + JSPropertyNameArrayRef properties = JSObjectCopyPropertyNames(_ctx, objValue); size_t paramCount = JSPropertyNameArrayGetCount(properties); bool isArray = true; diff --git a/src/uscxml/plugins/datamodel/ecmascript/TypedArray.cpp b/src/uscxml/plugins/datamodel/ecmascript/TypedArray.cpp index 5b34181..913d2ce 100644 --- a/src/uscxml/plugins/datamodel/ecmascript/TypedArray.cpp +++ b/src/uscxml/plugins/datamodel/ecmascript/TypedArray.cpp @@ -23,51 +23,51 @@ #define DATAVIEW_TYPED_GET(type) \ type retVal;\ -if (index + _start + sizeof(type) > _buffer->size)\ +if (index + _start + sizeof(type) > _blob._impl->size)\ return 0;\ -memcpy(&retVal, _buffer->data + (_start + index), sizeof(type)); +memcpy(&retVal, _blob._impl->data + (_start + index), sizeof(type)); #define DATAVIEW_TYPED_SET(type) \ -if (index + _start + sizeof(type) > _buffer->size)\ +if (index + _start + sizeof(type) > _blob._impl->size)\ return;\ -memcpy(_buffer->data + (_start + index), &value, sizeof(type)); +memcpy(_blob._impl->data + (_start + index), &value, sizeof(type)); namespace uscxml { ArrayBuffer::ArrayBuffer(unsigned long length) { - _buffer = boost::shared_ptr(new Blob(length)); + _blob = Blob(length); } -ArrayBuffer::ArrayBuffer(boost::shared_ptr buffer) : _buffer(buffer) { +ArrayBuffer::ArrayBuffer(const Blob& blob) : _blob(blob) { } ArrayBuffer::ArrayBuffer(void* data, unsigned int size) { - _buffer = boost::shared_ptr(new Blob((const char*)data, size, "application/octet-stream")); + _blob = Blob((const char*)data, size, "application/octet-stream"); } unsigned long ArrayBuffer::getByteLength() { - if (!_buffer) + if (!_blob) return 0; - return _buffer->size; + return _blob._impl->size; } ArrayBuffer ArrayBuffer::slice(long begin, long end) { - if (!_buffer) { + if (!_blob) { return ArrayBuffer(0); } - unsigned int realBegin = (begin + _buffer->size) % _buffer->size; - unsigned int realEnd = (end + _buffer->size) % _buffer->size; + unsigned int realBegin = (begin + _blob._impl->size) % _blob._impl->size; + unsigned int realEnd = (end + _blob._impl->size) % _blob._impl->size; if (realEnd < realBegin) { return ArrayBuffer(0); } ArrayBuffer arrBuffer(realEnd - realBegin); - memcpy(arrBuffer._buffer->data, _buffer->data + realBegin, realEnd - realBegin); + memcpy(arrBuffer._blob._impl->data, _blob._impl->data + realBegin, realEnd - realBegin); return arrBuffer; } ArrayBuffer ArrayBuffer::slice(long begin) { - return slice(begin, _buffer->size); + return slice(begin, _blob._impl->size); } bool ArrayBuffer::isView(void*) { @@ -75,35 +75,35 @@ bool ArrayBuffer::isView(void*) { } ArrayBuffer::operator bool() { - return _buffer; + return _blob; } ArrayBuffer ArrayBufferView::getBuffer() { - return ArrayBuffer(_buffer); + return ArrayBuffer(_blob); } DataView::DataView(ArrayBuffer* buffer, unsigned long byteOffset, unsigned long byteLength) { _start = byteOffset; - if (_start > buffer->_buffer->size) + if (_start > buffer->_blob._impl->size) return; _end = _start + byteLength; - if (_end > buffer->_buffer->size) + if (_end > buffer->_blob._impl->size) return; - _buffer = buffer->_buffer; + _blob = buffer->_blob; } DataView::DataView(ArrayBuffer* buffer , unsigned long byteOffset) { _start = byteOffset; - _end = buffer->_buffer->size; - if (_start > buffer->_buffer->size) + _end = buffer->_blob._impl->size; + if (_start > buffer->_blob._impl->size) return; - _buffer = buffer->_buffer; + _blob = buffer->_blob; } DataView::DataView(ArrayBuffer* buffer) { _start = 0; - _end = (buffer->_buffer->size); - _buffer = buffer->_buffer; + _end = (buffer->_blob._impl->size); + _blob = buffer->_blob; } unsigned long DataView::getByteOffset() { diff --git a/src/uscxml/plugins/datamodel/ecmascript/TypedArray.h b/src/uscxml/plugins/datamodel/ecmascript/TypedArray.h index 205ef5a..7509390 100644 --- a/src/uscxml/plugins/datamodel/ecmascript/TypedArray.h +++ b/src/uscxml/plugins/datamodel/ecmascript/TypedArray.h @@ -51,7 +51,7 @@ public: * be allocated an exception is raised. */ ArrayBuffer(unsigned long length); - ArrayBuffer(boost::shared_ptr); + ArrayBuffer(const Blob&); /** * The length of the ArrayBuffer in bytes, as fixed at construction time. @@ -78,42 +78,42 @@ public: } operator bool(); bool operator== (const ArrayBuffer& other) { - return other._buffer == _buffer; + return other._blob == _blob; } // unsigned char get(unsigned long index) { // if (index >= getLength()) // return 0; // unsigned char retVal; -// memcpy(&retVal, _buffer->_data + index * sizeof(unsigned char), sizeof(unsigned char)); +// memcpy(&retVal, _blob->_data + index * sizeof(unsigned char), sizeof(unsigned char)); // return retVal; // } // // void set(unsigned long index, unsigned char value) { -// memcpy(_buffer->_data + index * sizeof(unsigned char), &value, sizeof(unsigned char)); +// memcpy(_blob->_data + index * sizeof(unsigned char), &value, sizeof(unsigned char)); // } // non-standard extension - std::string md5() { - return _buffer->md5(); + std::string md5() const { + return _blob.md5(); } // non-standard extension - std::string base64() { - return _buffer->base64(); + std::string base64() const { + return _blob.base64(); } - std::string getMimeType() { - if (_buffer) - return _buffer->mimeType; + std::string getMimeType() const { + if (_blob) + return _blob.getMimeType(); return ""; } void setMimeType(const std::string& mimeType) { - if (_buffer) - _buffer->mimeType = mimeType; + if (_blob) + _blob.setMimeType(mimeType); } - boost::shared_ptr _buffer; + Blob _blob; }; class ArrayBufferView { @@ -139,7 +139,7 @@ public: virtual unsigned long getByteLength() = 0; virtual unsigned long getLength() = 0; protected: - boost::shared_ptr _buffer; + Blob _blob; unsigned long _start; unsigned long _end; }; @@ -239,36 +239,36 @@ public: _start = byteOffset / sizeof(S); _end = _start + length; - if (_end > buffer->_buffer->size / sizeof(S)) + if (_end > buffer->_blob._impl->size / sizeof(S)) return; - _buffer = buffer->_buffer; + _blob = buffer->_blob; } TypedArray(uscxml::ArrayBuffer* buffer, unsigned long byteOffset) { if (byteOffset % sizeof(S)) return; _start = byteOffset / sizeof(S); - _end = buffer->_buffer->size / sizeof(S); - _buffer = buffer->_buffer; + _end = buffer->_blob._impl->size / sizeof(S); + _blob = buffer->_blob; } TypedArray(uscxml::ArrayBuffer* buffer) { _start = 0; - _end = (buffer->_buffer->size) / sizeof(S); - _buffer = buffer->_buffer; + _end = (buffer->_blob._impl->size) / sizeof(S); + _blob = buffer->_blob; } - TypedArray(boost::shared_ptr buffer, unsigned long byteOffset, unsigned long length) { + TypedArray(Blob blob, unsigned long byteOffset, unsigned long length) { if (byteOffset % sizeof(S)) return; _start = byteOffset / sizeof(S); _end = _start + length; - if (_end > buffer->size / sizeof(S)) + if (_end > blob._impl->size / sizeof(S)) return; - _buffer = buffer; + _blob = blob; } /** @@ -281,7 +281,7 @@ public: TypedArray(unsigned long length) { _start = 0; _end = length; - _buffer = boost::shared_ptr(new Blob(length * sizeof(S))); + _blob = Blob(length * sizeof(S)); } /** @@ -294,13 +294,13 @@ public: TypedArray(std::vector data) { _start = 0; _end = data.size(); - _buffer = boost::shared_ptr(new Blob(data.size() * sizeof(S))); + _blob = Blob(data.size() * sizeof(S)); set(data, 0); } TypedArray(TypedArray* other) { _start = other->_start; _end = other->_end; - _buffer = other->_buffer; + _blob = other->_blob; } /** @@ -311,7 +311,7 @@ public: if (index >= getLength()) return static_cast(0); S retVal; - memcpy(&retVal, _buffer->data + (_start + index) * sizeof(S), sizeof(S)); + memcpy(&retVal, _blob._impl->data + (_start + index) * sizeof(S), sizeof(S)); return retVal; } @@ -320,7 +320,7 @@ public: * Sets the element at the given numeric index to the given value. */ void set(unsigned long index, T value) { - memcpy(_buffer->data + (_start + index) * sizeof(S), &value, sizeof(S)); + memcpy(_blob._impl->data + (_start + index) * sizeof(S), &value, sizeof(S)); } /** @@ -336,15 +336,15 @@ public: * current TypedArray, an exception is raised. */ void set(TypedArray* value, unsigned long offset) { - if (!_buffer) + if (!_blob) return; - if (offset * sizeof(S) + value->getByteLength() > _buffer->size) + if (offset * sizeof(S) + value->getByteLength() > _blob._impl->size) return; unsigned long otherOffset = value->_start * sizeof(S); // will this work if we use the same buffer? - memcpy(_buffer->data + (_start + offset) * sizeof(S), value->_buffer->data + otherOffset, value->getByteLength()); + memcpy(_blob._impl->data + (_start + offset) * sizeof(S), value->_blob._impl->data + otherOffset, value->getByteLength()); } void set(TypedArray* value) { @@ -364,13 +364,13 @@ public: * current TypedArray, an exception is raised. */ void set(std::vector data, unsigned long offset) { - if (!_buffer) + if (!_blob) return; if (data.size() + offset > _end) return; if (sizeof(T) == sizeof(S)) { - memcpy(_buffer->data + offset, (void*)&data[0], data.size() * sizeof(S)); + memcpy(_blob._impl->data + offset, (void*)&data[0], data.size() * sizeof(S)); } else { S* buffer = (S*)malloc(data.size() * sizeof(S)); typename std::vector::const_iterator dataIter = data.begin(); @@ -380,7 +380,7 @@ public: dataIter++; i++; } - memcpy(_buffer->data + offset, buffer, data.size() * sizeof(S)); + memcpy(_blob._impl->data + offset, buffer, data.size() * sizeof(S)); free (buffer); } } @@ -403,7 +403,7 @@ public: * method is invoked. */ TypedArray* subarray(long begin, long end) { - if (!_buffer) + if (!_blob) return NULL; unsigned int length = getLength(); unsigned int realBegin = (begin + length) % length; @@ -414,29 +414,29 @@ public: if (realEnd < realBegin) return NULL; - return new TypedArray(_buffer, realBegin * sizeof(S), realEnd - realBegin); + return new TypedArray(_blob, realBegin * sizeof(S), realEnd - realBegin); } TypedArray* subarray(long begin) { - if (!_buffer) + if (!_blob) return NULL; return subarray(begin, getLength()); } unsigned long getLength() { - if (!_buffer) + if (!_blob) return 0; return _end - _start; } unsigned long getByteLength() { - if (!_buffer) + if (!_blob) return 0; return (_end - _start) * sizeof(S); } unsigned long getByteOffset() { - if (!_buffer) + if (!_blob) return 0; return _start * sizeof(S); } diff --git a/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp b/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp index 51a2167..f4bfabd 100644 --- a/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp +++ b/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp @@ -328,7 +328,7 @@ Data V8DataModel::getValueAsData(const v8::Handle& value, std::setIsObject()) { if (V8ArrayBuffer::hasInstance(value)) { uscxml::V8ArrayBuffer::V8ArrayBufferPrivate* privObj = V8DOM::toClassPtr(value->ToObject()->GetInternalField(0)); - data.binary = privObj->nativeObj->_buffer; + data.binary = privObj->nativeObj->_blob; return data; } if (V8Node::hasInstance(value)) { diff --git a/src/uscxml/plugins/element/file/FileElement.cpp b/src/uscxml/plugins/element/file/FileElement.cpp index 606cddc..d5908ec 100644 --- a/src/uscxml/plugins/element/file/FileElement.cpp +++ b/src/uscxml/plugins/element/file/FileElement.cpp @@ -99,8 +99,8 @@ void FileElement::enterElement(const Arabica::DOM::Node& node) { } else { Data data = _interpreter->getDataModel().getStringAsData(ATTR(node, "contentexpr")); if (data.binary) { - content = data.binary->data; - contentSize = data.binary->size; + content = data.binary.getData(); + contentSize = data.binary.getSize(); } else if (data.atom.length() > 0) { contentStr = data.atom; } diff --git a/src/uscxml/plugins/element/respond/RespondElement.cpp b/src/uscxml/plugins/element/respond/RespondElement.cpp index 06d89a7..6445249 100644 --- a/src/uscxml/plugins/element/respond/RespondElement.cpp +++ b/src/uscxml/plugins/element/respond/RespondElement.cpp @@ -89,8 +89,8 @@ void RespondElement::enterElement(const Arabica::DOM::Node& node) { httpReply.content = contentData.atom; httpReply.headers["Content-Type"] = "text/plain"; } else if (contentData.binary) { - httpReply.content = std::string(contentData.binary->data, contentData.binary->size); - httpReply.headers["Content-Type"] = contentData.binary->mimeType; + httpReply.content = std::string(contentData.binary.getData(), contentData.binary.getSize()); + httpReply.headers["Content-Type"] = contentData.binary.getMimeType(); } else if (contentData.node) { std::stringstream ss; ss << contentData.node; diff --git a/src/uscxml/plugins/invoker/ffmpeg/FFMPEGInvoker.cpp b/src/uscxml/plugins/invoker/ffmpeg/FFMPEGInvoker.cpp index 4a0ec00..eda4ce8 100644 --- a/src/uscxml/plugins/invoker/ffmpeg/FFMPEGInvoker.cpp +++ b/src/uscxml/plugins/invoker/ffmpeg/FFMPEGInvoker.cpp @@ -373,7 +373,7 @@ void FFMPEGInvoker::openVideo(EncodingContext* ctx, AVFormatContext *oc, AVCodec *((AVPicture *)ctx->frame) = ctx->dst_picture; } -void FFMPEGInvoker::writeVideoFrame(EncodingContext* ctx, AVFormatContext *oc, AVStream *st, boost::shared_ptr image) { +void FFMPEGInvoker::writeVideoFrame(EncodingContext* ctx, AVFormatContext *oc, AVStream *st, Blob image) { int ret; AVCodecContext *c = st->codec; @@ -392,14 +392,14 @@ void FFMPEGInvoker::writeVideoFrame(EncodingContext* ctx, AVFormatContext *oc, A } uint32_t headerOffset = 0; - headerOffset += image->data[10] << 0; - headerOffset += image->data[11] << 8; - headerOffset += image->data[12] << 16; - headerOffset += image->data[13] << 24; + headerOffset += image._impl->data[10] << 0; + headerOffset += image._impl->data[11] << 8; + headerOffset += image._impl->data[12] << 16; + headerOffset += image._impl->data[13] << 24; // std::cout << headerOffset + (c->width * c->height) << " / " << image->_size << std::endl; - ret = avpicture_fill(&ctx->src_picture, (uint8_t*)(image->data + headerOffset), BMP_FORMAT, c->width, c->height); + ret = avpicture_fill(&ctx->src_picture, (uint8_t*)(image._impl->data + headerOffset), BMP_FORMAT, c->width, c->height); if (ret < 0) { fprintf(stderr, "Could not fill image from given bitmap\n"); @@ -408,7 +408,7 @@ void FFMPEGInvoker::writeVideoFrame(EncodingContext* ctx, AVFormatContext *oc, A (const uint8_t * const *)ctx->src_picture.data, ctx->src_picture.linesize, 0, c->height, ctx->dst_picture.data, ctx->dst_picture.linesize); } else { - avpicture_fill(&ctx->dst_picture, (uint8_t*)image->data, c->pix_fmt, c->width, c->height); + avpicture_fill(&ctx->dst_picture, (uint8_t*)image._impl->data, c->pix_fmt, c->width, c->height); } if (oc->oformat->flags & AVFMT_RAWPICTURE) { diff --git a/src/uscxml/plugins/invoker/ffmpeg/FFMPEGInvoker.h b/src/uscxml/plugins/invoker/ffmpeg/FFMPEGInvoker.h index 964681b..81589bb 100644 --- a/src/uscxml/plugins/invoker/ffmpeg/FFMPEGInvoker.h +++ b/src/uscxml/plugins/invoker/ffmpeg/FFMPEGInvoker.h @@ -92,7 +92,7 @@ protected: AVStream* addStream(EncodingContext* ctx, AVFormatContext *oc, AVCodec **codec, enum AVCodecID codec_id); void openVideo(EncodingContext* ctx, AVFormatContext *oc, AVCodec *codec, AVStream *st); - void writeVideoFrame(EncodingContext* ctx, AVFormatContext *oc, AVStream *st, boost::shared_ptr image); + void writeVideoFrame(EncodingContext* ctx, AVFormatContext *oc, AVStream *st, Blob image); void closeVideo(EncodingContext* ctx, AVFormatContext *oc, AVStream *st); static void run(void*); diff --git a/src/uscxml/plugins/invoker/smtp/SMTPInvoker.cpp b/src/uscxml/plugins/invoker/smtp/SMTPInvoker.cpp index 2a974ab..90b777c 100644 --- a/src/uscxml/plugins/invoker/smtp/SMTPInvoker.cpp +++ b/src/uscxml/plugins/invoker/smtp/SMTPInvoker.cpp @@ -101,8 +101,8 @@ void SMTPInvoker::getAttachments(std::list list, std::list& attachme Data att = data; if (!att.hasKey("mimetype")) { - if (att["data"].binary && att["data"].binary->mimeType.size() > 0) { - att.compound["mimetype"] = Data(att["data"].binary->mimeType, Data::VERBATIM); + if (att["data"].binary && att["data"].binary.getMimeType().size() > 0) { + att.compound["mimetype"] = Data(att["data"].binary.getMimeType(), Data::VERBATIM); } else { att.compound["mimetype"] = Data("text/plain", Data::VERBATIM); } @@ -127,8 +127,8 @@ void SMTPInvoker::getAttachments(std::list list, std::list& attachme att.compound["data"].binary = data.binary; - if (data.binary->mimeType.size() > 0) { - att.compound["mimetype"] = Data(attachments.back()["data"].binary->mimeType, Data::VERBATIM); + if (data.binary.getMimeType().size() > 0) { + att.compound["mimetype"] = Data(attachments.back()["data"].binary.getMimeType(), Data::VERBATIM); } else { att.compound["mimetype"] = Data("application/octet-stream", Data::VERBATIM); } @@ -271,7 +271,7 @@ void SMTPInvoker::send(const SendRequest& req) { if (attIter->compound["data"].binary) { contentSS << "Content-Transfer-Encoding: base64"; contentSS << "\n\n"; - contentSS << attIter->compound["data"].binary->base64(); + contentSS << attIter->compound["data"].binary.base64(); } else { contentSS << "Content-Transfer-Encoding: 7Bit"; contentSS << "\n\n"; diff --git a/src/uscxml/plugins/ioprocessor/basichttp/BasicHTTPIOProcessor.cpp b/src/uscxml/plugins/ioprocessor/basichttp/BasicHTTPIOProcessor.cpp index bd62467..845e142 100644 --- a/src/uscxml/plugins/ioprocessor/basichttp/BasicHTTPIOProcessor.cpp +++ b/src/uscxml/plugins/ioprocessor/basichttp/BasicHTTPIOProcessor.cpp @@ -247,7 +247,7 @@ void BasicHTTPIOProcessor::send(const SendRequest& req) { xmlStream << req.data.node; valueCStr = evhttp_encode_uri(xmlStream.str().c_str()); } else if(req.data.binary) { - valueCStr = evhttp_encode_uri(req.data.binary->base64().c_str()); + valueCStr = evhttp_encode_uri(req.data.binary.base64().c_str()); } if (valueCStr != NULL) { kvps << kvpSeperator << keyCStr << "=" << valueCStr; diff --git a/src/uscxml/server/InterpreterServlet.cpp b/src/uscxml/server/InterpreterServlet.cpp index f90d111..285add9 100644 --- a/src/uscxml/server/InterpreterServlet.cpp +++ b/src/uscxml/server/InterpreterServlet.cpp @@ -163,8 +163,8 @@ void InterpreterWebSocketServlet::send(const SendRequest& req) { } else if (req.data.binary) { HTTPServer::wsSend(_requests[req.target], EVWS_BINARY_FRAME, - req.data.binary->data, - req.data.binary->size); + req.data.binary.getData(), + req.data.binary.getSize()); } else if (req.data.node) { std::stringstream ssXML; ssXML << req.data.node; @@ -188,8 +188,8 @@ void InterpreterWebSocketServlet::send(const SendRequest& req) { } else if (req.data.binary) { HTTPServer::wsBroadcast(req.target.c_str(), EVWS_BINARY_FRAME, - req.data.binary->data, - req.data.binary->size); + req.data.binary.getData(), + req.data.binary.getSize()); } else if (req.data.node) { std::stringstream ssXML; ssXML << req.data.node; diff --git a/test/src/test-datamodel.cpp b/test/src/test-datamodel.cpp index 0dc8d8a..71189d6 100644 --- a/test/src/test-datamodel.cpp +++ b/test/src/test-datamodel.cpp @@ -20,6 +20,23 @@ int main(int argc, char** argv) { WSAStartup(MAKEWORD(2, 2), &wsaData); #endif + { + char* testData = (char*)malloc(1024); + for (int i = 0; i < 1024; i++) { + testData[i] = (char)i; + } + + Data data(testData, 1024, "", false); + Blob blob = data.getBinary(); + char* otherData = blob.getData(); + + for (int i = 0; i < 1024; i++) { + assert(testData[i] == otherData[i]); + } + + exit(0); + } + Interpreter interpreter = Interpreter::fromXML(""); DataModel dm(Factory::getInstance()->createDataModel("ecmascript", interpreter.getImpl().get())); dm.evalAsString("var foo = 12"); diff --git a/test/src/test-predicates.cpp b/test/src/test-predicates.cpp index d672577..fb4551e 100644 --- a/test/src/test-predicates.cpp +++ b/test/src/test-predicates.cpp @@ -10,16 +10,16 @@ int main(int argc, char** argv) { using namespace Arabica::XPath; const char* xml = - "" - " " - " " - " " - " " - " " - " " - " " - ""; - + "" + " " + " " + " " + " " + " " + " " + " " + ""; + Interpreter interpreter = Interpreter::fromXML(xml); assert(interpreter); interpreter.getImpl()->init(); -- cgit v0.12