[gmpi] Re: Linux C++ question

  • To: <gmpi@xxxxxxxxxxxxx>
  • Date: Wed, 23 Feb 2005 16:24:06 -0500

A few more stylistic nits:

[1] For in arguments, I like to use const references.  Pointers are
reserved for out.  That gives a very handy way of knowing, at the call
site, what might change and what might not.  So:

GMPI_Factory::CreateInstance( const GMPI_GUID& iid, ... )

Note that a reference is the same as a pointer in C, or at least that's
how they do it in COM.  They declare the interface as const GUID*, but
the impl can be const GUID&.

Another nit:  when comparing values in an 'if', put the constant value
on the left side, not the right. So not this:
        if (foo == 0)
but this:
        if (0 == foo)

Why?  If you accidentally type '=' instead of '==' the former can get
you in trouble if your compiler isn't strict.  The latter will always
complain.

Lastly, I still like to use NULL instead of 0 for pointers.  I know in
C++ you can use 0, but it's an old habit that helps reinforce that you
are dealing with a pointer.

By the way, those coding style survey results... are pretty
inconclusive.  There really isn't a clear majority of one style vs.
another.  I'm not sure what there is to tablulate.

PPS: I'm leaving tomorrow for vacation, back in the office next
Wednesday.
 

-----Original Message-----
From: gmpi-bounce@xxxxxxxxxxxxx [mailto:gmpi-bounce@xxxxxxxxxxxxx] On
Behalf Of Ron Kuper
Sent: Wednesday, February 23, 2005 4:17 PM
To: gmpi@xxxxxxxxxxxxx
Subject: [gmpi] Re: Linux C++ question

At what point in the exit sequence does it crash?  Where in the DLL
specifically?

Also, could plugin_list be a map instead?  That would avoid having to
iterate to lookup.

Lastly, is plugin_list a member of class GMPI_Factory?  Pretty please,
m_plugin_list? :-) 

-----Original Message-----
From: gmpi-bounce@xxxxxxxxxxxxx [mailto:gmpi-bounce@xxxxxxxxxxxxx] On
Behalf Of Jeff McClintock
Sent: Wednesday, February 23, 2005 4:14 PM
To: gmpi@xxxxxxxxxxxxx
Subject: [gmpi] Linux C++ question

A question for someone familier (very familier) with C++ on Linux.

A GMPI dll may contain several plugins. In the GMPI prototype.  An 
object called the factory maintains a list.  There is only ever one 
factory.  I used a singleton to implement such.  However Tim reports 
that it crashes while unloading the dll.  Any help appreciated.

details:

The technique comes from "Modern C++ Design - Andrei Alexandrescu", 
author of the Loki library.

This is the essential code.  Ignore the GMPIness of this...If you want 
to see the entire file, let me know.

/* factory is a singleton, this retrieves a pointer to it */
GMPI_Factory *Factory(void)
{
        static GMPI_Factory the_factory;
        return &the_factory;
}

/* register a plugin with this factory */
GMPI_RESULT GMPI_Factory::RegisterPlugin( const GMPI_GUID *iid, 
PLUGIN_CREATE_FUNCTION create_function  )
{
        plugin_list.push_back( pair<const GMPI_GUID 
*,PLUGIN_CREATE_FUNCTION>(iid, create_function) );
        return GMPI_SUCCESS;
}

/* Instantiate a GMPI_Plugin */
GMPI_RESULT GMPI_Factory::CreateInstance( GMPI_GUID *plugin_id, void 
**object )
{
        /* search plugin list for requested type */
        for( list< pair<const GMPI_GUID *,PLUGIN_CREATE_FUNCTION>
>::iterator 
it = plugin_list.begin() ; it != plugin_list.end() ; it++ )
        {
                if( GIMPI_IsEqualIID( plugin_id,  (*it).first ) )
                {
                        /* call plugin create function */
                        GMPI_Plugin *plugin = ((*it).second)();

                        if (plugin == 0)
                        {
                                *object = 0;
                                return GMPI_FAIL;// E_OUTOFMEMORY;
                        }

                    return plugin->QueryInterface( (GMPI_GUID *)
&IID_GMPI_IUnknown , 
object );
                }
        }

        /* couln't find requested type, return error */
        *object = 0;
        return GMPI_FAIL;
}


----------------------------------------------------------------------
Generalized Music Plugin Interface (GMPI) public discussion list
Participation in this list is contingent upon your abiding by the
following rules:  Please stay on topic.  You are responsible for your
own
words.  Please respect your fellow subscribers.  Please do not
redistribute anyone else's words without their permission.

Archive: //www.freelists.org/archives/gmpi
Email gmpi-request@xxxxxxxxxxxxx w/ subject "unsubscribe" to unsubscribe


----------------------------------------------------------------------
Generalized Music Plugin Interface (GMPI) public discussion list
Participation in this list is contingent upon your abiding by the
following rules:  Please stay on topic.  You are responsible for your
own
words.  Please respect your fellow subscribers.  Please do not
redistribute anyone else's words without their permission.

Archive: //www.freelists.org/archives/gmpi
Email gmpi-request@xxxxxxxxxxxxx w/ subject "unsubscribe" to unsubscribe


----------------------------------------------------------------------
Generalized Music Plugin Interface (GMPI) public discussion list
Participation in this list is contingent upon your abiding by the
following rules:  Please stay on topic.  You are responsible for your own
words.  Please respect your fellow subscribers.  Please do not
redistribute anyone else's words without their permission.

Archive: //www.freelists.org/archives/gmpi
Email gmpi-request@xxxxxxxxxxxxx w/ subject "unsubscribe" to unsubscribe

Other related posts: