copy - copies an object
copyProperty - copies each of the objects properties handling the differences in structs, arrays, simple objects, and objects.
there is no error handling what-so-ever. that means if an object is invalid it will blow up, if an object doesn't have the method "copy" and "copyProperty" it will blow up.
what I should do is add code that will just pass by reference any object that can't be copied (missing the two methods). However, I'm going to stop posting to this thread method updates unless it seems like people actually want this code.
I will post updates to the code at my blog (and its eventual replacement):
http://bill.coldfusionjournal.com
http://cf-bill.blogspot.com (will be my only cf blog once mxna aggregates atom feeds)
<cffunction name="copy" access="public" output="false">
<cfscript>
var params = getMetaData(this.init).parameters;
var i = 0;
var thisKey = "";
var args = structNew();
var thisVal = "";
for(i=1;i LTE ArrayLen(params); i=i+1){
thisKey = params[i].name;
thisVal = copyProperty(this[thisKey]);
args[thisKey] = thisVal;
}
return createObject("component",getMetaData(this).name).init(argumentCollection = args);
</cfscript>
</cffunction>
<cffunction name="copyProperty" access="public" output="false" returntype="any">
<cfargument name="prop" type="any" hint="property to duplicate">
<cfscript>
var thisVal = arguments.prop;
var keyList = "";
var i = 0;
var tempVal = "";
var tempStruct = structNew();
var tempArray = arrayNew(1);
if(isObject(thisVal)){
thisVal = thisVal.copy();
} else if(isStruct(thisVal)){
keyList = ListToArray(structKeyList(thisVal));
for(i=1;i LTE ArrayLen(keyList);i=i+1){
tempStruct[keyList[i]] = copyProperty(thisVal[i]);
}
thisVal = tempStruct;
}else if(isArray(thisVal)){
for(i=1;i LTE ArrayLen(thisVal);i=i+1){
tempArray[i] = copyProperty(thisVal[i]);
}
thisVal = tempArray;
}
return thisVal;
</cfscript>
</cffunction>
I have updated my "copy" method a little bit more.
It may not be perfect for every scenario but it works fairly well for what I need:
<cffunction name="copy" access="public" output="false">
<cfscript>
var params = getMetaData(this.init).parameters;
var i = 0;
var thisKey = "";
var args = structNew();
for(i=1;i LTE ArrayLen(params); i=i+1){
thisKey = params[i].name;
args[thisKey] = this[thisKey];
}
return createObject("component",getMetaData(this).name).init(argumentCollection = args);
</cfscript>
</cffunction>
Now, there are some limitations already, especially concerning properties that are objects. currently they aren't copied, they are just pointed at by reference. I will probably keep working on this till I resolve that issue (handling recursive copying).
But, for the time being this is a decent start.
On Apr 1, 2005 3:02 PM, Bill Rawlinson <[EMAIL PROTECTED]> wrote:well it turns out i was able to answer my own question:
<cffunction name="copy" access="public" output="false">
<cfreturn createObject("component",getMetaData(this).name).init(argumentCollection=variables.instance) />
</cffunction>
here is a link that tries to do more, and where I found out about the getmetadata method - however, the example code on the following page doesn't work as near as I can tell:
http://www.prismix.com/blog/archives/2004/08/coldfusion_cfc.cfm
becuase I couldn't reference the properties value of getMetaData(this)
If I figure out a way to actually reference the properties of an object as well I'll let you know.
BillOn Apr 1, 2005 2:53 PM, Jim Davis <[EMAIL PROTECTED]> wrote:----------------------------------------------------------I've been meaning to look into a generic clone method myself so this interests me as well.
As for getting the object type that's pretty simple using getMetaData() – but I think a true clone method will be more difficult than that.
Jim Davis
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] On Behalf Of Bill Rawlinson
Sent: Friday, April 01, 2005 2:26 PM
To: CFCDev@cfczone.org
Subject: [CFCDev] dynamically identifying an objects TYPE
Is there a way in CF to identify an objects type on the fly.I have 47! gmail invites,want one? ----------------------------------------------------------
for instance, say I have a generic object that has some objects inheirited from it.
In the generic object I would like to put a generic "copy" method that would work sort of like this:
<cffunction name="copy" output="false" returntype="any" />
<cfset var objectType = this.type />
<cfreturn createObject("component",objectType).init(argumentCollection = variables.instance)) />
</cffunction>
this probably isn't possible, but I figured I would ask. It would be much cleaner to have this simple copy operation in the base class than having to basically duplicate it in each inheirited class.
thanks
Bill
--
[EMAIL PROTECTED]
http://blog.rawlinson.us
You are subscribed to cfcdev. To unsubscribe, send an email to cfcdev@cfczone.org with the words 'unsubscribe cfcdev' as the subject of the email.
CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com).
An archive of the CFCDev list is available at
www.mail-archive.com/cfcdev@cfczone.org
You are subscribed to cfcdev. To unsubscribe, send an email to cfcdev@cfczone.org with the words 'unsubscribe cfcdev' as the subject of the email.
CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com).
An archive of the CFCDev list is available at
www.mail-archive.com/cfcdev@cfczone.org
--
[EMAIL PROTECTED]
http://blog.rawlinson.us
I have 47! gmail invites,want one?
--
[EMAIL PROTECTED]
http://blog.rawlinson.us
I have 47! gmail invites,want one?
--
[EMAIL PROTECTED]
http://blog.rawlinson.us
I have 47! gmail invites,want one?(¹ªÞ²æìr¸›yÛhq÷zôèº{.nÇ+‰·¬zwZ?隊[hq÷z÷s:'zŠàÂ+a¶°¢·lº{.nÇ+‰·œ}Ç^½«-…ë.n7œ¶‡í…ç¦j)ADB Þ¾++ºvòP™¢w°Ãs:'zŠàjwlºšh®×?o …\z,¶)àÃh²Ø§?Ê&Qv«r¯z‡í…à…7¯–+-ŠÆ¯j)ZnWš´pà šŠV«r¯yÊ&ýÇÜuëÜ}Ìè?ê+