----------------------------------------------------------------------------
-- |
-- Module      :  Data.Emacs.Module.Raw.Value
-- Copyright   :  (c) Sergey Vinokurov 2018
-- License     :  Apache-2.0 (see LICENSE)
-- Maintainer  :  serg.foo@gmail.com
----------------------------------------------------------------------------

{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module Data.Emacs.Module.Raw.Value (RawValue(..), GlobalRef(..)) where

import Control.DeepSeq

import Foreign

-- | Basic handle on an Emacs value. Can be GC'ed after any call into Emacs.
-- To overcome that, use 'ValueGC'.
--
-- Not a real pointer because emacs values are not really pointers. That is,
-- they're completely opaque.
newtype RawValue = RawValue { RawValue -> Ptr RawValue
unRawValue :: Ptr RawValue }
  deriving (RawValue -> ()
(RawValue -> ()) -> NFData RawValue
forall a. (a -> ()) -> NFData a
rnf :: RawValue -> ()
$crnf :: RawValue -> ()
NFData, Ptr b -> Int -> IO RawValue
Ptr b -> Int -> RawValue -> IO ()
Ptr RawValue -> IO RawValue
Ptr RawValue -> Int -> IO RawValue
Ptr RawValue -> Int -> RawValue -> IO ()
Ptr RawValue -> RawValue -> IO ()
RawValue -> Int
(RawValue -> Int)
-> (RawValue -> Int)
-> (Ptr RawValue -> Int -> IO RawValue)
-> (Ptr RawValue -> Int -> RawValue -> IO ())
-> (forall b. Ptr b -> Int -> IO RawValue)
-> (forall b. Ptr b -> Int -> RawValue -> IO ())
-> (Ptr RawValue -> IO RawValue)
-> (Ptr RawValue -> RawValue -> IO ())
-> Storable RawValue
forall b. Ptr b -> Int -> IO RawValue
forall b. Ptr b -> Int -> RawValue -> IO ()
forall a.
(a -> Int)
-> (a -> Int)
-> (Ptr a -> Int -> IO a)
-> (Ptr a -> Int -> a -> IO ())
-> (forall b. Ptr b -> Int -> IO a)
-> (forall b. Ptr b -> Int -> a -> IO ())
-> (Ptr a -> IO a)
-> (Ptr a -> a -> IO ())
-> Storable a
poke :: Ptr RawValue -> RawValue -> IO ()
$cpoke :: Ptr RawValue -> RawValue -> IO ()
peek :: Ptr RawValue -> IO RawValue
$cpeek :: Ptr RawValue -> IO RawValue
pokeByteOff :: Ptr b -> Int -> RawValue -> IO ()
$cpokeByteOff :: forall b. Ptr b -> Int -> RawValue -> IO ()
peekByteOff :: Ptr b -> Int -> IO RawValue
$cpeekByteOff :: forall b. Ptr b -> Int -> IO RawValue
pokeElemOff :: Ptr RawValue -> Int -> RawValue -> IO ()
$cpokeElemOff :: Ptr RawValue -> Int -> RawValue -> IO ()
peekElemOff :: Ptr RawValue -> Int -> IO RawValue
$cpeekElemOff :: Ptr RawValue -> Int -> IO RawValue
alignment :: RawValue -> Int
$calignment :: RawValue -> Int
sizeOf :: RawValue -> Int
$csizeOf :: RawValue -> Int
Storable)

-- | Value that is independent of environment ('Env') that produced it.
--
-- Can be used to e.g. cache values that are expensive to compute from scratch.
newtype GlobalRef = GlobalRef { GlobalRef -> RawValue
unGlobalRef :: RawValue }
  deriving (GlobalRef -> ()
(GlobalRef -> ()) -> NFData GlobalRef
forall a. (a -> ()) -> NFData a
rnf :: GlobalRef -> ()
$crnf :: GlobalRef -> ()
NFData, Ptr b -> Int -> IO GlobalRef
Ptr b -> Int -> GlobalRef -> IO ()
Ptr GlobalRef -> IO GlobalRef
Ptr GlobalRef -> Int -> IO GlobalRef
Ptr GlobalRef -> Int -> GlobalRef -> IO ()
Ptr GlobalRef -> GlobalRef -> IO ()
GlobalRef -> Int
(GlobalRef -> Int)
-> (GlobalRef -> Int)
-> (Ptr GlobalRef -> Int -> IO GlobalRef)
-> (Ptr GlobalRef -> Int -> GlobalRef -> IO ())
-> (forall b. Ptr b -> Int -> IO GlobalRef)
-> (forall b. Ptr b -> Int -> GlobalRef -> IO ())
-> (Ptr GlobalRef -> IO GlobalRef)
-> (Ptr GlobalRef -> GlobalRef -> IO ())
-> Storable GlobalRef
forall b. Ptr b -> Int -> IO GlobalRef
forall b. Ptr b -> Int -> GlobalRef -> IO ()
forall a.
(a -> Int)
-> (a -> Int)
-> (Ptr a -> Int -> IO a)
-> (Ptr a -> Int -> a -> IO ())
-> (forall b. Ptr b -> Int -> IO a)
-> (forall b. Ptr b -> Int -> a -> IO ())
-> (Ptr a -> IO a)
-> (Ptr a -> a -> IO ())
-> Storable a
poke :: Ptr GlobalRef -> GlobalRef -> IO ()
$cpoke :: Ptr GlobalRef -> GlobalRef -> IO ()
peek :: Ptr GlobalRef -> IO GlobalRef
$cpeek :: Ptr GlobalRef -> IO GlobalRef
pokeByteOff :: Ptr b -> Int -> GlobalRef -> IO ()
$cpokeByteOff :: forall b. Ptr b -> Int -> GlobalRef -> IO ()
peekByteOff :: Ptr b -> Int -> IO GlobalRef
$cpeekByteOff :: forall b. Ptr b -> Int -> IO GlobalRef
pokeElemOff :: Ptr GlobalRef -> Int -> GlobalRef -> IO ()
$cpokeElemOff :: Ptr GlobalRef -> Int -> GlobalRef -> IO ()
peekElemOff :: Ptr GlobalRef -> Int -> IO GlobalRef
$cpeekElemOff :: Ptr GlobalRef -> Int -> IO GlobalRef
alignment :: GlobalRef -> Int
$calignment :: GlobalRef -> Int
sizeOf :: GlobalRef -> Int
$csizeOf :: GlobalRef -> Int
Storable)