WinRT C++ Create a shallow copy of an object - microsoft-metro

According to Platform::Object Class docs ( http://msdn.microsoft.com/en-us/library/windows/apps/hh748265.aspx ) there is a method to create a shallow copy:
[MemberwiseClone] method
Creates a shallow copy of the current Object.
How to use it ? I have tried object->MemberwiseClone(), Object::MemberiwseClone(), without success ?
thanks

There is no such member function; the documentation is incorrect. If you want to make one of your types copyable, you can implement a constructor that makes a copy or a member function that returns a copy.
(I'll inform the appropriate people so that the documentation is corrected.)

Related

Copy MATLAB object of a handle class

I've implemented a handle class in MATLAB, and I've tried to copy an object of the class by calling the following two functions:
objByteArray = getByteStreamFromArray(obj);
newObj = getArrayFromByteStream(objByteArray);
But from time to time I get the following error:
Error using getArrayFromByteStream
Unable to read data stream because the data contains a bad version or endian-key
Is there another way to copy an object of a handle class?
Since you're working with a handle class, you can inherit your class from matlab.mixin.Copyable, which will give your class a customizable copy method.
By default, the copy method will implement a shallow copy of the class properties (i.e. if the class properties are themselves handle classes, the copies will be references to the original properties), but you can customize the copy operation to implement a deep copy (i.e. a copy operation is performed on the class properties as well).
Documentation for matlab.mixin.Copyable.

Is conditional binding in swift pass by value or reference?

I've been having some issues with conditional binding returning an invalid (but non-nil) object from the watch accelerometer. I was thinking maybe making a copy of the object could help the problem, but I wasn't sure if that was already occurring. If I use code such as:
if let data = recorder.accelerometerData(from: startDate, to: endDate){...}
is this already creating a copy of the CMSensorDataList object or am I simply getting a reference to it?
It just depends upon whether the type wrapped by the optional was a value type or reference type. If reference type, it's obviously pass by reference. If value type, it's copied (unless CoW, copy-on-write, in which case it's copied if and when it's mutated).
In this case, CMSensorDataList is a class, so it's a reference to that instance, not a copy of it.

How can getter/setter code be generated automatically for a class in Pharo or Squeak?

I have a long list of instance variables to create for a class that I want to generate the code for, rather than do it by hand. The list comes from an existing SQL database. My intention is to do it all in a pure object-oriented way with Smalltalk first, and as I learn more, save the data back to the database and work from it directly.
Is there a way of passing the list of names to method that will generate them and add them to the class definition?
In fact is there a way of adding or modifying class definitions dynamically in Smalltalk? I suspect there must and I would like to know a best practices approach.
Update: What I have in mind is more like passing a list of the instance variables to a method that will create them automatically.
It is more like:
addVariablesAndAccessors className: MyClass variablesList: ('aaaa', 'bbbb', 'cccc')
which will then result in a call to
AddVariables className: MyClass variableList: ('aaaa' 'bbbb' cccc')
and
generateAccessors className: MyClass variableList: ('aaaa' 'bbbb' cccc')
In OmniBrowser with the refactoring tools loaded you select the class and in the context menu Refactor class > Accessors.
Alternatively, if you only want to create an accessor for a single variable, select Refactor instance/class variable > Accessor, and select the variable you want to access.
In Squeak, you have Behavior>>addInstVarName: aString, so for instance, you could do something like:
String addInstVarName: 'foo'
Squeak also has refactoring support to generate accessors automatically. You can either use it directly or have a look at AbstractInstanceVariableRefactoring>>createAccessors to get some inspiration on how to implement your own ;-)
Another quite hacky but not so uncommon solution would be to just generate the instance variables, but instead of adding accessors, you overwrite doesNotUnderstand:, which gets called when an undefined selector is sent to your objects. There, you could check if you have an instance variable named according to the message, and return / change it if it is the case. Otherwise you just do super doesNotUnderstand: aMessage.
Regarding your comment: Classes are objects, too, so you don't have to do anything special to use them as parameters. On which class you add it is totally up to you and doesn't really matter. So a method to add instance variables could look like this:
addVariablesNamed: aCollection on: aClass
aCollection do: [:each | aClass addInstVarName: each]
and you could call it like this:
yourObject addVariablesNamed: #('foo' 'bar' 'baz') on: ClassX
You can find examples on how to generate accessor methods in the class CreateAccessorsForVariableRefactoring
In Squeak, open a Browser on the class. If you "right click" (I can never remember the button colours) the class name in the class list you'll get the standard context menu - "browse full (b)", and so on. Select "more..." and you'll see "create inst var accessors". Select that, and you'll get basic getters and setters for the instance variables.

How do you set the class of an object to something else?

I've seen this recently and now I can't find it …
How do you set the class of an object to something else?
--Update: Well, in Pharo! Like:
d:=Object new. d setClass: Dictionary.
Only that it isn't actually setClass. How can you modify the class pointer of an object?
There is #primitiveChangeClassTo:.
It requires that both original and target class have the same class layout. For some strange reason it expects an instance of the target class as parameter, which is however not used.
So you would do
d := Object new.
d primitiveChangeClassTo: Dictionary new.
however this fails, since dictionaries have two instance variables but plain objects have none.
If you are into meta-programming, you might also be interesting in using any object as a class. I used that in Protalk to realize a prototype based language that works directly on top of Smalltalk.
The method #setClass: is used in some specific contexts and with different implementations (Check it with the Method Finder).
Object has some helpers to conver the current object in other sort of, as for example #asOrderedCollection, because this last permit the operation:
asOrderedCollection
"Answer an OrderedCollection with the receiver as its only element."
^ OrderedCollection with: self
HTH.
ok, then you can try something as:
d := Object new.
e := Dictionary new.
d become: e.
But, please, try #become: with caution, because in lot of situations it break the image.
Take a look at Class ClassBuilder. It creates the a new class, when you modify a class, and then switches the instances of the former to instances of the later. Therefor it should provide some method that does, what you ask for.

Does Scala AnyRef.clone perform a shallow or deep copy?

In Scala, does AnyRef.clone perform a shallow or deep copy?
Short answer: shallow.
Not-so-short answer:
Unless it's overridden, AnyRef.clone() uses the Java's Object.clone() as its implementation.
Javadoc on Object.clone():
The method clone for class Object
performs a specific cloning operation.
First, if the class of this object
does not implement the interface
Cloneable, then a
CloneNotSupportedException is thrown.
Note that all arrays are considered to
implement the interface Cloneable.
Otherwise, this method creates a new
instance of the class of this object
and initializes all its fields with
exactly the contents of the
corresponding fields of this object,
as if by assignment; the contents of
the fields are not themselves cloned.
Thus, this method performs a "shallow
copy" of this object, not a "deep
copy" operation.
Please note:
AnyRef.clone(), like its counterpart in Java, has a "protected" access level, so its not callable from everywhere.
You will need to implement Cloneable in order for clone() to work.
Long answer: Read Effective Java, 2nd Edition, Item 11: Override clone judiciously
Summary: Don't use it. There are better alternatives.