Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TIMOB-10161: BlackBerry: Implement Ti.Buffer for Anvil [Partial implementation] #125

Merged
208 changes: 208 additions & 0 deletions blackberry/tibb/NativeBufferObject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/

#include "NativeBufferObject.h"
#include "NativeControlObject.h"
#include "TiObject.h"

#define PROP_SETGET_FUNCTION(NAME) prop_##NAME

#define PROP_SETGET(NAME) static int prop_##NAME(NativeBufferObject* buffer, TiObject* obj) \
{\
return buffer->NAME(obj);\
}

#define GET_ARRAY_SIZE(ARRAY) (int)(sizeof(ARRAY)/sizeof(*(ARRAY)))

typedef int (*NATIVE_PROPSETGET_CALLBACK)(NativeBufferObject*, TiObject*);

struct NATIVE_PROPSETGET_SETTING
{
NATIVE_BUFFER_PROP propNumber;
NATIVE_PROPSETGET_CALLBACK setter;
NATIVE_PROPSETGET_CALLBACK getter;
};

class SetGetProperties
{
public:
SetGetProperties(const NATIVE_PROPSETGET_SETTING* map, int mapEntries)
{
setters_ = new NATIVE_PROPSETGET_CALLBACK[N_PROP_LAST];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why N_PROP_LAST? Seems like it should be N_BUFFER_PROP_LAST. Same with the rest of them.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

memset(setters_, 0, sizeof(NATIVE_PROPSETGET_CALLBACK) * N_PROP_LAST);
getters_ = new NATIVE_PROPSETGET_CALLBACK[N_PROP_LAST];
memset(getters_, 0, sizeof(NATIVE_PROPSETGET_CALLBACK) * N_PROP_LAST);
for (int i = 0; i < mapEntries; i++)
{
setters_[map[i].propNumber] = map[i].setter;
getters_[map[i].propNumber] = map[i].getter;
}
}
~SetGetProperties()
{
if (setters_ != NULL)
{
delete[] setters_;
setters_ = NULL;
}
if (getters_ != NULL)
{
delete[] getters_;
getters_ = NULL;
}
}
NATIVE_PROPSETGET_CALLBACK GetSetterCallback(size_t prop)
{
if (prop >= (std::size_t)N_PROP_LAST)
{
return NULL;
}
return setters_[prop];
}
NATIVE_PROPSETGET_CALLBACK GetGetterCallback(size_t prop)
{
if (prop >= (std::size_t)N_PROP_LAST)
{
return NULL;
}
return getters_[prop];
}
private:
// Disabled default and copy constructors
SetGetProperties();
SetGetProperties(const SetGetProperties& prop);
// Disabled assignment operator
const SetGetProperties& operator = (const SetGetProperties& prop);
NATIVE_PROPSETGET_CALLBACK* setters_;
NATIVE_PROPSETGET_CALLBACK* getters_;
};

NativeBufferObject::~NativeBufferObject()
{
}

int NativeBufferObject::getObjectType() const
{
return N_TYPE_BUFFER;
}

NativeBufferObject* NativeBufferObject::createBuffer()
{
return new NativeBufferObject;
}

PROP_SETGET(getByteOrder)
int NativeBufferObject::getByteOrder(TiObject* obj)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the warning about obj not being used. Same for the rest of the times too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

{
return NATIVE_ERROR_NOTSUPPORTED;
}

PROP_SETGET(getLength)
int NativeBufferObject::getLength(TiObject* obj)
{
obj->setValue(Number::New(internalData_.size()));
return NATIVE_ERROR_OK;
}

PROP_SETGET(setLength)
int NativeBufferObject::setLength(TiObject* obj)
{
int lengthValue;
int error = NativeControlObject::getInteger(obj, &lengthValue);
if (!N_SUCCEEDED(error))
{
return error;
}
internalData_.resize(lengthValue);
return NATIVE_ERROR_OK;
}

PROP_SETGET(getType)
int NativeBufferObject::getType(TiObject* obj)
{
return NATIVE_ERROR_NOTSUPPORTED;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to update the documentation. For the others too.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Some of functionlity not implemented yet, but will be in next patches.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only need to implement the ones needed for anvil, the rest will be implemented in TIMOB-9542

}

PROP_SETGET(setValue)
int NativeBufferObject::setValue(TiObject* obj)
{
Handle<Value> v8Value = obj->getValue();
if (v8Value.IsEmpty())
{
return NATIVE_ERROR_INVALID_ARG;
}
if (v8Value->IsNumber() || !v8Value->IsNumberObject())
{
if (v8Value->IsInt32())
{
Handle<Number> num = Handle<Number>::Cast(v8Value);
int value = (int)num->Value();
internalData_ = QByteArray::number(value);
}
else
{
Handle<Number> num = Handle<Number>::Cast(v8Value);
double value = (double)num->Value();
internalData_ = QByteArray::number(value);
}
}
else if (v8Value->IsString() || v8Value->IsStringObject())
{
Handle<Value> value = v8Value->ToString();
Handle<String> v8string = Handle<String>::Cast(value);
String::Utf8Value v8UtfString(v8string);
internalData_ = QByteArray(*v8UtfString);
}
return NATIVE_ERROR_OK;
}

PROP_SETGET(getValue)
int NativeBufferObject::getValue(TiObject* obj)
{
return NATIVE_ERROR_NOTSUPPORTED;
}

void NativeBufferObject::clear()
{
internalData_.clear();
}

const static NATIVE_PROPSETGET_SETTING g_propSetGet[] =
{
{N_BUFFER_PROP_BYTEORDER, NULL, PROP_SETGET_FUNCTION(getByteOrder)},
{N_BUFFER_PROP_LENGTH, PROP_SETGET_FUNCTION(setLength), PROP_SETGET_FUNCTION(getLength)},
{N_BUFFER_PROP_TYPE, NULL, PROP_SETGET_FUNCTION(getType)},
{N_BUFFER_PROP_VALUE, PROP_SETGET_FUNCTION(setValue), PROP_SETGET_FUNCTION(getValue)}
};

static SetGetProperties g_props(g_propSetGet, GET_ARRAY_SIZE(g_propSetGet));


int NativeBufferObject::setPropertyValue(size_t propertyNumber, TiObject* obj)
{
NATIVE_PROPSETGET_CALLBACK cb = g_props.GetSetterCallback(propertyNumber);
if (cb == NULL)
{
return NATIVE_ERROR_NOTSUPPORTED;
}
return (cb)(this, obj);
}

int NativeBufferObject::getPropertyValue(size_t propertyNumber, TiObject* obj)
{
NATIVE_PROPSETGET_CALLBACK cb = g_props.GetGetterCallback(propertyNumber);
if (cb == NULL)
{
return NATIVE_ERROR_NOTSUPPORTED;
}
return (cb)(this, obj);
}

void NativeBufferObject::setupEvents(TiEventContainerFactory* containerFactory)
{
NativeProxyObject::setupEvents(containerFactory);
}
70 changes: 70 additions & 0 deletions blackberry/tibb/NativeBufferObject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2012 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/

#ifndef NATIVEBUFFEROBJECT_H_
#define NATIVEBUFFEROBJECT_H_

#pragma GCC diagnostic ignored "-Wunused-parameter"
#include <v8.h>
#pragma GCC diagnostic warning "-Wunused-parameter"

#include "NativeProxyObject.h"
#include <qbytearray>

using namespace v8;

enum NATIVE_BUFFER_PROP
{
N_BUFFER_PROP_UNDEFINED
, N_BUFFER_PROP_BYTEORDER
, N_BUFFER_PROP_LENGTH
, N_BUFFER_PROP_TYPE
, N_BUFFER_PROP_VALUE

/* This MUST be the last element */
, N_PLATFORM_PROP_LAST

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be N_BUFFER_PROP_LAST

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

};

class TiObject;
class TiEventContainerFactory;

/*
* NativeBufferObject
*
* NativeBufferObject methods
*/
class NativeBufferObject : public NativeProxyObject
{
public:
static NativeBufferObject* createBuffer();
int getObjectType() const;
int setPropertyValue(size_t propertyNumber, TiObject* obj);
int getPropertyValue(size_t propertyNumber, TiObject* obj);
int getByteOrder(TiObject* obj);
int getLength(TiObject* obj);
int setLength(TiObject* obj);
int getType(TiObject* obj);
int getValue(TiObject* obj);
int setValue(TiObject* obj);
void setupEvents(TiEventContainerFactory* containerFactory);

void clear();

protected:
virtual ~NativeBufferObject();

private:
NativeBufferObject() {};
NativeBufferObject(const NativeBufferObject&);
NativeBufferObject& operator=(const NativeBufferObject&);
// Buffer containing raw data
QByteArray internalData_;
};



#endif /* NATIVEBUFFEROBJECT_H_ */
1 change: 1 addition & 0 deletions blackberry/tibb/NativeObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ enum NATIVE_TYPE
N_TYPE_UNDEFINED
, N_TYPE_ACTIVITYINDICATOR
, N_TYPE_ALERTDIALOG
, N_TYPE_BUFFER
, N_TYPE_BUTTON
, N_TYPE_CONTAINER
, N_TYPE_DATE_TIME_PICKER
Expand Down
5 changes: 5 additions & 0 deletions blackberry/tibb/NativeObjectFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "NativeActivityIndicatorObject.h"
#include "NativeAlertDialogObject.h"
#include "NativeBufferObject.h"
#include "NativeButtonObject.h"
#include "NativeDateTimePickerObject.h"
#include "NativeDropDownObject.h"
Expand Down Expand Up @@ -69,6 +70,10 @@ NativeObject* NativeObjectFactory::createNativeObject(int type)
obj = NativeLoggerObject::getInstance();
break;

case N_TYPE_BUFFER:
obj = NativeBufferObject::createBuffer();
break;

case N_TYPE_BUTTON:
obj = new NativeButtonObject;
break;
Expand Down