{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}

module Control.Process.Pid
  ( Pid,
    GetPid (..),
    HasPid (..),
    ReviewPid (..),
    AsPid (..),
  )
where

import Control.Category (id, (.))
import Control.Lens (Getter, Lens', Prism', Review, iso, unto)
import Data.Int (Int32)
import System.Posix.Types (CPid (CPid))
import System.Process (Pid)

-- $setup
-- >>> import Prelude
-- >>> import Control.Lens

-- | Type class for values that can be viewed as a @Pid@.
--
-- >>> view viewPid (CPid 42)
-- 42
class GetPid a where
  viewPid ::
    Getter a Pid
  {-# INLINE viewPidInt32 #-}
  viewPidInt32 ::
    Getter a Int32
  viewPidInt32 =
    (Pid -> f Pid) -> a -> f a
forall a. GetPid a => Getter a Pid
Getter a Pid
viewPid
      ((Pid -> f Pid) -> a -> f a)
-> ((Int32 -> f Int32) -> Pid -> f Pid)
-> (Int32 -> f Int32)
-> a
-> f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Pid -> Int32) -> (Int32 -> Pid) -> Iso Pid Pid Int32 Int32
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso
        (\(CPid Int32
x) -> Int32
x)
        Int32 -> Pid
CPid

-- |
--
-- >>> view viewPid (CPid 42)
-- 42
-- >>> view viewPidInt32 (CPid 42)
-- 42
instance GetPid Pid where
  viewPid :: Getter Pid Pid
viewPid = (Pid -> f Pid) -> Pid -> f Pid
forall a. a -> a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
  {-# INLINE viewPid #-}

-- | Type class for values with a lens into a @Pid@.
--
-- >>> view pid (CPid 42)
-- 42
class (GetPid a) => HasPid a where
  pid ::
    Lens' a Pid
  {-# INLINE pidInt32 #-}
  pidInt32 ::
    Lens' a Int32
  pidInt32 =
    (Pid -> f Pid) -> a -> f a
forall a. HasPid a => Lens' a Pid
Lens' a Pid
pid
      ((Pid -> f Pid) -> a -> f a)
-> ((Int32 -> f Int32) -> Pid -> f Pid)
-> (Int32 -> f Int32)
-> a
-> f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Pid -> Int32) -> (Int32 -> Pid) -> Iso Pid Pid Int32 Int32
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso
        (\(CPid Int32
x) -> Int32
x)
        Int32 -> Pid
CPid

-- |
--
-- >>> view pid (CPid 42)
-- 42
-- >>> view pidInt32 (CPid 42)
-- 42
-- >>> set pidInt32 99 (CPid 42)
-- 99
instance HasPid Pid where
  pid :: Lens' Pid Pid
pid =
    (Pid -> f Pid) -> Pid -> f Pid
forall a. a -> a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

-- | Type class for values that can be constructed from a @Pid@.
--
-- >>> review reviewPid (CPid 42) :: Pid
-- 42
class ReviewPid a where
  reviewPid ::
    Review a Pid
  {-# INLINE reviewPidInt32 #-}
  reviewPidInt32 ::
    Review a Int32
  reviewPidInt32 =
    Optic' p f a Pid
forall a. ReviewPid a => Review a Pid
Review a Pid
reviewPid
      Optic' p f a Pid
-> (p Int32 (f Int32) -> p Pid (f Pid))
-> p Int32 (f Int32)
-> p a (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Pid -> Int32) -> (Int32 -> Pid) -> Iso Pid Pid Int32 Int32
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso
        (\(CPid Int32
x) -> Int32
x)
        Int32 -> Pid
CPid

-- |
--
-- >>> review reviewPid (CPid 42) :: Pid
-- 42
-- >>> review reviewPidInt32 99 :: Pid
-- 99
instance ReviewPid Pid where
  reviewPid :: Review Pid Pid
reviewPid = (Pid -> Pid) -> Optic p f Pid Pid Pid Pid
forall (p :: * -> * -> *) (f :: * -> *) b t s a.
(Profunctor p, Bifunctor p, Functor f) =>
(b -> t) -> Optic p f s t a b
unto Pid -> Pid
forall a. a -> a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
  {-# INLINE reviewPid #-}

-- | Type class for values with a prism into a @Pid@.
--
-- >>> preview _Pid (CPid 42)
-- Just 42
class (ReviewPid a) => AsPid a where
  _Pid ::
    Prism' a Pid
  {-# INLINE _PidInt32 #-}
  _PidInt32 ::
    Prism' a Int32
  _PidInt32 =
    p Pid (f Pid) -> p a (f a)
forall a. AsPid a => Prism' a Pid
Prism' a Pid
_Pid
      (p Pid (f Pid) -> p a (f a))
-> (p Int32 (f Int32) -> p Pid (f Pid))
-> p Int32 (f Int32)
-> p a (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Pid -> Int32) -> (Int32 -> Pid) -> Iso Pid Pid Int32 Int32
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso
        (\(CPid Int32
x) -> Int32
x)
        Int32 -> Pid
CPid

-- |
--
-- >>> preview _Pid (CPid 42)
-- Just 42
-- >>> preview _PidInt32 (CPid 42)
-- Just 42
-- >>> review _PidInt32 99 :: Pid
-- 99
instance AsPid Pid where
  _Pid :: Prism' Pid Pid
_Pid =
    p Pid (f Pid) -> p Pid (f Pid)
forall a. a -> a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id