Пример #1
0
//-------------------------------------------------------------------
bool cpp_xloper::AsVariant(VARIANT &var)
{
   return xloper_to_vt(&m_Op, var, true);
}
Пример #2
0
//-------------------------------------------------------------------
// Returns true and a Variant equivalent to the passed-in xloper, or
// false if xloper's type could not be converted.
//-------------------------------------------------------------------
bool xloper_to_vt(const xloper *p_op, VARIANT &var, bool convert_array)
{
	VariantInit(&var); // type is set to VT_EMPTY

	switch(p_op->xltype)
	{
	case xltypeNum:
		var.vt = VT_R8;
		var.dblVal = p_op->val.num;
		break;

	case xltypeInt:
		var.vt = VT_I2;
		var.iVal = p_op->val.w;
		break;

	case xltypeBool:
		var.vt = VT_BOOL;
		var.boolVal = p_op->val.xbool;
		break;

	case xltypeStr:
		var.vt = VT_BSTR;
		var.bstrVal = xlstring_to_vt_bstr(p_op->val.str);
		break;

	case xltypeErr:
		var.vt = VT_ERROR;
		var.ulVal = VT_XL_ERR_OFFSET + p_op->val.err;
		break;

	case xltypeMulti:
		if(convert_array)
		{
			VARIANT temp_vt;
			SAFEARRAYBOUND bound[2];
			long elt_index[2];

			bound[0].lLbound = bound[1].lLbound = 0;
			bound[0].cElements = p_op->val.array.rows;
			bound[1].cElements = p_op->val.array.columns;

			var.vt = VT_ARRAY | VT_VARIANT; // array of Variants
			var.parray = SafeArrayCreate(VT_VARIANT, 2, bound);

			if(!var.parray)
				return false;

			xloper *p_op_temp = p_op->val.array.lparray;

			for(WORD r = 0; r < p_op->val.array.rows; r++)
			{
				for(WORD c = 0; c < p_op->val.array.columns;)
				{
// Call with last arg false, so not to convert array within array
					xloper_to_vt(p_op_temp++, temp_vt, false);
					elt_index[0] = r;
					elt_index[1] = c++;
					SafeArrayPutElement(var.parray, elt_index, &temp_vt);
				}
			}
			break;
		}
		// else, fall through to default option

	default: // type not converted
		return false;
	}
	return true;
}