Beispiel #1
0
        /// <summary>
        /// Takes the value, out puts it, and casts it to the type destType. We use heuristics at the moment to figure out what
        /// the reference is. We are forced to do this at the moment because in C++ there are points and also references to objects,
        /// a distinction not made in .NET...
        ///
        /// 1) If the object is an array (like int[]) then:
        ///    If the array is a member access from some obj, then we assume we need a pointer
        ///    If the array is off another array, then we assume we don't need a pointer.
        ///
        /// 2) If it is a class, we assume it needs to be dereferenced.
        ///
        /// 3) We assume no deref is required.
        ///
        /// A few consequences of this logic:
        ///   1) Any non .NET class doesn't get dereferenced. We are probably pretty safe here
        ///   2) Any array doesn't get dereferenced unless it is coming from a root object. So if you call a function that returns a pointer
        ///      to an array then you'll get the wrong thing.
        ///   3) Any object that is part of a class will get dereferenced, even if it doesn't need it. This can cause problems
        ///      when there is a sub-object that is completely contained.
        ///
        /// </summary>
        /// <param name="sourceValue"></param>
        /// <param name="expressionDestType"></param>
        /// <returns></returns>
        public static string CastToType(this IValue sourceValue, Expression expressionDestType, bool ignorePointer = false)
        {
            if (sourceValue == null || expressionDestType == null)
            {
                throw new ArgumentNullException("Cannot pass ource or dest type/vars as null");
            }

            string objRefForm = sourceValue.RawValue;

            if (!ignorePointer)
            {
                objRefForm = sourceValue.AsObjectReference(expressionDestType);
            }

            ///
            /// If the type is already there or if the type is an array, then we will do no conversion.
            /// Already there: not needed
            /// Array: vector or regular C style array - we don't know this deep in the code, so
            ///        conversion would probably make a mess of things!
            ///

            if (!RequiresConversion(expressionDestType.Type, sourceValue.Type) || expressionDestType.Type.IsArray)
            {
                return(objRefForm);
            }

            ///
            /// Type conversion required!
            ///

            StringBuilder bld = new StringBuilder();

            bld.AppendFormat("(({0}){1})", expressionDestType.Type.AsCPPType(), objRefForm);
            return(bld.ToString());
        }