Browse Source

Add iterators for strdict

windows-imgui
Macoy Madson 8 months ago
parent
commit
446f97b07c
  1. 34
      src/Dictionary.cake

34
src/Dictionary.cake

@ -53,6 +53,35 @@
(def-c-function-alias strdict-value-at shget)
;; (dict (* <your Key Value type>) &return <your value type>)
(defmacro each-in-strdict (strdict any iterator-name symbol &rest body any)
(tokenize-push output
(c-for
(var (token-splice iterator-name) size_t 0)
;; We could hoist this out but it should be a quick op anyways
(< (token-splice iterator-name) (strdict-length (token-splice strdict)))
(incr (token-splice iterator-name))
(token-splice-rest body tokens)))
(return true))
(defmacro each-item-in-strdict (strdict any index-name symbol
item-name symbol item-type any
&rest body any)
(tokenize-push output
(each-in-strdict (token-splice strdict) (token-splice index-name)
(var (token-splice item-name item-type) (at (token-splice index-name strdict)))
(token-splice-rest body tokens)))
(return true))
(defmacro each-item-addr-in-strdict (strdict any index-name symbol
item-name symbol ptr-to-item-type any
&rest body any)
(tokenize-push output
(each-in-strdict (token-splice strdict) (token-splice index-name)
(var (token-splice item-name ptr-to-item-type)
(addr (at (token-splice index-name strdict))))
(token-splice-rest body tokens)))
(return true))
(comptime-cond
('auto-test
(c-import "stdio.h")
@ -129,6 +158,11 @@
(path returned-entry > thing-category)
(path returned-entry > thing-description)))
(each-item-addr-in-strdict my-dictionary i entry (* complex-dictionary-entry)
(fprintf stderr "[%d] %s = %s\n" (type-cast i int)
(path entry > key)
(path entry > thing-description)))
(scope
(var returned-entry (* complex-dictionary-entry)
(strdict-ptr-or-default-at my-dictionary "bad value"))

Loading…
Cancel
Save