Click here to Skip to main content
13,350,861 members (53,425 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 17 Jun 2010

Getting in the RAII

, 17 Jun 2010
Rate this:
Please Sign up or sign in to vote.
Looking at a simple MIDI handle wrapping class

One of Web Biscuit's long-term secret projects is the MidiWrapper, an object-oriented interface for MIDI. A cornerstone of MIDI operations is the HMIDIOUT handle, which requires opening before use, and closing when we're done with it.

This can be done in a RAII class. RAII stands for Resource Acquisition is Initialization, not the snappiest acronym in the whole wide world and not that self explanatory. Basically, we use the guarantee that creating an object always calls a constructor and eventually, a destructor. Played smartly, these two opposing calls can give us automatic Open/Close functionality.

Here's our class:

class CMidiOut
    CMidiOut() {}
    ~CMidiOut() {}
    HMIDIOUT m_hMidiOutHandle;

We could add Open and Close as public members, but there really is no need. We don't want to burden the creator of our object with things they might forget to do. So let's do it for them:

class CMidiOut
    CMidiOut()  :
            &m_hMidiOutHandle,  	// Handle
            MIDI_MAPPER,  		// Default device
            NULL,  		// No callback
            NULL,  		// No callback parameters
            CALLBACK_NULL);  	// Flags
        if(m_hMidiOutHandle != nullptr)
            m_hMidiOutHandle = nullptr;
    HMIDIOUT m_hMidiOutHandle;

This is very cute. For a full solution, you'll want an accessor to your handle, and you'll need to prevent or handle copying of your class. These are all exciting subjects we can discuss next time.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer Web Biscuit
United Kingdom United Kingdom
At Web Biscuit, you can find software, articles, a good dollop of quality and an unhealthy obsession over biscuits.
Twitter Watch:

You may also be interested in...


Comments and Discussions

GeneralRAII is not about initialisation... Pin
Aescleal18-Jun-10 7:58
memberAescleal18-Jun-10 7:58 
GeneralRe: RAII is not about initialisation... Pin
WebBiscuit18-Jun-10 8:10
memberWebBiscuit18-Jun-10 8:10 
The encapsulation of raw API functions is also nice though.

Error handling shall be done via exceptions, with an errorcode -> exception lookup. This will be a post for the future. I intend to write about most phases of design and development I go through, from the ground up really.
GeneralRe: RAII is not about initialisation... Pin
Aescleal18-Jun-10 10:26
memberAescleal18-Jun-10 10:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 17 Jun 2010
Article Copyright 2010 by WebBiscuit
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid