packages feed

checked (empty) → 0.1.0.0

raw patch · 6 files changed

+196/−0 lines, 6 filesdep +basesetup-changed

Dependencies added: base

Files

+ Data/Int/Checked.hs view
@@ -0,0 +1,40 @@+{-# LANGUAGE CPP, GeneralizedNewtypeDeriving #-}++-- |+-- Module    :  Data.Int.Checked+-- Copyright :  (c) Antoine Latter 2008+-- License   :  BSD3+--+-- Maintainer:  Antoine Latter <aslatter@gmail.com>+-- Stability :  provisional+-- Portability: portable  -- requires GeneralizedNewtypeDeriving+--+-- A drop-in replacements for the Data.Int types.+-- Exceptions are thrown whenever an operation would over-flow.+--++module Data.Int.Checked(Int()+                       ,Int8()+                       ,Int16()+                       ,Int32()+                       ,Int64()+                       ) where++import Prelude hiding (Int)+import qualified Data.Int as I+import Control.Exception(assert)++import Data.Typeable+import Foreign.Storable (Storable)+import Text.Printf (PrintfArg)+import Data.Ix (Ix)+-- import Data.Generics.Basics (Data)+import Data.Bits (Bits)++#include "checked.h"++CHECKED_WRAPPER(Int,   I,   I.Int)+CHECKED_WRAPPER(Int8,  I8,  I.Int8)+CHECKED_WRAPPER(Int16, I16, I.Int16)+CHECKED_WRAPPER(Int32 ,I32, I.Int32)+CHECKED_WRAPPER(Int64, I64, I.Int64)
+ Data/Word/Checked.hs view
@@ -0,0 +1,39 @@+{-# LANGUAGE CPP, GeneralizedNewtypeDeriving #-}++-- |+-- Module    :  Data.Word.Checked+-- Copyright :  (c) Antoine Latter 2008+-- License   :  BSD3+--+-- Maintainer:  Antoine Latter <aslatter@gmail.com>+-- Stability :  provisional+-- Portability: portable  -- requires GeneralizedNewtypeDeriving+--+-- A drop-in replacements for the Data.Word types.+-- Exceptions are thrown whenever an operation would over-flow.+--++module Data.Word.Checked(Word()+                       ,Word8()+                       ,Word16()+                       ,Word32()+                       ,Word64()+                       ) where++import qualified Data.Word as W+import Control.Exception(assert)++import Data.Typeable+import Foreign.Storable (Storable)+import Text.Printf (PrintfArg)+import Data.Ix (Ix)+-- import Data.Generics.Basics (Data)+import Data.Bits (Bits)++#include "checked.h"++CHECKED_WRAPPER(Word,   W,   W.Word)+CHECKED_WRAPPER(Word8,  W8,  W.Word8)+CHECKED_WRAPPER(Word16, W16, W.Word16)+CHECKED_WRAPPER(Word32, W32, W.Word32)+CHECKED_WRAPPER(Word64, W64, W.Word64)
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Antoine Latter 2008++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Antoine Latter nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ checked.cabal view
@@ -0,0 +1,18 @@+Name:                checked+Version:             0.1.0.0+Synopsis:            Bounds-checking integer types.+Description:         Includes replacements for all of the 'Data.Int' and 'Data.Word' types.+                     No effort has been made towards performance.+Category:            Data+License:             BSD3+License-file:        LICENSE+Author:              Antoine Latter+Maintainer:          Antoine Latter <aslatter@gmail.com>+Cabal-Version:       >= 1.2+Build-Depends:       base < 4, base >= 3.0.1.0+Build-Type:          Simple++Exposed-modules:     Data.Int.Checked,+                     Data.Word.Checked++Include-dirs:        .
+ checked.h view
@@ -0,0 +1,67 @@++#define TYPEABLE_INSTANCE(Type,Cons) instance Typeable Type where {\+    typeOf (Cons x) = typeOf x; }++#define SHOW_INSTANCE(Type,Cons) instance Show Type where {\+    showsPrec p (Cons x) = showsPrec p x; \+    show (Cons x) = show x; }++++#define MAX(typ) (fromIntegral (maxBound :: (typ)))+#define MIN(typ) (fromIntegral (minBound :: (typ)))++#define ASSERT(x,typ) (if not (x) then error "Datatype \"typ\" out of bounds." else id)++#define ASSERT_BOUNDS(var,typ) (ASSERT((var) <= MAX(typ) && (var) >= MIN(typ),typ))+#define MAP_ASSERT(typ,f) (map (\a -> ASSERT_BOUNDS(a,typ) ((f) a)))+++++#define READ_INSTANCE(Type) instance Read Type where {\+    readsPrec n str \+        = map (\(n, str) -> (ASSERT_BOUNDS(n,Type) $ fromInteger n, str)) (readsPrec n str) ;}+++++#define INTEGER_BINOP(x,y,op) ((op) (toInteger (x)) (toInteger (y)))+#define SAFE_BINOP(op,Type,Cons) \+  (op) (Cons x) (Cons y) = let z = INTEGER_BINOP(x,y,op) in ASSERT_BOUNDS(z,Type) (Cons ((op) (x) (y)))++#define INTEGER_UNOP(x,op) ((op) (toInteger (x)))+#define SAFE_UNOP(op,Type,Cons) \+  (op) (Cons x) = let z = INTEGER_UNOP(x,op) in ASSERT_BOUNDS(z,Type) (Cons ((op) (x)))++#define NUM_INSTANCE(Type,Cons) instance Num Type where { \+    SAFE_BINOP((+),Type,Cons) ; \+    SAFE_BINOP((-),Type,Cons) ; \+    SAFE_BINOP((*),Type,Cons) ; \+    SAFE_UNOP(negate,Type,Cons) ; \+    SAFE_UNOP(abs,Type,Cons) ; \+    SAFE_UNOP(signum,Type,Cons) ; \+    fromInteger i = ASSERT_BOUNDS(i,Type) $ Cons $ fromInteger i; }++++#define ENUM_INSTANCE(Type,Cons) instance Enum Type where { \+    succ (Cons y) = ASSERT(y < maxBound,Type) Cons $ succ y;    \+    pred (Cons y) = ASSERT(y > minBound,Type) Cons $ pred y;	\+    toEnum n = ASSERT_BOUNDS(n,Type) $ Cons $ toEnum n; \+    fromEnum (Cons y) = fromEnum y; \+    enumFrom (Cons y) = MAP_ASSERT(Type,Cons) $ enumFrom y; \+    enumFromThen (Cons x) (Cons y) = MAP_ASSERT(Type,Cons) $ enumFromThen x y; \+    enumFromTo (Cons x) (Cons y) = MAP_ASSERT(Type,Cons) $ enumFromTo x y; \+    enumFromThenTo (Cons x) (Cons y) (Cons z) = map Cons $ enumFromThenTo x y z; }++++#define STANDARD_DERIVING deriving (Bits, Bounded, Eq, Integral, Ix, Ord, PrintfArg, Real, Storable)++#define CHECKED_WRAPPER(Type,Cons,BaseType) newtype Type = Cons BaseType STANDARD_DERIVING; \+  TYPEABLE_INSTANCE(Type,Cons); \+  SHOW_INSTANCE(Type,Cons); \+  READ_INSTANCE(Type); \+  NUM_INSTANCE(Type,Cons); \+  ENUM_INSTANCE(Type,Cons);