"C + + meditation" Handle 1

Source: Internet
Author: User
Tags gety

1, in the "C + + meditation" proxy class, the use of proxy classes, there are problems:
A, agent replication, each time you create a copy, the cost is likely to be large
b, some objects can not easily create a copy, such as file
2, how to solve this problem?
Encapsulates a dynamic resource with a reference-count handle, a handle that contains pointers, and multiple handles can point to the same object. When copying, just copy the handle of the pointer.
3. Using a reference count handle is to avoid unnecessary copying of objects, so we need to know how many handles are bound to the current object, that is, the reference count,
This determines when resources can be freed.
4. Note that the reference count cannot be part of a handle, and if so, the current handle must know the other handles that point to the same object, and the reference count is consistent.
At the same time, the reference count cannot be part of the object, and if it does, it requires us to rewrite the object class that already exists.
5, take the point class as an example, the workaround is: Add a new class Upoint, including the point object and the reference count U, as follows:
Class Point
{
Public
Point (): _x (0), _y (0) {}
Point (int x,int y): _x (x), _y (y) {}
Point (const point& RHS): _x (rhs._x), _y (rhs._y) {}

point& SetX (int x)
{
_x = x;
return *this;
}

int GetX ()
{
return _x;
}

point& sety (int y)
{
_y = y;
return *this;
}

int GetY ()
{
return _y;
}

Private
int_x;
int_y;
};

#include "Point.h"

The purpose of Upoint is to encapsulate the point and reference count, which the user is not visible
Class Upoint
{
Friend class Handle_1;

Private
Point P;
int u;

Upoint (): U (1) {}

Upoint (int x,int y)
{
P.setx (x);
P.sety (y);
u = 1;
}

Upoint (const point& RHS)
{
p = RHS;
u = 1;
}
};

6, now consider the implementation of handle_1 details,
#include "U_point.h"

Class Handle_1
{
Public
Handle_1 (): _up (New Upoint) {}

handle_1 (int x,int y): _up (New Upoint (x, y)) {}

Handle_1 (const point& RHS): _up (New Upoint (RHS)) {}

~handle_1 ()
{
Subref ();
}

Copy construct, copy pointer, add Reference
Handle_1 (const handle_1& RHS)
{
AddRef (RHS._UP);
}

Copy assignment, reduce reference count on left, determine if delete, add reference count to right, consider self-assignment
handle_1& operator= (const handle_1& RHS)
{
if (This! = &RHS)
{
Subref ();
AddRef (RHS._UP);
}

return * this;
}


int GetX ()
{
return _up->p.getx ();
}

int GetY ()
{
return _up->p.gety ();
}

handle_1& SetX (int x)
{
_up->p.setx (x);
return *this;
}

handle_1& sety (int y)
{
_up->p.sety (y);
return *this;
}

Private
void AddRef (upoint* up)//copy pointer, add Reference
{
_up = up;
++_up->u;
}

void Subref ()//reduce references, determine if delete
{
if (--_up->u = = 0)
{
Delete _up;
}
}

Private
upoint* _up;
};

7, consider the following situation,
Handle_1 H1 (3,4);
Handle_1 H2 (H1);
H2. SetX (5);
int DD = H1. GetX ();
The value of DD is 5, which means that multiple handles point to the same object, avoiding unnecessary copying of objects and implementing pointer semantics. But for the above situation, is often not what the user expects,
How to solve this problem?
8. Use copy on write, re-create an object at each modification. That is, when the modification becomes value semantics, the original object H1 is an immutable object, and using H2 modification will result in the re-creation of an object.
As follows:
handle_1& SetX (int x)
{
_up->p.setx (x);
if (_up->u = = 1)//is currently the only reference
{
_up->p.setx (x);
}
Else
{
--_up->u;
_up = new Upoint (X,_up->p.gety ());
}
return *this;
}

handle_1& sety (int y)
{
_up->p.sety (y);
if (_up->u = = 1)//is currently the only reference
{
_up->p.sety (y);
}
Else
{
--_up->u;
_up = new Upoint (_up->p.getx (), y);
}
return *this;
}

"C + + meditation" Handle 1

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.