Finger pointer 4

Source: Internet
Author: User

 

# Include "stdafx. H"

# Ifndef Win32
# Include <unistd. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <pthread. h>
Typedef pthread_mutex_t vmutex_t;
# Else
# Include <windows. h>
Typedef critical_section vmutex_t;
# Endif

 

//////////////////////////////////////// ///////////////////
Class mutex
{
Public:
Mutex (void );
Virtual ~ Mutex (void );

Public:
Virtual void lock ();

Virtual void unlock ();

Vmutex_t * GETID () const;

Protected:
Mutable vmutex_t myid;
};

 

Mutex: mutex (void)
{
# Ifndef Win32
Pthread_mutex_init (& myid, null );
# Else
Initializecriticalsection (& myid );
# Endif
}

Mutex ::~ Mutex (void)
{
# Ifndef Win32
Pthread_mutex_destroy (& myid );
# Else
Deletecriticalsection (& myid );
# Endif

}

Void mutex: Lock ()
{
# Ifndef Win32
Pthread_mutex_lock (& myid );
# Else
Entercriticalsection (& myid );
# Endif
}

Void mutex: Unlock ()
{
# Ifndef Win32
Pthread_mutex_unlock (& myid );
# Else
Leavecriticalsection (& myid );
# Endif
}

Vmutex_t * mutex: GETID () const
{
Return (& myid );
}

//////////////////////////////////////// ///
# Ifndef inline _
# Define inline _ inline
# Endif

# Define Vlock () mutex. Lock ()
# Define vunlock () mutex. Unlock ()
# Define Inc (x) ++ x
# Define Dec (x) -- X
# Define dec_and_cmp (x, r) -- X, r = (x = 0)
# Define Exchange (PTR, Val) void * TMP; TMP = * PTR, * PTR = Val, Val = TMP

 

Class countsemaphore
{
Public:
/// Constructor
Inline _ explicit countsemaphore (INT value = 0)
: Mutex (), count (value)
{}

/** Compares the current value in the reference count
* Value. returns true if equal.
* @ Param value integer to compare against.
*/
Inline _ bool compare (INT value)
{
Bool retval = false;
Vlock ();
If (value = count) retval = true;
Vunlock ();
Return retval;
}

/** Increment the reference count by one. This operation is atomic.
*/
Inline _ void increment ()
{
Vlock ();
INC (count );
Vunlock ();
}

/** Decrement the reference count by one. This operation is
* Atomic. If the reference count now equals 0, Decrement
* Returns true.
*/
Inline _ bool decrement ()
{
Bool retval;
Vlock ();
Dec_and_cmp (count, retval );
Vunlock ();
Return retval;
}

 

Inline _ void Exchange (void ** PTR, void ** Val)
{
Vlock ();
Exchange (PTR, * val );
Vunlock ();
}

Inline _ int getcount () const
{
Return count;
}

Bool operator = (INT value) const
{
Return (value = count );
}

Void lock ()
{
Vlock ();
}

Void unlock ()
{
Vunlock ();
}

PRIVATE:
/// Suppress copying
Countsemaphore (const countsemaphore * pvalue );
/// Suppress copying
Const countsemaphore & operator = (const countsemaphore * pvalue );
/// Suppress comparison
Bool operator = (const countsemaphore * pvalue );

Mutex;

Volatile int count; // Why is this volatile?
};

Countsemaphore: countsemaphore (const countsemaphore * pvalue)
: Mutex (), count (0)
{
Count = pvalue-> getcount ();
}

Const countsemaphore & countsemaphore: Operator = (const countsemaphore * pvalue)
{
Int icount = pvalue-> getcount ();
Vlock ();
Count = icount;
Vunlock ();

Return * this;
}

 

Bool countsemaphore: Operator = (const countsemaphore * pvalue)
{
Int icount = pvalue-> getcount ();
Return COUNT = icount;
}

//////////////////////////////////////// /////////////////

Template <class T>
Class sptr
{
// We Need to work around und visual c ++'s limitations. This
// Is needed so that we can grab these bits directly

PRIVATE:

Mutable T * PTR;
Mutable countsemaphore * count;

Public:

/// Increment the reference count.
Void increment ()
{
If (PTR)
{
If (! Count)
{
Count = new countsemaphore ();
}

Count-> increment ();
}
}

/// Decrement the reference count
Void decrement ()
{
If (PTR & COUNT)
{

If (count-> decrement ())
{
Delete PTR;
Delete count;
}
}
PTR = 0;
Count = 0;
}

/** Conversion operator converts pointers of this const class
* To Class const sptr <t2>., where T2 is a different base
* Class. This is most often used when attempting to call
* Const method of the base class through a derived class
* Pointer. This is a workaround for sunpro.
*/

 

Template <class T2>
Operator const sptr <t2> () const
{
Return sptr <t2> (PTR, count );
}

 

 

/// Default constructor. Points to null.
Sptr (): PTR (0), count (0)
{}
;

 

/** Constructor used most often as the constructor from
* Plain pointer. Do not use this to convert a single pointer
* To a smart pointer multiple times -- this will result in
* Error (see class introduction for details ).
*/

 

Sptr (T * Original, countsemaphore * mycount = 0)
: PTR (original), count (mycount)
{
If (PTR)
{
Increment ();
}
};

/** Copy constructor
*/
Sptr (const sptr & X)
: PTR (X. PTR), count (X. Count)
{
Increment ();
};

/// Destructor
~ Sptr ()
{
{

Decrement ();

}
}

 

/// Dereference operator
T & operator * () const
{
Return * PTR;
}

///! Operator. returns true if PTR = 0, false otherwise.
Int Operator! () Const
{
If (PTR)
{
Return (PTR = 0 );
}
Else
Return true;
}

 

/// Pointer operator.
T * operator-> () const
{
Return PTR;
}

Template <class T2> sptr & dynamiccast (const sptr <t2> & X)
{
If (PTR = x. getptr () return * this;

Decrement ();
If (T * P = dynamic_cast <t *> (X. getptr ()))
{
Count = x. getcount ();
PTR = P;
Increment ();
}

Return * this;
}

 

/** Assignment operator -- this is most often used to assign
* From a smart pointer to a derived type to a smart pointer
* Of the base type.
*/
Template <class T2>
Sptr & operator = (const sptr <t2> & X)
{
If (PTR = x. getptr () return * this;

Decrement ();
PTR = x. getptr ();
Count = x. getcount ();
Increment ();

Return * this;
}

 

/** Assignment operator from plain pointer. Do not use this
* To convert a single pointer to a smart pointer multiple
* Times -- this will result in an error (see class
* Introduction for details ).
*/
Sptr & operator = (T * original)
{
If (PTR = original) return * this;

Decrement ();

PTR = original;
Increment ();

Return * this;
};

 

/// Assignment operator
Sptr & operator = (const sptr & X)
{
If (PTR = x. PTR) return * this;

Decrement ();
PTR = x. PTR;
Count = x. count;
Increment ();

Return * this;
}

 

/// Compare whether a pointer and a smart pointer point to different things
Friend bool Operator! = (Const void * y, const sptr & X)
{
If (X. PTR! = Y)
Return true;
Else
Return false;
}

/// Compare whether a smart pointer and a pointer point to different things
Friend bool Operator! = (Const sptr & X, const void * Y)
{
Return (y! = X );
}

 

/// Compare whether a pointer and a smart pointer point to the same thing
Friend bool operator = (const void * y, const sptr & X)
{
If (X. PTR = y)
Return true;
Else
Return false;
}

/// Compare whether a smart pointer and a pointer point to the same thing
Friend bool operator = (const sptr & X, const void * Y)
{
Return (y = X );
}

 

/// Compare whether two smart pointers point to the same thing
Bool operator = (const sptr & X) const
{
If (X. PTR = PTR)
Return true;
Else
Return false;
}

/// Compare whether two smart pointers point to the same thing
Bool Operator! = (Const sptr & X) const
{
Return! (Operator = (x ));
}

 

 

/**
This interface is here because it may sometimes be
Necessary. Do not use unless you must use it.
Get the value of the reference count of the smart pointer.
*/
Countsemaphore * getcount () const
{
Return count;
}

 

/**
This interface is here because it may sometimes be
Necessary. Do not use unless you must use it.
Get the pointer to which the smart pointer points.
*/
T * getptr () const
{
Return PTR;
}

};

//////////////////////////////////////// /////////////////////

Struct Stu
{
Int;
Int B;
};

Struct stuc
{
Int;
Int B;
};

 

Int _ tmain (INT argc, _ tchar * argv [])
{
Sptr <Stu> pstu = new Stu;

Sptr <Stu> pstuc = pstu;

Pstu-> A = 10;
Pstu-> B = 5;

 
Return 0;
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.