Get rid of C++ style NSIndexSet iteration

This commit is contained in:
Allan Odgaard
2012-09-19 16:18:44 +02:00
parent 2739b4cf20
commit 4273bf968b
6 changed files with 12 additions and 105 deletions

View File

@@ -1164,8 +1164,8 @@ OAK_DEBUG_VAR(DocumentController);
if(NSIndexSet* indexSet = [self tryObtainIndexSetFrom:sender])
{
std::vector<document::document_ptr> documents;
iterate(index, indexSet)
documents.push_back(*documentTabs[*index]);
for(NSUInteger index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex:index])
documents.push_back(*documentTabs[index]);
DocumentController* delegate = [[DocumentController alloc] initWithDocuments:documents];
[delegate showWindow:self];
[self closeTabsAtIndexes:indexSet quiet:YES];

View File

@@ -329,9 +329,9 @@ struct operation_t
std::vector<std::string> res;
NSIndexSet* selectedRows = [findAllResultsOutlineView numberOfSelectedRows] == 0 ? [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [findAllResultsOutlineView numberOfRows])] : [findAllResultsOutlineView selectedRowIndexes];
iterate(it, selectedRows)
for(NSUInteger index = [selectedRows firstIndex]; index != NSNotFound; index = [selectedRows indexGreaterThanIndex:index])
{
FFMatch* item = [findAllResultsOutlineView itemAtRow:*it];
FFMatch* item = [findAllResultsOutlineView itemAtRow:index];
if([self outlineView:findAllResultsOutlineView isGroupItem:item])
continue;

View File

@@ -63,8 +63,9 @@ static NSSet* ExpandedURLs (NSOutlineView* outlineView, FSItem* root, NSMutableS
static NSSet* SelectedURLs (NSOutlineView* outlineView, FSItem* root)
{
NSMutableSet* selectedURLs = [NSMutableSet set];
citerate(index, [outlineView selectedRowIndexes])
[selectedURLs addObject:[[outlineView itemAtRow:*index] url]];
NSIndexSet* indexSet = [outlineView selectedRowIndexes];
for(NSUInteger index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex:index])
[selectedURLs addObject:[[outlineView itemAtRow:index] url]];
[selectedURLs intersectSet:VisibleURLs(outlineView, root)];
return selectedURLs;

View File

@@ -123,8 +123,9 @@ static bool is_binary (std::string const& path)
- (NSArray*)selectedItems
{
NSMutableArray* res = [NSMutableArray array];
citerate(index, [view.outlineView selectedRowIndexes])
[res addObject:[view.outlineView itemAtRow:*index]];
NSIndexSet* indexSet = [view.outlineView selectedRowIndexes];
for(NSUInteger index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex:index])
[res addObject:[view.outlineView itemAtRow:index]];
return res;
}

View File

@@ -162,8 +162,8 @@
if(draggedRows && [self.dataSource respondsToSelector:@selector(outlineView:draggedItems:endedWithOperation:)])
{
NSMutableArray* items = [NSMutableArray array];
iterate(index, draggedRows)
[items addObject:[self itemAtRow:*index]];
for(NSUInteger index = [draggedRows firstIndex]; index != NSNotFound; index = [draggedRows indexGreaterThanIndex:index])
[items addObject:[self itemAtRow:index]];
[(id <FSDataSourceDragSource>)self.dataSource outlineView:self draggedItems:items endedWithOperation:aDragOperation];
}
self.draggedRows = nil;

View File

@@ -4,45 +4,6 @@
#import "misc.h"
#import "iterator_macros.h"
/* =================================== */
/* = Reference counted memory buffer = */
/* = This is a private class! = */
/* =================================== */
template <typename T> void OakDeleteArray (T* array) { delete[] array; }
template <typename T, void(*F)(T*) = OakDeleteArray>
struct OakSharedBuffer
{
struct impl
{
impl () : M_retainCount(1) { M_buffer = NULL; }
~impl () { F(M_buffer); }
T* M_buffer;
size_t M_retainCount;
};
typedef OakSharedBuffer self;
mutable impl* M_pimpl;
void retain () { ++M_pimpl->M_retainCount; }
void release () { if(M_pimpl && --M_pimpl->M_retainCount == 0) delete M_pimpl; }
size_t retain_count () const { return M_pimpl ? M_pimpl->M_retainCount : 1; }
impl* get_pimpl () const { if(M_pimpl == NULL) M_pimpl = new impl(); return M_pimpl; }
OakSharedBuffer () { M_pimpl = NULL; }
OakSharedBuffer (T* buf) { M_pimpl = NULL; set(buf); }
OakSharedBuffer (self const& rhs) { M_pimpl = rhs.get_pimpl(); retain(); }
~OakSharedBuffer () { release(); }
self& operator= (self const& rhs) { if(this != &rhs) { this->~self(); new (this) self(rhs); } return *this; }
T* get () const { return M_pimpl ? M_pimpl->M_buffer : NULL; }
void set (T* buf) { if(M_pimpl) F(M_pimpl->M_buffer); get_pimpl()->M_buffer = buf; }
T& operator* () { assert(M_pimpl); assert(M_pimpl->M_buffer); return *M_pimpl->M_buffer; }
T& operator[] (int i) { assert(M_pimpl); assert(M_pimpl->M_buffer); return M_pimpl->M_buffer[i]; }
T* operator-> () { assert(M_pimpl); assert(M_pimpl->M_buffer); return M_pimpl->M_buffer; }
};
/* ============================= */
/* = Objective-C smart pointer = */
/* ============================= */
@@ -72,60 +33,4 @@ struct objc_ptr
}
};
/* ========================================== */
/* = Generic superclass for iterators which = */
/* = need to copy values from the container = */
/* ========================================== */
template <typename C, typename T, T*(*F)(C)>
struct OakBufferIterator : public std::iterator<std::random_access_iterator_tag, T>
{
typedef OakBufferIterator self;
objc_ptr<C> M_container;
mutable OakSharedBuffer<T> M_buffer;
size_t M_index;
OakBufferIterator () { }
OakBufferIterator (C container, size_t index) : M_container(container), M_index(index) { }
OakBufferIterator (C container, size_t index, OakSharedBuffer<T> const& buffer) : M_container(container), M_buffer(buffer), M_index(index) { }
self& operator++ () { M_index++; return *this; }
self& operator-- () { M_index--; return *this; }
self operator++ (int) { self tmp(*this); M_index++; return tmp; }
self operator-- (int) { self tmp(*this); M_index--; return tmp; }
self operator+ (int i) const { return self(M_container, M_index + i, M_buffer); }
self operator- (int i) const { return self(M_container, M_index - i, M_buffer); }
self& operator+= (int i) { M_index += i; return *this; }
self& operator-= (int i) { M_index -= i; return *this; }
int operator- (self const& rhs) const { return M_index - rhs.M_index; }
bool operator== (self const& rhs) const { return M_index == rhs.M_index && M_container.get() == rhs.M_container.get(); }
bool operator!= (self const& rhs) const { return M_index != rhs.M_index || M_container.get() != rhs.M_container.get(); }
bool operator< (self const& rhs) const { return M_index < rhs.M_index; }
void acquire () const { if(M_buffer.get() == NULL) M_buffer.set(F(M_container)); }
T& operator* () const { acquire(); return M_buffer[M_index]; }
T& operator[] (int i) const { acquire(); return M_buffer[M_index + i]; }
};
inline NSUInteger const* OakIndexSetIteratorInit (NSIndexSet* indexSet)
{
NSUInteger size = [indexSet count];
NSUInteger* res = new NSUInteger[size];
NSRange range = NSMakeRange([indexSet firstIndex], [indexSet lastIndex]-[indexSet firstIndex] + 1);
[indexSet getIndexes:res maxCount:size inIndexRange:&range];
return res;
}
typedef OakBufferIterator<NSIndexSet*, NSUInteger const, OakIndexSetIteratorInit> OakIndexSetIterator;
/* =============================================== */
/* = beginof/endof functions to create iterators = */
/* =============================================== */
inline OakIndexSetIterator beginof (NSIndexSet* anIndexSet) { return OakIndexSetIterator(anIndexSet, 0); }
inline OakIndexSetIterator endof (NSIndexSet* anIndexSet) { return OakIndexSetIterator(anIndexSet, [anIndexSet count]); }
#endif /* end of include guard: OAK_COCOASTL_H_MGEOT7R7 */