Code Monkey home page Code Monkey logo

dbc_cpp's Introduction

Design by contract - C++

This is an implementation of Eiffel's Design By Contract (tm) features; Invariants, Require, Ensure, Do and Old in the C++ language that supports inheritance.

How it works

dbc_cpp works by keeping a stack of predicates (requires, ensures and invariants) for each method call. When it detects a method call has completed, it computes the total require, ensure and invariant predicates (including inherited methods) and throws an exception if they fail.

All the code must be within the DO() lambda clause to execute properly, you can specify capturing the entire state by using the form & { code }

Capabilities

All of the below work with inheritance, but, the library has not been battle tested so use with caution.

  • Invariants
  • Require
  • Do
  • Ensure
  • Old

Example

class base {
INVARIANT(true, "base")
public:
 	virtual ~base() = default;
  
	virtual void func() {
		BASE_CALLS(); // always required at the start
    
		CHECK_INVARIANT
    
		REQUIRE(true);
    
		OLD("m_count", m_count);
    
		DO([this]() {
			m_count++;
		});
    
		ENSURE(
			m_count == (std::any_cast<int>(OLD("m_count")) + 1)
		);
    
		CHECK_INVARIANT
	}
}

class example : public base {
INVARIANT(true, "example")

public:
 	virtual ~example() = default;
  
	virtual void func() {
		BASE_CALLS(
			base::func();
		);
    
		CHECK_INVARIANT
    
		REQUIRE(true);
    
		OLD("m_count", m_count);
    
		DO([this]() {
			m_count += 2;
		});
    
		ENSURE(
			m_count == (std::any_cast<int>(OLD("m_count")) + 2)
		);
    
		CHECK_INVARIANT
	}
}

dbc_cpp's People

Contributors

bambofy 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.