1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir using System; 29*cdf0e10cSrcweir using System.Text; 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir namespace uno 32*cdf0e10cSrcweir { 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir /** This class can be used as a base class for UNO objects. 35*cdf0e10cSrcweir It implements the capability to be kept weakly 36*cdf0e10cSrcweir (unoidl.com.sun.star.uno.XWeak) and it implements 37*cdf0e10cSrcweir unoidl.com.sun.star.lang.XTypeProvider which is necessary for 38*cdf0e10cSrcweir using the object from StarBasic. 39*cdf0e10cSrcweir */ 40*cdf0e10cSrcweir public struct Any 41*cdf0e10cSrcweir { 42*cdf0e10cSrcweir private object _value; 43*cdf0e10cSrcweir private Type _type; 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir public static Any VOID = new Any(typeof(void), null); 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir private static void checkArgs(Type type, Object value) 48*cdf0e10cSrcweir { 49*cdf0e10cSrcweir //value can only be null if type == void 50*cdf0e10cSrcweir if (type == null 51*cdf0e10cSrcweir || (value == null 52*cdf0e10cSrcweir && type != typeof(void) 53*cdf0e10cSrcweir && type != typeof(object) 54*cdf0e10cSrcweir && type.IsInterface == false)) 55*cdf0e10cSrcweir throw new System.Exception( 56*cdf0e10cSrcweir "uno.Any: Constructor called with illegal arguments!"); 57*cdf0e10cSrcweir //If value is a polymorphic struct then type must be 58*cdf0e10cSrcweir //uno.Polymorphic 59*cdf0e10cSrcweir if (value != null) 60*cdf0e10cSrcweir { 61*cdf0e10cSrcweir TypeParametersAttribute t = (TypeParametersAttribute) Attribute.GetCustomAttribute( 62*cdf0e10cSrcweir value.GetType(), typeof(TypeParametersAttribute)); 63*cdf0e10cSrcweir if (t != null && !(type is PolymorphicType)) 64*cdf0e10cSrcweir throw new System.Exception( 65*cdf0e10cSrcweir "uno.Any: The value has a polymorphic type but the type argument is not " + 66*cdf0e10cSrcweir "uno.PolymorphicType. Please use the constructor Any(Type, object) and " + 67*cdf0e10cSrcweir "supply a uno.PolymorphicType as type argument!"); 68*cdf0e10cSrcweir } 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir /** constructs an instance. 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir <p>If the arguments ar invalid then an exception is thrown.</p> 74*cdf0e10cSrcweir @exception System.Exception 75*cdf0e10cSrcweir */ 76*cdf0e10cSrcweir public Any(Type type, object value) 77*cdf0e10cSrcweir { 78*cdf0e10cSrcweir checkArgs(type, value); 79*cdf0e10cSrcweir _type = type; 80*cdf0e10cSrcweir _value = value; 81*cdf0e10cSrcweir } 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir /** sets the type and value. 84*cdf0e10cSrcweir <p>If the arguments ar invalid then an exception is thrown.</p> 85*cdf0e10cSrcweir @exception System.Exception 86*cdf0e10cSrcweir */ 87*cdf0e10cSrcweir public void setValue(Type type, object value) 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir checkArgs(type, value); 90*cdf0e10cSrcweir _type = type; 91*cdf0e10cSrcweir _value = value; 92*cdf0e10cSrcweir } 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir public Type Type 95*cdf0e10cSrcweir { 96*cdf0e10cSrcweir get 97*cdf0e10cSrcweir { 98*cdf0e10cSrcweir if (_type == null) 99*cdf0e10cSrcweir _type = typeof(void); 100*cdf0e10cSrcweir return _type; 101*cdf0e10cSrcweir } 102*cdf0e10cSrcweir } 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir public Object Value 105*cdf0e10cSrcweir { 106*cdf0e10cSrcweir get 107*cdf0e10cSrcweir { 108*cdf0e10cSrcweir return _value; 109*cdf0e10cSrcweir } 110*cdf0e10cSrcweir } 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir public Any(char value): this(typeof(char), value) 113*cdf0e10cSrcweir { 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir public Any(bool value): this(typeof(bool), value) 117*cdf0e10cSrcweir { 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir public Any(byte value): this(typeof(byte), value) 121*cdf0e10cSrcweir { 122*cdf0e10cSrcweir } 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir public Any(short value): this(typeof(short), value) 125*cdf0e10cSrcweir { 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir public Any(ushort value): this(typeof(ushort), value) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir } 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir public Any(int value): this(typeof(int), value) 133*cdf0e10cSrcweir { 134*cdf0e10cSrcweir } 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir public Any(uint value): this(typeof(uint), value) 137*cdf0e10cSrcweir { 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir public Any(long value): this(typeof(long), value) 141*cdf0e10cSrcweir { 142*cdf0e10cSrcweir } 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir public Any(ulong value): this(typeof(ulong), value) 145*cdf0e10cSrcweir { 146*cdf0e10cSrcweir } 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir public Any(float value): this(typeof(float), value) 149*cdf0e10cSrcweir { 150*cdf0e10cSrcweir } 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir public Any(double value): this(typeof(double), value) 153*cdf0e10cSrcweir { 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir public Any(Type value): this(typeof(Type), value) 157*cdf0e10cSrcweir { 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir public Any(string value): this(typeof(string), value) 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir } 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir public override string ToString() 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir StringBuilder msg = new StringBuilder("uno.Any { Type= "); 167*cdf0e10cSrcweir msg.Append(Type.ToString()); 168*cdf0e10cSrcweir msg.Append(", Value="); 169*cdf0e10cSrcweir msg.Append(Value.ToString()); 170*cdf0e10cSrcweir msg.Append("}"); 171*cdf0e10cSrcweir return msg.ToString(); 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir public bool hasValue() 175*cdf0e10cSrcweir { 176*cdf0e10cSrcweir if (Type == null || Type == typeof(void)) 177*cdf0e10cSrcweir return false; 178*cdf0e10cSrcweir return true; 179*cdf0e10cSrcweir } 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir public override bool Equals(object obj) 182*cdf0e10cSrcweir { 183*cdf0e10cSrcweir if (obj != null) 184*cdf0e10cSrcweir { 185*cdf0e10cSrcweir try 186*cdf0e10cSrcweir { 187*cdf0e10cSrcweir return Equals((Any) obj); 188*cdf0e10cSrcweir } 189*cdf0e10cSrcweir catch (InvalidCastException) 190*cdf0e10cSrcweir { 191*cdf0e10cSrcweir } 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir return false; 194*cdf0e10cSrcweir } 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir public bool Equals(Any obj) 197*cdf0e10cSrcweir { 198*cdf0e10cSrcweir return Type.Equals(obj.Type) 199*cdf0e10cSrcweir && (Value == null ? 200*cdf0e10cSrcweir obj.Value == null : 201*cdf0e10cSrcweir Value.Equals(obj.Value)); 202*cdf0e10cSrcweir } 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir public override int GetHashCode() 205*cdf0e10cSrcweir { 206*cdf0e10cSrcweir return Type.GetHashCode() ^ (Value != null ? Value.GetHashCode() : 0); 207*cdf0e10cSrcweir } 208*cdf0e10cSrcweir } 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir } 211*cdf0e10cSrcweir 212