This commit is contained in:
warren%netscape.com 1999-04-22 08:27:34 +00:00
Родитель d550cc2e2e
Коммит d5baf9b75e
6 изменённых файлов: 989 добавлений и 0 удалений

Просмотреть файл

149
base/public/nsIVariant.h Normal file
Просмотреть файл

@ -0,0 +1,149 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef nsIVariant_h___
#define nsIVariant_h___
#include "nsISupports.h"
#include "nscore.h"
#include "prtime.h"
enum nsVariantType {
// primitive values
nsVariantType_PRBool,
nsVariantType_PRInt16,
nsVariantType_PRUint16,
nsVariantType_PRInt32,
nsVariantType_PRUint32,
nsVariantType_PRInt64,
nsVariantType_PRUint64,
nsVariantType_float,
nsVariantType_PRFloat64,
nsVariantType_PRTime,
// only pointers after this point -- these will be deleted
// when the variant is deleted
nsVariantType_voidPtr,
nsVariantType_charPtr,
nsVariantType_PRUnicharPtr,
};
class nsVariantValue {
public:
nsVariantValue() {}
// nsVariantValue(PRBool value) { mUnion._PRBool = value; }
nsVariantValue(PRInt16 value) { mUnion._PRInt16 = value; }
nsVariantValue(PRUint16 value) { mUnion._PRUint16 = value; }
nsVariantValue(PRInt32 value) { mUnion._PRInt32 = value; }
nsVariantValue(PRUint32 value) { mUnion._PRUint32 = value; }
nsVariantValue(PRInt64 value) { mUnion._PRInt64 = value; }
nsVariantValue(PRUint64 value) { mUnion._PRUint64 = value; }
nsVariantValue(float value) { mUnion._float = value; }
nsVariantValue(PRFloat64 value) { mUnion._PRFloat64 = value; }
// nsVariantValue(PRTime value) { mUnion._PRTime = value; }
nsVariantValue(void* value) { mUnion._voidPtr = value; }
nsVariantValue(char* value) { mUnion._charPtr = value; }
nsVariantValue(PRUnichar* value) { mUnion._PRUnicharPtr = value; }
// operator PRBool() { return mUnion._PRBool; }
operator PRInt16() { return mUnion._PRInt16; }
operator PRUint16() { return mUnion._PRUint16; }
operator PRInt32() { return mUnion._PRInt32; }
operator PRUint32() { return mUnion._PRUint32; }
operator PRInt64() { return mUnion._PRInt64; }
operator PRUint64() { return mUnion._PRUint64; }
operator float() { return mUnion._float; }
operator PRFloat64() { return mUnion._PRFloat64; }
// operator PRTime() { return mUnion._PRTime; }
operator void*() { return mUnion._voidPtr; }
operator const char*() { return mUnion._charPtr; }
operator const PRUnichar*() { return mUnion._PRUnicharPtr; }
friend class nsVariant;
protected:
union nsVariantValueUnion {
PRBool _PRBool;
PRInt16 _PRInt16;
PRUint16 _PRUint16;
PRInt32 _PRInt32;
PRUint32 _PRUint32;
PRInt64 _PRInt64;
PRUint64 _PRUint64;
float _float;
PRFloat64 _PRFloat64;
PRTime _PRTime;
void* _voidPtr;
char* _charPtr;
PRUnichar* _PRUnicharPtr;
};
nsVariantValueUnion mUnion;
};
#define NS_IVARIANT_IID \
{ /* 3b5799d0-dc28-11d2-9311-00e09805570f */ \
0x3b5799d0, \
0xdc28, \
0x11d2, \
{0x93, 0x11, 0x00, 0xe0, 0x98, 0x05, 0x57, 0x0f} \
}
class nsIVariant : public nsISupports {
public:
static const nsIID& GetIID() { static nsIID iid = NS_IVARIANT_IID; return iid; }
/**
* Gets the type and value of a Variant.
* When the value is a pointer type, the pointer points into the variant's
* internal storage. It is the caller's responsibility to copy.
*/
NS_IMETHOD GetValue(nsVariantType *type, nsVariantValue *value) = 0;
/**
* Gets the value of a Variant.
* @return NS_ERROR_FAILURE if the value is not of the expected type.
* When the value is a pointer type, the pointer points into the variant's
* internal storage. It is the caller's responsibility to copy.
*/
NS_IMETHOD GetValue(nsVariantType expectedType, nsVariantValue *value) = 0;
/**
* Sets the type and value of a Variant.
* When the value is a pointer type, the variant takes ownership of the
* pointer. When the variant is released, the pointer will be deleted.
*/
NS_IMETHOD SetValue(nsVariantType type, nsVariantValue& value) = 0;
/**
* Determines whether two variants have the same internal type and value.
* @return NS_OK if they are equal
* @return NS_COMFALSE if not, or if the other parameter is not an nsIVariant
*/
NS_IMETHOD Equals(nsISupports* other) = 0;
#ifdef NS_DEBUG
NS_IMETHOD GetDescription(char* *result) = 0;
#endif
};
extern NS_BASE nsresult
NS_NewIVariant(nsVariantType initialType, nsVariantValue& initialValue,
nsIVariant* *result);
#endif // nsIVariant_h___

231
base/src/nsScriptable.cpp Normal file
Просмотреть файл

@ -0,0 +1,231 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "nsIScriptable.h"
#include "nscore.h"
#include "xptcall.h"
#include "nsIInterfaceInfoManager.h"
class nsScriptable : public nsIScriptable {
public:
NS_DECL_ISUPPORTS
// nsIProperties methods:
NS_IMETHOD DefineProperty(const char* prop, nsISupports* initialValue);
NS_IMETHOD UndefineProperty(const char* prop);
NS_IMETHOD GetProperty(const char* prop, nsISupports* *result);
NS_IMETHOD SetProperty(const char* prop, nsISupports* value);
NS_IMETHOD HasProperty(const char* prop, nsISupports* value);
// nsIScriptable methods:
NS_IMETHOD Call(const char* command,
nsISupportsArray* arguments,
nsISupports* *result);
// nsScriptable methods:
nsScriptable(REFNSIID iid, nsISupports* object);
virtual ~nsScriptable();
nsresult Init();
// XXX should this be a method on nsIScriptable?
NS_IMETHOD QueryInterfaceScriptable(REFNSIID aIID, void** aInstancePtr);
// XXX later this will be a service
static nsIInterfaceInfoManager* gInterfaceInfoManager;
protected:
nsISupports* mObject;
nsIInterfaceInfo* mInterfaceInfo;
nsID mIID;
// nsXPCWrappedNativeClass* mClazz;
// nsXPCWrappedNative* mWrapper;
};
// XXX later this will be a service
nsIInterfaceInfoManager* nsScriptable::gInterfaceInfoManager = nsnull;
////////////////////////////////////////////////////////////////////////////////
nsScriptable::nsScriptable(REFNSIID iid, nsISupports* object)
: mObject(object), mInterfaceInfo(nsnull), mIID(iid)
// , mClazz(nsnull), mWrapper(nsnull)
{
NS_INIT_REFCNT();
NS_ADDREF(mObject);
}
nsScriptable::~nsScriptable()
{
NS_RELEASE(mObject);
}
NS_IMPL_ISUPPORTS(nsScriptable, nsIScriptable::GetIID());
nsresult
nsScriptable::Init()
{
nsresult rv;
if (gInterfaceInfoManager == nsnull) {
gInterfaceInfoManager = XPTI_GetInterfaceInfoManager();
if (gInterfaceInfoManager == nsnull)
return NS_ERROR_FAILURE;
}
// get the interface info
NS_IF_RELEASE(mInterfaceInfo);
rv = gInterfaceInfoManager->GetInfoForIID(&mIID, &mInterfaceInfo);
if (NS_FAILED(rv)) return rv;
return rv;
}
////////////////////////////////////////////////////////////////////////////////
// nsIProperties methods:
NS_IMETHODIMP
nsScriptable::DefineProperty(const char* prop, nsISupports* initialValue)
{
return NS_OK;
}
NS_IMETHODIMP
nsScriptable::UndefineProperty(const char* prop)
{
return NS_OK;
}
NS_IMETHODIMP
nsScriptable::GetProperty(const char* prop, nsISupports* *result)
{
return NS_OK;
}
NS_IMETHODIMP
nsScriptable::SetProperty(const char* prop, nsISupports* value)
{
return NS_OK;
}
NS_IMETHODIMP
nsScriptable::HasProperty(const char* prop, nsISupports* expectedValue)
{
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// nsIScriptable methods:
#define PARAM_BUFFER_COUNT 16
NS_IMETHODIMP
nsScriptable::Call(const char* methodName,
nsISupportsArray* arguments,
nsISupports* *result)
{
nsresult rv;
const nsXPTMethodInfo* methodInfo;
PRUint16 vtblIndex;
PRUint8 paramCount = arguments->Count();
nsXPTCVariant paramBuffer[PARAM_BUFFER_COUNT];
nsXPTCVariant* dispatchParams;
PRUint8 requiredArgs;
// get the method info and vtable index
rv = mInterfaceInfo->GetMethodInfoForName(methodName, &vtblIndex, &methodInfo);
if (NS_FAILED(rv)) return rv;
if (paramCount < PARAM_BUFFER_COUNT) {
dispatchParams = paramBuffer;
}
else {
dispatchParams = new nsXPTCVariant[paramCount];
if (dispatchParams == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
}
// put together the parameter list
for (PRUint8 i = 0; i < paramCount; i++) {
nsISupports* arg = (*arguments)[i];
const nsXPTParamInfo& paramInfo = methodInfo->GetParam(i);
const nsXPTType& paramType = paramInfo.GetType();
nsXPTCVariant* dp = &dispatchParams[i];
if (paramInfo.IsOut()) {
}
else {
}
switch (paramType.TagPart()) {
case nsXPTType::T_INTERFACE:
dp->flags |= nsXPTCVariant::VAL_IS_IFACE;
nsID* iid;
rv = mInterfaceInfo->GetIIDForParam(&paramInfo, &iid);
if (NS_FAILED(rv)) goto done;
break;
case nsXPTType::T_INTERFACE_IS:
break;
default:
break;
}
}
// invoke the method
rv = XPTC_InvokeByIndex(mObject, vtblIndex, paramCount, dispatchParams);
// return any out parameters
done:
if (dispatchParams != paramBuffer)
delete[] dispatchParams;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
NS_IMETHODIMP
nsScriptable::QueryInterfaceScriptable(REFNSIID aIID, void** aInstancePtr)
{
NS_PRECONDITION(aInstancePtr, "null aInstancePtr");
nsresult rv;
rv = mObject->QueryInterface(aIID, aInstancePtr);
if (NS_FAILED(rv)) return rv;
NS_RELEASE(mObject);
mObject = *(nsISupports**)aInstancePtr;
mIID = aIID;
return Init();
}
////////////////////////////////////////////////////////////////////////////////
extern nsresult
NS_NewIScriptable(REFNSIID iid, nsISupports* object, nsIScriptable* *result)
{
nsScriptable* s = new nsScriptable(iid, object);
if (s == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
nsresult rv = s->Init();
if (NS_FAILED(rv)) {
delete s;
return rv;
}
NS_ADDREF(s);
*result = s;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////

230
base/src/nsVariant.cpp Normal file
Просмотреть файл

@ -0,0 +1,230 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "nsIVariant.h"
#include "nsCRT.h"
#ifdef NS_DEBUG
#include "prprf.h"
#endif
class nsVariant : public nsIVariant {
public:
NS_DECL_ISUPPORTS
// nsIVariant methods:
NS_IMETHOD GetValue(nsVariantType *type, nsVariantValue *value);
NS_IMETHOD GetValue(nsVariantType expectedType, nsVariantValue *value);
NS_IMETHOD SetValue(nsVariantType type, nsVariantValue& value);
NS_IMETHOD Equals(nsISupports* other);
#ifdef NS_DEBUG
NS_IMETHOD GetDescription(char* *result);
#endif
// nsVariant methods:
nsVariant(nsVariantType type, nsVariantValue& value);
virtual ~nsVariant();
protected:
nsVariantType mType;
nsVariantValue mValue;
};
NS_IMPL_ISUPPORTS(nsVariant, nsIVariant::GetIID());
nsVariant::nsVariant(nsVariantType type, nsVariantValue& value)
: mType(type), mValue(value)
{
NS_INIT_REFCNT();
}
nsVariant::~nsVariant()
{
switch (mType) {
case nsVariantType_voidPtr:
delete (void*)mValue;
break;
case nsVariantType_charPtr:
nsCRT::free(mValue.mUnion._charPtr);
break;
case nsVariantType_PRUnicharPtr:
nsCRT::free(mValue.mUnion._PRUnicharPtr);
break;
default:
break;
}
}
NS_IMETHODIMP
nsVariant::GetValue(nsVariantType *type, nsVariantValue *value)
{
NS_PRECONDITION(type && value, "no place to put the result");
*type = mType;
*value = mValue;
return NS_OK;
}
NS_IMETHODIMP
nsVariant::GetValue(nsVariantType expectedType, nsVariantValue *value)
{
NS_PRECONDITION(value, "no place to put the result");
if (mType != expectedType)
return NS_ERROR_FAILURE;
*value = mValue;
return NS_OK;
}
NS_IMETHODIMP
nsVariant::SetValue(nsVariantType type, nsVariantValue& value)
{
mType = type;
mValue = value;
return NS_OK;
}
NS_IMETHODIMP
nsVariant::Equals(nsISupports* other)
{
nsIVariant* otherVariant;
nsresult rv = other->QueryInterface(nsIVariant::GetIID(), (void**)&otherVariant);
if (NS_FAILED(rv)) return NS_COMFALSE;
nsVariantType otherType;
nsVariantValue otherValue;
rv = otherVariant->GetValue(&otherType, &otherValue);
if (NS_FAILED(rv)) return rv;
if (mType != otherType)
return NS_COMFALSE;
PRBool eq = PR_FALSE;
// this is gross, but I think it's the only way to compare unions:
switch (mType) {
case nsVariantType_PRBool:
eq = (PRBool)mValue == (PRBool)otherValue;
break;
case nsVariantType_PRInt16:
eq = (PRInt16)mValue == (PRInt16)otherValue;
break;
case nsVariantType_PRUint16:
eq = (PRUint16)mValue == (PRUint16)otherValue;
break;
case nsVariantType_PRInt32:
eq = (PRInt32)mValue == (PRInt32)otherValue;
break;
case nsVariantType_PRUint32:
eq = (PRUint32)mValue == (PRUint32)otherValue;
break;
case nsVariantType_PRInt64:
eq = LL_EQ((PRInt64)mValue, (PRInt64)otherValue);
break;
case nsVariantType_PRUint64:
eq = LL_EQ((PRUint64)mValue, (PRUint64)otherValue);
break;
case nsVariantType_float:
eq = (float)mValue == (float)otherValue;
break;
case nsVariantType_PRFloat64:
eq = (PRFloat64)mValue == (PRFloat64)otherValue;
break;
case nsVariantType_PRTime:
eq = LL_EQ((PRTime)mValue, (PRTime)otherValue);
break;
case nsVariantType_voidPtr:
eq = (void*)mValue == (void*)otherValue;
break;
case nsVariantType_charPtr:
// I hope this shouldn't be comparing pointers:
eq = nsCRT::strcmp((const char*)mValue, (const char*)otherValue) == 0;
break;
case nsVariantType_PRUnicharPtr:
// I hope this shouldn't be comparing pointers:
eq = nsCRT::strcmp((const PRUnichar*)mValue, (const PRUnichar*)otherValue) == 0;
break;
default:
NS_ERROR("unknown variant type");
}
return eq ? NS_OK : NS_COMFALSE;
}
#ifdef NS_DEBUG
NS_IMETHODIMP
nsVariant::GetDescription(char* *result)
{
char* desc;
switch (mType) {
case nsVariantType_PRBool:
desc = nsCRT::strdup((PRBool)mValue ? "true" : "false");
break;
case nsVariantType_PRInt16:
desc = PR_smprintf("%d", (PRInt16)mValue);
break;
case nsVariantType_PRUint16:
desc = PR_smprintf("%u", (PRUint16)mValue);
break;
case nsVariantType_PRInt32:
desc = PR_smprintf("%l", (PRInt32)mValue);
break;
case nsVariantType_PRUint32:
desc = PR_smprintf("%u", (PRUint32)mValue);
break;
case nsVariantType_PRInt64:
desc = PR_smprintf("%ll", (PRInt64)mValue);
break;
case nsVariantType_PRUint64:
desc = PR_smprintf("%ll", (PRUint64)mValue);
break;
case nsVariantType_float:
desc = PR_smprintf("%g", (float)mValue);
break;
case nsVariantType_PRFloat64:
desc = PR_smprintf("%lg", (PRFloat64)mValue);
break;
case nsVariantType_PRTime:
desc = PR_smprintf("%l", (PRTime)mValue);
break;
case nsVariantType_voidPtr:
desc = PR_smprintf("0x%x", (void*)mValue);
break;
case nsVariantType_charPtr:
desc = PR_smprintf("'%s'", (const char*)mValue);
break;
case nsVariantType_PRUnicharPtr:
desc = PR_smprintf("\"%s\"", (const PRUnichar*)mValue);
break;
default:
desc = PR_smprintf("<Variant 0x%x>", this);
}
*result = desc;
return NS_OK;
}
#endif
////////////////////////////////////////////////////////////////////////////////
NS_BASE nsresult
NS_NewIVariant(nsVariantType initialType, nsVariantValue& initialValue,
nsIVariant* *result)
{
nsVariant* v = new nsVariant(initialType, initialValue);
if (v == NULL)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(v);
*result = v;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////

149
xpcom/ds/nsIVariant.h Normal file
Просмотреть файл

@ -0,0 +1,149 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef nsIVariant_h___
#define nsIVariant_h___
#include "nsISupports.h"
#include "nscore.h"
#include "prtime.h"
enum nsVariantType {
// primitive values
nsVariantType_PRBool,
nsVariantType_PRInt16,
nsVariantType_PRUint16,
nsVariantType_PRInt32,
nsVariantType_PRUint32,
nsVariantType_PRInt64,
nsVariantType_PRUint64,
nsVariantType_float,
nsVariantType_PRFloat64,
nsVariantType_PRTime,
// only pointers after this point -- these will be deleted
// when the variant is deleted
nsVariantType_voidPtr,
nsVariantType_charPtr,
nsVariantType_PRUnicharPtr,
};
class nsVariantValue {
public:
nsVariantValue() {}
// nsVariantValue(PRBool value) { mUnion._PRBool = value; }
nsVariantValue(PRInt16 value) { mUnion._PRInt16 = value; }
nsVariantValue(PRUint16 value) { mUnion._PRUint16 = value; }
nsVariantValue(PRInt32 value) { mUnion._PRInt32 = value; }
nsVariantValue(PRUint32 value) { mUnion._PRUint32 = value; }
nsVariantValue(PRInt64 value) { mUnion._PRInt64 = value; }
nsVariantValue(PRUint64 value) { mUnion._PRUint64 = value; }
nsVariantValue(float value) { mUnion._float = value; }
nsVariantValue(PRFloat64 value) { mUnion._PRFloat64 = value; }
// nsVariantValue(PRTime value) { mUnion._PRTime = value; }
nsVariantValue(void* value) { mUnion._voidPtr = value; }
nsVariantValue(char* value) { mUnion._charPtr = value; }
nsVariantValue(PRUnichar* value) { mUnion._PRUnicharPtr = value; }
// operator PRBool() { return mUnion._PRBool; }
operator PRInt16() { return mUnion._PRInt16; }
operator PRUint16() { return mUnion._PRUint16; }
operator PRInt32() { return mUnion._PRInt32; }
operator PRUint32() { return mUnion._PRUint32; }
operator PRInt64() { return mUnion._PRInt64; }
operator PRUint64() { return mUnion._PRUint64; }
operator float() { return mUnion._float; }
operator PRFloat64() { return mUnion._PRFloat64; }
// operator PRTime() { return mUnion._PRTime; }
operator void*() { return mUnion._voidPtr; }
operator const char*() { return mUnion._charPtr; }
operator const PRUnichar*() { return mUnion._PRUnicharPtr; }
friend class nsVariant;
protected:
union nsVariantValueUnion {
PRBool _PRBool;
PRInt16 _PRInt16;
PRUint16 _PRUint16;
PRInt32 _PRInt32;
PRUint32 _PRUint32;
PRInt64 _PRInt64;
PRUint64 _PRUint64;
float _float;
PRFloat64 _PRFloat64;
PRTime _PRTime;
void* _voidPtr;
char* _charPtr;
PRUnichar* _PRUnicharPtr;
};
nsVariantValueUnion mUnion;
};
#define NS_IVARIANT_IID \
{ /* 3b5799d0-dc28-11d2-9311-00e09805570f */ \
0x3b5799d0, \
0xdc28, \
0x11d2, \
{0x93, 0x11, 0x00, 0xe0, 0x98, 0x05, 0x57, 0x0f} \
}
class nsIVariant : public nsISupports {
public:
static const nsIID& GetIID() { static nsIID iid = NS_IVARIANT_IID; return iid; }
/**
* Gets the type and value of a Variant.
* When the value is a pointer type, the pointer points into the variant's
* internal storage. It is the caller's responsibility to copy.
*/
NS_IMETHOD GetValue(nsVariantType *type, nsVariantValue *value) = 0;
/**
* Gets the value of a Variant.
* @return NS_ERROR_FAILURE if the value is not of the expected type.
* When the value is a pointer type, the pointer points into the variant's
* internal storage. It is the caller's responsibility to copy.
*/
NS_IMETHOD GetValue(nsVariantType expectedType, nsVariantValue *value) = 0;
/**
* Sets the type and value of a Variant.
* When the value is a pointer type, the variant takes ownership of the
* pointer. When the variant is released, the pointer will be deleted.
*/
NS_IMETHOD SetValue(nsVariantType type, nsVariantValue& value) = 0;
/**
* Determines whether two variants have the same internal type and value.
* @return NS_OK if they are equal
* @return NS_COMFALSE if not, or if the other parameter is not an nsIVariant
*/
NS_IMETHOD Equals(nsISupports* other) = 0;
#ifdef NS_DEBUG
NS_IMETHOD GetDescription(char* *result) = 0;
#endif
};
extern NS_BASE nsresult
NS_NewIVariant(nsVariantType initialType, nsVariantValue& initialValue,
nsIVariant* *result);
#endif // nsIVariant_h___

230
xpcom/ds/nsVariant.cpp Normal file
Просмотреть файл

@ -0,0 +1,230 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "nsIVariant.h"
#include "nsCRT.h"
#ifdef NS_DEBUG
#include "prprf.h"
#endif
class nsVariant : public nsIVariant {
public:
NS_DECL_ISUPPORTS
// nsIVariant methods:
NS_IMETHOD GetValue(nsVariantType *type, nsVariantValue *value);
NS_IMETHOD GetValue(nsVariantType expectedType, nsVariantValue *value);
NS_IMETHOD SetValue(nsVariantType type, nsVariantValue& value);
NS_IMETHOD Equals(nsISupports* other);
#ifdef NS_DEBUG
NS_IMETHOD GetDescription(char* *result);
#endif
// nsVariant methods:
nsVariant(nsVariantType type, nsVariantValue& value);
virtual ~nsVariant();
protected:
nsVariantType mType;
nsVariantValue mValue;
};
NS_IMPL_ISUPPORTS(nsVariant, nsIVariant::GetIID());
nsVariant::nsVariant(nsVariantType type, nsVariantValue& value)
: mType(type), mValue(value)
{
NS_INIT_REFCNT();
}
nsVariant::~nsVariant()
{
switch (mType) {
case nsVariantType_voidPtr:
delete (void*)mValue;
break;
case nsVariantType_charPtr:
nsCRT::free(mValue.mUnion._charPtr);
break;
case nsVariantType_PRUnicharPtr:
nsCRT::free(mValue.mUnion._PRUnicharPtr);
break;
default:
break;
}
}
NS_IMETHODIMP
nsVariant::GetValue(nsVariantType *type, nsVariantValue *value)
{
NS_PRECONDITION(type && value, "no place to put the result");
*type = mType;
*value = mValue;
return NS_OK;
}
NS_IMETHODIMP
nsVariant::GetValue(nsVariantType expectedType, nsVariantValue *value)
{
NS_PRECONDITION(value, "no place to put the result");
if (mType != expectedType)
return NS_ERROR_FAILURE;
*value = mValue;
return NS_OK;
}
NS_IMETHODIMP
nsVariant::SetValue(nsVariantType type, nsVariantValue& value)
{
mType = type;
mValue = value;
return NS_OK;
}
NS_IMETHODIMP
nsVariant::Equals(nsISupports* other)
{
nsIVariant* otherVariant;
nsresult rv = other->QueryInterface(nsIVariant::GetIID(), (void**)&otherVariant);
if (NS_FAILED(rv)) return NS_COMFALSE;
nsVariantType otherType;
nsVariantValue otherValue;
rv = otherVariant->GetValue(&otherType, &otherValue);
if (NS_FAILED(rv)) return rv;
if (mType != otherType)
return NS_COMFALSE;
PRBool eq = PR_FALSE;
// this is gross, but I think it's the only way to compare unions:
switch (mType) {
case nsVariantType_PRBool:
eq = (PRBool)mValue == (PRBool)otherValue;
break;
case nsVariantType_PRInt16:
eq = (PRInt16)mValue == (PRInt16)otherValue;
break;
case nsVariantType_PRUint16:
eq = (PRUint16)mValue == (PRUint16)otherValue;
break;
case nsVariantType_PRInt32:
eq = (PRInt32)mValue == (PRInt32)otherValue;
break;
case nsVariantType_PRUint32:
eq = (PRUint32)mValue == (PRUint32)otherValue;
break;
case nsVariantType_PRInt64:
eq = LL_EQ((PRInt64)mValue, (PRInt64)otherValue);
break;
case nsVariantType_PRUint64:
eq = LL_EQ((PRUint64)mValue, (PRUint64)otherValue);
break;
case nsVariantType_float:
eq = (float)mValue == (float)otherValue;
break;
case nsVariantType_PRFloat64:
eq = (PRFloat64)mValue == (PRFloat64)otherValue;
break;
case nsVariantType_PRTime:
eq = LL_EQ((PRTime)mValue, (PRTime)otherValue);
break;
case nsVariantType_voidPtr:
eq = (void*)mValue == (void*)otherValue;
break;
case nsVariantType_charPtr:
// I hope this shouldn't be comparing pointers:
eq = nsCRT::strcmp((const char*)mValue, (const char*)otherValue) == 0;
break;
case nsVariantType_PRUnicharPtr:
// I hope this shouldn't be comparing pointers:
eq = nsCRT::strcmp((const PRUnichar*)mValue, (const PRUnichar*)otherValue) == 0;
break;
default:
NS_ERROR("unknown variant type");
}
return eq ? NS_OK : NS_COMFALSE;
}
#ifdef NS_DEBUG
NS_IMETHODIMP
nsVariant::GetDescription(char* *result)
{
char* desc;
switch (mType) {
case nsVariantType_PRBool:
desc = nsCRT::strdup((PRBool)mValue ? "true" : "false");
break;
case nsVariantType_PRInt16:
desc = PR_smprintf("%d", (PRInt16)mValue);
break;
case nsVariantType_PRUint16:
desc = PR_smprintf("%u", (PRUint16)mValue);
break;
case nsVariantType_PRInt32:
desc = PR_smprintf("%l", (PRInt32)mValue);
break;
case nsVariantType_PRUint32:
desc = PR_smprintf("%u", (PRUint32)mValue);
break;
case nsVariantType_PRInt64:
desc = PR_smprintf("%ll", (PRInt64)mValue);
break;
case nsVariantType_PRUint64:
desc = PR_smprintf("%ll", (PRUint64)mValue);
break;
case nsVariantType_float:
desc = PR_smprintf("%g", (float)mValue);
break;
case nsVariantType_PRFloat64:
desc = PR_smprintf("%lg", (PRFloat64)mValue);
break;
case nsVariantType_PRTime:
desc = PR_smprintf("%l", (PRTime)mValue);
break;
case nsVariantType_voidPtr:
desc = PR_smprintf("0x%x", (void*)mValue);
break;
case nsVariantType_charPtr:
desc = PR_smprintf("'%s'", (const char*)mValue);
break;
case nsVariantType_PRUnicharPtr:
desc = PR_smprintf("\"%s\"", (const PRUnichar*)mValue);
break;
default:
desc = PR_smprintf("<Variant 0x%x>", this);
}
*result = desc;
return NS_OK;
}
#endif
////////////////////////////////////////////////////////////////////////////////
NS_BASE nsresult
NS_NewIVariant(nsVariantType initialType, nsVariantValue& initialValue,
nsIVariant* *result)
{
nsVariant* v = new nsVariant(initialType, initialValue);
if (v == NULL)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(v);
*result = v;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////