CS345 – Event-driven Programming Dr. Randy Ribler
Who’s in charge here? The Computer The User Many text-based console applications Computer says: “Enter username: “ Choices Enter a username Stop the program The User Event-driven/Graphical applications Computer responds to what the user does (events): Click a button Type a command Drag an icon
Event-driven Programming Desktop Programming Windows Programming X Window Programming (linux) Mobile Programming Android Programming iPhone Programming Web Programming ASP.NET JavaScript
Events (Messages) Mouse Clicks Mouse Movements Keyboard events (a character is pressed) Timer events (timer expires) Windows Events (e.g., a window gets/loses focus) System events (e.g., system shutdown)
Events are Described in Messages Messages have a unique identifier (message number) Message can carry parameters – for example: Cursor coordinates on a mouse click The object that caused the message to be sent The character that was typed The mouse button that was pressed
Messages Enter a Message Queue Third Message Second Message First Message Are Processed by a Message Loop
Message Loop While the program is still running (no quit message) Read a message from the message queue Decode the message Invoke the functions that have been associated with that message
Associating Functions with Messages C/C++ Function Pointers Point to functions called “callbacks” C# Delegates Safer types of function pointers
Function Pointers in C++ Functions that return pointers A function that returns a char* char* functionThatReturnsAPointer(int x); Pointers to functions A pointer to a function that returns a char* char* (*ptrName)(int x);
Memory Management in C++ Automatic variables are allocated on the run-time stack class Car { private: int year_; float mileage_; }; int x = 10; Car myCar; myCar.mileage_ 112000 myCar.year_ 2001 x 10 Return Address 0xabcdef04
Dynamically Allocated Memory in C++ Dynamically allocated memory in C++ is allocated from the “heap.” int x = 10; Car myCar; Car* ptr = new Car(); ptr 0x12341234 myCar.mileage_ 112000 myCar.year_ 2001 x 10 Return Address 0xabcdef04 The memory that stores the data member for the dynamically allocated car comes from the “heap,” and must be explicitly deallocated.
The Stack and the HeaP <allocated> <unallocated> ptr 0x12341234 myCar.mileage_ 112000 myCar.year_ 2001 x 10 Return Address 0xabcdef04
.Net Introduces “Managed Memory” Managed Memory is memory that is “Garbage Collected” Garbage Collected Memory is allocated with new, but does not need to be explicitly deallocated. A separate heap is maintained for garbage collected memory. A special kind of pointer is used to point to garbage collected memory. These pointers are called “handles.” The Garbage Collection Process Periodically, mark all the memory that is referenced by handles. All other memory on the garbage collected heap is deallocated. Memory in the garbage collected heap might be “compacted” to make all the allocated memory contiguous. This might require changing handle values.
Handles in C++/CLI There are two class types in C++/CLI Conventional Classes Can be automatically allocated on the stack Can be dynamically allocated from the standard heap using “new” Reference Classes Can only be allocated with gcnew Use handles to point to them Use garbage-collected memory to store them.
Conventional class in C++/CLI class Car { // : public: int myPublicDataMember_; }; int main() Car myCar; // myCar.myPublicDataMember is on the stack. Car* ptr = new Car(); // ptr->myPublicDataMember is on the heap. }
Reference class in C++/CLI Ref class RCar { // : public: int myPublicDataMember_; }; int main() Rcar^ myCar; // myCar.myPublicDataMember does not exist! myCar= gcnew Car(); // myCar->myPublicDataMember is on the gc heap. }