Code Monkey home page Code Monkey logo

safe's Introduction

Safe

Header only C++11 safe integer and utilities.

Provides:

  • Safe::Integer<T>, a class template which wraps an integer of any type, providing safe arithmetic, conversions, and comparisons
  • Safe::Make<T>, a function template which wraps an integer in a Safe::Integer<T>
  • Safe::Cast<B,A>, a function template which safely casts an integer of type A to type B
  • Convenience typedefs:
    • Safe::SizeType and Safe::size_t which provide a safe wrapper for std::size_t (i.e. they are typedef'd to Safe::Integer<std::size_t>)
    • Safe::PointerDifferenceType, Safe::SignedSizeType, Safe::ptrdiff_t, and Safe::ssize_t which provide a safe wrapper for std::ptrdiff_t (i.e. they are typedef'd to Safe::Integer<std::ptrdiff_t>)

Any unsafe (i.e. lossy) operation causes a std::overflow_error to be thrown.

Installation

If you just want to use Safe and aren't concerned with building/running the unit tests, just #include <safe/safe.hpp>.

Dependencies

  • C++11 standard library
  • Catch (for unit tests only)

Compiler Support

  • Clang 3.4 (perhaps earlier, untested)
  • GCC 4.8.1 (definitely uses language features not supported in GCC 4.8)

MSVC++ untested. I wouldn't hold your breath.

Examples

Safe addition:

#include <safe/safe.hpp>
#include <iostream>
#include <limits>

int main (void) {

    auto i=std::numeric_limits<int>::max();
    i=Safe::Make(i)+1;  //  Throws std::overflow_error
    std::cout << i << std::endl;

}

Safe conversion:

#include <safe/safe.hpp>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <limits>
#include <vector>

//  Poorly chosen parameter type
void print (std::uint8_t s) {

    std::cout << static_cast<std::uint16_t>(s) << std::endl;

}

int main (void) {

    std::vector<std::size_t> vec;
    
    std::size_t i=0;
    for (;i<100;++i) vec.push_back(i);
    
    print(Safe::Make(vec.size()));   //  Fine
    
    for (;i<1024;++i) vec.push_back(i);
    
    print(Safe::Make(vec.size()));   //  Throws std::overflow_error

}

safe's People

Contributors

robertleahy avatar

Watchers

Mads Ravn avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.