Add MicroBitImage::isReadOnly,clone.

Add MicroBitImage,ManagedString::leakData().
Work more on incr()/decr() protocol.
This commit is contained in:
Michal Moskal 2015-10-25 10:03:14 -07:00
parent 11c99d0b84
commit d0b75c96d1
5 changed files with 110 additions and 8 deletions

View File

@ -32,7 +32,7 @@ class ManagedString
/**
* Constructor.
* Create a managed string from a specially prepared string literal.
* Create a managed string from a specially prepared string literal. It will ptr->incr().
*
* @param ptr The literal - first two bytes should be 0xff, then the length in little endian, then the literal. The literal has to be 4-byte aligned.
*
@ -42,7 +42,13 @@ class ManagedString
* ManagedString s((StringData*)(void*)hello);
* @endcode
*/
ManagedString(StringData *p) : ptr(p) {}
ManagedString(StringData *ptr);
/**
* Get current ptr, do not decr() it, and set the current instance to empty string.
* This is to be used by specialized runtimes which pass StringData around.
*/
StringData *leakData();
/**
* Constructor.

View File

@ -5,6 +5,7 @@
#pragma GCC diagnostic ignored "-Wconversion-null"
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wparentheses"
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#include "mbed.h"

View File

@ -39,6 +39,12 @@ class MicroBitImage
public:
static MicroBitImage EmptyImage; // Shared representation of a null image.
/**
* Get current ptr, do not decr() it, and set the current instance to empty image.
* This is to be used by specialized runtimes which pass ImageData around.
*/
ImageData *leakData();
/**
* Return a 2D array representing the bitmap image.
*/
@ -49,17 +55,17 @@ class MicroBitImage
/**
* Constructor.
* Create an image from a specially prepared constant array, with no copying.
* Create an image from a specially prepared constant array, with no copying. Will call ptr->incr().
*
* @param p The literal - first two bytes should be 0xff, then width, height, and the bitmap. The literal has to be 4-byte aligned.
* @param ptr The literal - first two bytes should be 0xff, then width, height, and the bitmap. The literal has to be 4-byte aligned.
*
* Example:
* @code
* static const uint8_t heart[] __attribute__ ((aligned (4))) = { 0xff, 0xff, 10, 5, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
* ManagedString s((ImageData*)(void*)heart);
* MicroBitImage i((ImageData*)(void*)heart);
* @endcode
*/
MicroBitImage(ImageData *p) : ptr(p) {}
MicroBitImage(ImageData *ptr);
/**
* Default Constructor.
@ -412,6 +418,17 @@ class MicroBitImage
*/
MicroBitImage crop(int startx, int starty, int finx, int finy);
/**
* Check if image is read-only (i.e., residing in flash).
*/
bool isReadOnly();
/**
* Create a copy of the image bitmap. Used particularly, when isReadOnly() is true.
*
* @return an instance of MicroBitImage which can be modified independently of the current instance
*/
MicroBitImage clone();
};
#endif

View File

@ -31,6 +31,34 @@ void ManagedString::initString(const char *str)
memcpy(ptr->data, str, len+1);
}
/**
* Constructor.
* Create a managed string from a specially prepared string literal. It will ptr->incr().
*
* @param ptr The literal - first two bytes should be 0xff, then the length in little endian, then the literal. The literal has to be 4-byte aligned.
*
* Example:
* @code
* static const char hello[] __attribute__ ((aligned (4))) = "\xff\xff\x05\x00" "Hello";
* ManagedString s((StringData*)(void*)hello);
* @endcode
*/
ManagedString::ManagedString(StringData *p)
{
ptr = p;
ptr->incr();
}
/**
* Get current ptr, do not decr() it, and set the current instance to empty string.
* This is to be used by specialized runtimes which pass StringData around.
*/
StringData* ManagedString::leakData()
{
StringData *res = ptr;
initEmpty();
return res;
}
/**
* Constructor.

View File

@ -7,10 +7,12 @@
#include "MicroBit.h"
static const uint8_t empty[] __attribute__ ((aligned (4))) = { 0xff, 0xff, 1, 1, 0, 0 };
/*
* The null image. We actally create a small one byte buffer here, just to keep NULL pointers out of the equation.
*/
MicroBitImage MicroBitImage::EmptyImage(1,1);
MicroBitImage MicroBitImage::EmptyImage((ImageData*)(void*)empty);
/**
* Default Constructor.
@ -163,6 +165,36 @@ MicroBitImage::MicroBitImage(const char *s)
}
}
/**
* Constructor.
* Create an image from a specially prepared constant array, with no copying. Will call ptr->incr().
*
* @param ptr The literal - first two bytes should be 0xff, then width, height, and the bitmap. The literal has to be 4-byte aligned.
*
* Example:
* @code
* static const uint8_t heart[] __attribute__ ((aligned (4))) = { 0xff, 0xff, 10, 5, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
* MicroBitImage i((ImageData*)(void*)heart);
* @endcode
*/
MicroBitImage::MicroBitImage(ImageData *p)
{
ptr = p;
ptr->incr();
}
/**
* Get current ptr, do not decr() it, and set the current instance to empty image.
* This is to be used by specialized runtimes which pass ImageData around.
*/
ImageData *MicroBitImage::leakData()
{
ImageData* res = ptr;
init_empty();
return res;
}
/**
* Constructor.
* Create a bitmap representation of a given size, based on a given buffer.
@ -762,3 +794,21 @@ MicroBitImage MicroBitImage::crop(int startx, int starty, int cropWidth, int cro
return MicroBitImage(newWidth, newHeight, cropped);
}
/**
* Check if image is read-only (i.e., residing in flash).
*/
bool MicroBitImage::isReadOnly()
{
return ptr->isReadOnly();
}
/**
* Create a copy of the image bitmap. Used particularly, when isReadOnly() is true.
*
* @return an instance of MicroBitImage which can be modified independently of the current instance
*/
MicroBitImage MicroBitImage::clone()
{
return MicroBitImage(getWidth(), getHeight(), getBitmap());
}