contract CappedCrowdsale is Crowdsale {
    using SafeMath for uint256;

    uint256 private _cap;

    bool private _cappedActive;

    /**
     * @dev Constructor, takes maximum amount of wei accepted in the crowdsale.
     * @param cap Max amount of wei to be contributed
     */
    constructor (bool _active, uint256 cap) public {

        // set if capped is active or not
        _setCappedActive(_active);

        // validate only if capped
        if (_active) {
            require(cap > 0, "CappedCrowdsale: cap is 0");
        }
        _cap = cap;

    }

    /**
     * @return the cap of the crowdsale.
     */
    function cap() public view returns (uint256) {
        return _cap;
    }

    /**
     * @dev Checks whether the cap has been reached.
     * @return Whether the cap was reached
     */
    function capReached() public view returns (bool) {
        return weiRaised() >= _cap;
    }

    /**
     * @dev Extend parent behavior requiring purchase to respect the funding cap.
     * @param beneficiary Token purchaser
     * @param weiAmount Amount of wei contributed
     */
    function _preValidatePurchase(address beneficiary, uint256 weiAmount) internal view {
        super._preValidatePurchase(beneficiary, weiAmount);
        if (_cappedActive) {
            require(weiRaised().add(weiAmount) <= _cap, "CappedCrowdsale: cap exceeded");
        }
    }

    /**
     * Activate or deactivate capped functionality on contract creation
     */
    function _setCappedActive(bool _active) internal {
        _cappedActive = _active;
    }

}
/**
 * @title CappedCrowdsale
 * @dev Crowdsale with a limit for total contributions.
 */
function cap() public view returns (uint256) {
    return _cap;
}

/**
 * @return the cap of the crowdsale.
 */