Back to index

lightning-sunbird  0.9+nobinonly
nsInt64.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nsInt64_h__
00039 #define nsInt64_h__
00040 
00041 #include "prlong.h"
00042 #include "nscore.h"
00043 
00049 // If you ever decide that you need to add a non-inline method to this
00050 // class, be sure to change the class declaration to "class NS_BASE
00051 // nsInt64".
00052 
00053 template<class T>
00054 class nsTInt64
00055 {
00056 public: //XXX should be private
00057     T mValue;
00058 
00059 public:
00063     nsTInt64(void) : mValue(LL_ZERO) {
00064     }
00065 
00069     nsTInt64(const PRInt32 aInt32) {
00070         LL_I2L(mValue, aInt32);
00071     }
00072 
00076     nsTInt64(const PRUint32 aUint32) {
00077         LL_UI2L(mValue, aUint32);
00078     }
00079 
00083     nsTInt64(const PRFloat64 aFloat64) {
00084         LL_D2L(mValue, aFloat64);
00085     }
00086 
00090     nsTInt64(const T aInt64) : mValue(aInt64) {
00091     }
00092 
00096     nsTInt64(const nsTInt64& aObject) : mValue(aObject.mValue) {
00097     }
00098 
00099     // ~nsTInt64(void) -- XXX destructor unnecessary
00100 
00104     const nsTInt64& operator =(const nsTInt64& aObject) {
00105         mValue = aObject.mValue;
00106         return *this;
00107     }
00108 
00112     operator PRInt32(void) const {
00113         PRInt32 result;
00114         LL_L2I(result, mValue);
00115         return result;
00116     }
00117 
00121     operator PRUint32(void) const {
00122         PRUint32 result;
00123         LL_L2UI(result, mValue);
00124         return result;
00125     }
00126 
00130     operator PRFloat64(void) const {
00131         PRFloat64 result;
00132         LL_L2D(result, mValue);
00133         return result;
00134     }
00135 
00139     operator T() const {
00140         return mValue;
00141     }
00142 
00146     const nsTInt64 operator -(void) {
00147         nsTInt64 result;
00148         LL_NEG(result.mValue, mValue);
00149         return result;
00150     }
00151 
00152     // Arithmetic operators
00153 
00157     nsTInt64& operator +=(const nsTInt64& aObject) {
00158         LL_ADD(mValue, mValue, aObject.mValue);
00159         return *this;
00160     }
00161 
00165     nsTInt64& operator -=(const nsTInt64& aObject) {
00166         LL_SUB(mValue, mValue, aObject.mValue);
00167         return *this;
00168     }
00169 
00173     nsTInt64& operator *=(const nsTInt64& aObject) {
00174         LL_MUL(mValue, mValue, aObject.mValue);
00175         return *this;
00176     }
00177 
00181     nsTInt64& operator /=(const nsTInt64& aObject) {
00182         LL_DIV(mValue, mValue, aObject.mValue);
00183         return *this;
00184     }
00185 
00189     nsTInt64& operator %=(const nsTInt64& aObject) {
00190         LL_MOD(mValue, mValue, aObject.mValue);
00191         return *this;
00192     }
00193 
00197     nsTInt64& operator <<=(int aCount) {
00198         LL_SHL(mValue, mValue, aCount);
00199         return *this;
00200     }
00201 
00205     nsTInt64& operator >>=(int aCount) {
00206         LL_SHR(mValue, mValue, aCount);
00207         return *this;
00208     }
00209 
00210     // Comparison operators
00214     inline const nsTInt64
00215     operator +(const nsTInt64& aObject2) const {
00216         return nsTInt64(*this) += aObject2;
00217     }
00218 
00222     inline const nsTInt64
00223     operator -(const nsTInt64& aObject2) const {
00224         return nsTInt64(*this) -= aObject2;
00225     }
00226 
00230     inline const nsTInt64
00231     operator *(const nsTInt64& aObject2) const {
00232         return nsTInt64(*this) *= aObject2;
00233     }
00234 
00238     inline const nsTInt64
00239     operator /(const nsTInt64& aObject2) const {
00240         return nsTInt64(*this) /= aObject2;
00241     }
00242 
00246     inline const nsTInt64
00247     operator %(const nsTInt64& aObject2) const {
00248         return nsTInt64(*this) %= aObject2;
00249     }
00250 
00254     inline const nsTInt64
00255     operator <<(int aCount) const {
00256         return nsTInt64(*this) <<= aCount;
00257     }
00258 
00262     inline const nsTInt64
00263     operator >>(int aCount) const {
00264         return nsTInt64(*this) >>= aCount;
00265     }
00266 
00270     inline PRBool
00271     operator ==(const nsTInt64& aObject2) const {
00272         return LL_EQ(mValue, aObject2.mValue);
00273     }
00274 
00275     inline PRBool
00276     operator ==(T aValue) const {
00277         return LL_EQ(mValue, aValue);
00278     }
00279 
00283     inline PRBool
00284     operator !=(const nsTInt64& aObject2) const {
00285         return LL_NE(mValue, aObject2.mValue);
00286     }
00287 
00288     inline PRBool
00289     operator !=(T aValue) const {
00290         return LL_NE(mValue, aValue);
00291     }
00292 
00296     inline const nsTInt64
00297     operator &(const nsTInt64& aObject2) const {
00298         return nsTInt64(*this) &= aObject2;
00299     }
00300 
00304     inline const nsTInt64
00305     operator |(const nsTInt64& aObject2) const {
00306         return nsTInt64(*this) |= aObject2;
00307     }
00308 
00312     inline const nsTInt64
00313     operator ^(const nsTInt64& aObject2) const {
00314         return nsTInt64(*this) ^= aObject2;
00315     }
00316 
00317     // Bitwise operators
00318 
00322     const nsTInt64 operator ~(void) const {
00323         nsTInt64 result;
00324         LL_NOT(result.mValue, mValue);
00325         return result;
00326     }
00327 
00331     nsTInt64& operator &=(const nsTInt64& aObject) {
00332         LL_AND(mValue, mValue, aObject.mValue);
00333         return *this;
00334     }
00335 
00339     nsTInt64& operator |=(const nsTInt64& aObject) {
00340         LL_OR(mValue, mValue, aObject.mValue);
00341         return *this;
00342     }
00343 
00347     nsTInt64& operator ^=(const nsTInt64& aObject) {
00348         LL_XOR(mValue, mValue, aObject.mValue);
00349         return *this;
00350     }
00351 
00352 
00356     PRBool operator!() const {
00357         return LL_IS_ZERO(mValue);
00358     }
00359     
00360 };
00361 
00362 typedef nsTInt64<PRInt64> nsInt64;
00363 typedef nsTInt64<PRUint64> nsUint64;
00364 
00368 inline PRBool
00369 operator >(const nsInt64& aObject1, const nsInt64& aObject2) {
00370     return LL_CMP(aObject1.mValue, >, aObject2.mValue);
00371 }
00372 
00373 inline PRBool
00374 operator >(const nsUint64& aObject1, const nsUint64& aObject2) {
00375     return LL_UCMP(aObject1.mValue, >, aObject2.mValue);
00376 }
00377 
00381 inline PRBool
00382 operator >=(const nsInt64& aObject1, const nsInt64& aObject2) {
00383     return ! LL_CMP(aObject1.mValue, <, aObject2.mValue);
00384 }
00385 
00386 inline PRBool
00387 operator >=(const nsUint64& aObject1, const nsUint64& aObject2) {
00388     return ! LL_UCMP(aObject1.mValue, <, aObject2.mValue);
00389 }
00390 
00394 inline PRBool 
00395 operator <(const nsInt64& aObject1, const nsInt64& aObject2) {
00396     return LL_CMP(aObject1.mValue, <, aObject2.mValue);
00397 }
00398 
00399 inline PRBool 
00400 operator <(const nsUint64& aObject1, const nsUint64& aObject2) {
00401     return LL_UCMP(aObject1.mValue, <, aObject2.mValue);
00402 }
00403 
00407 inline PRBool
00408 operator <=(const nsInt64& aObject1, const nsInt64& aObject2) {
00409     return ! LL_CMP(aObject1.mValue, >, aObject2.mValue);
00410 }
00411 
00412 inline PRBool
00413 operator <=(const nsUint64& aObject1, const nsUint64& aObject2) {
00414     return ! LL_UCMP(aObject1.mValue, >, aObject2.mValue);
00415 }
00416 
00417 #endif // nsInt64_h__