1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
Copyright (c) 2001 PocketPyro, Inc.
All rights reserved.
This file is part of the Palm OS Emulator.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
\* ===================================================================== */
#ifndef EcmObject_h
#define EcmObject_h
#include "EcmIf.h"
/*
This file define a concrete implementation of the IEcmComponent
interface, as defined in EcmIf.h. IEcmComponent describes an object
with RequestInterface, Refer, and Release functions. EcmObject
provides implementations of those functions that find the requested
interface, increment a refcount, and decrement a refcount (deleting
the object if necessary), respectively.
RequestInterface works by making use of the FindInterface helper
function. FindInterface is implemented by all EcmObject subclasses
to cast itself to the right type and return the casted pointer if
the subclass supports the requested interface.
In order to make the implementation of FindInterface in all subclasses
simple and consistant, three macros are provided: ECM_CLASS_IF_LIST_BEGIN,
ECM_CLASS_IF, and ECM_CLASS_IF_LIST_END. The first macro introduces
a list of interfaces a particular class supports, the second macro is
used once for each interface supported, and the final macro cleans up
the list. In all, the three macros as used as follows in the class
declaration of the class implementing an interface:
class EmImplementation : public EcmObject,
ecm_implements IEmBaseInterface1,
ecm_implements IEmBaseInterface2
{
public:
ECM_CLASS_IF_LIST_BEGIN(EmImplementation, EcmObject)
ECM_CLASS_IF(kBaseIfn1, IEmBaseInterface1)
ECM_CLASS_IF(kBaseIfn2, IEmBaseInterface2)
ECM_CLASS_IF_LIST_END(EmImplementation, EcmObject)
...
};
*/
// ===========================================================================
//
// Extended Component Model (ECM) -- Base Class
//
// We're adding support for component interfaces because
// exporting c++ classes is a real pain from loadable libraries
// (at least Windows DLLs), Anyway, component design using interfaces
// works really well for "plugin" type designs.
//
// In our implementation, an interface is a c++ abstract base class.
// ===========================================================================
#ifdef _MSC_VER
#pragma warning( disable : 4250 )
// 'class1' : inherits 'class2::member' via dominance
//
// There were two or more members with the same name. The one in class2
// was inherited since it was a base class for the other classes that
// contained this member.
#endif
class EcmObject :
ecm_implements IEcmComponent
{
public:
EcmObject() : fRefCount(0)
{
}
//This forms the root of the interface request implementation:
// Derived classes will implement the same function, then a request for an interface will move up
// through the ranks, until a match has been found.
//
virtual EcmErr FindInterface(const EcmIfName &name, void **iPP)
{
if (iPP == NULL)
return kEcmErrInvalidParameter;
if (name == kEcmComponentIfn)
{
*iPP = (void *) static_cast<IEcmComponent *>(this);
return kEcmErrNone;
}
//Interface never found:
return kEcmErrInvalidIfName;
}
/***********************************************************************
*
* FUNCTION: RequestInterface
*
* DESCRIPTION: Requests an interface of type EcmIfName from the component
*
* PARAMETERS: name [IN ] Name of the interface being requested.
* iPP [OUT ] Interface to the event being sent.
*
* RETURNED: kEcmErrNone
* kEcmErrInvalidName
*
*
***********************************************************************/
virtual EcmErr RequestInterface(const EcmIfName &name, void **iPP)
{
EcmErr err = FindInterface(name, iPP);
if (err == kEcmErrNone)
{
Refer();
}
return err;
}
/***********************************************************************
*
* FUNCTION: Refer
*
* DESCRIPTION: Called before handing an interface pointer off to another
* "piece of code", adding an addition owner. This is used to
* maintain the reference count so the component can be destroyed
* at the appropriate time.
*
* PARAMETERS: none
*
* RETURNED: kEcmErrNone
*
*
***********************************************************************/
virtual EcmErr Refer()
{
fRefCount++;
return kEcmErrNone;
}
/***********************************************************************
*
* FUNCTION: Release
*
* DESCRIPTION: Releases the interface from a reference, when the reference count is 0,
* the component can be destroyed.
*
* PARAMETERS: none
*
* RETURNED: kEcmErrNone
*
*
***********************************************************************/
virtual EcmErr Release()
{
fRefCount--;
if (fRefCount == 0)
delete this;
return kEcmErrNone;
}
protected:
unsigned long fRefCount;
};
#endif // EcmObject_h
|