Iterator.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_ITERATOR_H
00014 #define _SUPPORT_ITERATOR_H
00015 
00021 #include <support/IIterable.h>
00022 #include <support/IRandomIterator.h>
00023 #include <support/Locker.h>
00024 #include <support/Vector.h>
00025 #include <support/Observer.h>
00026 
00027 #include <stdint.h>
00028 
00029 #if _SUPPORTS_NAMESPACE
00030 namespace palmos {
00031 namespace support {
00032 #endif
00033 
00038 B_CONST_STRING_VALUE_LARGE(BV_ITERATOR_CHANGED,     "IteratorChanged", );
00039 
00041 
00048 class SIterator
00049 {
00050 public:
00051     // --------------------------------------------------------------
00057 
00058     SIterator();
00059     SIterator(const SContext& context, const SString& path, const SValue& args = B_UNDEFINED_VALUE);
00060     SIterator(const sptr<IIterable>& dir, const SValue& args = B_UNDEFINED_VALUE);
00061     SIterator(const sptr<IIterator>& it);
00062     SIterator(const sptr<INode>& node, const SValue& args = B_UNDEFINED_VALUE);
00063     ~SIterator();
00064     
00065     status_t SetTo(const SContext& context, const SString& path, const SValue& args = B_UNDEFINED_VALUE);
00066     status_t SetTo(const sptr<IIterable>& dir, const SValue& args = B_UNDEFINED_VALUE);
00067     status_t SetTo(const sptr<IIterator>& it);
00068 
00069     status_t StatusCheck() const;
00070 
00072     status_t ErrorCheck() const;
00073 
00075 
00076     // --------------------------------------------------------------
00080 
00081     status_t Next(SValue* key, SValue* value, uint32_t flags = 0, size_t count = 0);
00082     status_t Remove();
00083 
00085 
00086     static SValue ApplyFlags(const sptr<IIterable> &iterable, uint32_t flags);
00087 
00088 private:
00089     SIterator(const SIterator& o);
00090     SIterator& operator=(const SIterator& o);
00091 
00092     SLocker m_lock;
00093     sptr<IIterator> m_iterator;
00094     IIterator::ValueList m_keys;
00095     IIterator::ValueList m_values;
00096     size_t m_index;
00097 };
00098 
00099 class BValueIterator : public BnIterator
00100 {
00101 public:
00102     BValueIterator(const SValue& value);
00103 
00104     virtual SValue Options() const;
00105     virtual status_t Next(IIterator::ValueList* keys, IIterator::ValueList* values, uint32_t flags, size_t count = 0);
00106     virtual status_t Remove();
00107     
00108 protected:
00109     virtual ~BValueIterator();
00110     
00111 private:
00112     SLocker m_lock;
00113     SValue m_value;
00114     void* m_cookie;
00115 };
00116 
00117 class BRandomIterator : public BnRandomIterator, public BObserver
00118 {
00119 public:
00120     BRandomIterator(const sptr<IIterator>& iter);
00121 
00122     virtual void    InitAtom();
00123 
00124     // IIterator
00125     virtual SValue      Options() const;
00126     virtual status_t    Next(IIterator::ValueList* keys, IIterator::ValueList* values, uint32_t flags, size_t count = 0);
00127     virtual status_t    Remove();
00128 
00129     // IRandomIterator
00130     virtual size_t      Count() const;
00131     virtual size_t      Position() const;
00132     virtual void        SetPosition(size_t value);
00133 
00134     // BObserver
00135     virtual void        Observed(const SValue& key, const SValue& value);
00136 
00137 protected:
00138     virtual ~BRandomIterator();
00139 
00140 private:
00141     // No default initialization and no copying!
00142     BRandomIterator();
00143     BRandomIterator(const BRandomIterator& o);
00144     BRandomIterator& operator=(const BRandomIterator& o);
00145 
00146     mutable SNestedLocker   m_lock;
00147     const sptr<IIterator>   m_iterator;
00148     size_t                  m_position;
00149     mutable IIterator::ValueList m_keys;
00150     mutable IIterator::ValueList m_values;
00151 };
00152 
00155 #if _SUPPORTS_NAMESPACE
00156 } } // namespace palmos::support
00157 #endif
00158 
00159 #endif // _SUPPORT_ITERATOR_H