Skip to content

Commit

Permalink
Added better error messages and use of last on iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
louisponet committed Feb 22, 2023
1 parent be1cb99 commit c63aff8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Overseer"
uuid = "1ada24be-c16d-4464-9f61-27c2e0f16645"
authors = ["louisponet <[email protected]>"]
version = "0.2.4"
version = "0.2.5"

[deps]
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
Expand Down
9 changes: 7 additions & 2 deletions src/entity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ function Base.show(io::IO, e::EntityState)
end
end

Base.in(::Type{T}, e::EntityState{TT}) where {T, TT} = any(x->eltype(x) == T, TT.parameters)

# TODO: Cleanup, can these two be merged?
@generated function Base.getproperty(e::EntityState{TT}, f::Symbol) where {TT}
fn_to_DT = Dict{Symbol, DataType}()
Expand All @@ -96,7 +98,7 @@ end
ex = MacroTools.postwalk(ex) do x
if @capture(x, return getfield(e[$DT_], $fnq)::$ft_)
return quote
throw(error("Field $f found in multiple components in $e.\nPlease use entity_state[$($DT)].$f instead."))
error("Field $f found in multiple components in $e.\nPlease use entity_state[$($DT)].$f instead.")
end
else
return x
Expand Down Expand Up @@ -131,7 +133,7 @@ end
ex = MacroTools.postwalk(ex) do x
if @capture(x, setfield!(e[$DT_], $fnq, val))
return quote
throw(error("Field $f found in multiple components in $e.\nPlease use entity_state[$($DT)].$f instead."))
error("Field $f found in multiple components in $e.\nPlease use entity_state[$($DT)].$f instead.")
end
else
return x
Expand All @@ -154,6 +156,9 @@ end

@generated function component(e::EntityState{TT}, ::Type{T}) where {TT<:Tuple, T}
id = findfirst(x -> x <: AbstractComponent ? eltype(x) == T : x == T, TT.parameters)
if id === nothing
error("EntityState{$TT} has no Component{$T}.")
end
return quote
$(Expr(:meta, :inline))
@inbounds getfield(e,:components)[$id]
Expand Down
21 changes: 20 additions & 1 deletion src/iteration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ Base.in(i::Int, it::AbstractIndicesIterator) = it.test(i)
return it_length
end

@inline function Base.last(it::IndicesIterator)
for i = length(it.shortest):-1:1
@inbounds id = entity_index(it.shortest, i)
if it.test(id)
return id
end
end
end

Base.@propagate_inbounds @inline entity_index(c::Union{<:AbstractComponent, Indices}, i::Int) = indices(c).packed[i]

@inline function Base.iterate(it::IndicesIterator, state=1)
Expand Down Expand Up @@ -60,6 +69,16 @@ end
end
end

@inline function Base.last(it::ReverseIndicesIterator)
for i = 1:length(it.shortest)
@inbounds id = entity_index(it.shortest, i)
if it.test(id)
return id
end
end
end


for (m, it) in zip((:indices_in, :reverse_indices_in), (:IndicesIterator, :ReverseIndicesIterator))
@eval macro $m(indices_expr)
expr, t_sets, t_notsets, t_orsets = expand_indices_bool(indices_expr)
Expand Down Expand Up @@ -188,7 +207,7 @@ Base.in(e::AbstractEntity, i::EntityIterator) = in(e.id, i.it)
return EntityState(e, i.components), n[2]
end

Base.getindex(iterator::EntityIterator, i) = Entity(iterator.it.shortest.packed[i])
Base.last(i::EntityIterator) = EntityState(Entity(last(i.it)), i.components)

for (m, it_short, it) in zip((:entities_in, :safe_entities_in), (:indices_iterator, :reverse_indices_iterator), (:IndicesIterator, :ReverseIndicesIterator))
@eval macro $m(indices_expr)
Expand Down
19 changes: 12 additions & 7 deletions src/ledger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ end
Ledger(components::Type...) = Ledger(map(x -> component_type(x)(), components)...)

function Ledger(stages::Stage...)
comps = Type[]
for stage in stages
append!(comps, requested_components(stage))
end
m = Ledger(comps...)
m = Ledger()
m.stages=[stages...]
prepare(m)
return m
Expand Down Expand Up @@ -261,8 +257,17 @@ function update(m::AbstractLedger)
end
end

"""
prepare(m)
Goes through all [`Stages`](@ref Stage) in `m`, makes sure that all [`requested_components`](@ref) are present in `m`,
and then calls [`prepare`](@ref) on the [`Stage`](@ref).
"""
function prepare(m::AbstractLedger)
for s in stages(m)
prepare(s, m)
for stage in stages(m)
for ct in requested_components(stage)
ensure_component!(m, ct)
end
prepare(stage, m)
end
end

2 comments on commit c63aff8

@louisponet
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/78259

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.5 -m "<description of version>" c63aff8fe996bb7b2b52fb085cd51b757bbcdfb4
git push origin v0.2.5

Please sign in to comment.