Move RefCounted class to a separate file

master
Michal Moskal 7 years ago
parent 5a10bda561
commit 9330b401d5
  1. 6
      .gitignore
  2. 19
      inc/ManagedString.h
  3. 51
      inc/RefCounted.h
  4. 1
      source/CMakeLists.txt
  5. 30
      source/ManagedString.cpp
  6. 31
      source/RefCounted.cpp

6
.gitignore vendored

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

@ -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
{

@ -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

@ -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"

@ -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()
{
}

@ -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()
{
}
Loading…
Cancel
Save