Skip to content

Instantly share code, notes, and snippets.

@timsgardner
Created November 18, 2014 05:54
Show Gist options
  • Save timsgardner/ff1f3e08d4a317a34e0a to your computer and use it in GitHub Desktop.
Save timsgardner/ff1f3e08d4a317a34e0a to your computer and use it in GitHub Desktop.
thrilling
;; at the moment, array population works by swapping out existing
;; array for new one (possibly of greater length). Should work for
;; components, which copy their arrays anyway, might be problematic in
;; other cases though. wonder if this will even work in the case of eg
;; meshes, or if some other setting pattern is needed. This whole
;; thing is kind of sketchy, treatment of arrays breaks idea of spec
;; type agnosticism.
(defn- set-clause-array-populate [setable ctx]
(let [et (element-type ...)]
`(let [~instsym (. ~targsym ~name)
~instlensym (count ~instsym)
~ressym (cond
(instance? ~setable-type ~valsym)
~valsym
(vector? ~valsym)
(into-array ~element-type ; first pass, optimize later
(map-indexed ; grumble
(fn [element-spec# i#]
(if (< i# ~instlensym)
(populate! (nth ~inst i#) element-spec# ~element-type)
(hydrate element-spec# ~element-type)))
~valsym))
:else (populate! ~instsym ~valsym))] ; for user weirdness
(set! (. ~targsym ~name) ~ressym))))
(defn- set-clause-array-hydrate [setable ctx]
(let [et (element-type ...)]
`(let [~ressym (cond
(instance? ~setable-type ~valsym)
~valsym
(vector? ~valsym)
(into-array ~element-type ; first pass, optimize later
(map ; grumble
#(hydrate % ~element-type)
~valsym))
:else (hydrate ~valsym))] ; for user weirdness
(set! (. ~targsym ~name) ~ressym))))
;; the set! part below is there to enable the user to force hydration
;; with a custom IPopulater protocol or whatever
(defn- set-clause-element-populate [setable ctx]
`(let [~ressym (if (or (nil? ~valsym)
(instance? ~setable-type ~valsym))
~valsym
(populate! ~valsym ~setable-type))]
(when-not (identical? (. ~targsym ~name) ~ressym)
(set! (. ~targsym ~name) ~ressym))))
(defn- set-clause-element-hydrate [setable ctx]
`(let [~ressym (if (or (nil? ~valsym)
(instance? ~setable-type ~valsym))
~valsym
(hydrate ~valsym ~setable-type))]
(set! (. ~targsym ~name) ~ressym)))
(defn- set-clause-hydrate [setable ctx]
(if (array-type? (type-for-setable setable))
(set-clause-array-hydrate setable ctx)
(set-clause-element-hydrate setable ctx)))
(defn- set-clause-populate [setable ctx]
(if (array-type? (type-for-setable setable))
(set-clause-array-populate setable ctx)
(set-clause-element-populate setable ctx)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment