Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 98697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:14:44+00:00 2026-05-11T00:14:44+00:00

Say you were designing a C++ windowing library. It may or may not provide

  • 0

Say you were designing a C++ windowing library. It may or may not provide a callback API, but needs to provide a polling API to facilitate a functional style of programming.

What would the polling API look like?

Some options

SDL style

struct Event {     enum { MousePress, KeyPress } type;     union {         struct { Point pos; MouseButton b; } mousePress;         struct { Modifiers mods; char key; } keyPress;     }; }; void userCode() {     for(;;) {         Event e; if(pollEvent(&e)) {             switch(e.type) {                 case MousePress: cout<<event.mousePress.pos.x; break; // not typesafe                 case KeyPress: cout<<event.keyPress.key; break;             }         }     } } 

State style

struct Input {     enum { Mouse, Keyboard, Nothing } whatChanged;     MouseButtonsBitfield pressedButtons;     bool keysPressed[keyCount]; }; void userCode() {     for(;;) {         Input in = pollInput();         switch(in.whatChanged) {             // typesafe yay             case Mouse: cout << 'is LMB pressed? ' << bool(in.pressedButtons&LeftButton); break;             case Keyboard: cout << 'is A pressed? ' << in.keysPressed['A']; break;         }     } } 

Fun functional pseudo-C++ style

struct Event {     // transforms listener by notifying it of event,     // returns transormed listener. nondestructive.     template<class Listener> // sadly invalid, templates can't be virtual.                                               // a solution is to make Listener the base                                               // of a hierarchy and make Listener::handle virtual                                               // but then we're forced to use imperative style     virtual Listener transform(Listener const&) =0; }; struct MousePress : Event { // yay we're extensible via inheritance     template<class Listener>     virtual Listener transform(Listener const& listener) {         return listener.handle(*this); // calls the MousePress overload     }     Point pos; MouseButton b; }; struct KeyPress : Event {     template<class Listener>     virtual Listener transform(Listener const& listener) {         return listener.handle(*this); // calls the KeyPress overload     }     Modifiers mods; char key; }; struct NoEvent : Event {     template<class Listener>     virtual Listener transform(Listener const& listener) {         return listener.handle(*this);     } }; struct UserWidget {     UserWidget handle(NoEvent) {         return UserWidget();     }     UserWidget handle(MousePress p) {         return (UserWidget) { string('pressed at')+lex_cast<string>(p.pos)) };     }     UserWidget handle(KeyPress k) {         return (UserWidget) { string('pressed key=')+lex_cast<string>(k.key)) };     }     string pendingOutput; }; void userTick(UserWidget const& w) {     cout<<w.pendingOutput;     userTick(pollEvent().transform(w)); } void userCode() {     userTick(UserWidget()); } 

Answers for other languages than C++ are OK, if they provide interesting insight.

No comments on encapsulation please – yes public fields should really be accessors, i left that out for clarity.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:14:45+00:00Added an answer on May 11, 2026 at 12:14 am

    To answer your question quickly, I prefer the simplicity of the ‘SDL-style code’. Mainly because your slightly more complicated ‘State Style’ wastes memory and buys you absolutely nothing (see below), and the recursion in your tortured ‘Functional pseudo-C++’ style will overflow the stack within a few milliseconds.

    ‘State Style’: Your ‘typesafe yay’ in the ‘State Style’ code is a bit unwarranted. You are still deciding which member to access based on a switch on another member, so the code has all the same weaknesses that the ‘SDL Style’ code has — for any mistake that you could make with the SDL-style code that leads to interpreting memory as the wrong type, you would make the equally bad mistake of accessing an uninitialised member with the State-style code.

    ‘Functional pseudo-C++ style’: Now you’re getting somewhere, inheriting different event types from a base event type. Obviously the silly recursion needs to become a loop, and there are a few little things to tidy up (I think your 3 methods named transform() in UserWidget want to be called handle(); I’m guessing that you can resolve the problem of no template virtual methods using Boost.Function or similar). I think this approach has potential, though I prefer the simplicity of SDL style.

    But more fundamentally: I question the need for a polling interface. Is there a reason why pollEvent() cannot block? As it stands, all 3 code segments are burning CPU time doing nothing 99.99% of the time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 59k
  • Answers 59k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Yes, go to http://localhost:8000/_ah/admin (supposing you're running your local instance… May 11, 2026 at 9:02 am
  • added an answer Check that if your UpdatePanel - updatemode is set to… May 11, 2026 at 9:01 am
  • added an answer You need a proper value for the following php.ini settings:… May 11, 2026 at 9:01 am

Related Questions

Say you were designing a C++ windowing library. It may or may not provide
Say you were mainly a C-syntax like programmer and Linux systems administrator, and you
Say you have an application divided into 3-tiers: GUI, business logic, and data access.
Say you have 2 database servers, one database is the 'master' database where all
Say you want a simple maze on an N by M grid, with one
Say you have a shipment. It needs to go from point A to point
Say you've got a credit card number with an expiration date of 05/08 -
Say you want to generate a matched list of identifiers and strings enum {
Say you have several webparts, one as a controller and several which take information
Say you're coding some kind of web application. Something where people can contribute content,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.