1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is the Mozilla SMIL module.
16 : *
17 : * The Initial Developer of the Original Code is Brian Birtles.
18 : * Portions created by the Initial Developer are Copyright (C) 2006
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Robert O'Callahan <roc+moz@cs.cmu.edu>
23 : * Brian Birtles <birtles@gmail.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either of the GNU General Public License Version 2 or later (the "GPL"),
27 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsSMILValue.h"
40 : #include "nsDebug.h"
41 : #include <string.h>
42 :
43 : //----------------------------------------------------------------------
44 : // Public methods
45 :
46 0 : nsSMILValue::nsSMILValue(const nsISMILType* aType)
47 0 : : mType(&nsSMILNullType::sSingleton)
48 : {
49 0 : if (!aType) {
50 0 : NS_ERROR("Trying to construct nsSMILValue with null mType pointer");
51 0 : return;
52 : }
53 :
54 0 : InitAndCheckPostcondition(aType);
55 : }
56 :
57 0 : nsSMILValue::nsSMILValue(const nsSMILValue& aVal)
58 0 : : mType(&nsSMILNullType::sSingleton)
59 : {
60 0 : InitAndCheckPostcondition(aVal.mType);
61 0 : mType->Assign(*this, aVal);
62 0 : }
63 :
64 : const nsSMILValue&
65 0 : nsSMILValue::operator=(const nsSMILValue& aVal)
66 : {
67 0 : if (&aVal == this)
68 0 : return *this;
69 :
70 0 : if (mType != aVal.mType) {
71 0 : DestroyAndReinit(aVal.mType);
72 : }
73 :
74 0 : mType->Assign(*this, aVal);
75 :
76 0 : return *this;
77 : }
78 :
79 : bool
80 0 : nsSMILValue::operator==(const nsSMILValue& aVal) const
81 : {
82 0 : if (&aVal == this)
83 0 : return true;
84 :
85 0 : return mType == aVal.mType && mType->IsEqual(*this, aVal);
86 : }
87 :
88 : void
89 0 : nsSMILValue::Swap(nsSMILValue& aOther)
90 : {
91 0 : nsSMILValue tmp;
92 0 : memcpy(&tmp, &aOther, sizeof(nsSMILValue)); // tmp = aOther
93 0 : memcpy(&aOther, this, sizeof(nsSMILValue)); // aOther = this
94 0 : memcpy(this, &tmp, sizeof(nsSMILValue)); // this = tmp
95 :
96 : // |tmp| is about to die -- we need to clear its mType, so that its
97 : // destructor doesn't muck with the data we just transferred out of it.
98 0 : tmp.mType = &nsSMILNullType::sSingleton;
99 0 : }
100 :
101 : nsresult
102 0 : nsSMILValue::Add(const nsSMILValue& aValueToAdd, PRUint32 aCount)
103 : {
104 0 : if (aValueToAdd.mType != mType) {
105 0 : NS_ERROR("Trying to add incompatible types");
106 0 : return NS_ERROR_FAILURE;
107 : }
108 :
109 0 : return mType->Add(*this, aValueToAdd, aCount);
110 : }
111 :
112 : nsresult
113 0 : nsSMILValue::SandwichAdd(const nsSMILValue& aValueToAdd)
114 : {
115 0 : if (aValueToAdd.mType != mType) {
116 0 : NS_ERROR("Trying to add incompatible types");
117 0 : return NS_ERROR_FAILURE;
118 : }
119 :
120 0 : return mType->SandwichAdd(*this, aValueToAdd);
121 : }
122 :
123 : nsresult
124 0 : nsSMILValue::ComputeDistance(const nsSMILValue& aTo, double& aDistance) const
125 : {
126 0 : if (aTo.mType != mType) {
127 0 : NS_ERROR("Trying to calculate distance between incompatible types");
128 0 : return NS_ERROR_FAILURE;
129 : }
130 :
131 0 : return mType->ComputeDistance(*this, aTo, aDistance);
132 : }
133 :
134 : nsresult
135 0 : nsSMILValue::Interpolate(const nsSMILValue& aEndVal,
136 : double aUnitDistance,
137 : nsSMILValue& aResult) const
138 : {
139 0 : if (aEndVal.mType != mType) {
140 0 : NS_ERROR("Trying to interpolate between incompatible types");
141 0 : return NS_ERROR_FAILURE;
142 : }
143 :
144 0 : if (aResult.mType != mType) {
145 : // Outparam has wrong type
146 0 : aResult.DestroyAndReinit(mType);
147 : }
148 :
149 0 : return mType->Interpolate(*this, aEndVal, aUnitDistance, aResult);
150 : }
151 :
152 : //----------------------------------------------------------------------
153 : // Helper methods
154 :
155 : // Wrappers for nsISMILType::Init & ::Destroy that verify their postconditions
156 : void
157 0 : nsSMILValue::InitAndCheckPostcondition(const nsISMILType* aNewType)
158 : {
159 0 : aNewType->Init(*this);
160 0 : NS_ABORT_IF_FALSE(mType == aNewType,
161 : "Post-condition of Init failed. nsSMILValue is invalid");
162 0 : }
163 :
164 : void
165 0 : nsSMILValue::DestroyAndCheckPostcondition()
166 : {
167 0 : mType->Destroy(*this);
168 0 : NS_ABORT_IF_FALSE(IsNull(), "Post-condition of Destroy failed. "
169 : "nsSMILValue not null after destroying");
170 0 : }
171 :
172 : void
173 0 : nsSMILValue::DestroyAndReinit(const nsISMILType* aNewType)
174 : {
175 0 : DestroyAndCheckPostcondition();
176 0 : InitAndCheckPostcondition(aNewType);
177 0 : }
|