A Reusable Duff Device
#define DUFF_DEVICE_8(aCount, aAction) \
do { \
int count_ = (aCount); \
int times_ = (count_ + 7) >> 3; \
switch (count_ & 7){ \
case 0: do { aAction; \
case 7: aAction; \
case 6: aAction; \
case 5: aAction; \
case 4: aAction; \
case 3: aAction; \
case 2: aAction; \
case 1: aAction; \
} while (--times_ > 0); \
} \
} while (0)
Or just use template and lambda:
#include <iostream>
template<typename INPUT>
void duffDevice(INPUT process, int count)
{
int n = (count + 7) >> 3;
switch(count % 8)
{
case 0: do { process();
case 7: process();
case 6: process();
case 5: process();
case 4: process();
case 3: process();
case 2: process();
case 1: process();
} while(--n >0);
};
}
int main()
{
duffDevice([]{std::cout << "poll data" << std::endl;}, 108);
}
Reference:
Macro Note
Clifford's Device
GOTO statement
Switch statement
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.