Re: [Vala] Treating a uint as a reference type?
I tried a different variation and added copy_function and destroy_function explicitly: [CCode (has_copy_function=true, copy_function="entity_copy", has_destroy_function=true, destroy_function="entity_unref", default_value = "0")] public struct Entity : uint { internal static inline void copy(uint* src, uint* dst) { *dst = ref(*src); } internal static inline uint ref(uint id) { /*TODO*/ return id; } internal static inline void unref(uint data) { /*TODO*/ } Entity dup() { return this; } static void free(owned Entity e) { } } /// The above now properly automates ownership referencing and dereferncing, and even supports ownership transfer: static Entity mine; static void set_mine(owned Entity e) { mine = (owned) e; } /* C: */ void set_mine (Entity e) { Entity _tmp0_ = 0; _tmp0_ = e; e = NULL; entity_unref (mine); mine = _tmp0_; entity_unref (e); } The above is correct: I shouldn't ref the given entity e, since its ownership was transferred to me, and I'm transferring ownership to the "mine" variable. e gets unreffed, but it was set to the null entity before, so it's fine. The "e = NULL" is worrying. IN THEORY, a computer could exist where the null pointer is not 0. As it is, the C compiler warns about casting pointers to integers. Oh well. Tell me when a computer with a non-0 null pointer becomes popular. For transferring unowned objects: static Entity yours; static void set_yours(Entity e) { yours = e; } /*C:*/ void set_yours (Entity e) { Entity _tmp0_ = 0; Entity _tmp1_ = 0; _tmp0_ = e; entity_copy (&_tmp0_, &_tmp1_); entity_unref (yours); yours = _tmp1_; } The above is correct. Entity e is not owned, so before assigning it to the "yours" variable, we need to increment the refcount. We also need to free the previous value of "yours". Finally, copying unowned to unowned: static unowned Entity nobodys; static void set_nobodys(Entity e) { nobodys = e; } /* C: */ void set_nobodys (Entity e) { Entity _tmp0_ = 0; _tmp0_ = e; nobodys = _tmp0_; } A flat assignment, as expected from unowned to unowned assignment. On Thu, Jul 30, 2015 at 11:28 PM, Alan Manuel Gloria wrote: > I tried that, but it loses the automatic insertion of ref and unref > function calls. The entity_dup and entity_free functions are never called > in the example I gave, and Vala complains that it can't transfer ownership > using (owned) - there is no reference to transfer. > > Semantically, an Entity is a reference to a row in an entity component > system; it just happens to be implemented using uint. I want to do this > because when I delete a row in the ECS table, I want the row to be retained > as "deleted" until all references to it are dropped, so that I avoid the > "missile suddenly chases a new entity" problem. > > > > On Thu, Jul 30, 2015 at 10:56 PM, yannick inizan > wrote: > >> why don't you "subclass" uint ? >> >> public struct Entity : uint { >> // these methods are required at C-side. >> internal Entity dup() { return this; } >> internal void free() { delete &this; } >> // your code >> } >> >> 2015-07-30 16:43 GMT+02:00 Alan Manuel Gloria : >> >>> Hi Vala World, >>> >>> I was wondering if it's possible to implement a Vala class as a C-side >>> guint, but have Vala emit calls to a ref and unref function, and to >>> treat a >>> C-side 0 as a Vala-side null. >>> >>> Basically I want to make something like this: >>> >>> // Vala >>> static >>> Entity? mine = null; >>> >>> static >>> void >>> set_mine(owned Entity e) { >>> mine = (owned) e; >>> } >>> >>> static >>> void >>> do_stuff() { >>> Entity e = Entity.create(); >>> set_mine(e); >>> } >>> >>> And get it compiled to C: >>> >>> /* C */ >>> guint mine = NULL; >>> void >>> set_mine(guint e) { >>> if (mine != NULL) entity_unref(mine); >>> mine = e; >>> } >>> >>> void >>> do_stuff() { >>> guint e = entity_create(); >>> set_mine(entity_ref(e)); >>> } >>> >>> So far, I've tried using a [Compact][CCode (cname="guint")] class, but >>> that >>> is passed around as a guint* and Vala feels obliged to add a "typedef >>> struct _guint guint". which fails since guint is already defined as >>> unsigned int. >>> >>> I've also tried using a [CCode (cname="guint")] extern struct, but that >>> causes any ref_function and unref_function attributes to be ignored, and >>> still adds "typedef struct _guint guint". >>> >>> Is it possible to have Vala remove the "*" in reference types and still >>> have it use ref/unref functions and have it remove typedef? >>> >>> -- >>> >>> I'm trying to make an Entity Component System, and ensure that destroyed >>> entities are not reused until all references to them are destroyed. >>> Entities are normally implemented as simple integer ID's, which are >>> semantically a pointer to a row of component slots. >>> >>> My current design has each attached compone
Re: [Vala] Treating a uint as a reference type?
On Fri, Jul 31, 2015 at 1:00 AM, Al Thomas wrote: > > From: Alan Manuel Gloria > > Semantically, an Entity is a reference to a row in an entity component > > system; it just happens to be implemented using uint. > > > This kind of sounds like referential integrity rules in > a relational database. I presume you not using a database > for performance and/or memory issues? > > Yes. > > > I want to do this> because when I delete a row in the ECS table, I want > the row to be retained > > as "deleted" until all references to it are dropped, so that I avoid > > the "missile suddenly chases a new entity" problem. > > By 'retained as "deleted"' do you mean changing the state of the row so it > can > no longer be used when a new entity is created? Would it cause too much of > a > performance hit if the row had an 'allow new references' boolean that > caused > a newly created entity to skip it if the row was closed? > The refcount would serve as something like that; if the refcount drops to 0, the row can be reused, otherwise not. > Another thought is GLib signals, these are Vala signals, with a return > value. > So you would emit an I am about to delete xyz referenced component and if > anything returns no don't do that the component is kept. I've not used > return > values so not sure how this would work and is a bit more heavy weight > compared > to reference counting. > > Just a few thoughts. I got the impression it could be a design issue rather > than an implementation one. > I'd prefer to keep it automated; keeping a reference to the Entity should be enough to keep the row alive, and in each subsystem that is interested, query the liveness. class TrackingMissile: Component { public Entity target; } class TrackingMissileSys: Subsystem { protected override void run() { foreach(Entity e in component_column()) { TrackingMissile tm = e.get(); if (!tm.target.is_live()) { // missile loses its ability to track anything once target is dead. e.detach(); continue; } // ... track target here ... } } } Of course, maybe signals would also work; maybe I could add hooks for attaching and detaching components to entities. > > All the best, > > > > Al > ___ > vala-list mailing list > vala-list@gnome.org > https://mail.gnome.org/mailman/listinfo/vala-list > ___ vala-list mailing list vala-list@gnome.org https://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] Treating a uint as a reference type?
> From: Alan Manuel Gloria > Semantically, an Entity is a reference to a row in an entity component > system; it just happens to be implemented using uint. This kind of sounds like referential integrity rules in a relational database. I presume you not using a database for performance and/or memory issues? > I want to do this> because when I delete a row in the ECS table, I want the > row to be retained > as "deleted" until all references to it are dropped, so that I avoid > the "missile suddenly chases a new entity" problem. By 'retained as "deleted"' do you mean changing the state of the row so it can no longer be used when a new entity is created? Would it cause too much of a performance hit if the row had an 'allow new references' boolean that caused a newly created entity to skip it if the row was closed? Another thought is GLib signals, these are Vala signals, with a return value. So you would emit an I am about to delete xyz referenced component and if anything returns no don't do that the component is kept. I've not used return values so not sure how this would work and is a bit more heavy weight compared to reference counting. Just a few thoughts. I got the impression it could be a design issue rather than an implementation one. All the best, Al ___ vala-list mailing list vala-list@gnome.org https://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] Treating a uint as a reference type?
Maybe defining an IntegerRank it would work Enviado desde Yahoo Mail en Android De:"Alan Manuel Gloria" Fecha:jue., jul. 30, AM a 10:28 AM Asunto:Re: [Vala] Treating a uint as a reference type? I tried that, but it loses the automatic insertion of ref and unref function calls. The entity_dup and entity_free functions are never called in the example I gave, and Vala complains that it can't transfer ownership using (owned) - there is no reference to transfer. Semantically, an Entity is a reference to a row in an entity component system; it just happens to be implemented using uint. I want to do this because when I delete a row in the ECS table, I want the row to be retained as "deleted" until all references to it are dropped, so that I avoid the "missile suddenly chases a new entity" problem. On Thu, Jul 30, 2015 at 10:56 PM, yannick inizan wrote: > why don't you "subclass" uint ? > > public struct Entity : uint { > // these methods are required at C-side. > internal Entity dup() { return this; } > internal void free() { delete &this; } > // your code > } > > 2015-07-30 16:43 GMT+02:00 Alan Manuel Gloria : > >> Hi Vala World, >> >> I was wondering if it's possible to implement a Vala class as a C-side >> guint, but have Vala emit calls to a ref and unref function, and to treat >> a >> C-side 0 as a Vala-side null. >> >> Basically I want to make something like this: >> >> // Vala >> static >> Entity? mine = null; >> >> static >> void >> set_mine(owned Entity e) { >> mine = (owned) e; >> } >> >> static >> void >> do_stuff() { >> Entity e = Entity.create(); >> set_mine(e); >> } >> >> And get it compiled to C: >> >> /* C */ >> guint mine = NULL; >> void >> set_mine(guint e) { >> if (mine != NULL) entity_unref(mine); >> mine = e; >> } >> >> void >> do_stuff() { >> guint e = entity_create(); >> set_mine(entity_ref(e)); >> } >> >> So far, I've tried using a [Compact][CCode (cname="guint")] class, but >> that >> is passed around as a guint* and Vala feels obliged to add a "typedef >> struct _guint guint". which fails since guint is already defined as >> unsigned int. >> >> I've also tried using a [CCode (cname="guint")] extern struct, but that >> causes any ref_function and unref_function attributes to be ignored, and >> still adds "typedef struct _guint guint". >> >> Is it possible to have Vala remove the "*" in reference types and still >> have it use ref/unref functions and have it remove typedef? >> >> -- >> >> I'm trying to make an Entity Component System, and ensure that destroyed >> entities are not reused until all references to them are destroyed. >> Entities are normally implemented as simple integer ID's, which are >> semantically a pointer to a row of component slots. >> >> My current design has each attached component add a reference to the >> entity, and the entity manager also adds a reference; when an entity is >> destroyed, the entity manager detaches all components (which cause each >> component to drop the reference) and drops its own reference; this frees >> the entity unless another reference is kept elsewhere. If a reference to >> an entity is kept elsewhere, it can then be queried as to whether it has >> been destroyed without worrying that its ID has been re-allocated to a >> freshly-made entity (for instance, consider a missile locked on to some >> game entity; if the target dies, we want the missile to know it has no >> longer any valid target, not have it suddenly chase another entity that >> happened to get allocated the same ID as the missile's original target in >> a >> different subsystem). >> >> In my current design entities are compact classes with an ID and a >> refcount >> and custom ref_function and unref_function, and freed entities are >> retained >> in a freelist to reuse the ID. I was wondering if I could make entities >> just the ID itself, and store the refcount in a separate array. >> >> I could possibly live with passing around Entity*, and use (guintptr) to >> extract the ID from the pointer, but I worry since C does not guarantee >> that a pointer can retain all possible guintptr values. guintptr can hold >> any pointer type, but a pointer type is not necessarily capable of holding >> any guintptr. >> >> Sincerely, >> AmkG >> ___ >> vala-list mailing list >> vala-list@gnome.org >> https://mail.gnome.org/mailman/listinfo/vala-list >> > > ___ vala-list mailing list vala-list@gnome.org https://mail.gnome.org/mailman/listinfo/vala-list ___ vala-list mailing list vala-list@gnome.org https://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] Treating a uint as a reference type?
I tried that, but it loses the automatic insertion of ref and unref function calls. The entity_dup and entity_free functions are never called in the example I gave, and Vala complains that it can't transfer ownership using (owned) - there is no reference to transfer. Semantically, an Entity is a reference to a row in an entity component system; it just happens to be implemented using uint. I want to do this because when I delete a row in the ECS table, I want the row to be retained as "deleted" until all references to it are dropped, so that I avoid the "missile suddenly chases a new entity" problem. On Thu, Jul 30, 2015 at 10:56 PM, yannick inizan wrote: > why don't you "subclass" uint ? > > public struct Entity : uint { > // these methods are required at C-side. > internal Entity dup() { return this; } > internal void free() { delete &this; } > // your code > } > > 2015-07-30 16:43 GMT+02:00 Alan Manuel Gloria : > >> Hi Vala World, >> >> I was wondering if it's possible to implement a Vala class as a C-side >> guint, but have Vala emit calls to a ref and unref function, and to treat >> a >> C-side 0 as a Vala-side null. >> >> Basically I want to make something like this: >> >> // Vala >> static >> Entity? mine = null; >> >> static >> void >> set_mine(owned Entity e) { >> mine = (owned) e; >> } >> >> static >> void >> do_stuff() { >> Entity e = Entity.create(); >> set_mine(e); >> } >> >> And get it compiled to C: >> >> /* C */ >> guint mine = NULL; >> void >> set_mine(guint e) { >> if (mine != NULL) entity_unref(mine); >> mine = e; >> } >> >> void >> do_stuff() { >> guint e = entity_create(); >> set_mine(entity_ref(e)); >> } >> >> So far, I've tried using a [Compact][CCode (cname="guint")] class, but >> that >> is passed around as a guint* and Vala feels obliged to add a "typedef >> struct _guint guint". which fails since guint is already defined as >> unsigned int. >> >> I've also tried using a [CCode (cname="guint")] extern struct, but that >> causes any ref_function and unref_function attributes to be ignored, and >> still adds "typedef struct _guint guint". >> >> Is it possible to have Vala remove the "*" in reference types and still >> have it use ref/unref functions and have it remove typedef? >> >> -- >> >> I'm trying to make an Entity Component System, and ensure that destroyed >> entities are not reused until all references to them are destroyed. >> Entities are normally implemented as simple integer ID's, which are >> semantically a pointer to a row of component slots. >> >> My current design has each attached component add a reference to the >> entity, and the entity manager also adds a reference; when an entity is >> destroyed, the entity manager detaches all components (which cause each >> component to drop the reference) and drops its own reference; this frees >> the entity unless another reference is kept elsewhere. If a reference to >> an entity is kept elsewhere, it can then be queried as to whether it has >> been destroyed without worrying that its ID has been re-allocated to a >> freshly-made entity (for instance, consider a missile locked on to some >> game entity; if the target dies, we want the missile to know it has no >> longer any valid target, not have it suddenly chase another entity that >> happened to get allocated the same ID as the missile's original target in >> a >> different subsystem). >> >> In my current design entities are compact classes with an ID and a >> refcount >> and custom ref_function and unref_function, and freed entities are >> retained >> in a freelist to reuse the ID. I was wondering if I could make entities >> just the ID itself, and store the refcount in a separate array. >> >> I could possibly live with passing around Entity*, and use (guintptr) to >> extract the ID from the pointer, but I worry since C does not guarantee >> that a pointer can retain all possible guintptr values. guintptr can hold >> any pointer type, but a pointer type is not necessarily capable of holding >> any guintptr. >> >> Sincerely, >> AmkG >> ___ >> vala-list mailing list >> vala-list@gnome.org >> https://mail.gnome.org/mailman/listinfo/vala-list >> > > ___ vala-list mailing list vala-list@gnome.org https://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] Treating a uint as a reference type?
why don't you "subclass" uint ? public struct Entity : uint { // these methods are required at C-side. internal Entity dup() { return this; } internal void free() { delete &this; } // your code } 2015-07-30 16:43 GMT+02:00 Alan Manuel Gloria : > Hi Vala World, > > I was wondering if it's possible to implement a Vala class as a C-side > guint, but have Vala emit calls to a ref and unref function, and to treat a > C-side 0 as a Vala-side null. > > Basically I want to make something like this: > > // Vala > static > Entity? mine = null; > > static > void > set_mine(owned Entity e) { > mine = (owned) e; > } > > static > void > do_stuff() { > Entity e = Entity.create(); > set_mine(e); > } > > And get it compiled to C: > > /* C */ > guint mine = NULL; > void > set_mine(guint e) { > if (mine != NULL) entity_unref(mine); > mine = e; > } > > void > do_stuff() { > guint e = entity_create(); > set_mine(entity_ref(e)); > } > > So far, I've tried using a [Compact][CCode (cname="guint")] class, but that > is passed around as a guint* and Vala feels obliged to add a "typedef > struct _guint guint". which fails since guint is already defined as > unsigned int. > > I've also tried using a [CCode (cname="guint")] extern struct, but that > causes any ref_function and unref_function attributes to be ignored, and > still adds "typedef struct _guint guint". > > Is it possible to have Vala remove the "*" in reference types and still > have it use ref/unref functions and have it remove typedef? > > -- > > I'm trying to make an Entity Component System, and ensure that destroyed > entities are not reused until all references to them are destroyed. > Entities are normally implemented as simple integer ID's, which are > semantically a pointer to a row of component slots. > > My current design has each attached component add a reference to the > entity, and the entity manager also adds a reference; when an entity is > destroyed, the entity manager detaches all components (which cause each > component to drop the reference) and drops its own reference; this frees > the entity unless another reference is kept elsewhere. If a reference to > an entity is kept elsewhere, it can then be queried as to whether it has > been destroyed without worrying that its ID has been re-allocated to a > freshly-made entity (for instance, consider a missile locked on to some > game entity; if the target dies, we want the missile to know it has no > longer any valid target, not have it suddenly chase another entity that > happened to get allocated the same ID as the missile's original target in a > different subsystem). > > In my current design entities are compact classes with an ID and a refcount > and custom ref_function and unref_function, and freed entities are retained > in a freelist to reuse the ID. I was wondering if I could make entities > just the ID itself, and store the refcount in a separate array. > > I could possibly live with passing around Entity*, and use (guintptr) to > extract the ID from the pointer, but I worry since C does not guarantee > that a pointer can retain all possible guintptr values. guintptr can hold > any pointer type, but a pointer type is not necessarily capable of holding > any guintptr. > > Sincerely, > AmkG > ___ > vala-list mailing list > vala-list@gnome.org > https://mail.gnome.org/mailman/listinfo/vala-list > ___ vala-list mailing list vala-list@gnome.org https://mail.gnome.org/mailman/listinfo/vala-list
[Vala] Treating a uint as a reference type?
Hi Vala World, I was wondering if it's possible to implement a Vala class as a C-side guint, but have Vala emit calls to a ref and unref function, and to treat a C-side 0 as a Vala-side null. Basically I want to make something like this: // Vala static Entity? mine = null; static void set_mine(owned Entity e) { mine = (owned) e; } static void do_stuff() { Entity e = Entity.create(); set_mine(e); } And get it compiled to C: /* C */ guint mine = NULL; void set_mine(guint e) { if (mine != NULL) entity_unref(mine); mine = e; } void do_stuff() { guint e = entity_create(); set_mine(entity_ref(e)); } So far, I've tried using a [Compact][CCode (cname="guint")] class, but that is passed around as a guint* and Vala feels obliged to add a "typedef struct _guint guint". which fails since guint is already defined as unsigned int. I've also tried using a [CCode (cname="guint")] extern struct, but that causes any ref_function and unref_function attributes to be ignored, and still adds "typedef struct _guint guint". Is it possible to have Vala remove the "*" in reference types and still have it use ref/unref functions and have it remove typedef? -- I'm trying to make an Entity Component System, and ensure that destroyed entities are not reused until all references to them are destroyed. Entities are normally implemented as simple integer ID's, which are semantically a pointer to a row of component slots. My current design has each attached component add a reference to the entity, and the entity manager also adds a reference; when an entity is destroyed, the entity manager detaches all components (which cause each component to drop the reference) and drops its own reference; this frees the entity unless another reference is kept elsewhere. If a reference to an entity is kept elsewhere, it can then be queried as to whether it has been destroyed without worrying that its ID has been re-allocated to a freshly-made entity (for instance, consider a missile locked on to some game entity; if the target dies, we want the missile to know it has no longer any valid target, not have it suddenly chase another entity that happened to get allocated the same ID as the missile's original target in a different subsystem). In my current design entities are compact classes with an ID and a refcount and custom ref_function and unref_function, and freed entities are retained in a freelist to reuse the ID. I was wondering if I could make entities just the ID itself, and store the refcount in a separate array. I could possibly live with passing around Entity*, and use (guintptr) to extract the ID from the pointer, but I worry since C does not guarantee that a pointer can retain all possible guintptr values. guintptr can hold any pointer type, but a pointer type is not necessarily capable of holding any guintptr. Sincerely, AmkG ___ vala-list mailing list vala-list@gnome.org https://mail.gnome.org/mailman/listinfo/vala-list