1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 using System; 25 using System.Text; 26 27 namespace uno 28 { 29 30 /** This class can be used as a base class for UNO objects. 31 It implements the capability to be kept weakly 32 (unoidl.com.sun.star.uno.XWeak) and it implements 33 unoidl.com.sun.star.lang.XTypeProvider which is necessary for 34 using the object from StarBasic. 35 */ 36 public struct Any 37 { 38 private object _value; 39 private Type _type; 40 41 public static Any VOID = new Any(typeof(void), null); 42 checkArgsuno.Any43 private static void checkArgs(Type type, Object value) 44 { 45 //value can only be null if type == void 46 if (type == null 47 || (value == null 48 && type != typeof(void) 49 && type != typeof(object) 50 && type.IsInterface == false)) 51 throw new System.Exception( 52 "uno.Any: Constructor called with illegal arguments!"); 53 //If value is a polymorphic struct then type must be 54 //uno.Polymorphic 55 if (value != null) 56 { 57 TypeParametersAttribute t = (TypeParametersAttribute) Attribute.GetCustomAttribute( 58 value.GetType(), typeof(TypeParametersAttribute)); 59 if (t != null && !(type is PolymorphicType)) 60 throw new System.Exception( 61 "uno.Any: The value has a polymorphic type but the type argument is not " + 62 "uno.PolymorphicType. Please use the constructor Any(Type, object) and " + 63 "supply a uno.PolymorphicType as type argument!"); 64 } 65 } 66 67 /** constructs an instance. 68 69 <p>If the arguments ar invalid then an exception is thrown.</p> 70 @exception System.Exception 71 */ Anyuno.Any72 public Any(Type type, object value) 73 { 74 checkArgs(type, value); 75 _type = type; 76 _value = value; 77 } 78 79 /** sets the type and value. 80 <p>If the arguments ar invalid then an exception is thrown.</p> 81 @exception System.Exception 82 */ setValueuno.Any83 public void setValue(Type type, object value) 84 { 85 checkArgs(type, value); 86 _type = type; 87 _value = value; 88 } 89 90 public Type Type 91 { 92 get 93 { 94 if (_type == null) 95 _type = typeof(void); 96 return _type; 97 } 98 } 99 100 public Object Value 101 { 102 get 103 { 104 return _value; 105 } 106 } 107 Anyuno.Any108 public Any(char value): this(typeof(char), value) 109 { 110 } 111 Anyuno.Any112 public Any(bool value): this(typeof(bool), value) 113 { 114 } 115 Anyuno.Any116 public Any(byte value): this(typeof(byte), value) 117 { 118 } 119 Anyuno.Any120 public Any(short value): this(typeof(short), value) 121 { 122 } 123 Anyuno.Any124 public Any(ushort value): this(typeof(ushort), value) 125 { 126 } 127 Anyuno.Any128 public Any(int value): this(typeof(int), value) 129 { 130 } 131 Anyuno.Any132 public Any(uint value): this(typeof(uint), value) 133 { 134 } 135 Anyuno.Any136 public Any(long value): this(typeof(long), value) 137 { 138 } 139 Anyuno.Any140 public Any(ulong value): this(typeof(ulong), value) 141 { 142 } 143 Anyuno.Any144 public Any(float value): this(typeof(float), value) 145 { 146 } 147 Anyuno.Any148 public Any(double value): this(typeof(double), value) 149 { 150 } 151 Anyuno.Any152 public Any(Type value): this(typeof(Type), value) 153 { 154 } 155 Anyuno.Any156 public Any(string value): this(typeof(string), value) 157 { 158 } 159 ToStringuno.Any160 public override string ToString() 161 { 162 StringBuilder msg = new StringBuilder("uno.Any { Type= "); 163 msg.Append(Type.ToString()); 164 msg.Append(", Value="); 165 msg.Append(Value.ToString()); 166 msg.Append("}"); 167 return msg.ToString(); 168 } 169 hasValueuno.Any170 public bool hasValue() 171 { 172 if (Type == null || Type == typeof(void)) 173 return false; 174 return true; 175 } 176 Equalsuno.Any177 public override bool Equals(object obj) 178 { 179 if (obj != null) 180 { 181 try 182 { 183 return Equals((Any) obj); 184 } 185 catch (InvalidCastException) 186 { 187 } 188 } 189 return false; 190 } 191 Equalsuno.Any192 public bool Equals(Any obj) 193 { 194 return Type.Equals(obj.Type) 195 && (Value == null ? 196 obj.Value == null : 197 Value.Equals(obj.Value)); 198 } 199 GetHashCodeuno.Any200 public override int GetHashCode() 201 { 202 return Type.GetHashCode() ^ (Value != null ? Value.GetHashCode() : 0); 203 } 204 } 205 206 } 207 208