1 /*
2  * This source file is part of libRocket, the HTML/CSS Interface Middleware
3  *
4  * For the latest information, see http://www.librocket.com
5  *
6  * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  *
26  */
27 
28 #include "precompiled.h"
29 #include "../../Include/Rocket/Core/ElementReference.h"
30 #include "../../Include/Rocket/Core/Element.h"
31 
32 namespace Rocket {
33 namespace Core {
34 
ElementReference(Element * _element)35 ElementReference::ElementReference(Element* _element)
36 {
37 	element = NULL;
38 	*this = _element;
39 }
40 
ElementReference(const ElementReference & copy)41 ElementReference::ElementReference(const ElementReference& copy)
42 {
43 	element = NULL;
44 	*this = copy;
45 }
46 
~ElementReference()47 ElementReference::~ElementReference()
48 {
49 	*this = NULL;
50 }
51 
52 // Returns true if this reference is bound to an element.
operator bool() const53 ElementReference::operator bool() const
54 {
55 	return (element != NULL);
56 }
57 
58 // Assigns a new element for this reference to point to.
operator =(Element * _element)59 ElementReference& ElementReference::operator=(Element* _element)
60 {
61 	if (_element != NULL)
62 		_element->AddReference();
63 
64 	if (element != NULL)
65 		element->RemoveReference();
66 
67 	element = _element;
68 	return *this;
69 }
70 
71 // Assigns a new element for this reference, from another reference.
operator =(const ElementReference & element_reference)72 ElementReference& ElementReference::operator=(const ElementReference& element_reference)
73 {
74 	*this = (element_reference.element);
75 	return *this;
76 }
77 
78 // Returns a reference to the underlying element.
operator *()79 Element* ElementReference::operator*()
80 {
81 	return element;
82 }
83 
84 // Returns a reference to the underlying element.
operator ->()85 Element* ElementReference::operator->()
86 {
87 	return element;
88 }
89 
90 // Equality operator for the reference. Used for STL containers.
operator ==(const ElementReference & rhs) const91 bool ElementReference::operator==(const ElementReference& rhs) const
92 {
93 	return element == rhs.element;
94 }
95 
96 // Equality operator for the reference.
operator ==(const Element * rhs) const97 bool ElementReference::operator==(const Element* rhs) const
98 {
99 	return element == rhs;
100 }
101 
102 // Less-than operator for the reference. Used for STL containers.
operator <(const ElementReference & rhs) const103 bool ElementReference::operator<(const ElementReference& rhs) const
104 {
105 	return element < rhs.element;
106 }
107 
108 // Inequality operator for the reference.
operator !=(const ElementReference & rhs) const109 bool ElementReference::operator!=(const ElementReference& rhs) const
110 {
111 	return element != rhs.element;
112 }
113 
114 // Inequality operator for the reference.
operator !=(const Element * rhs) const115 bool ElementReference::operator!=(const Element* rhs) const
116 {
117 	return element != rhs;
118 }
119 
120 }
121 }
122