|
| 1 | +{-# LANGUAGE FunctionalDependencies #-} |
| 2 | +{-# LANGUAGE NamedFieldPuns #-} |
| 3 | +{-# LANGUAGE OverloadedStrings #-} |
| 4 | +{-# LANGUAGE QuasiQuotes #-} |
| 5 | +{-# LANGUAGE TemplateHaskell #-} |
| 6 | + |
| 7 | +module Swarm.DB.Replica ( |
| 8 | + Replica (get), |
| 9 | +) where |
| 10 | + |
| 11 | +import Control.Exception (mask_) |
| 12 | +import qualified Data.ByteString as BS |
| 13 | +import qualified Data.Map.Strict as Map |
| 14 | +import Foreign (FinalizerPtr, ForeignPtr, Ptr, allocaArray, |
| 15 | + newForeignPtr, peekElemOff, wordPtrToPtr) |
| 16 | +import Language.C.Inline.Context (ctxTypesTable) |
| 17 | +import qualified Language.C.Inline.Cpp as Cpp |
| 18 | +import Language.C.Types (TypeSpecifier (TypeName)) |
| 19 | + |
| 20 | +import RON.UUID (UUID (UUID)) |
| 21 | + |
| 22 | +import Swarm.RON.Status (Status (Status, code, comment), StatusC) |
| 23 | +import qualified Swarm.RON.Status as Status |
| 24 | +import Swarm.RON.Text (TextFrame (TextFrame), TextFrameC) |
| 25 | + |
| 26 | +-- | Tag for 'Ptr' to @ron::Replica<TextFrame>@ |
| 27 | +data TextReplicaC |
| 28 | + |
| 29 | +-- | Equivalent of @ron::Replica<TextFrame>@ |
| 30 | +newtype TextReplica = TextReplica (Ptr TextReplicaC) |
| 31 | + |
| 32 | +$(Cpp.context |
| 33 | + $ Cpp.cppCtx |
| 34 | + <> Cpp.fptrCtx |
| 35 | + <> mempty |
| 36 | + { ctxTypesTable = Map.fromList |
| 37 | + [ (TypeName "Status" , [t| StatusC |]) |
| 38 | + , (TypeName "TextFrame" , [t| TextFrameC |]) |
| 39 | + , (TypeName "TextReplica", [t| TextReplicaC |]) |
| 40 | + ] |
| 41 | + } |
| 42 | + ) |
| 43 | +Cpp.include "<swarm/db/replica.hpp>" |
| 44 | +Cpp.include "<swarm/ron/status.hpp>" |
| 45 | +Cpp.include "<swarm/ron/text.hpp>" |
| 46 | +Cpp.verbatim "typedef ron::Status Status;" |
| 47 | +Cpp.verbatim "typedef ron::TextFrame TextFrame;" |
| 48 | +Cpp.verbatim "typedef ron::Replica<ron::TextFrame> TextReplica;" |
| 49 | + |
| 50 | +-- | @class ron::Replica<>@ |
| 51 | +class Replica replica frame | frame -> replica, replica -> frame where |
| 52 | + |
| 53 | + -- | @ron::Replica::Get()@ |
| 54 | + get :: UUID -> replica -> IO (Either Status frame) |
| 55 | + |
| 56 | +instance Replica TextReplica TextFrame where |
| 57 | + |
| 58 | + get (UUID x y) (TextReplica replicaP) = do |
| 59 | + frameFP <- newTextFrame |
| 60 | + status <- do |
| 61 | + statusFP <- mask_ $ do |
| 62 | + statusP <- [Cpp.exp| Status * { |
| 63 | + new Status( |
| 64 | + $(TextReplica * replicaP) |
| 65 | + ->Get( |
| 66 | + * $fptr-ptr:(TextFrame * frameFP), |
| 67 | + {$(uint64_t x), $(uint64_t y)} |
| 68 | + ) |
| 69 | + ) |
| 70 | + } |] |
| 71 | + newForeignPtr deleteStatus statusP |
| 72 | + allocaArray 4 $ \arena -> do |
| 73 | + [Cpp.block| void { |
| 74 | + uint64_t * const arena = $(uint64_t * arena); |
| 75 | + uint64_t & x = arena[0]; |
| 76 | + uint64_t & y = arena[1]; |
| 77 | + uint64_t & ptr = arena[2]; |
| 78 | + uint64_t & len = arena[3]; |
| 79 | + Status & status = * $fptr-ptr:(Status * statusFP); |
| 80 | + x = uint64_t(status.code().value()); |
| 81 | + y = uint64_t(status.code().origin()); |
| 82 | + ptr = uintptr_t(status.comment().data()); |
| 83 | + len = status.comment().length(); |
| 84 | + } |] |
| 85 | + code <- UUID <$> peekElemOff arena 0 <*> peekElemOff arena 1 |
| 86 | + ptr <- wordPtrToPtr . fromIntegral <$> peekElemOff arena 2 |
| 87 | + len <- fromIntegral <$> peekElemOff arena 3 |
| 88 | + comment <- BS.packCStringLen (ptr, len) |
| 89 | + pure Status{code, comment} |
| 90 | + pure $ case status of |
| 91 | + Status code _ | code == Status.ok -> Right $ TextFrame frameFP |
| 92 | + _ -> Left status |
| 93 | + |
| 94 | +newTextFrame :: IO (ForeignPtr TextFrameC) |
| 95 | +newTextFrame = mask_ $ do |
| 96 | + p <- [Cpp.exp| TextFrame * { new TextFrame } |] |
| 97 | + newForeignPtr deleteTextFrame p |
| 98 | + |
| 99 | +foreign import ccall "&deleteStatus" deleteStatus :: FinalizerPtr StatusC |
| 100 | + |
| 101 | +foreign import ccall "&deleteTextFrame" deleteTextFrame |
| 102 | + :: FinalizerPtr TextFrameC |
0 commit comments