How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 ....

61
How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012

Transcript of How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 ....

Page 1: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

How I Code and Why Tony Van Eerd, Research In Motion

May 17, 2012

Page 2: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

How do You Code and Why? Tony Van Eerd, Research In Motion

May 17, 2012

Page 3: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

Examples That Stick/Stuck Tony Van Eerd, Research In Motion

May 17, 2012

Page 4: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

BoostCon/C++Now (2013?)

C++ Solution Station (?)

Page 5: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

www.bobdevol.com

Page 6: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

Single Responsibility Principle

Open/Closed Principle

Liskov Substitution Principle

Interface Segregation Principle

Dependency Inversion Principle

Page 7: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

8 May 17, 2012

“Thanks”

P.S. github.com/blackberry/Boost

Page 8: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

{ // // reads a tga, writes out a tga with the image copied 4 times across and 4 times down (4x4) ie 16 times. // if (argc < 3 || argc > 5) { return -1; } char const * intga = argv[1]; char const * outtga = argv[2]; int replicateX = argc >= 4 ? atoi(argv[3]) : 4; int replicateY = argc >= 5 ? atoi(argv[4]) : replicateX; TGAFileReader in(intga); static const int pixelSize = 4; // bytes per pixel - ie 32bpp //static const int replicate = 4; // 4 x 4 int dstWidth = in.getWidth() * replicateX; int dstHeight = in.getHeight() * replicateY; // final height, not height of the dst buffer! // MUST do Bassamatic BEFORE Splunker bassamatic_init(); splunker_init(); char * dst = new char[dstWidth * in.getHeight() * pixelSize]; // buffer only needs to be sourceHeight high, and we will reuse 4 times char * dstStart = dst; int sourceLineByteLength = in.getWidth() * pixelSize; // read in image, replicating it across into 4 copies for (int y = 0; y < in.getHeight(); y++) { in.readLine(dst); // copy that line across 3 times, so we have it 4 times as wide for (int r = 1; r <= replicateX; r++) { std::memcpy(dst + r * sourceLineByteLength, dst, sourceLineByteLength); } dst += replicateX * sourceLineByteLength; } // now it is copied 4 times across, but still only 1x high if (in.isUpsideDown()) { TGAFileFormat::flip_vert(dstStart, dstWidth, in.getHeight()); } // now write out the 4x wide 4 times TGAFileWriter out(outtga, dstWidth, dstHeight); for (int z = 0; z < replicateY; z++) { out.writeLines(in.getHeight(), dstStart); }

Page 9: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

10 May 17, 2012

Page 10: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

{ // // reads a tga, writes out a tga with the image copied 4 times across and 4 times down (4x4) ie 16 times. // if (argc < 3 || argc > 5) { return -1; } char const * intga = argv[1]; char const * outtga = argv[2]; int replicateX = argc >= 4 ? atoi(argv[3]) : 4; int replicateY = argc >= 5 ? atoi(argv[4]) : replicateX; TGAFileReader in(intga); static const int pixelSize = 4; // bytes per pixel - ie 32bpp //static const int replicate = 4; // 4 x 4 int dstWidth = in.getWidth() * replicateX; int dstHeight = in.getHeight() * replicateY; // final height, not height of the dst buffer! // MUST do Bassamatic BEFORE Splunker bassamatic_init(); splunker_init(); char * dst = new char[dstWidth * in.getHeight() * pixelSize]; // buffer only needs to be sourceHeight high, and we will reuse 4 times char * dstStart = dst; int sourceLineByteLength = in.getWidth() * pixelSize; // read in image, replicating it across into 4 copies for (int y = 0; y < in.getHeight(); y++) { in.readLine(dst); // copy that line across 3 times, so we have it 4 times as wide for (int r = 1; r <= replicateX; r++) { std::memcpy(dst + r * sourceLineByteLength, dst, sourceLineByteLength); } dst += replicateX * sourceLineByteLength; } // now it is copied 4 times across, but still only 1x high if (in.isUpsideDown()) { TGAFileFormat::flip_vert(dstStart, dstWidth, in.getHeight()); } // now write out the 4x wide 4 times TGAFileWriter out(outtga, dstWidth, dstHeight); for (int z = 0; z < replicateY; z++) { out.writeLines(in.getHeight(), dstStart); }

Page 11: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

{ // // reads a tga, writes out a tga with the image copied 4 times across and 4 times down (4x4) ie 16 times. // if (argc < 3 || argc > 5) { return -1; } char const * intga = argv[1]; char const * outtga = argv[2]; int replicateX = argc >= 4 ? atoi(argv[3]) : 4; int replicateY = argc >= 5 ? atoi(argv[4]) : replicateX; TGAFileReader in(intga); static const int pixelSize = 4; // bytes per pixel - ie 32bpp //static const int replicate = 4; // 4 x 4 int dstWidth = in.getWidth() * replicateX; int dstHeight = in.getHeight() * replicateY; // final height, not height of the dst buffer!

// MUST do Bassamatic BEFORE Splunker bassamatic_init(); splunker_init(); char * dst = new char[dstWidth * in.getHeight() * pixelSize]; // buffer only needs to be sourceHeight high, and we will reuse 4 times char * dstStart = dst; int sourceLineByteLength = in.getWidth() * pixelSize; // read in image, replicating it across into 4 copies for (int y = 0; y < in.getHeight(); y++) { in.readLine(dst); // copy that line across 3 times, so we have it 4 times as wide for (int r = 1; r <= replicateX; r++) { std::memcpy(dst + r * sourceLineByteLength, dst, sourceLineByteLength); } dst += replicateX * sourceLineByteLength; } // now it is copied 4 times across, but still only 1x high if (in.isUpsideDown()) { TGAFileFormat::flip_vert(dstStart, dstWidth, in.getHeight()); } // now write out the 4x wide 4 times TGAFileWriter out(outtga, dstWidth, dstHeight); for (int z = 0; z < replicateY; z++)

Page 12: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

{ // // reads a tga, writes out a tga with the image copied 4 times across and 4 times down (4x4) ie 16 times. // if (argc < 3 || argc > 5) { return -1; } char const * intga = argv[1]; char const * outtga = argv[2]; int replicateX = argc >= 4 ? atoi(argv[3]) : 4; int replicateY = argc >= 5 ? atoi(argv[4]) : replicateX; TGAFileReader in(intga); static const int pixelSize = 4; // bytes per pixel - ie 32bpp //static const int replicate = 4; // 4 x 4 int dstWidth = in.getWidth() * replicateX; int dstHeight = in.getHeight() * replicateY; // final height, not height of the dst buffer!

// MUST do Bassamatic BEFORE Splunker // *otherwise* the splunker table… bassamatic_init(); splunker_init(); char * dst = new char[dstWidth * in.getHeight() * pixelSize]; // buffer only needs to be sourceHeight high, and we will reuse 4 times char * dstStart = dst; int sourceLineByteLength = in.getWidth() * pixelSize; // read in image, replicating it across into 4 copies for (int y = 0; y < in.getHeight(); y++) { in.readLine(dst); // copy that line across 3 times, so we have it 4 times as wide for (int r = 1; r <= replicateX; r++) { std::memcpy(dst + r * sourceLineByteLength, dst, sourceLineByteLength); } dst += replicateX * sourceLineByteLength; } // now it is copied 4 times across, but still only 1x high if (in.isUpsideDown()) { TGAFileFormat::flip_vert(dstStart, dstWidth, in.getHeight()); } // now write out the 4x wide 4 times TGAFileWriter out(outtga, dstWidth, dstHeight);

Page 13: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

My favourite comment word is

Otherwise.

Thus…

Page 14: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

case DOWN: ... break; case MOVE: // disable popup menu for this touch sequence, // *otherwise* if we got a HOVER later (user stopped moving for a while) // then we would bring up the Menu, // and the UX team says we don't want the popup menu to happen after a MOVE // (ie scroll then pause should not bring up the menu) _disablePopupMenu = true; ... break; case HOVER: if ( !_disablePopupMenu) { showPopupMenu(); } break; case UP: _disablePopupMenu = false; // reset ... break;

Page 15: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

case DOWN: ... break; case MOVE: // disable popup menu for this touch sequence, // *otherwise* if we got a HOVER later (user stopped moving for a while) // then we would bring up the Menu, // and the UX team says we don't want the popup menu to happen after a MOVE // (ie scroll then pause should not bring up the menu) _movedSinceDown = true; ... break; case HOVER: if ( !_movedSinceDown) { showPopupMenu(); } break; case UP: _movedSinceDown = false; // reset ... break;

Page 16: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

case DOWN: ... break; case MOVE: // disable popup menu for this touch sequence, // *otherwise* if we got a HOVER later (user stopped moving for a while) // then we would bring up the Menu, // and the UX team says we don't want the popup menu to happen after a MOVE // (ie scroll then pause should not bring up the menu) _disablePopupMenu = true; ... break; case HOVER: if ( !_disablePopupMenu) { showPopupMenu(); } break; case UP: _disablePopupMenu = false; // reset ... break;

Page 17: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

case DOWN: ... break; case MOVE: // disable popup menu for this touch sequence, // *otherwise* if we got a HOVER later (user stopped moving for a while) // then we would bring up the Menu, // and the UX team says we don't want the popup menu to happen after a MOVE // (ie scroll then pause should not bring up the menu) _movedSinceDown = true; ... break; case HOVER: if ( !_movedSinceDown) { showPopupMenu(); } break; case UP: _movedSinceDown = false; // reset ... break;

Page 18: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

case DOWN: ... break; case MOVE: // disable popup menu for this touch sequence, // *otherwise* if we got a HOVER later (user stopped moving for a while) // then we would bring up the Menu, // and the UX team says we don't want the popup menu to happen after a MOVE // (ie scroll then pause should not bring up the menu) _disablePopupMenu = true; ... break; case HOVER: if ( !_disablePopupMenu) { showPopupMenu(); } break; case UP: _disablePopupMenu = false; // reset ... break;

Page 19: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

case DOWN: ... break; case MOVE: // disable popup menu for this touch sequence, // *otherwise* if we got a HOVER later (user stopped moving for a while) // then we would bring up the Menu, // and the UX team says we don't want the popup menu to happen after a MOVE // (ie scroll then pause should not bring up the menu) _disablePopupMenu = true; ... break; case HOVER: if ( !_disablePopupMenu) { showPopupMenu(); } break; case UP: _disablePopupMenu = false; // reset ... break;

Think about other code that needs to

disable the popup menu.

Does it also set _disablePopupMenu?

or popupMenu.disable()?

who resets it?

Page 20: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

case DOWN: ... break; case MOVE: // disable popup menu for this touch sequence, // *otherwise* if we got a HOVER later (user stopped moving for a while) // then we would bring up the Menu, // and the UX team says we don't want the popup menu to happen after a MOVE // (ie scroll then pause should not bring up the menu) _movedSinceDown = true; ... break; case HOVER: if ( !_movedSinceDown) { showPopupMenu(); } break; case UP: _movedSinceDown = false; // reset ... break;

Alternatively, think about other code

that needs to set _movedSinceDown…

…Hopefully there is none!

Page 21: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

case DOWN: ... break; case MOVE: _movedSinceDown = true; ... break; case HOVER: // the UX team says we don't want the popup menu to happen after a MOVE // (ie scroll then pause should not bring up the menu) if ( !_movedSinceDown) { showPopupMenu(); } break; case UP: _movedSinceDown = false; // reset ... break;

Page 22: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

case DOWN: ... break; case MOVE: break; case HOVER: break; case DOWNHOVER: // or some better name showPopupMenu(); break; case UP: ... break;

Page 23: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

“Separation of Concerns”

Thus…

Page 24: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

if ( !_disablePopupMenu)

Page 25: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

Avoid Double Negatives

Thus…

Page 26: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

Page 27: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

How a Button invokes a „click‟ action:

virtual Button::invokeAction()

virtual Invokeable::invokeAction() // Button : private Invokeable {};

(*invokeAction)(theirdata) // C styles

_listener->invokeAction()

boost::function

“callable” // template<typename F> onClick(F f); // converts to function<> for you

os/framework_sendmessage(destId, buttonId, actionId, etc)

os/framework_postmessage(destId, buttonId, actionId, etc) //**

queue a boost::function to a threaded work queue //**

condvar //**

boost::signal<>, Qt signal, framework signal

member.invokeAction() where Button<T> has a T member.

Base::invokeAction() // template <typename Base> Button : Base {};

invokeAction()

Page 28: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

template <typename ActionFramework> class Button : ActionFramework // use CRTP? { ... void handleInput(...) { if (...decide to invoke...) { invokeAction(); } } };

Page 29: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

Separation of Concerns (?)

Inversion of Everything (?)

Top Down (?)

I don’t care / not my problem

Thus…

Page 30: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

UIFView

CurrentStyleView

UIFImageView

UIFHandleSizeChange

UIFHappyPixelsAutoAnytimeDevice

UIFViewManager

ImageInterface

DeviceUpdater

ViewSettings

SomeoneHandleSizeChange

SimpleImageView

UIFHappyPixels AnytimeDevice

HappyPixelsImage

HappyPixelsAutoDevice

HappyPixelsDevice

GetDCFromUIFView

ViewManager

HappyPixels

UIFHappyPixelsDevice

SomeoneGetMeTheDC

UIFHappyPixels HasaIsaHappyPixels

Page 31: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

Thus…

?!

Page 32: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

Speaking of Buttons…

Page 33: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

class CheckBox { public: bool isChecked() { ... } };

Page 34: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

CheckBox::isChecked()

you‟re doing it wrong.

Sean Parent (paraphrased)

Page 35: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

No raw loops

Speaking of Sean Parent…

Page 36: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

OH NO! Pointers!

How to go from Java to C++…

Page 37: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

OH NO! Pointers!

Oh, No Pointers.

How to go from Java to C++…

Page 38: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

OH NO! Pointers!

Oh, No Pointers.

Value Types.

How to go from Java to C++…

Page 39: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

Value Types /

“shared_ptr is as good as a global”

Speaking of Sean Parent…

Page 40: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

Speaking of Sean Parent…

Value Types /

“shared_ptr considered harmful”

Page 41: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

Speaking of „is‟…

Page 42: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

class LockFreeList { public: bool isEmpty() // or just empty() { ... } };

Page 43: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

{ if (!list.isEmpty()) { Foo foo = list.pop(); ... } };

Page 44: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

class LockFreeList { public: bool wasEmpty() { ... } };

Page 45: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

was not is

in threaded programming.

Thus…

Page 46: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

from not to.

Also…

Page 47: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

Page 48: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

M + N vs M x N

Page 49: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

M + N vs M x N is for Unit Tests

Page 50: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

Examples That Suck Tony Van Eerd, Research In Motion

May 17, 2012

Page 51: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

__try { // guarded code } __except ( expression ) { // exception handler code }

“Structured Exception Handling” (http://msdn.microsoft.com/en-us/library/s58ftw19%28v=vs.80%29.aspx)

MS Windows

Page 52: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

__try { // guarded code } __except ( expression ) { // exception handler code }

“Structured Exception Handling” (http://msdn.microsoft.com/en-us/library/s58ftw19%28v=vs.80%29.aspx)

OS_TRY { // guarded code } OS_CATCH() { // exception handler code }

MS Windows Portable

Page 53: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

class OSCatcher { static atomic<jmp_buf*> _current; jmp_buf _local, *_prev; bool _ok; OSCatcher() : _ok(true) { _prev = _current.exchange(&_local); if (setjmp(_local)) { // example! not thread safe _ok = false; _current = _prev; } } operator bool() { return _ok; } };

Page 54: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

class OSCatcher { static atomic<jmp_buf*> _current; jmp_buf _local, *_prev; bool _ok; OSCatcher() : _ok(true) { _prev = _current.exchange(&_local); if (setjmp(_local)) { // example! not thread safe _ok = false; _current = _prev; } } operator bool() { return _ok; } }; #define OS_TRY if (OSCatcher catcher) #define OS_CATCH() else

Page 55: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

class OSCatcher { static atomic<jmp_buf*> _current; jmp_buf _local, *_prev; bool _ok; OSCatcher() : _ok(true) { _prev = _current.exchange(&_local);; if (setjmp(_local)) { _ok = false; _current = _prev; } } operator bool() { return _ok; } }; #define OS_TRY if (OSCatcher catcher) #define OS_CATCH() else

int main() { signal(SIGINT, sigint_handler); ... } sigint_handler() { longjmp(*OSCatcher::_current, 1); }

Page 56: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

Can != Should.

Thus…

Page 57: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

int func() { static Once once; if (Once::Guard guard(once)) { // init… } ... }

Page 58: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

May 17, 2012

#define once static Once UNIQUE(once); \ if (Once::Guard guard(once)) int func() { once { // init… } ... }

Page 59: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

MACROS are evil

(Mostly unrelated actually…)

Page 60: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

Use Locks

As Always…

Page 61: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples

Experiment

Thank you for participating.

Thus…