Move RefCounted class to a separate file

This commit is contained in:
Michal Moskal 2015-10-24 20:30:15 -07:00
parent 5a10bda561
commit 9330b401d5
6 changed files with 90 additions and 48 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
.yotta.json
build
*.swp
yotta_modules
yotta_targets
Makefile

View File

@ -1,24 +1,7 @@
#ifndef MANAGED_STRING_H
#define MANAGED_STRING_H
#include "mbed.h"
struct RefCounted
{
public:
uint16_t refcnt;
uint16_t size;
void incr();
void decr();
};
class VirtualRefCounted : RefCounted
{
public:
virtual ~VirtualRefCounted();
};
#include "RefCounted.h"
struct StringData : RefCounted
{

51
inc/RefCounted.h Normal file
View File

@ -0,0 +1,51 @@
#ifndef REF_COUNTED_H
#define REF_COUNTED_H
#include "mbed.h"
/**
* Base class for payload for ref-counted objects. Used by ManagedString and MicroBitImage.
* There is no constructor, as this struct is typically malloc()ed.
*/
struct RefCounted
{
public:
/**
* Number of outstanding references. Should never be zero (object should be deleted then).
* When it's set to 0xffff, it means the object sits in flash and should not be counted.
*/
uint16_t refcnt;
/**
* For strings this is length. For images this is both width and length (8 bit each).
* A value of 0xffff indicates that this is in fact an instance of VirtualRefCounted
* and therefore when the reference count reaches zero, the virtual destructor
* should be called.
*/
union {
uint16_t size;
struct {
uint8_t width;
uint8_t height;
};
};
/** Increment reference count. */
void incr();
/** Decrement reference count. */
void decr();
};
/**
* Base class for ref-counted objects. Used in native compiler in TD for Collections,
* user-defined records, closures etc.
*/
class VirtualRefCounted : RefCounted
{
public:
virtual ~VirtualRefCounted();
};
#endif

View File

@ -28,6 +28,7 @@ set(YOTTA_AUTO_MICROBIT-DAL_CPP_FILES
"MicroBitSerial.cpp"
"MicroBitHeapAllocator.cpp"
"MicroBitListener.cpp"
"RefCounted.cpp"
"MemberFunctionCallback.cpp"
"ble-services/MicroBitDFUService.cpp"
"ble-services/MicroBitEventService.cpp"

View File

@ -366,33 +366,3 @@ char ManagedString::charAt(int16_t index)
* Empty string constant literal
*/
ManagedString ManagedString::EmptyString((StringData*)(void*)empty);
void RefCounted::incr()
{
if (refcnt == 0xffff)
return;
if (refcnt == 0)
uBit.panic(30);
refcnt++;
}
void RefCounted::decr()
{
if (refcnt == 0xffff)
return;
if (refcnt == 0)
uBit.panic(31);
refcnt--;
if (refcnt == 0) {
// size of 0xffff indicates a class with a virtual destructor
if (size == 0xffff)
delete (VirtualRefCounted*)this;
else
free(this);
}
}
VirtualRefCounted::~VirtualRefCounted()
{
}

31
source/RefCounted.cpp Normal file
View File

@ -0,0 +1,31 @@
#include "mbed.h"
#include "MicroBit.h"
void RefCounted::incr()
{
if (refcnt == 0xffff)
return;
if (refcnt == 0)
uBit.panic(30);
refcnt++;
}
void RefCounted::decr()
{
if (refcnt == 0xffff)
return;
if (refcnt == 0)
uBit.panic(31);
refcnt--;
if (refcnt == 0) {
// size of 0xffff indicates a class with a virtual destructor
if (size == 0xffff)
delete (VirtualRefCounted*)this;
else
free(this);
}
}
VirtualRefCounted::~VirtualRefCounted()
{
}