Adobe.com
Contents Suites Classes Class Index Member Index

AIContract.hpp

Go to the documentation of this file.
00001 /*************************************************************************
00002 *
00003 * ADOBE CONFIDENTIAL
00004 *
00005 * Copyright 2017 Adobe
00006 *
00007 * All Rights Reserved.
00008 *
00009 * NOTICE: Adobe permits you to use, modify, and distribute this file in
00010 * accordance with the terms of the Adobe license agreement accompanying
00011 * it. If you have received this file from a source other than Adobe,
00012 * then your use, modification, or distribution of it requires the prior
00013 * written permission of Adobe.
00014 *
00015 **************************************************************************/
00016 
00017 #pragma once
00018 
00019 #include "AITypes.h"
00020 #include "AIAssert.hpp"
00021 #include "IAILiteralString.h"
00022 
00057 namespace ai
00058 {
00059         namespace Contract
00060         {
00061                 constexpr ai::LiteralString kDefaultPreconditionMsg {"Failed Precondition"};
00062                 constexpr ai::LiteralString kDefaultPostconditionMsg {"Failed Postcondition"};
00063 
00067                 struct Violation : public ai::LogicError
00068                 {
00069                         explicit Violation(const char* message) : ai::LogicError(kBadParameterErr, message)
00070                         {
00071                         }
00072                 };
00073                 
00077                 struct AssertPolicy {};
00078                 struct ThrowPolicy {};
00079                 struct AssertAndThrowPolicy {};
00080 
00084                 template <typename T> /* Assert */
00085                 void Check(T condition, const char* msg, AssertPolicy)
00086                 {
00087                         AIMsgAssert(condition, msg);
00088                 }
00089 
00090                 template <typename T> /* Throw */
00091                 constexpr void Check(T condition, const char* msg, ThrowPolicy)
00092                 {
00093                         if (!condition)
00094                         {
00095                                 throw Violation{msg};
00096                         }
00097                 }
00098 
00099                 template <typename T> /* Assert and Throw */
00100                 void Check(T condition, const char* msg, AssertAndThrowPolicy)
00101                 {
00102                         if (!condition)
00103                         {
00104                                 AIMsgAssert(false, msg);
00105                                 throw Violation{msg};
00106                         }
00107                 }
00108 
00109         } // namespace Contract
00110 
00111         namespace ContractPolicy
00112         {
00113                 // Instances of the Contract policy types
00114                 constexpr Contract::AssertPolicy                        kAssert {};
00115                 constexpr Contract::ThrowPolicy                         kThrow {};
00116                 constexpr Contract::AssertAndThrowPolicy        kAssertAndThrow {};
00117 
00118         } // namespace ContractPolicy
00119 
00120         template <typename T, typename Policy = Contract::ThrowPolicy>
00121         constexpr void Expects(T condition, const char* msg = Contract::kDefaultPreconditionMsg, Policy policy = ContractPolicy::kThrow)
00122         {
00123                 Contract::Check(condition, msg, policy);
00124         }
00125 
00126         template <typename T, typename Policy>
00127         constexpr void Expects(T condition, Policy policy)
00128         {
00129                 Contract::Check(condition, Contract::kDefaultPreconditionMsg, policy);
00130         }
00131 
00132         template <typename T, typename Policy = Contract::ThrowPolicy>
00133         constexpr void Ensures(T condition, const char* msg = Contract::kDefaultPostconditionMsg, Policy policy = ContractPolicy::kThrow)
00134         {
00135                 Contract::Check(condition, msg, policy);
00136         }
00137 
00138         template <typename T, typename Policy>
00139         constexpr void Ensures(T condition, Policy policy)
00140         {
00141                 Contract::Check(condition, Contract::kDefaultPostconditionMsg, policy);
00142         }
00143 
00144         template <typename T, typename Policy = Contract::ThrowPolicy>
00145         constexpr void EnsureRange(const T& value, const char* msg = Contract::kDefaultPostconditionMsg, Policy policy = ContractPolicy::kThrow)
00146         {
00147                 Ensures(value > T::kUnknown && value < T::kLast, msg, policy);
00148         }
00149 
00150         
00151 } // namespace ai


Contents Suites Classes Class Index Member Index
Adobe Solutions Network
 
Copyright © 2014 Adobe Systems Incorporated. All rights reserved.
Terms of Use Online Privacy Policy Adobe and accessibility Avoid software piracy Permissions and Trademarks