Skip to content

Instantly share code, notes, and snippets.

@jonsterling
Created November 21, 2010 06:33
Show Gist options
  • Select an option

  • Save jonsterling/708514 to your computer and use it in GitHub Desktop.

Select an option

Save jonsterling/708514 to your computer and use it in GitHub Desktop.
struct arr : obj
{
arr(id *input)
{
size_t s = sizeof(input) - 1;
this->substrate_object = [NSArray arrayWithObjects:input count:s];
}
arr(NSArray *input) : obj::obj(input) {}
id operator[](int index)
{
return [this->substrate_object objectAtIndex:index];
}
id operator[](NSRange r)
{
id is = [NSIndexSet indexSetWithIndexesInRange:r];
return [this->substrate_object objectsAtIndexes:is];
}
id operator id()
{
return this->substrate_object;
}
};
id a = [NSArray arrayWithObjects:@"a",@"b",@"c",nil];
arr b = a;
arr c = (id[]){@"a",@"b",@"c"};
NSLog(@"%@ == %@ == %@ == %@",
[a objectAtIndex:0],
[b objectAtIndex:0],
[c objectAtIndex:0],
b[0],
c[0]);
// => a == a == a== a
struct dict : obj
{
dict(NSDictionary *input) : obj::obj(input) {}
dict::dict(id *input)
{
size_t s = sizeof(input) - 1;
NSMutableDictionary *buffer = [NSMutableDictionary dictionaryWithCapacity:s/2];
for (int i = 0; i < s; i = i+2)
{
id key = input[i];
id values = input[i+1];
[buffer setObject:value forKey:key];
}
this->substrate_object = [[buffer copy] autorelease];
}
id dict::operator[](id key)
{
return [this->substrate_object objectForKey:key];
}
dict::operator id()
{
return this->substrate_object;
}
};
dict d = (id[]){
@"aKey",@"myValue",
@"awesome",@"puppies",
@"omg",@"z"
};
NSLog(@"%@ == %@",
d[@"awesome"],
[d objectForKey:@"awesome"]);
// => puppies == puppies
struct obj
{
obj(id substrate)
{
this->substrate_object = substrate;
}
operator id()
{
return this->substrate_object;
}
protected:
obj() { }
id substrate_object;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment