use smart macros to make the future easier

git-svn-id: https://profuse.googlecode.com/svn/branches/v2@351 aa027e90-d47c-11dd-86d7-074df07e0730
This commit is contained in:
ksherlock 2011-02-26 03:52:54 +00:00
parent 89e80dcc10
commit 9746117f71
16 changed files with 96 additions and 37 deletions

View File

@ -51,7 +51,8 @@ void BlockCache::read(unsigned block, void *bp)
BlockCachePointer BlockCache::Create(BlockDevicePointer device) BlockCachePointer BlockCache::Create(BlockDevicePointer device)
{ {
if (!device.get()) return BlockCachePointer(); // this just calls the device virtual function to create a cache.
if (!device) return BlockCachePointer();
return device->createBlockCache(); return device->createBlockCache();
} }

View File

@ -60,7 +60,9 @@ using ProFUSE::POSIXException;
BlockCachePointer ConcreteBlockCache::Create(BlockDevicePointer device, unsigned size) BlockCachePointer ConcreteBlockCache::Create(BlockDevicePointer device, unsigned size)
{ {
return BlockCachePointer(new ConcreteBlockCache(device, size)); //return BlockCachePointer(new ConcreteBlockCache(device, size));
return MAKE_SHARED(ConcreteBlockCache, device, size);
} }
ConcreteBlockCache::ConcreteBlockCache(BlockDevicePointer device, unsigned size) : ConcreteBlockCache::ConcreteBlockCache(BlockDevicePointer device, unsigned size) :

View File

@ -21,7 +21,8 @@ using ProFUSE::POSIXException;
BlockCachePointer MappedBlockCache::Create(BlockDevicePointer device, void *data) BlockCachePointer MappedBlockCache::Create(BlockDevicePointer device, void *data)
{ {
return BlockCachePointer(new MappedBlockCache(device, data)); //return BlockCachePointer(new MappedBlockCache(device, data));
return MAKE_SHARED(MappedBlockCache, device, data);
} }

View File

@ -8,13 +8,13 @@
#include <Device/TrackSector.h> #include <Device/TrackSector.h>
#include <ProFUSE/Exception.h> #include <ProFUSE/Exception.h>
#include <ProFUSE/smart_pointers.h>
#include <File/File.h> #include <File/File.h>
namespace Device { namespace Device {
class BlockDevice : public std::tr1::enable_shared_from_this<BlockDevice> { class BlockDevice : public ENABLE_SHARED_FROM_THIS(BlockDevice) {
public: public:

View File

@ -91,7 +91,9 @@ BlockDevicePointer DavexDiskImage::Open(MappedFile *file)
#define __METHOD__ "DavexDiskImage::Open" #define __METHOD__ "DavexDiskImage::Open"
Validate(file); Validate(file);
return BlockDevicePointer(new DavexDiskImage(file)); //return BlockDevicePointer(new DavexDiskImage(file));
return MAKE_SHARED(DavexDiskImage, file);
} }
BlockDevicePointer DavexDiskImage::Create(const char *name, size_t blocks) BlockDevicePointer DavexDiskImage::Create(const char *name, size_t blocks)
@ -154,7 +156,9 @@ BlockDevicePointer DavexDiskImage::Create(const char *name, size_t blocks, const
std::memcpy(file->address(), header.buffer(), 512); std::memcpy(file->address(), header.buffer(), 512);
file->sync(); file->sync();
return BlockDevicePointer(new DavexDiskImage(file)); //return BlockDevicePointer(new DavexDiskImage(file));
return MAKE_SHARED(DavexDiskImage, file);
} }

View File

@ -9,18 +9,15 @@
#ifndef __DEVICE_DEVICE_H__ #ifndef __DEVICE_DEVICE_H__
#define __DEVICE_DEVICE_H__ #define __DEVICE_DEVICE_H__
#include <tr1/memory> #include <ProFUSE/smart_pointers.h>
namespace Device { namespace Device {
class BlockDevice; class BlockDevice;
class BlockCache; class BlockCache;
typedef SHARED_PTR(BlockDevice) BlockDevicePointer;
typedef std::tr1::shared_ptr<BlockDevice> BlockDevicePointer; typedef SHARED_PTR(BlockCache) BlockCachePointer;
typedef std::tr1::shared_ptr<BlockCache> BlockCachePointer;
} }

View File

@ -78,7 +78,9 @@ uint32_t DiskCopy42Image::Checksum(void *data, size_t size)
BlockDevicePointer DiskCopy42Image::Open(MappedFile *f) BlockDevicePointer DiskCopy42Image::Open(MappedFile *f)
{ {
Validate(f); Validate(f);
return BlockDevicePointer(new DiskCopy42Image(f)); //return BlockDevicePointer(new DiskCopy42Image(f));
return MAKE_SHARED(DiskCopy42Image, f);
} }
static uint8_t DiskFormat(size_t blocks) static uint8_t DiskFormat(size_t blocks)
@ -163,7 +165,9 @@ BlockDevicePointer DiskCopy42Image::Create(const char *name, size_t blocks, cons
std::memcpy(file->address(), header.buffer(), oUserData); std::memcpy(file->address(), header.buffer(), oUserData);
file->sync(); file->sync();
return BlockDevicePointer(new DiskCopy42Image(file)); //return BlockDevicePointer(new DiskCopy42Image(file));
return MAKE_SHARED(DiskCopy42Image, file);
} }
void DiskCopy42Image::Validate(MappedFile *file) void DiskCopy42Image::Validate(MappedFile *file)

View File

@ -115,13 +115,15 @@ ProDOSOrderDiskImage::ProDOSOrderDiskImage(MappedFile *file) :
BlockDevicePointer ProDOSOrderDiskImage::Create(const char *name, size_t blocks) BlockDevicePointer ProDOSOrderDiskImage::Create(const char *name, size_t blocks)
{ {
MappedFile *file = MappedFile::Create(name, blocks * 512); MappedFile *file = MappedFile::Create(name, blocks * 512);
return BlockDevicePointer(new ProDOSOrderDiskImage(file)); //return BlockDevicePointer(new ProDOSOrderDiskImage(file));
return MAKE_SHARED(ProDOSOrderDiskImage, file);
} }
BlockDevicePointer ProDOSOrderDiskImage::Open(MappedFile *file) BlockDevicePointer ProDOSOrderDiskImage::Open(MappedFile *file)
{ {
Validate(file); Validate(file);
return BlockDevicePointer(new ProDOSOrderDiskImage(file)); //return BlockDevicePointer(new ProDOSOrderDiskImage(file));
return MAKE_SHARED(ProDOSOrderDiskImage, file);
} }
void ProDOSOrderDiskImage::Validate(MappedFile *f) void ProDOSOrderDiskImage::Validate(MappedFile *f)
@ -168,13 +170,16 @@ DOSOrderDiskImage::DOSOrderDiskImage(MappedFile *file) :
BlockDevicePointer DOSOrderDiskImage::Create(const char *name, size_t blocks) BlockDevicePointer DOSOrderDiskImage::Create(const char *name, size_t blocks)
{ {
MappedFile *file = MappedFile::Create(name, blocks * 512); MappedFile *file = MappedFile::Create(name, blocks * 512);
return BlockDevicePointer(new DOSOrderDiskImage(file)); //return BlockDevicePointer(new DOSOrderDiskImage(file));
return MAKE_SHARED(DOSOrderDiskImage, file);
} }
BlockDevicePointer DOSOrderDiskImage::Open(MappedFile *file) BlockDevicePointer DOSOrderDiskImage::Open(MappedFile *file)
{ {
Validate(file); Validate(file);
return BlockDevicePointer(new DOSOrderDiskImage(file)); //return BlockDevicePointer(new DOSOrderDiskImage(file));
return MAKE_SHARED(DOSOrderDiskImage, file);
} }
void DOSOrderDiskImage::Validate(MappedFile *f) void DOSOrderDiskImage::Validate(MappedFile *f)

View File

@ -198,7 +198,8 @@ RawDevice::~RawDevice()
BlockDevicePointer RawDevice::Open(const char *name, File::FileFlags flags) BlockDevicePointer RawDevice::Open(const char *name, File::FileFlags flags)
{ {
return BlockDevicePointer(new RawDevice(name, flags)); //return BlockDevicePointer(new RawDevice(name, flags));
return MAKE_SHARED(RawDevice, name, flags);
} }

View File

@ -81,13 +81,17 @@ BlockDevicePointer UniversalDiskImage::Create(const char *name, size_t blocks)
std::memcpy(file->address(), header.buffer(), 64); std::memcpy(file->address(), header.buffer(), 64);
return BlockDevicePointer(new UniversalDiskImage(file)); //return BlockDevicePointer(new UniversalDiskImage(file));
return MAKE_SHARED(UniversalDiskImage, file);
} }
BlockDevicePointer UniversalDiskImage::Open(MappedFile *file) BlockDevicePointer UniversalDiskImage::Open(MappedFile *file)
{ {
Validate(file); Validate(file);
return BlockDevicePointer(new UniversalDiskImage(file));
//return BlockDevicePointer(new UniversalDiskImage(file));
return MAKE_SHARED(UniversalDiskImage, file);
} }

View File

@ -1,9 +1,7 @@
#ifndef __PASCAL_ENTRY_H__ #ifndef __PASCAL_ENTRY_H__
#define __PASCAL_ENTRY_H__ #define __PASCAL_ENTRY_H__
#include <tr1/memory> #include <ProFUSE/smart_pointers.h>
#include <Pascal/Date.h>
namespace Device { namespace Device {
@ -34,13 +32,13 @@ namespace Pascal {
class VolumeEntry; class VolumeEntry;
typedef std::tr1::shared_ptr<FileEntry> FileEntryPointer; typedef SHARED_PTR(FileEntry) FileEntryPointer;
typedef std::tr1::shared_ptr<VolumeEntry> VolumeEntryPointer; typedef SHARED_PTR(VolumeEntry) VolumeEntryPointer;
typedef std::tr1::weak_ptr<FileEntry> FileEntryWeakPointer; typedef WEAK_PTR(FileEntry) FileEntryWeakPointer;
typedef std::tr1::weak_ptr<VolumeEntry> VolumeEntryWeakPointer; typedef WEAK_PTR(VolumeEntry) VolumeEntryWeakPointer;
class Entry : public std::tr1::enable_shared_from_this<Entry> { class Entry : public ENABLE_SHARED_FROM_THIS(Entry) {
public: public:

View File

@ -44,12 +44,14 @@ unsigned FileEntry::ValidName(const char *cp)
FileEntryPointer FileEntry::Open(void *vp) FileEntryPointer FileEntry::Open(void *vp)
{ {
return FileEntryPointer(new FileEntry(vp)); //return FileEntryPointer(new FileEntry(vp));
return MAKE_SHARED(FileEntry, vp);
} }
FileEntryPointer FileEntry::Create(const char *name, unsigned fileKind) FileEntryPointer FileEntry::Create(const char *name, unsigned fileKind)
{ {
return FileEntryPointer(new FileEntry(name, fileKind)); //return FileEntryPointer(new FileEntry(name, fileKind));
return MAKE_SHARED(FileEntry, name, fileKind);
} }
FileEntry::FileEntry(void *vp) : FileEntry::FileEntry(void *vp) :

View File

@ -2,6 +2,7 @@
#define __PASCAL_FILEENTRY_H__ #define __PASCAL_FILEENTRY_H__
#include <Pascal/Entry.h> #include <Pascal/Entry.h>
#include <Pascal/Date.h>
#include <vector> #include <vector>
#include <string> #include <string>
@ -61,7 +62,7 @@ namespace Pascal {
FileEntryPointer thisPointer() FileEntryPointer thisPointer()
{ {
return std::tr1::static_pointer_cast<FileEntry>(shared_from_this()); return STATIC_POINTER_CAST(FileEntry, shared_from_this());
} }

View File

@ -64,15 +64,19 @@ unsigned VolumeEntry::ValidName(const char *cp)
VolumeEntryPointer VolumeEntry::Open(Device::BlockDevicePointer device) VolumeEntryPointer VolumeEntry::Open(Device::BlockDevicePointer device)
{ {
VolumeEntryPointer ptr(new VolumeEntry(device)); VolumeEntryPointer ptr;
//ptr = new VolumeEntry(device));
ptr = MAKE_SHARED(VolumeEntry, device);
return ptr; return ptr;
} }
VolumeEntryPointer VolumeEntry::Create(Device::BlockDevicePointer device, const char *name) VolumeEntryPointer VolumeEntry::Create(Device::BlockDevicePointer device, const char *name)
{ {
VolumeEntryPointer ptr(new VolumeEntry(device, name)); VolumeEntryPointer ptr;
//ptr = new VolumeEntry(device, name);
ptr = MAKE_SHARED(VolumeEntry, device, name);
// set up the weak references from the file entry to this. // set up the weak references from the file entry to this.
if (ptr) ptr->setParents(); if (ptr) ptr->setParents();

View File

@ -71,7 +71,7 @@ namespace Pascal {
VolumeEntryPointer thisPointer() VolumeEntryPointer thisPointer()
{ {
return std::tr1::static_pointer_cast<VolumeEntry>(shared_from_this()); return STATIC_POINTER_CAST(VolumeEntry, shared_from_this());
} }
void init(void *); void init(void *);

35
ProFUSE/smart_pointers.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef __PROFUSE_SMART_POINTERS_H__
#define __PROFUSE_SMART_POINTERS_H__
#ifdef CPP0X
//C++0x
#include <memory>
#define SHARED_PTR(T) std::shared_ptr<T>
#define WEAK_PTR(T) std::weak_ptr<T>
#define MAKE_SHARED(T, ...) std::make_shared<T>(__VA_ARGS__)
#define ENABLE_SHARED_FROM_THIS(T) std::enable_shared_from_this<T>
#define STATIC_POINTER_CAST(T, ARG) std::static_pointer_cast<T>(ARG)
#define DYNAMIC_POINTER_CAST(T, ARG) std::dynamic_pointer_cast<T>(ARG)
#else
// tr1
#include <tr1/memory>
#define SHARED_PTR(T) std::tr1::shared_ptr<T>
#define WEAK_PTR(T) std::tr1::weak_ptr<T>
#define MAKE_SHARED(T, ...) std::tr1::shared_ptr<T>(new T(__VA_ARGS__))
#define ENABLE_SHARED_FROM_THIS(T) std::tr1::enable_shared_from_this<T>
#define STATIC_POINTER_CAST(T, ARG) std::tr1::static_pointer_cast<T>(ARG)
#define DYNAMIC_POINTER_CAST(T, ARG) std::tr1::dynamic_pointer_cast<T>(ARG)
#endif
#endif