Skip to content

Instantly share code, notes, and snippets.

@toulouse
Created October 8, 2013 10:07
Show Gist options
  • Save toulouse/ca501c3621992238ecf6 to your computer and use it in GitHub Desktop.
Save toulouse/ca501c3621992238ecf6 to your computer and use it in GitHub Desktop.
typedef NS_ENUM(NSUInteger, MeasurementsType) {
MeasurementsTypeUnknown,
MeasurementsTypePhone,
MeasurementsTypePhoneTall,
MeasurementsTypePadPortrait,
MeasurementsTypePadLandscape
};
struct Measurements {
public:
Measurements() {
}
void initialize(MeasurementsType measurementsType) {
NSCAssert(!_initialized, @"Already initialized");
switch (measurementsType) {
case MeasurementsTypePhone:
base();
basePhone();
phone();
break;
case MeasurementsTypePhoneTall:
base();
basePhone();
phoneTall();
break;
case MeasurementsTypePadPortrait:
base();
basePad();
padPortrait();
break;
case MeasurementsTypePadLandscape:
base();
basePad();
padLandscape();
break;
case MeasurementsTypeUnknown:
default:
NSCAssert(NO, @"Unknown device");
break;
}
_initialized = YES;
}
static MeasurementsType _getMeasurementsType(BOOL portrait) {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
return portrait ? MeasurementsTypePadPortrait : MeasurementsTypePadLandscape;
} else if (/* logic goes here */) {
return MeasurementsTypePhoneTall;
} else if (/* logic goes here */) {
return MeasurementsTypePhone;
} else {
return MeasurementsTypeUnknown
}
}
private:
BOOL _initialized;
protected:
virtual void base() {}
virtual void basePhone() {}
virtual void basePad() {}
virtual void phone () {}
virtual void phoneTall() {}
virtual void padPortrait() {}
virtual void padLandscape() {}
};
template <typename T> static T GetMeasurements(BOOL isPortrait) {
T *measurements = new T();
measurements->initialize(Measurements::_getMeasurementsType(isPortrait));
const T constMeasurements = *measurements;
delete measurements;
return constMeasurements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment