coherence/lang/Object.hpp

00001 /*
00002 * Object.hpp
00003 *
00004 * Copyright 2001-2008 by Oracle. All rights reserved.
00005 *
00006 * Oracle is a registered trademarks of Oracle Corporation and/or its
00007 * affiliates.
00008 *
00009 * This software is the confidential and proprietary information of Oracle
00010 * Corporation. You shall not disclose such confidential and proprietary
00011 * information and shall use it only in accordance with the terms of the
00012 * license agreement you entered into with Oracle.
00013 *
00014 * This notice may not be removed or altered.
00015 */
00016 #ifndef COH_OBJECT_HPP
00017 #define COH_OBJECT_HPP
00018 
00019 #include "coherence/lang/compatibility.hpp"
00020 
00021 #include "coherence/lang/cloneable_spec.hpp"
00022 #include "coherence/lang/TypedHandle.hpp"
00023 #include "coherence/lang/TypedHolder.hpp"
00024 #include "coherence/native/NativeAtomic64.hpp"
00025 
00026 #include <ostream>
00027 
00028 COH_OPEN_NAMESPACE2(coherence,native)
00029 class NativeCondition;
00030 COH_CLOSE_NAMESPACE2
00031 
00032 COH_OPEN_NAMESPACE2(coherence,lang)
00033 
00034 class SmartMember;
00035 class WeakReference;
00036 
00037 using coherence::native::NativeAtomic64;
00038 using coherence::native::NativeCondition;
00039 
00040 
00041 /**
00042 * Object is the base class for all Coherence managed objects.
00043 *
00044 * The interface for Object provides support basic operations such as equality,
00045 * hashing, printing, cloning, synchronization, and notification.
00046 *
00047 * Objects have automatically managed memory, through built-in thread-safe
00048 * reference counting. The reference count is maintained through the use of
00049 * smart-pointers, known as handles. All references to an Object should be
00050 * maintained through a handle rather then a raw pointer, or C++ reference.
00051 *
00052 * Each managed class includes inner definitions of handles which can be used
00053 * to reference instance of that class, as well as derived class instances.
00054 * These definitions provide functionality corresponding to const and non-const
00055 * pointers. The names for these handles are Handle for the non-const, View for
00056 * the const pointer equivalent, and Holder for a hybrid. The Holder type acts
00057 * like a View but allows for a safe down-cast to a Handle, which will only
00058 * succeede if the Holder was assigned from a Handle, and will fail with a
00059 * ClassCastException if it had been assigned from a View. Note that in
00060 * documentation the term "handle" is used to describe any handle type
00061 * including Handle/View/Holder, while "Handle" referes to the specific
00062 * "Handle" type.
00063 *
00064 * The handles are aware of the inheritance hierarchy, allowing direct
00065 * assignment from a handle from a derived class to a handle of a base class.
00066 * These up-casts are automatic, and an explict cast operation is rarely
00067 * needed, except to resolve compiler type ambiguities. Assignment from a View
00068 * to a Handle is also automatic as the const-interface of a class can
00069 * naturanlly be considered a base-class. Assingment of a handle to a derived
00070 * type is not automatic, and requires an explicit down-cast via the coherence
00071 * cast<H>(h) function. The template paramter is the desired handle type, while
00072 * the function parmeter is the handle to cast. If the supplied handle is
00073 * non-NULL and is not an instance of the class associated with H, a
00074 * ClassCastException will be thrown. There is a corresponding instanceof<H>(h)
00075 * method which can be used to perform the type-check without the risk of
00076 * triggering an exception. The use of instanceof, is optional, and is only
00077 * needed if the type is actually in question.
00078 *
00079 * @code
00080 * Object::Handle hObject = ...
00081 * Foo::Handle    hFoo;
00082 *
00083 * if (instanceof<Foo::Handle>(hObject))
00084 *   {
00085 *   hFoo = cast<Foo::Handle>(hObject);
00086 *   }
00087 * @endcode
00088 *
00089 * These convenience handles are not thread-safe, and should not be used in a
00090 * multi-threaded context if the handle will be updated. They should only be
00091 * used as local variables, though they are free to reference objects shared
00092 * by many threads. The most common place where thread-safe handles are
00093 * needed is for data members of managed objects. For these cases the
00094 * Coherence API includes a variety of thread-safe handles/views, namely
00095 * MemberHandle, MemberView, MemberHolder, WeakHandle, WeakView. It is strongly
00096 * encouraged that any class which extends from Object use only thread-safe
00097 * handles when referencing other Objects.
00098 *
00099 * By convention managed objects will also block direct external access to
00100 * their constructor and destructors. Constructors are accessed via public
00101 * static "create" factory methods. The copy constructor is accessed via the
00102 * public virtual "clone" method. Destructors are protected and destruction
00103 * is triggered automatically when the internal reference count reaches zero.
00104 *
00105 * @see class_spec     for defining non-cloneable classes
00106 * @see cloneable_spec for defining cloneable classes
00107 * @see abstract_spec  for defining abstract classes
00108 * @see interface_spec for defining interfaces
00109 * @see throwable_spec for defining exceptions
00110 *
00111 * @author mf 2007.07.05
00112 */
00113 class COH_EXPORT Object
00114     : public cloneable_spec<Object,
00115         extends<> >
00116     {
00117     friend class factory<Object>;
00118 
00119     // ----- constructors ---------------------------------------------------
00120 
00121     protected:
00122         /**
00123         * Construct a new Object.
00124         */
00125         Object();
00126 
00127         /**
00128         * Copy constructor.
00129         */
00130         Object(const Object& that);
00131 
00132         /**
00133         * Destructor.
00134         */
00135         virtual ~Object();
00136 
00137     private:
00138         /**
00139         * Blocked assignment operator.
00140         *
00141         * @internal
00142         */
00143         Object& operator=(const Object&);
00144 
00145 
00146     // ----- Object interface -----------------------------------------------
00147 
00148     public:
00149         /**
00150         * Return true iff the specified Object is "equal" to this Object.
00151         *
00152         * This method implements an <i>equivalence relation</i> on Objects:
00153         * <ul>
00154         * <li>It is <i>reflexive</i>: for any non-null handle
00155         *     <code>h</code>, <code>h->equals(h)</code> must return
00156         *     <code>true</code>.
00157         * <li>It is <i>symmetric</i>: for any non-null handles
00158         *     <code>h1</code> and <code>h2</code>, <code>h1->equals(h2)</code>
00159         *     should return <code>true</code> if and only if
00160         *     <code>h2->equals(h1)</code> returns <code>true</code>.
00161         * <li>It is <i>transitive</i>: for any non-null handles
00162         *     <code>h1</code>, <code>h2</code>, and <code>h3</code>, if
00163         *     <code>h1->equals(h2)</code> returns <code>true</code> and
00164         *     <code>h2->equals(h3)</code> returns <code>true</code>, then
00165         *     <code>h1->equals(h3)</code> should return <code>true</code>.
00166         * <li>It is <i>consistent</i>: for any non-null handles
00167         *     <code>h1</code> and <code>h2</code>, multiple invocations of
00168         *     <tt>h1->equals(h2)</tt> consistently return <code>true</code>
00169         *     or consistently return <tt>false</tt>, provided no
00170         *     information used in comparisons on the objects is modified.
00171         * <li>If the supplied handle is <tt>NULL</tt> then <tt>false</tt>
00172         *     must be returned.
00173         * </ul>
00174         *
00175         * The default implementation is a reference equality comparision.
00176         *
00177         * @param v  the Object::View to compare against, may be <tt>NULL</tt>
00178         *
00179         * @return <tt>true</tt> iff the given handle references an Object
00180         *         that is "equal" to this Object
00181         *
00182         * @see #equals(Object::View v1, Object::View v2)
00183         */
00184         virtual bool equals(Object::View v) const;
00185 
00186         /**
00187         * Return a hash code value for the Object. This method is supported
00188         * for the benefit of hash-based containers.
00189         *
00190         * The general contract of <code>hashCode</code> is:
00191         * <ul>
00192         * <li>Whenever it is invoked on the same Object more than once
00193         *     during an execution of an application, the <tt>hashCode</tt>
00194         *     method must consistently return the same value, provided no
00195         *     information used in <tt>equals</tt> comparisons on the object
00196         *     is modified. This value need not remain consistent from one
00197         *     execution of an application to another execution of the same
00198         *     application.
00199         * <li>If two Objects are equal according to the <tt>equals</tt>
00200         *     method, then calling the <code>hashCode</code> method on each
00201         *     of the two Objects must produce the same value.
00202         * <li>It is <em>not</em> required that if two Objects are unequal
00203         *     according to the <tt>equals</tt> method, then calling the
00204         *     <tt>hashCode</tt> method on each of the two objects must
00205         *     produce distinct results. However, the programmer should be
00206         *     aware that producing distinct results for unequal objects may
00207         *     improve the performance of hash-based containers.
00208         * </ul>
00209         *
00210         * The default implementation is identity based.
00211         *
00212         * @return a hash code value for this Object
00213         */
00214         virtual size32_t hashCode() const;
00215 
00216         /**
00217         * Return a handle to a deep copy of the original Object.
00218         *
00219         * The returned clone should be sufficient decoupled from the original
00220         * such that futher modifications to the original Object will not be
00221         * visible within the cloned object.  More specifically, the following
00222         * is expected to hold true.
00223         *
00224         * @code
00225         * h->clone() != h &&
00226         * h->clone()->equals(h) &&
00227         * typeid(*h) == typeid(*(h->clone()))
00228         * @endcode
00229         *
00230         * Note that this suggests that data members of a cloned object are
00231         * expected to cloned if they are tested for equality (via a call to
00232         * equals) within the cloned object's equals method.  If a data member
00233         * is compared via reference equality, or not even considered within
00234         * equals, then it does not necessarily need to be deeply cloned.
00235         *
00236         * Object is cloneable, but it's derived classes are not automatically
00237         * cloneable, and CloneNotSupportedExceptions are thrown. To be made
00238         * cloneable the derived class should use the cloneable_spec<> helper
00239         * template in its declaration, and define a protected copy constructor.
00240         * The derived class does not need to override this method, as that is
00241         * done by the cloneable_spec<> template.
00242         *
00243         * @return a copy of the original Object
00244         *
00245         * @throws CloneNotSupportedException if the object cannot be deep
00246         *         cloned
00247         *
00248         * @see isImmutable()
00249         */
00250         virtual Object::Handle clone() const;
00251 
00252         /**
00253         * Return <tt>true</tt> iff no further changes can be made to the
00254         * Object, that would effect the outcome of a call to its equals method.
00255         * Except for Objects which are naturally immutable (such as String),
00256         * being immutable generally implies that the Object is only referenced
00257         * via const pointers or views. Objects which hold references to child
00258         * Objects, may need to take the immutability of their children into
00259         * account when determining their own immutability.
00260         *
00261         * This extended check is not performed by the default implementation,
00262         * but can be integrated into the immutability checks by overiding
00263         * this method, as well as making use of MemberHandles to reference
00264         * child Objects.
00265         *
00266         * A typicial derived implementaiton may look as follows:
00267         *
00268         * <pre>
00269         * bool isImmutable() const
00270         *     {
00271         *     if (m_fImmutable) // check recorded state
00272         *         {
00273         *         return true; // already marked as immutable, avoid calculation
00274         *         }
00275         *     else if (Object::isImmutable()) // ensure shallow immutability
00276         *         {
00277         *         // ensure deap immutability
00278         *         if (m_child1->isImmutable() && m_child2->isImmutable() ...
00279         *                                     && m_childN->isImmutable())
00280         *             {
00281         *             // record and return immutability
00282         *             return m_fImmutable = true;
00283         *             }
00284         *         // some Objects which comprise this Object are still mutable
00285         *         }
00286         *     return false;
00287         *     }
00288         * </pre>
00289         *
00290         * The default implementation return true iff the Object is only
00291         * referenced via const pointers and or views.
00292         *
00293         * @return true iff the Object is immutable
00294         */
00295         virtual bool isImmutable() const;
00296 
00297         /**
00298         * Output a human-readable description of this Object to the given
00299         * stream.
00300         *
00301         * coherence::lang::operator<<(std::ostream, Object::View) is defined
00302         * and will call into the toStream method, to output Objects.  If a
00303         * manged String object is desired, the COH_TO_STRING macro can be used
00304         * to build up a String from streamable contents.
00305         *
00306         * @code
00307         * Object::View vKey   = ...
00308         * Object::View vValue = ...
00309         * std::cout << vKey << " = " << vValue << std::endl;
00310         *
00311         * String::Handle hs = COH_TO_STRING(vKey << " = " << vValue);
00312         * @endcode
00313         *
00314         * @param out  the stream used to output the description
00315         */
00316         virtual void toStream(std::ostream& out) const;
00317 
00318         /**
00319         * Return an estimate as to the shallow byte size of the object.
00320         *
00321         * The shallow size should not include the cost of referenced managed
00322         * objects, or non-fixed dynamically allocated memory. That for a
00323         * given object it is assumed that a call to this method will always
00324         * return the same value.
00325         *
00326         * For classes defined via the class_spec template an auto-generated
00327         * implementation is provided which utilizes the standard C sizeof()
00328         * operator to compute the size.  User defined implementations are
00329         * only needed to account for dymacic memory allocated during
00330         * construction.
00331         *
00332         * @return the approximate shallow byte size of the object
00333         */
00334         virtual size32_t sizeOf() const;
00335 
00336 
00337     // ----- synchronization and notification interface ---------------------
00338 
00339     public:
00340         /**
00341         * Block the calling thread while waiting for notification.
00342         *
00343         * The caller must be synchronized on the Object's monitor when calling
00344         * this method. The monitor will be automatically released while the
00345         * thread awaits notification, and reacquired before the call returns.
00346         * This method is subject to spurious wakeups, and the caller should
00347         * externally verify that the condition being waited for has been
00348         * reached.
00349         *
00350         * To synchronize on an object, the COH_SYNCHRONIZED macro is used.
00351         * The macro defines a block of code which is a critical section,
00352         * during which no other thread may acquire synchronization on the same
00353         * object.
00354         *
00355         * @code
00356         * Object::Handle hObject = ...
00357         * COH_SYNCHRONIZED (hObject)
00358         *   {
00359         *   while (testSomeCondition() == false)
00360         *     {
00361         *     hObject->wait();
00362         *     }
00363         *   }
00364         * @endcode
00365         *
00366         * @see notify
00367         * @see notifyAll
00368         */
00369         void wait() const;
00370 
00371         /**
00372         * Block the calling thread while waiting for notification.
00373         *
00374         * The caller must be synchronized on the Object's monitor when calling
00375         * this method. The monitor will be automatically released while the
00376         * thread awaits notification, and reacquired before the call returns.
00377         * This method is subject to spurious wakeups, and the caller should
00378         * externally verify that the condition being waited for has been
00379         * reached.
00380         *
00381         * @param cMillis  the duration to wait to wait, a value of zero will
00382         *                 wait indefinitely for notification
00383         *
00384         * @see wait
00385         * @see notify
00386         * @see notifyAll
00387         */
00388         void wait(int64_t cMillis) const;
00389 
00390         /**
00391         * Notify a waiting thread.
00392         *
00393         * The caller must be synchronized on the Object's monitor when calling
00394         * this method.
00395         *
00396         * @code
00397         * Object::Handle hObject = ...
00398         * COH_SYNCHRONIZED (hObject)
00399         *   {
00400         *   setSomeCondition(true);
00401         *   hObject->notify();
00402         *   }
00403         * @endcode
00404         *
00405         * @see wait
00406         * @see notifyAll
00407         */
00408         void notify() const;
00409 
00410         /**
00411         * Notify all waiting threads.
00412         *
00413         * The caller must be synchronized on the Object's monitor when calling
00414         * this method.
00415         *
00416         * @see wait
00417         * @see notify
00418         */
00419         void notifyAll() const;
00420 
00421     private:
00422         /**
00423         * Enter the Object's monitor, waiting as long as necessary.
00424         * <p/>
00425         * The monitor supports recursive entry, allowing the same thread to
00426         * reenter the same monitor. The thread must exit the monitor the
00427         * same number of times, by calling the exitMonitor, for the Monitor
00428         * to be released.
00429         *
00430         * It is recommended that the COH_SYNCHRONIZED macro be used in place
00431         * of direct calls to enterMonitor/exitMonitor. The use of this macro
00432         * only requires public level view access to the Object
00433         * <pre>
00434         * // outside of synchronized block
00435         * COH_SYNCHRONIZED (vObject) // monitor entered
00436         *     {
00437         *     // critical section goes here
00438         *     // ...
00439         *     // ...
00440         *     } // monitor automatically exited
00441         * // outside of synchronized block
00442         * </pre>
00443         *
00444         * @see exitMonitor
00445         */
00446         void enterMonitor() const;
00447 
00448         /**
00449         * Exit the Object's monitor.
00450         *
00451         * This must be called by the thread which locked the monitor.
00452         *
00453         * @see enterMonitor
00454         */
00455         void exitMonitor() const;
00456 
00457         /**
00458         * Acquire the shared read lock for the Object's data members.
00459         *
00460         * The data member read/write lock is intended for protecting small
00461         * sections of non-blocking code, such as primitive data-member access
00462         * and assignment. The read lock does support limited recursion,
00463         * and each call to acquire must be matched by a corresponding
00464         * call to release. For more advanced read/write lock support the
00465         * ThreadGate class should be utilized.
00466         *
00467         * The member read lock may be acquired using the
00468         * COH_SYNCHRONIZED_MEMBER_READ macro.
00469         *
00470         * @see SynchronizedMemberReadBlock
00471         * @see coherence::util::ThreadGate
00472         */
00473         void acquireMemberReadLock() const;
00474 
00475         /**
00476         * Release the read lock for the Object's data members
00477         */
00478         void releaseMemberReadLock() const;
00479 
00480         /**
00481         * Acquire the write lock for the Object's data members.
00482         *
00483         * Unlike the member read lock, write locks do not support
00484         * recursion, nor do they support lock promotion from a read to a
00485         * write lock. Attempts to use them such a manor will result in
00486         * dead-lock.
00487         *
00488         * The member write lock may be acquired using the
00489         * COH_SYNCHRONIZED_MEMBER_WRITE macro.
00490         *
00491         * @see SynchronizedMemberWriteBlockm
00492         */
00493         void acquireMemberWriteLock() const;
00494 
00495         /**
00496         * Release the write lock for the Object's data members.
00497         */
00498         void releaseMemberWriteLock() const;
00499 
00500 
00501     // ----- reference counting interface -----------------------------------
00502 
00503     public:
00504         /**
00505         * @internal
00506         *
00507         * Increment the non-const reference count for this Object.
00508         *
00509         * NOTE: This method is intended for use by smart pointers such as
00510         * TypedHandle, and its usage should be limited.
00511         *
00512         * This operation must return <tt>NULL</tt> if the Object is
00513         * immutable.
00514         *
00515         * @return a pointer to the Object or <tt>NULL</tt> if new non-const
00516         *         attachments are disallowed
00517         *
00518         * @see _detach
00519         */
00520         Object* _attach();
00521 
00522         /**
00523         * @internal
00524         *
00525         * Increment the const reference count for this Object.
00526         *
00527         * NOTE: This method is intended for use by smart pointers such as
00528         * TypedHandle, and its usage should be limited.
00529         *
00530         * @return a constant pointer to the Object, or <tt>NULL</tt> if new
00531         *         const attachments are disallowed
00532         *
00533         * @see _detach
00534         */
00535         const Object* _attach() const;
00536 
00537         /**
00538         * @internal
00539         *
00540         * Return a WeakReference to this Object.
00541         *
00542         * Unlike non-weak attachments, there is no corresponding _detachWeak,
00543         * as the return type is a handle.
00544         *
00545         * See the WeakHandle template class which will automatically manage
00546         * the weak attachment to the Object.
00547         *
00548         * NOTE: This method is intended for use by smart pointers such as
00549         * WeakHandle, and its usage should be limited.
00550         *
00551         * @see WeakReference
00552         * @see WeakHandle
00553         *
00554         * @return a WeakReference to this Object
00555         */
00556         TypedHandle<WeakReference> _attachWeak();
00557 
00558         /**
00559         * @internal
00560         *
00561         * Return a WeakReference View to this Object.
00562         *
00563         * Unlike non-weak attachments, there is no corresponding _detachWeak,
00564         * as the return type is a handle.
00565         *
00566         * See the WeakView template class which will automatically manage the
00567         * weak attachment to the Object.
00568         *
00569         * NOTE: This method is intended for use by smart pointers such as
00570         * WeakHandle, and its usage should be limited.
00571         *
00572         * @see WeakReference
00573         * @see WeakView
00574         *
00575         * @return a WeakReference to this Object
00576         */
00577         TypedHandle<const WeakReference> _attachWeak() const;
00578 
00579         /**
00580         * @internal
00581         *
00582         * Decrement the non-const reference count for this Object and
00583         * automatically delete this Object if the total reference count drops
00584         * to zero.
00585         *
00586         * NOTE: This method is intended for use by smart pointers such as
00587         * TypedHandle, and its usage should be limited.
00588         *
00589         * @see _attach
00590         */
00591         void _detach();
00592 
00593         /**
00594         * @internal
00595         *
00596         * Decrement the const reference count for this Object and
00597         * automatically delete this Object if the total reference count drops
00598         * to zero.
00599         *
00600         * NOTE: This method is intended for use by smart pointers such as
00601         * TypedHandle, and its usage should be limited.
00602         *
00603         * @see _attach
00604         */
00605         void _detach() const;
00606 
00607 
00608     // ----- life cycle events ----------------------------------------------
00609 
00610     protected:
00611         /**
00612         * Event called once the Object has finished being constructed.
00613         * Specifically when the first attachment is made. This provides a
00614         * safe point at which Handles/Views to "this" can be created. It is
00615         * not safe to create Handles/Views to an Object from within its
00616         * constructor, thus any operations which rely upon this should be
00617         * deferred until the onInit event is triggered.
00618         *
00619         * As with all event methods any derived implementation should
00620         * include a call to the super class's implementation. Specifically
00621         * delegation to Object::onInit() must eventually occur or an
00622         * IllegalStateException will result.
00623         *
00624         * The default implementation calls the onInit() method of each of the
00625         * Object's SmartMembers.
00626         */
00627         virtual void onInit();
00628 
00629         /**
00630         * Event called when the Object becomes only referenced via const
00631         * pointers (Views). Assuming a const-correct class, once this method
00632         * returns no further visible changes can be made to the object.
00633         *
00634         * As with all event methods any derived implementation should include
00635         * a call to the super class's implementation.
00636         *
00637         * The default implmenetation calls the onConst() method of each of
00638         * the Object's SmartMembers.
00639         *
00640         * @see isImmutable
00641         */
00642         virtual void onConst();
00643 
00644 
00645     // ----- helper methods -------------------------------------------------
00646 
00647     protected:
00648         /**
00649         * Return a reference to the constructed base Object.
00650         *
00651         * This reference is suitable for use in derived class constructors as
00652         * the Object class will have completed construction.
00653         *
00654         * @return the Object reference
00655         */
00656         Object& self();
00657 
00658         /**
00659         * Return a reference to the constructed base Object.
00660         *
00661         * @return the const Object reference
00662         */
00663         const Object& self() const;
00664 
00665     private:
00666         /**
00667         * Increment the Object's reference count.
00668         *
00669         * @param fHandle  true if the attachment is via a handle, false if
00670         *                 via a view
00671         *
00672         * @return true iff the attachment succeeded
00673         */
00674         bool attachInternal(bool fHandle) const;
00675 
00676         /**
00677         * Decrement the Object's reference count.
00678         *
00679         * @param fHandle  true if the detachment is via a handle, false if
00680         *                 via a view
00681         */
00682         void detachInternal(bool fHandle) const;
00683 
00684         /**
00685         * Return the WeakReference associated with this Object, creating one
00686         * if necessary.
00687         *
00688         * @return the WeakReference associated with this Object
00689         */
00690         TypedHandle<WeakReference> ensureWeakReference() const;
00691 
00692         /**
00693         * Return the NativeCondition which backs the Object's monitor,
00694         * creating one if necessary.
00695         *
00696         * The life of the returned condition is bounded to the Object.
00697         *
00698         * @return the NativeCondition which backs the Object's monitor
00699         */
00700         NativeCondition& ensureMonitor() const;
00701 
00702 
00703     // ----- static methods -------------------------------------------------
00704 
00705     public:
00706         /**
00707         * Compare two Objects for equality.
00708         *
00709         * This method implements an <i>equivalence relation</i> for two
00710         * potentially <tt>NULL</tt> handles.
00711         *
00712         * @param v1  the first Object to compare
00713         * @param v2  the second Object to compare
00714         *
00715         * @return  <tt>true</tt> iff v1 and v2 are <tt>NULL</tt> or
00716         *          reference Objects which are equal
00717         */
00718         static bool equals(Object::View v1, Object::View v2);
00719 
00720         /**
00721         * Return a clone of the supplied Object.
00722         *
00723         * @param v  the Object to clone
00724         *
00725         * @return a clone of the Object, or NULL if NULL was supplied
00726         */
00727         static Object::Handle clone(Object::View v);
00728 
00729         /**
00730         * Output an Object to a stream.
00731         *
00732         * If the handle is NULL then the string "NULL" will be written to the
00733         * stream.
00734         *
00735         * @param out  the stream used to output the Object
00736         * @param v    the Object to output
00737         */
00738         static void toStream(std::ostream& out, Object::View v);
00739 
00740     // ----- memory management ----------------------------------------------
00741 
00742     public:
00743         /**
00744         * @internal
00745         *
00746         * Default allocator for Objects.
00747         *
00748         * @param cb  the number of bytes to allocate
00749         *
00750         * @return the allocated memory
00751         *
00752         * @throws IllegalStateException if the allocation fails
00753         */
00754         static void* operator new(size_t cb);
00755 
00756         /**
00757         * @internal
00758         *
00759         * Default deallocator for Objects.
00760         *
00761         * @param po  the memory to deallocate
00762         */
00763         static void operator delete(void* po);
00764 
00765 
00766     // ----- data members ---------------------------------------------------
00767 
00768     private:
00769         /**
00770         * 64bit atomic Object lifecycle.
00771         */
00772         mutable NativeAtomic64 m_atomicLifeCycle;
00773 
00774         /**
00775         * Pointer to the NativeCondition which is used to implement the
00776         * Object's monitor functionality.
00777         *
00778         * The validity of this pointer is governed by the Object's
00779         * life-cycle. A non-NULL pointer is not sufficient to ensure that
00780         * the condition is ready for use.
00781         */
00782         mutable NativeCondition* m_pCondition;
00783 
00784         /**
00785         * The WeakReference to this Object.
00786         *
00787         * The validity of this handle is governed by the Object's life-cycle.
00788         * A non-NULL handle is not sufficient to ensure that the reference is
00789         * ready for use.
00790         */
00791         mutable TypedHandle<WeakReference> m_hWeakThis;
00792 
00793         /**
00794         * Pointer to the top of the stack of SmartMembers bound to this
00795         * Object, or NULL if none exist.
00796         */
00797         SmartMember* m_pSmartMemberStack;
00798 
00799 
00800     // ----- friends --------------------------------------------------------
00801 
00802     friend class SmartMember;
00803     friend class SynchronizedBlock;
00804     friend class SynchronizedMemberReadBlock;
00805     friend class SynchronizedMemberWriteBlock;
00806     friend class System;
00807     };
00808 
00809 
00810 // ----- non-member operators and functions ---------------------------------
00811 
00812 /**
00813 * Output a human-readable description of the specified Object to the given
00814 * stream.
00815 *
00816 * @param out  the stream used to output the description
00817 * @param o    the Object to describe
00818 *
00819 * @return the supplied stream
00820 */
00821 COH_EXPORT std::ostream& operator<<(std::ostream& out, const Object& o);
00822 
00823 /**
00824 * @internal
00825 *
00826 * Specialized extends implementation for classes deriving directly from Object.
00827 *
00828 * This specialization bring Object in virtually (as interfaces dervie from it
00829 * virtually as well.
00830 */
00831 template<class A> class COH_EXPORT_SPEC extends<Object, A>
00832     : public virtual Object
00833     {
00834     public:
00835         typedef Void<Object> inherited;
00836         typedef A            alias;
00837     };
00838 
00839 /**
00840 * @internal
00841 *
00842 * Specialized extends implementation for classes deriving directly from Object.
00843 *
00844 * This specialization bring Object in virtually (as interfaces dervie from it
00845 * virtually as well.
00846 */
00847 template<> class COH_EXPORT_SPEC extends<Object, Void<Object> >
00848     : public virtual Object
00849     {
00850     public:
00851         typedef Void<Object> inherited;
00852         typedef Void<Object> alias;
00853     };
00854 
00855 COH_CLOSE_NAMESPACE2
00856 
00857 #endif // COH_OBJECT_HPP
Copyright (c) 2000-2008 Oracle. All rights reserved.