SmartProxy.h

00001 /*---------------------------------------------------------------------------*
00002  *  SmartProxy.h                                                             *
00003  *                                                                           *
00004  *  Copyright 2007 Nuance Communciations, Inc.                               *
00005  *                                                                           *
00006  *  Licensed under the Apache License, Version 2.0 (the 'License');          *
00007  *  you may not use this file except in compliance with the License.         *
00008  *                                                                           *
00009  *  You may obtain a copy of the License at                                  *
00010  *      http://www.apache.org/licenses/LICENSE-2.0                           *
00011  *                                                                           *
00012  *  Unless required by applicable law or agreed to in writing, software      *
00013  *  distributed under the License is distributed on an 'AS IS' BASIS,        *
00014  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
00015  *  See the License for the specific language governing permissions and      *
00016  *  limitations under the License.                                           *
00017  *                                                                           *
00018  *---------------------------------------------------------------------------*/
00019 
00020 #ifndef __UAPI__SMART_PROXY
00021 #define __UAPI__SMART_PROXY
00022 
00023 #include "exports.h"
00024 #include "types.h"
00025 #include "RefCounter.h"
00026 
00027 namespace android
00028 {
00029   namespace speech
00030   {
00031     namespace recognition
00032     {
00033       namespace utilities
00034       {
00035         class Mutex;
00036       }
00037     }
00038   }
00039 }
00040 
00041 namespace android
00042 {
00043   namespace speech
00044   {
00045     namespace recognition
00046     {
00047       class System;
00048       
00089       class SmartProxy
00090       {
00091         public:
00097         class Root: private utilities::RefCounter
00098           {
00106               Root(void* object, bool loggingAllowed, const char* name, ReturnCode::Type& returnCode);
00107               virtual ~Root();
00108 #ifdef UAPI_MT //multi threaded
00109 
00112               UAPI_EXPORT utilities::Mutex* getMutex() const;
00113               
00117               utilities::Mutex* mutex;
00118 #endif
00119               
00125               bool registeredWithSystem;
00126               char* name;
00127               
00128               friend class SmartProxy;
00129               friend class System;
00130           };
00131           
00137           UAPI_EXPORT virtual Root* getRoot() const;
00138           
00146           UAPI_EXPORT virtual SmartProxy& operator=(const SmartProxy& other);
00147           
00153           typedef void (*BoolConversion)();
00154           UAPI_EXPORT operator BoolConversion() const;
00155           
00161           UAPI_EXPORT virtual bool operator!() const;
00162         protected:
00169           UAPI_EXPORT explicit SmartProxy(void* object, const char* name);
00170           
00178           UAPI_EXPORT explicit SmartProxy(void* object, bool loggingAllowed, const char* name);
00179           
00186           UAPI_EXPORT explicit SmartProxy(Root* root);
00187           
00192           UAPI_EXPORT SmartProxy(const SmartProxy& other);
00193           
00197           UAPI_EXPORT SmartProxy();
00198           
00202           UAPI_EXPORT virtual ~SmartProxy();
00203           
00210           UAPI_EXPORT virtual void deleteObject(void* object);
00211           
00217           UAPI_EXPORT void* getObject() const;
00218           
00222           UAPI_EXPORT virtual void onDestruction();
00223         private:
00227           utilities::RefCounter& operator*();
00228           
00229           
00233           Root* root;
00234 
00235           friend class System;
00236       };
00237     }
00238   }
00239 }
00240 
00241 #define DECLARE_SMARTPROXY(ExportSymbol, Proxy, SuperProxy, Interface) \
00242   class Proxy: public SuperProxy \
00243   { \
00244     public: \
00245       /* Creates a root SmartProxy for the specified pointer */ \
00246       ExportSymbol explicit Proxy(Interface* object); \
00247       /* Creates a root SmartProxy for the specified pointer */ \
00248       ExportSymbol explicit Proxy(Interface* object, bool loggingAllowed); \
00249       /* Constructs a new SmartProxy from an existing root proxy */ \
00250       ExportSymbol explicit Proxy(Root* root); \
00251       /* Constructs a copy of an existing SmartProxy */ \
00252       ExportSymbol Proxy(const Proxy& other); \
00253       /* Enables the construction of arrays of proxies */ \
00254       ExportSymbol Proxy(); \
00255       /* Unsafe cast to a subclass type */ \
00256       ExportSymbol explicit Proxy(const SmartProxy& other); \
00257       /* Delegate method invocation to underlying object */ \
00258       ExportSymbol Interface* operator->() const; \
00259       /* Destroys the proxy */ \
00260       ExportSymbol virtual ~Proxy(); \
00261     protected: \
00262       /* deletes the underlying object */ \
00263       virtual void deleteObject(void* object); \
00264       /* Creates a root SmartProxy for the specified pointer */ \
00265       ExportSymbol explicit Proxy(Interface* object, const char* name); \
00266       /* Creates a root SmartProxy for the specified pointer */ \
00267       ExportSymbol explicit Proxy(Interface* object, bool loggingAllowed, const char* name); \
00268   };
00269 
00270 #define DEFINE_SMARTPROXY(Namespace, Proxy, SuperProxy, Interface) \
00271   Namespace::Proxy::Proxy(Interface* object): \
00272       SuperProxy(object, #Proxy) \
00273   {} \
00274   \
00275   Namespace::Proxy::Proxy(Interface* object, const char* name): \
00276       SuperProxy(object, name) \
00277   {} \
00278   \
00279   Namespace::Proxy::Proxy(Interface* object, bool loggingAllowed): \
00280       SuperProxy(object, loggingAllowed, #Proxy) \
00281   {} \
00282   \
00283   Namespace::Proxy::Proxy(Interface* object, bool loggingAllowed, const char* name): \
00284       SuperProxy(object, loggingAllowed, name) \
00285   {} \
00286   \
00287   Namespace::Proxy::Proxy(Root* root): \
00288       SuperProxy(root) \
00289   {} \
00290   \
00291   Namespace::Proxy::Proxy(const Namespace::Proxy& other): \
00292       SuperProxy(other) \
00293   {} \
00294   \
00295   Namespace::Proxy::Proxy(const SmartProxy& other): \
00296       SuperProxy(other) \
00297   {} \
00298   \
00299   Namespace::Proxy::Proxy() \
00300   {} \
00301   \
00302   Interface* Namespace::Proxy::operator->() const \
00303   { \
00304     return (Interface*) getObject(); \
00305   } \
00306   \
00307   void Namespace::Proxy::deleteObject(void* object) \
00308   { \
00309     delete (Interface*) object; \
00310   } \
00311   Namespace::Proxy::~Proxy() \
00312   { \
00313     onDestruction(); \
00314   }
00315 
00316 #endif

Generated on Thu May 1 17:16:37 2008 for UAPI by  doxygen 1.5.3