From 9746117f711e89504d19d58777690d66f6b1f3c3 Mon Sep 17 00:00:00 2001 From: ksherlock Date: Sat, 26 Feb 2011 03:52:54 +0000 Subject: [PATCH] use smart macros to make the future easier git-svn-id: https://profuse.googlecode.com/svn/branches/v2@351 aa027e90-d47c-11dd-86d7-074df07e0730 --- Cache/BlockCache.cpp | 3 ++- Cache/ConcreteBlockCache.cpp | 4 +++- Cache/MappedBlockCache.cpp | 3 ++- Device/BlockDevice.h | 4 ++-- Device/DavexDiskImage.cpp | 8 ++++++-- Device/Device.h | 9 +++------ Device/DiskCopy42Image.cpp | 8 ++++++-- Device/DiskImage.cpp | 13 +++++++++---- Device/RawDevice.cpp | 3 ++- Device/UniversalDiskImage.cpp | 8 ++++++-- Pascal/Entry.h | 14 ++++++-------- Pascal/FileEntry.cpp | 6 ++++-- Pascal/FileEntry.h | 3 ++- Pascal/VolumeEntry.cpp | 10 +++++++--- Pascal/VolumeEntry.h | 2 +- ProFUSE/smart_pointers.h | 35 +++++++++++++++++++++++++++++++++++ 16 files changed, 96 insertions(+), 37 deletions(-) create mode 100644 ProFUSE/smart_pointers.h diff --git a/Cache/BlockCache.cpp b/Cache/BlockCache.cpp index 61462ed..3c24d2a 100644 --- a/Cache/BlockCache.cpp +++ b/Cache/BlockCache.cpp @@ -51,7 +51,8 @@ void BlockCache::read(unsigned block, void *bp) 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(); } diff --git a/Cache/ConcreteBlockCache.cpp b/Cache/ConcreteBlockCache.cpp index 5bf7ede..6987ad4 100644 --- a/Cache/ConcreteBlockCache.cpp +++ b/Cache/ConcreteBlockCache.cpp @@ -60,7 +60,9 @@ using ProFUSE::POSIXException; 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) : diff --git a/Cache/MappedBlockCache.cpp b/Cache/MappedBlockCache.cpp index 4a65fb1..bf0892b 100644 --- a/Cache/MappedBlockCache.cpp +++ b/Cache/MappedBlockCache.cpp @@ -21,7 +21,8 @@ using ProFUSE::POSIXException; 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); } diff --git a/Device/BlockDevice.h b/Device/BlockDevice.h index 8d4a021..c047912 100644 --- a/Device/BlockDevice.h +++ b/Device/BlockDevice.h @@ -8,13 +8,13 @@ #include #include - +#include #include namespace Device { - class BlockDevice : public std::tr1::enable_shared_from_this { + class BlockDevice : public ENABLE_SHARED_FROM_THIS(BlockDevice) { public: diff --git a/Device/DavexDiskImage.cpp b/Device/DavexDiskImage.cpp index 54e6ad9..51270bc 100644 --- a/Device/DavexDiskImage.cpp +++ b/Device/DavexDiskImage.cpp @@ -91,7 +91,9 @@ BlockDevicePointer DavexDiskImage::Open(MappedFile *file) #define __METHOD__ "DavexDiskImage::Open" 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) @@ -154,7 +156,9 @@ BlockDevicePointer DavexDiskImage::Create(const char *name, size_t blocks, const std::memcpy(file->address(), header.buffer(), 512); file->sync(); - return BlockDevicePointer(new DavexDiskImage(file)); + //return BlockDevicePointer(new DavexDiskImage(file)); + + return MAKE_SHARED(DavexDiskImage, file); } diff --git a/Device/Device.h b/Device/Device.h index 83c6f08..458320c 100644 --- a/Device/Device.h +++ b/Device/Device.h @@ -9,18 +9,15 @@ #ifndef __DEVICE_DEVICE_H__ #define __DEVICE_DEVICE_H__ -#include +#include namespace Device { class BlockDevice; class BlockCache; - - typedef std::tr1::shared_ptr BlockDevicePointer; - typedef std::tr1::shared_ptr BlockCachePointer; - - + typedef SHARED_PTR(BlockDevice) BlockDevicePointer; + typedef SHARED_PTR(BlockCache) BlockCachePointer; } diff --git a/Device/DiskCopy42Image.cpp b/Device/DiskCopy42Image.cpp index 040d0c1..d6c6d59 100644 --- a/Device/DiskCopy42Image.cpp +++ b/Device/DiskCopy42Image.cpp @@ -78,7 +78,9 @@ uint32_t DiskCopy42Image::Checksum(void *data, size_t size) BlockDevicePointer DiskCopy42Image::Open(MappedFile *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) @@ -163,7 +165,9 @@ BlockDevicePointer DiskCopy42Image::Create(const char *name, size_t blocks, cons std::memcpy(file->address(), header.buffer(), oUserData); file->sync(); - return BlockDevicePointer(new DiskCopy42Image(file)); + //return BlockDevicePointer(new DiskCopy42Image(file)); + + return MAKE_SHARED(DiskCopy42Image, file); } void DiskCopy42Image::Validate(MappedFile *file) diff --git a/Device/DiskImage.cpp b/Device/DiskImage.cpp index 0b83da5..6025e5d 100644 --- a/Device/DiskImage.cpp +++ b/Device/DiskImage.cpp @@ -115,13 +115,15 @@ ProDOSOrderDiskImage::ProDOSOrderDiskImage(MappedFile *file) : BlockDevicePointer ProDOSOrderDiskImage::Create(const char *name, size_t blocks) { 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) { Validate(file); - return BlockDevicePointer(new ProDOSOrderDiskImage(file)); + //return BlockDevicePointer(new ProDOSOrderDiskImage(file)); + return MAKE_SHARED(ProDOSOrderDiskImage, file); } void ProDOSOrderDiskImage::Validate(MappedFile *f) @@ -168,13 +170,16 @@ DOSOrderDiskImage::DOSOrderDiskImage(MappedFile *file) : BlockDevicePointer DOSOrderDiskImage::Create(const char *name, size_t blocks) { 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) { Validate(file); - return BlockDevicePointer(new DOSOrderDiskImage(file)); + //return BlockDevicePointer(new DOSOrderDiskImage(file)); + return MAKE_SHARED(DOSOrderDiskImage, file); + } void DOSOrderDiskImage::Validate(MappedFile *f) diff --git a/Device/RawDevice.cpp b/Device/RawDevice.cpp index 1df2117..96f10b3 100644 --- a/Device/RawDevice.cpp +++ b/Device/RawDevice.cpp @@ -198,7 +198,8 @@ RawDevice::~RawDevice() 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); } diff --git a/Device/UniversalDiskImage.cpp b/Device/UniversalDiskImage.cpp index 82adafb..96ad4ef 100644 --- a/Device/UniversalDiskImage.cpp +++ b/Device/UniversalDiskImage.cpp @@ -81,13 +81,17 @@ BlockDevicePointer UniversalDiskImage::Create(const char *name, size_t blocks) 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) { Validate(file); - return BlockDevicePointer(new UniversalDiskImage(file)); + + //return BlockDevicePointer(new UniversalDiskImage(file)); + return MAKE_SHARED(UniversalDiskImage, file); } diff --git a/Pascal/Entry.h b/Pascal/Entry.h index c122951..7fe8605 100644 --- a/Pascal/Entry.h +++ b/Pascal/Entry.h @@ -1,9 +1,7 @@ #ifndef __PASCAL_ENTRY_H__ #define __PASCAL_ENTRY_H__ -#include - -#include +#include namespace Device { @@ -34,13 +32,13 @@ namespace Pascal { class VolumeEntry; - typedef std::tr1::shared_ptr FileEntryPointer; - typedef std::tr1::shared_ptr VolumeEntryPointer; + typedef SHARED_PTR(FileEntry) FileEntryPointer; + typedef SHARED_PTR(VolumeEntry) VolumeEntryPointer; - typedef std::tr1::weak_ptr FileEntryWeakPointer; - typedef std::tr1::weak_ptr VolumeEntryWeakPointer; + typedef WEAK_PTR(FileEntry) FileEntryWeakPointer; + typedef WEAK_PTR(VolumeEntry) VolumeEntryWeakPointer; - class Entry : public std::tr1::enable_shared_from_this { + class Entry : public ENABLE_SHARED_FROM_THIS(Entry) { public: diff --git a/Pascal/FileEntry.cpp b/Pascal/FileEntry.cpp index f4a1c18..35ba7ee 100644 --- a/Pascal/FileEntry.cpp +++ b/Pascal/FileEntry.cpp @@ -44,12 +44,14 @@ unsigned FileEntry::ValidName(const char *cp) 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) { - return FileEntryPointer(new FileEntry(name, fileKind)); + //return FileEntryPointer(new FileEntry(name, fileKind)); + return MAKE_SHARED(FileEntry, name, fileKind); } FileEntry::FileEntry(void *vp) : diff --git a/Pascal/FileEntry.h b/Pascal/FileEntry.h index 7bec9ac..80e3c61 100644 --- a/Pascal/FileEntry.h +++ b/Pascal/FileEntry.h @@ -2,6 +2,7 @@ #define __PASCAL_FILEENTRY_H__ #include +#include #include #include @@ -61,7 +62,7 @@ namespace Pascal { FileEntryPointer thisPointer() { - return std::tr1::static_pointer_cast(shared_from_this()); + return STATIC_POINTER_CAST(FileEntry, shared_from_this()); } diff --git a/Pascal/VolumeEntry.cpp b/Pascal/VolumeEntry.cpp index a4eff36..a209c22 100644 --- a/Pascal/VolumeEntry.cpp +++ b/Pascal/VolumeEntry.cpp @@ -64,15 +64,19 @@ unsigned VolumeEntry::ValidName(const char *cp) VolumeEntryPointer VolumeEntry::Open(Device::BlockDevicePointer device) { - VolumeEntryPointer ptr(new VolumeEntry(device)); - + VolumeEntryPointer ptr; + + //ptr = new VolumeEntry(device)); + ptr = MAKE_SHARED(VolumeEntry, device); return ptr; } 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. if (ptr) ptr->setParents(); diff --git a/Pascal/VolumeEntry.h b/Pascal/VolumeEntry.h index 4b3f8a4..ccb6b7f 100644 --- a/Pascal/VolumeEntry.h +++ b/Pascal/VolumeEntry.h @@ -71,7 +71,7 @@ namespace Pascal { VolumeEntryPointer thisPointer() { - return std::tr1::static_pointer_cast(shared_from_this()); + return STATIC_POINTER_CAST(VolumeEntry, shared_from_this()); } void init(void *); diff --git a/ProFUSE/smart_pointers.h b/ProFUSE/smart_pointers.h new file mode 100644 index 0000000..6ab4ecd --- /dev/null +++ b/ProFUSE/smart_pointers.h @@ -0,0 +1,35 @@ + +#ifndef __PROFUSE_SMART_POINTERS_H__ +#define __PROFUSE_SMART_POINTERS_H__ + +#ifdef CPP0X +//C++0x +#include + + +#define SHARED_PTR(T) std::shared_ptr +#define WEAK_PTR(T) std::weak_ptr + +#define MAKE_SHARED(T, ...) std::make_shared(__VA_ARGS__) +#define ENABLE_SHARED_FROM_THIS(T) std::enable_shared_from_this + +#define STATIC_POINTER_CAST(T, ARG) std::static_pointer_cast(ARG) +#define DYNAMIC_POINTER_CAST(T, ARG) std::dynamic_pointer_cast(ARG) + +#else + +// tr1 +#include + +#define SHARED_PTR(T) std::tr1::shared_ptr +#define WEAK_PTR(T) std::tr1::weak_ptr + +#define MAKE_SHARED(T, ...) std::tr1::shared_ptr(new T(__VA_ARGS__)) +#define ENABLE_SHARED_FROM_THIS(T) std::tr1::enable_shared_from_this + +#define STATIC_POINTER_CAST(T, ARG) std::tr1::static_pointer_cast(ARG) +#define DYNAMIC_POINTER_CAST(T, ARG) std::tr1::dynamic_pointer_cast(ARG) + +#endif + +#endif