Skip to content

Commit

Permalink
add on_clear parameter on storage.clear method
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre-LA committed Jan 12, 2023
1 parent 361b5ea commit f704e36
Show file tree
Hide file tree
Showing 29 changed files with 31 additions and 19 deletions.
Empty file modified .gitattributes
100644 → 100755
Empty file.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified docs/component.md
100644 → 100755
Empty file.
Empty file modified docs/concepts.md
100644 → 100755
Empty file.
Empty file modified docs/derived_entity.md
100644 → 100755
Empty file.
Empty file modified docs/entity.md
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion docs/gen_idx.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Generational Index module
```lua
local GenIdx = @record{
index: usize, -- 0-based, since this is an index for arrays
generation: usize, -- 1-based: 1 is the "first" valid generation, 0 is invalid
generation: uint16, -- 1-based: 1 is the "first" valid generation, 0 is invalid
}
```

Expand Down
23 changes: 13 additions & 10 deletions docs/storage.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
### Summary
* [storage](#storage)
* [storage:clear](#storageclear)
* [storage:push](#storagepush)
* [storage:remove](#storageremove)
* [storage:mget](#storagemget)
* [storage:__mnext](#storage__mnext)
* [storage:__mpairs](#storage__mpairs)
* [storage:clear](#storageclear)
* [storage](#storage)

## storage
Expand Down Expand Up @@ -33,14 +33,6 @@ local storage = @record{

the storage type

### storage:clear

```lua
function storage:clear()
```

resets storage to a zeroed state

### storage:push

```lua
Expand All @@ -57,14 +49,17 @@ the generational index of the used slot and a reference to the inserted entry.
### storage:remove

```lua
function storage:remove(idx: GenIdx): boolean
function storage:remove(idx: GenIdx, destroyer: facultative(function(*T))): boolean
```

Removes an entry associated with the generational index.

If the entry is found, then the slot it's zeroed and the function returns `true`. Othewise
it does nothing and returns `false`.

You can optionally pass a `destroyer` function, which will be called passing a pointer to
the entry, this is useful if you need to free resources on this entry.

### storage:mget

```lua
Expand Down Expand Up @@ -95,6 +90,14 @@ function storage:__mpairs(): (auto, *storage, isize)
Iterator for `mpairs`, this allows using `for in` using the `mpairs` iterator on
the storage, it iterates only on the entries and skips unused slots.

### storage:clear

```lua
function storage:clear(on_clear: facultative(function(*T)))
```

resets storage to a zeroed state

### storage

```lua
Expand Down
Empty file modified docs/system.md
100644 → 100755
Empty file.
Empty file modified docs/utils.md
100644 → 100755
Empty file.
Empty file modified examples/basic.nelua
100644 → 100755
Empty file.
Empty file modified examples/bench.nelua
100644 → 100755
Empty file.
Empty file modified examples/derived.nelua
100644 → 100755
Empty file.
Empty file modified examples/derived_entity_tree.nelua
100644 → 100755
Empty file.
Empty file modified examples/entity_tree.nelua
100644 → 100755
Empty file.
Empty file modified examples/entity_with_destroy.nelua
100644 → 100755
Empty file.
Empty file modified examples/override.nelua
100644 → 100755
Empty file.
Empty file modified gen-docs.lua
100644 → 100755
Empty file.
Empty file modified nldoc.lua
100644 → 100755
Empty file.
Empty file modified rotor/component.nelua
100644 → 100755
Empty file.
Empty file modified rotor/concepts.nelua
100644 → 100755
Empty file.
Empty file modified rotor/derived_entity.nelua
100644 → 100755
Empty file.
Empty file modified rotor/entity.nelua
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion rotor/gen_idx.nelua
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
-- each index unique between re-uses.
local GenIdx = @record{
index: usize, -- 0-based, since this is an index for arrays
generation: usize, -- 1-based: 1 is the "first" valid generation, 0 is invalid
generation: uint16, -- 1-based: 1 is the "first" valid generation, 0 is invalid
}

-- The `is_genidx` trait
Expand Down
23 changes: 16 additions & 7 deletions rotor/storage.nelua
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ local GenIdx = require 'rotor.gen_idx'
return found, next_idx
end

-- resets storage to a zeroed state
function storage:clear()
self.unavailables = {}
self.generations = {}
self.next_idx = 0
end

--[[
Push a new entry on the storage on the next free slot, if no free slots are available,
then a warning will be printed and this function will return `false`, a zeroed generational
Expand Down Expand Up @@ -148,6 +141,22 @@ local GenIdx = require 'rotor.gen_idx'
function storage:__mpairs(): (auto, *storage, isize)
return storage.__mnext, self, -1
end

-- resets storage to a zeroed state
function storage:clear(on_clear: facultative(function(*T)))
## if not on_clear.type.is_niltype then
check(on_clear, "Passed an nilptr 'on_clear' function!")

for _, entry in mpairs(self) do
on_clear(entry)
end
## end

self.entries = {}
self.unavailables = {}
self.generations = {}
self.next_idx = 0
end

## return storage
## end)
Expand Down
Empty file modified rotor/system.nelua
100644 → 100755
Empty file.
Empty file modified rotor/utils.nelua
100644 → 100755
Empty file.

0 comments on commit f704e36

Please sign in to comment.