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