Package.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef _SUPPORT_PACKAGE_H
00014 #define _SUPPORT_PACKAGE_H
00015 
00021 #include <support/KeyedVector.h>
00022 #include <support/Locker.h>
00023 #include <support/String.h>
00024 #include <support/Value.h>
00025 
00026 #if _SUPPORTS_NAMESPACE
00027 namespace palmos {
00028 namespace support {
00029 #endif
00030 
00035 #if (defined(__GNUC__))
00036 #define ATTRIBUTE_VISIBILITY_HIDDEN __attribute__ ((visibility("hidden")))
00037 #else
00038 #define ATTRIBUTE_VISIBILITY_HIDDEN
00039 #endif
00040 
00041 #if TARGET_HOST == TARGET_HOST_PALMOS
00042 #include <CmnLaunchCodes.h>
00043 #include <DataMgr.h>
00044 #else
00045 #define sysPackageLaunchAttachImage             71
00046 #endif
00047 
00048 class BSharedObject;
00049 
00050 struct SysPackageLaunchAttachImageType
00051 {
00052       size_t                  size;
00053       sptr<BSharedObject>     image;
00054 };
00055 
00056 typedef void    (*attach_func)(const sptr<BSharedObject>& image);
00057 
00058 
00059 /*---------------------------------------------------------------*/
00060 /*----- SPackage ------------------------------------------------*/
00063 class SPackage 
00064 {
00065 public:
00066     SPackage();
00067     SPackage(const SString& name);
00068     SPackage(const SValue& value, status_t* err=NULL);
00069     SValue AsValue();
00070 
00071     ~SPackage();
00072 
00073     status_t StatusCheck();
00074     SString Name();
00075     SString Path();
00076             
00077     sptr<IByteInput> OpenResource(const SString& fileName, const SString& locale = SString::EmptyString());
00078     
00079     SString LoadString(const SString& key, const SString& locale = SString::EmptyString());
00080     SString LoadString(uint32_t index, const SString& locale = SString::EmptyString());
00081 
00082 private:
00083     class MemoryMap : public virtual SAtom
00084     {
00085     public:
00086         MemoryMap(const SString& path);
00087 
00088         void* Buffer();
00089         size_t Size();
00090     protected:
00091         ~MemoryMap();
00092 
00093     private:
00094         int m_fd;
00095         void* m_buffer; 
00096         size_t m_length;
00097     };
00098 
00099     class Data : public virtual SAtom
00100     {
00101     public:
00102         Data(const SString& name, const SString& path);
00103         
00104         SString Name();
00105         SString Path();
00106         SString ResourcePath();
00107         
00108         SString LoadString(const SString& key, const SString& locale);
00109         SString LoadString(uint32_t index, const SString& locale);
00110 
00111     protected:
00112         virtual ~Data();
00113 
00114     private:    
00115         char* get_buffer_for(const SString& locale);
00116         
00117         SLocker m_lock;
00118         SString m_name;
00119         SString m_path;
00120         SString m_resourcePath;
00121 
00122         SKeyedVector<SString, sptr<SPackage::MemoryMap> > m_maps;
00123     };
00124 
00125     class Pool
00126     {
00127     public:
00128         Pool();
00129         
00130         sptr<SPackage::Data> DataFor(const SString& pkgName);
00131 
00132     private:
00133         SLocker m_lock;
00134         SKeyedVector<SString, wptr<SPackage::Data> > m_packages;
00135     };
00136 
00137 
00138     static SPackage::Pool g_pool;
00139     sptr<Data> m_data;
00140 };
00141 
00142 extern const SPackage B_NO_PACKAGE;
00143 
00144 
00145 /*---------------------------------------------------------------*/
00146 /*----- BSharedObject -------------------------------------------*/
00148 class BSharedObject : public virtual SAtom
00149 {
00150 public:
00152 
00153                             BSharedObject(const SValue& src, const SPackage &package, attach_func attach = NULL);
00155 
00156             //              BSharedObject(const image_id image);
00157 
00159             image_id        ID() const;
00161             SValue          Source() const;
00164             SString         File() const;
00165 
00167 
00168             status_t        GetSymbolFor(   const char* name,
00169                                             int32_t sclass,
00170                                             void** outPtr) const;
00172 
00173             status_t        GetSymbolAt(    int32_t index,
00174                                             SString* outName,
00175                                             int32_t* outSClass,
00176                                             void** outPtr) const;
00177 
00179 
00182             void            Detach();
00183 
00184             inline SPackage Package() { return m_package; }
00185 
00186 protected:
00187     virtual                 ~BSharedObject();
00188     virtual void            InitAtom();
00189 
00190 private:
00191             void            close_resource();
00192 
00193             const SValue    m_source;
00194             SValue          m_extra;
00195             image_id        m_handle;
00196             attach_func     m_attachFunc;
00197             SPackage        m_package;
00198 
00199 private:
00200 
00201     // copy constructor not supported
00202     BSharedObject( const BSharedObject& rhs );
00203 
00204     // operator= not supported
00205     BSharedObject& operator=( const BSharedObject& rhs );
00206 };
00207 
00208 
00209 /*-----------------------------------------------------------*/
00210 /*----- SPackageSptr ----------------------------------------*/
00212 
00233 class SPackageSptr
00234 {
00235 public:
00237 
00240                                     SPackageSptr()
00241                                      ATTRIBUTE_VISIBILITY_HIDDEN
00242                                      ;
00244 
00246     inline                          ~SPackageSptr()
00247                                      ATTRIBUTE_VISIBILITY_HIDDEN
00248                                      ;
00249 
00251     inline  const sptr<BSharedObject>&  SharedObject() const ATTRIBUTE_VISIBILITY_HIDDEN;
00252     inline  SPackage                    Package() const ATTRIBUTE_VISIBILITY_HIDDEN;
00253 
00255     sptr<IByteInput> OpenResource(const SString& fileName, const SString& locale = SString::EmptyString()) const ATTRIBUTE_VISIBILITY_HIDDEN;
00256     
00258     SString LoadString(const SString& key, const SString& locale = SString::EmptyString()) const ATTRIBUTE_VISIBILITY_HIDDEN;
00260     SString LoadString(uint32_t index, const SString& locale = SString::EmptyString()) const ATTRIBUTE_VISIBILITY_HIDDEN;
00261     
00262     // XXX This are old APIs from Cobalt.
00263     
00265     inline  sptr<IByteInput>        OpenResource(uint32_t type, int32_t id) const ATTRIBUTE_VISIBILITY_HIDDEN;
00267     inline  SString                 LoadString(int32_t id, int32_t index = 0) const ATTRIBUTE_VISIBILITY_HIDDEN;
00268 
00269 private:
00270             sptr<BSharedObject>     m_so;
00271 };
00272 
00273 inline SPackageSptr::~SPackageSptr()
00274 {
00275 }
00276 
00277 inline const sptr<BSharedObject>& SPackageSptr::SharedObject() const
00278 {
00279     return m_so;
00280 }
00281 inline SPackage SPackageSptr::Package() const
00282 {
00283     return m_so->Package();
00284 }
00285 
00286 inline sptr<IByteInput> SPackageSptr::OpenResource(const SString& fileName, const SString& locale) const
00287 {
00288     return m_so->Package().OpenResource(fileName, locale);
00289 }
00290 
00291 inline SString SPackageSptr::LoadString(const SString& key, const SString& locale) const
00292 {
00293     return m_so->Package().LoadString(key, locale);
00294 }
00295 
00296 inline SString SPackageSptr::LoadString(uint32_t index, const SString& locale) const
00297 {
00298     return m_so->Package().LoadString(index, locale);
00299 }
00300 
00301 inline sptr<IByteInput> SPackageSptr::OpenResource(uint32_t type, int32_t id) const
00302 {
00303     return NULL;
00304 }
00305 inline SString SPackageSptr::LoadString(int32_t id, int32_t index) const
00306 {
00307     return SString();
00308 }
00309 
00310 typedef SPackageSptr SDontForgetThis;
00311 
00314 #if _SUPPORTS_NAMESPACE
00315 } } // namespace palmos::support
00316 #endif
00317 
00318 #endif