Hi,
How to compare two objects which are implemented in C module?
What I don't know is how to make sure that other object is of same type. Is it field "subtype", or? Where object type is stored and how it is assigned, BTW? Thanks! Regards, /Al
Usually if you need to know if an object is of a particular type from a C module, it's because you need to access the object's storage. In this case you should use the get_storage() function, which will either return the storage or NULL if the object is not of the required type. This works with subclasses etc.
If it's not because you need to access the storage, then you'll have to explain why you need to know the objects type. :-)
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2004-02-03 13:20: Subject: Object comparision in C module
Hi,
How to compare two objects which are implemented in C module?
What I don't know is how to make sure that other object is of same type.
Is it field "subtype", or? Where object type is stored and how it is assigned, BTW?
Thanks!
Regards, /Al
/ Brevbäraren
On Tue, Feb 03, 2004 at 01:25:06PM +0100, Marcus Comstedt (ACROSS) (Hail Ilpalazzo!) @ Pike (-) developers forum wrote:
Usually if you need to know if an object is of a particular type from a C module, it's because you need to access the object's storage.
Exactly because of this :) But it would be nice to (just) query the type too (so I can be sure that two objects are of same class).
In this case you should use the get_storage() function, which will either return the storage or NULL if the object is not of the required type.
Well... It requires argument of type "struct program" - where do I get this one (I don't have any end_program() calls in my module)?
Regards, /Al
Exactly because of this :) But it would be nice to (just) query the type too (so I can be sure that two objects are of same class).
Why do you need that? What if something inherits your program? Or have another class with a compatible API?
/ Mirar
Previous text:
2004-02-03 13:45: Subject: Re: Object comparision in C module
On Tue, Feb 03, 2004 at 01:25:06PM +0100, Marcus Comstedt (ACROSS) (Hail Ilpalazzo!) @ Pike (-) developers forum wrote:
Usually if you need to know if an object is of a particular type from a C module, it's because you need to access the object's storage.
Exactly because of this :) But it would be nice to (just) query the type too (so I can be sure that two objects are of same class).
In this case you should use the get_storage() function, which will either return the storage or NULL if the object is not of the required type.
Well... It requires argument of type "struct program" - where do I get this one (I don't have any end_program() calls in my module)?
Regards, /Al
/ Brevbäraren
On Tue, Feb 03, 2004 at 01:50:01PM +0100, Mirar @ Pike developers forum wrote:
Why do you need that? What if something inherits your program? Or have another class with a compatible API?
What if I don't want this "something" to inherit my program?
It is possible to compare types in Pike (typeof(A) == typeof(B)), so why it shouldn't be possible to do in C?
Regards, /Al
What if I don't want this "something" to inherit my program?
And why should you disallow that?
Please do not take this as me being contrary, I am just trying to figure out why you would not want that.
It is possible to compare types in Pike (typeof(A) == typeof(B)),
It is possible, but that pike code does not do that, it compares the interfaces.
get_storage is the correct way to do it in pike.
If you do not currently have a program pointer in your C-module, you might want to get one.
Otherwise it will be neigh-on impossible to compare the types.
/ Per Hedbor ()
Previous text:
2004-02-03 14:05: Subject: Re: Object comparision in C module
On Tue, Feb 03, 2004 at 01:50:01PM +0100, Mirar @ Pike developers forum wrote:
Why do you need that? What if something inherits your program? Or have another class with a compatible API?
What if I don't want this "something" to inherit my program?
It is possible to compare types in Pike (typeof(A) == typeof(B)), so why it shouldn't be possible to do in C?
Regards, /Al
/ Brevbäraren
| get_storage is the correct way to do it in pike.
I did mean in a pike C-module.
/ Per Hedbor ()
Previous text:
2004-02-03 14:10: Subject: Re: Object comparision in C module
What if I don't want this "something" to inherit my program?
And why should you disallow that?
Please do not take this as me being contrary, I am just trying to figure out why you would not want that.
It is possible to compare types in Pike (typeof(A) == typeof(B)),
It is possible, but that pike code does not do that, it compares the interfaces.
get_storage is the correct way to do it in pike.
If you do not currently have a program pointer in your C-module, you might want to get one.
Otherwise it will be neigh-on impossible to compare the types.
/ Per Hedbor ()
On Tue, Feb 03, 2004 at 02:15:02PM +0100, Per Hedbor () @ Pike (-) developers forum wrote:
What if I don't want this "something" to inherit my program?
And why should you disallow that?
Security reasons. External modules must have access only to explicitly allowed operations, and must not (be able) inherit anything which is not exlicitly allowed (so no class behavior may be modified "in transit").
Regards, /Al
Ok. Then, given an object x, check that x->prog == wanted_program.
You still need the program pointer, though.
/ Per Hedbor ()
Previous text:
2004-02-03 14:28: Subject: Re: Object comparision in C module
On Tue, Feb 03, 2004 at 02:15:02PM +0100, Per Hedbor () @ Pike (-) developers forum wrote:
What if I don't want this "something" to inherit my program?
And why should you disallow that?
Security reasons. External modules must have access only to explicitly allowed operations, and must not (be able) inherit anything which is not exlicitly allowed (so no class behavior may be modified "in transit").
Regards, /Al
/ Brevbäraren
Hmm... What are you creating?
/ Mirar
Previous text:
2004-02-03 14:28: Subject: Re: Object comparision in C module
On Tue, Feb 03, 2004 at 02:15:02PM +0100, Per Hedbor () @ Pike (-) developers forum wrote:
What if I don't want this "something" to inherit my program?
And why should you disallow that?
Security reasons. External modules must have access only to explicitly allowed operations, and must not (be able) inherit anything which is not exlicitly allowed (so no class behavior may be modified "in transit").
Regards, /Al
/ Brevbäraren
On Tue, Feb 03, 2004 at 03:10:02PM +0100, Mirar @ Pike developers forum wrote:
Hmm... What are you creating?
Some kind of middle-ware, which will be extended by modules (which, in turn, will be written in Pike). I want to limit access to mid-ware internals, and to Pike itself - i.e. no access to files, sockets, no redefinition of classes and interfaces (some midware calls accept objects), etc.
Regards, /Al
Seems like there's a need for a modifier that disallow inherits. Currently there's none so you need to think of what would happen if someone inherits your programs anyway.
/ Martin Stjernholm, Roxen IS
Previous text:
2004-02-03 14:28: Subject: Re: Object comparision in C module
On Tue, Feb 03, 2004 at 02:15:02PM +0100, Per Hedbor () @ Pike (-) developers forum wrote:
What if I don't want this "something" to inherit my program?
And why should you disallow that?
Security reasons. External modules must have access only to explicitly allowed operations, and must not (be able) inherit anything which is not exlicitly allowed (so no class behavior may be modified "in transit").
Regards, /Al
/ Brevbäraren
isn't it enough to define all methods with final, so they can't be replaced?
greetings, martin.
Maybe. Just don't forget to include final `->, `->=, `[] and `[]= too. But I noticed that this works:
class A { final int f() {return 1;} }
class B { private inherit A; }
class C { inherit B; int f() {return 2;} }
int main() { werror ("%O\n", C()->f()); }
And get_storage for A in C would still work here.
/ Martin Stjernholm, Roxen IS
Previous text:
2004-02-05 00:51: Subject: Re: Object comparision in C module
isn't it enough to define all methods with final, so they can't be replaced?
greetings, martin.
/ Brevbäraren
Inherit this:
class A { private int x = this_program==A?0:lambda(){ error("MEEP\n"); return 1; }(); }
/ Martin Nilsson (saturator)
Previous text:
2004-02-05 01:04: Subject: Re: Object comparision in C module
Maybe. Just don't forget to include final `->, `->=, `[] and `[]= too. But I noticed that this works:
class A { final int f() {return 1;} }
class B { private inherit A; }
class C { inherit B; int f() {return 2;} }
int main() { werror ("%O\n", C()->f()); }
And get_storage for A in C would still work here.
/ Martin Stjernholm, Roxen IS
Well... It requires argument of type "struct program" - where do I get this one (I don't have any end_program() calls in my module)?
So you are adding storage to the module itself and instantiating multiple copies of it? In that case you need to store Pike_compiler->new_program for future reference in your PIKE_MODULE_INIT function.
/ Marcus Comstedt (ACROSS) (Hail Ilpalazzo!)
Previous text:
2004-02-03 13:45: Subject: Re: Object comparision in C module
On Tue, Feb 03, 2004 at 01:25:06PM +0100, Marcus Comstedt (ACROSS) (Hail Ilpalazzo!) @ Pike (-) developers forum wrote:
Usually if you need to know if an object is of a particular type from a C module, it's because you need to access the object's storage.
Exactly because of this :) But it would be nice to (just) query the type too (so I can be sure that two objects are of same class).
In this case you should use the get_storage() function, which will either return the storage or NULL if the object is not of the required type.
Well... It requires argument of type "struct program" - where do I get this one (I don't have any end_program() calls in my module)?
Regards, /Al
/ Brevbäraren
Well... It requires argument of type "struct program" - where do I get this one (I don't have any end_program() calls in my module)?
If you don't have a program, why do you need to check if some argument is of this non-existing program?
/ Mirar
Previous text:
2004-02-03 13:45: Subject: Re: Object comparision in C module
On Tue, Feb 03, 2004 at 01:25:06PM +0100, Marcus Comstedt (ACROSS) (Hail Ilpalazzo!) @ Pike (-) developers forum wrote:
Usually if you need to know if an object is of a particular type from a C module, it's because you need to access the object's storage.
Exactly because of this :) But it would be nice to (just) query the type too (so I can be sure that two objects are of same class).
In this case you should use the get_storage() function, which will either return the storage or NULL if the object is not of the required type.
Well... It requires argument of type "struct program" - where do I get this one (I don't have any end_program() calls in my module)?
Regards, /Al
/ Brevbäraren
It's rather odd to have a module that in itself has multiple instances.
/ Per Hedbor ()
Previous text:
2004-02-03 14:29: Subject: Re: Object comparision in C module
On Tue, Feb 03, 2004 at 02:20:02PM +0100, Mirar @ Pike developers forum wrote:
If you don't have a program, why do you need to check if some argument is of this non-existing program?
I do have a program (module itself). I just don't have a call to end_program() :)
Regards, /Al
/ Brevbäraren
As I said before, modules are objects, not programs. Don't create more then one of a module.
If you want the module to be a program, you can use __module_value to have "YourModule" to be a program instead of an object.
/ Mirar
Previous text:
2004-02-03 14:29: Subject: Re: Object comparision in C module
On Tue, Feb 03, 2004 at 02:20:02PM +0100, Mirar @ Pike developers forum wrote:
If you don't have a program, why do you need to check if some argument is of this non-existing program?
I do have a program (module itself). I just don't have a call to end_program() :)
Regards, /Al
/ Brevbäraren
On Tue, Feb 03, 2004 at 03:10:02PM +0100, Mirar @ Pike developers forum wrote:
As I said before, modules are objects, not programs. Don't create more then one of a module.
OK, OK... I already changed that... It was my old module, I didn't know at that time that I am doing bad thing :)
Regards, /Al
pike-devel@lists.lysator.liu.se