I'm creating a heterogenous circular linked list through inheritance. As a dynamically allocated data type, I need some form of deallocation, so I initially thought of Ada.Unchecked_Deallocation. Unfortunately, with the way that my access type works because the root Element is tagged and because I want to be able to use the same pointer type to access any object of the hierarchy, the following code snippet will not compile.
type Element is tagged private;
type ElementPtr is access all Element'Class;
-- fully define Element
procedure Free is new Ada.Unchecked_Deallocation(Element, ElementPtr);
Does anyone have any suggestions for alternate forms of deallocation that I can use to free the memory used by an object of any inherited Element type pointed to by an ElementPtr? Thanks!
Element and Element'Class are different types,
and you try to instantiate Unchecked_Deallocation with mismatching types.
Try instantiating with Element'Class instead:
procedure Free is new Ada.Unchecked_Deallocation(Element'Class, ElementPtr);
Related
I was reading about reflect.MakeFunc and was wondering if there's also a way to create a Method (a function with a receiver) at runtime.
No, this is not possible because the receiver's type method set would change on runtime if you did this. As you may know, Go in its current implementations is type checked at compile time. You would require runtime interface-implementation checks on every function-call that takes an interface argument if a type could suddenly acquire (or lose) methods at runtime.
a way to create a Method (a function with a receiver) at runtime
Technically, though, you could build a value representing a method attached to an arbitrary type by forking the reflect package. This would not, however, change said type's method set because it'd be essentially a hack around Go's type system.
What about swapping method pointers on an object?
Go, unlike e.g. Java does not embed a virtual method dispatch table in concrete values, only in interface values. If you're willing to get your hands dirty you could get a hold of a reflect.nonEmptyInterface and modify its itable (itab field).
I know the general philosophy behind std::unique_ptr<T> and std::shared_ptr<T>. A unique pointer should be used in cases where no other pointer variable will ask for access to the object / primitive data pointed to by the unique pointer. A shared pointer, however, exists for shared/concurrent access to a single resource such as a file.
This all remains true for class data members of pointer types as well of course. However, in particular regards at the implementation level, does this general rule cover all cases?
Assume that you have a class with 3 member variables, all of pointer type. If none of your member functions return a pointer to one of these variables (such as a getter function) then you should declare the member variable to be of type std::unique_ptr<T>. If however, you return one these member variables then you could potentially have a situation where another pointer tries to bind to the same resource. In which case, you should declare that particular member to be of type std::shared_ptr<T>.
This seems to be a logical rule to follow. I guess, what I'm trying to figure out is how to deal with smart pointers when declared as member variables because the decision is more difficult.
A unique pointer should be used in cases where no other pointer variable will ask for access to the object / primitive data pointed to by the unique pointer. A shared pointer, however, exists for shared/concurrent access to a single resource such as a file.
I have a different understanding. The difference between unique and shared ptr is not the access. It is the lifecycle.
unique_ptr doesn't support copy semantics so it always has a single owner.
So I think for a class member variable, it depends on whether you want the class instance to be the only owner of the lifecycle.
Another benefit of unique_ptr is that, in most cases, it has the same size as a raw ptr. So it is smaller and faster than shared_ptr.
I'm currently in the code generation phase of building a compiler for a Java-like language. I'm trying to understand how to implement dynamic dispatch for virtual methods.
I get how to build a virtual function table for every class and store a pointer to it in every object. What I don't get is- when generating code for a function call, how do you know what the offset is for that function in the table?
Thanks.
How do you know what anything is in your language? You write it down somewhere while parsing.
What I did in one of my toy languages was to keep a "vtable size" for each class, and when you add a new method to a class, you write down the vtable size as the offset of the method somewhere (i.e. you create a lookup table that maps method name to info about it, like its parameter types and its offset in the vtable), then add to the size to account for the newly-added method.
Of course, this assumes your language actually uses a vtable, like for example C++. If you use messaging in the style of Smalltalk or Objective-C, this table that you build actually gets saved to your compiled product and just used directly. Now a table look-up is slower than just accessing an offset directly, but also has the advantage that a caller does not need to know the type of an object to call a method on it, and you can easily add methods to objects without having to recompile the entire program.
Is there some way in Xcode to enter an object's name, and then get a list of the methods that can be used with that object, sorted either hierarchically by inheritance or by return value type?
I know you can get a list of methods by pressing Control-Space after entering the object's name in standard bracket messaging syntax; however, this list often contains numerous methods that are not applicable to the given object. As a result, you still have to scroll through a bunch of garbage, and then look up the class definitions for the object, or reference custom object implementations you have created in order to see which methods will work.
I want to create a base object that has only methods. The object would be QUEUABLE_OBJECT_TYPE and it will have an ENQUEUE method(s). The Payload of these messages (properties) would be added by subtyping this object.
I get an error that makes it sound like you cannot:
PLS-00589: no attributes found in object type "QUEUABLE_OBJECT_TYPE"
Does anyone know a way around this error? Or is it possible in the subtypes to hide this property of the supertype?
Either would be an acceptable answer.
Everything I've read suggests it is not possible to create a type without any attributes. Nor is it possible to hide a dummy attribute in a subtype. You may simply have to have an attribute in the master type, and utilise it - e.g. by making it identify the version of the type.
Oracle does provide some generic types, see documentation for details