StreamDatum.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 _STORAGE_STREAMDATUM_H
00014 #define _STORAGE_STREAMDATUM_H
00015 
00021 #include <storage/GenericDatum.h>
00022 
00023 #include <support/ByteStream.h>
00024 #include <support/IStorage.h>
00025 #include <support/Locker.h>
00026 #include <support/SortedVector.h>
00027 
00028 #if _SUPPORTS_NAMESPACE
00029 namespace palmos {
00030 namespace storage {
00031 #endif
00032 
00037 // ==========================================================================
00038 // ==========================================================================
00039 
00041 
00065 class BStreamDatum : public BGenericDatum
00066 {
00067 public:
00068     // --------------------------------------------------------------
00072                                     BStreamDatum(uint32_t mode = IDatum::READ_WRITE);
00073                                     BStreamDatum(const SContext& context, uint32_t mode = IDatum::READ_WRITE);
00074 protected:
00075     virtual                         ~BStreamDatum();
00076 public:
00077 
00079     static  uint32_t                RestrictMode(uint32_t requested, uint32_t limit);
00080 
00082             uint32_t                Mode() const;
00083 
00085 
00091     virtual lock_status_t           Lock() const;
00092 
00094     virtual void                    Unlock() const;
00095 
00097 
00098     //  ------------------------------------------------------------------
00103 
00105     virtual uint32_t                ValueType() const;
00107     virtual void                    SetValueType(uint32_t type);
00109     virtual off_t                   Size() const;
00111     virtual void                    SetSize(off_t size);
00113     virtual SValue                  Value() const;
00115     virtual void                    SetValue(const SValue& value);
00117     virtual sptr<IBinder>           Open(uint32_t mode, const sptr<IBinder>& editor = NULL, uint32_t newType = 0);
00118 
00120             status_t                SetValueTypeLocked(uint32_t type);
00122             status_t                SetSizeLocked(off_t size);
00124             status_t                SetValueLocked(const SValue& value);
00126 
00127     class Stream;
00128 
00129     //  ------------------------------------------------------------------
00139 
00141     virtual uint32_t                ValueTypeLocked() const;
00143     virtual status_t                StoreValueTypeLocked(uint32_t type);
00145     virtual off_t                   SizeLocked() const = 0;
00147     virtual status_t                StoreSizeLocked(off_t size) = 0;
00149 
00154     virtual SValue                  ValueLocked() const;
00156 
00159     virtual status_t                StoreValueLocked(const SValue& value);
00160 
00161             enum {
00162                 TYPE_CHANGED    = 0x0001,   
00163                 SIZE_CHANGED    = 0x0002,   
00164                 DATA_CHANGED    = 0x0004    
00165             };
00166 
00168 
00180     virtual void                    ReportChangeLocked(const sptr<IBinder>& editor, uint32_t changes, off_t start=-1, off_t length=-1);
00181 
00183 
00184     //  ------------------------------------------------------------------
00203 
00205     virtual ssize_t                 ReadLocked( const sptr<Stream>& stream, off_t position,
00206                                                 const struct iovec *vector, ssize_t count, uint32_t flags) const;
00208     virtual ssize_t                 WriteLocked(const sptr<Stream>& stream, off_t position,
00209                                                 const struct iovec *vector, ssize_t count, uint32_t flags);
00211     virtual status_t                SyncLocked();
00212     
00214 
00215     //  ------------------------------------------------------------------
00231 
00233     virtual const void*             StartReadingLocked( const sptr<Stream>& stream, off_t position,
00234                                                         ssize_t* inoutSize, uint32_t flags) const;
00236     virtual void                    FinishReadingLocked(const sptr<Stream>& stream, const void* data) const;
00238     virtual void*                   StartWritingLocked( const sptr<Stream>& stream, off_t position,
00239                                                         ssize_t* inoutSize, uint32_t flags);
00241     virtual void                    FinishWritingLocked(const sptr<Stream>& stream, void* data);
00242 
00244 
00245 protected:
00246 
00247 private:
00248                                     BStreamDatum(const BStreamDatum&);
00249             BStreamDatum&           operator=(const BStreamDatum&);
00250 
00251     friend class Stream;
00252 
00253             ssize_t                 write_wrapper_l(const sptr<Stream>& stream, off_t position,
00254                                                     const struct iovec *vector, ssize_t count, uint32_t flags);
00255 
00256     mutable SLocker                 m_lock;
00257     const   uint32_t                m_mode;
00258             SSortedVector<wptr<Stream> >
00259                                     m_streams;
00260 };
00261 
00262 // --------------------------------------------------------------------------
00263 
00265 
00268 class BStreamDatum::Stream : public BnByteInput, public BnByteOutput, public BnByteSeekable, public BnStorage
00269 {
00270 public:
00271     // --------------------------------------------------------------
00275                                 Stream( const SContext& context, const sptr<BStreamDatum>& datum,
00276                                         uint32_t mode, uint32_t type, const sptr<IBinder>& editor);
00277     
00279     virtual SValue              Inspect(const sptr<IBinder>& caller, const SValue &which, uint32_t flags = 0);
00280     
00281 protected:
00282     virtual                     ~Stream();
00284     virtual void                InitAtom();
00286     virtual status_t            FinishAtom(const void* id);
00287 public:
00288 
00290 
00291     // --------------------------------------------------------------
00294 
00295     virtual ssize_t             ReadV(const struct iovec *vector, ssize_t count, uint32_t flags = 0);
00296     virtual ssize_t             WriteV(const struct iovec *vector, ssize_t count, uint32_t flags = 0);
00297     virtual status_t            Sync();
00298     virtual off_t               Seek(off_t position, uint32_t seek_mode);
00299     virtual off_t               Position() const;
00300 
00302 
00303     // --------------------------------------------------------------
00306 
00307     virtual off_t               Size() const;
00308     virtual status_t            SetSize(off_t size);
00309     virtual ssize_t             ReadAtV(off_t position, const struct iovec *vector, ssize_t count);
00310     virtual ssize_t             WriteAtV(off_t position, const struct iovec *vector, ssize_t count);
00311 
00313 
00314     // --------------------------------------------------------------
00317 
00319             sptr<BStreamDatum>  Datum() const;
00321             uint32_t            Mode() const;
00323             uint32_t            Type() const;
00325             sptr<IBinder>       Editor() const;
00327             sptr<IBinder>       DefaultInterface() const;
00328 
00330 
00331 private:
00332                                 Stream(const Stream&);
00333 
00334     const   sptr<BStreamDatum>  m_datum;
00335     const   uint32_t            m_mode;
00336     const   uint32_t            m_type;
00337     const   sptr<IBinder>       m_editor;
00338             off_t               m_pos;
00339 };
00340 
00341 // ==========================================================================
00342 // ==========================================================================
00343 
00346 #if _SUPPORTS_NAMESPACE
00347 } } // namespace palmos::storage
00348 #endif
00349 
00350 #endif // _STORAGE_STREAMDATUM_H