checkGuard class

 

class Guard{
 public:
  /// <summary>
  /// Checks that the index is within range, if not it throws an exception.
  /// </summary>
  /// <param name="index">The ordinal position (index) of the item.</param>
  /// <param name="count">The number of items in the collection.</param>
  /// <param name="message">The exception message to throw.</param>
  /// <exception cref="DataException">Thrown when the index is out of range.</exception>
  static void CheckIndex(int index, int count, const std::string & message="Index out of range error."){
   if(index>=count||index<0){
    throw DataException(message);
   }
  }

  /// <summary>
  /// Checks that the condition is true, if not it throws an exception.
  /// </summary>
  /// <param name="condition">The condition to assert.</param>
  /// <param name="message">The exception message to throw.</param>
  /// <exception cref="DataException">Thrown when the condition is false.</exception>
  static void CheckTrue(bool condition, const std::string & message="Failed assertion error."){
   if(!condition){
    throw DataException(message);
   }
  }
  
  /// <summary>
  /// Checkes that the condition is false, if not it throws an exception.
  /// </summary>
  /// <param name="condition">The condition to assert.</param>
  /// <param name="message">The exception message to throw.</param>
  /// <exception cref="DataException">Throw when the condition is true.</exception>
  static void CheckNotTrue(bool condition, const std::string & message="Failed assertion error."){
   if(condition){
    throw DataException(message);
   }
  }

  /// <summary>
  /// Checks that a pointer is not null, otherwise it will throw an exception.
  /// </summary>
  /// <param name="p">The pointer to check.</param>
  /// <param name="message">The exception message to throw.</param>
  /// <exception cref="DataException">Thrown when the pointer is NULL.</exception>
  static void CheckNull(void * p, const std::string & message="Missing value error: pointer is NULL."){
   if(p==NULL){
    throw DataException(message);
   }
  }
  
  /// <summary>
  /// Checks that the string is not empty or only contains whitespace
  /// </summary>
  /// <param name="value">The string to check.</param>
  /// <param name="message">The exception message to throw.</param>
  /// <exception cref="DataException">Thrown when the string is empty or only contains whitespace.</exception>
  static void NotEmptyOrWhitespace(const std::string & value, const std::string & message="String is empty or only whitespace error."){
   if(value.length()==0 || StringHelper::Trim(value).length()==0){
    throw DataException(message);
   }
  }  

  /// <summary>
  /// Checks that the string is not empty or only contains whitespace
  /// </summary>
  /// <param name="value">The string to check.</param>
  /// <param name="message">The exception message to throw.</param>
  /// <exception cref="DataException">Thrown when the string is empty or only contains whitespace.</exception>
  static void NotEmptyOrWhitespace(const std::wstring & value, const std::string & message="String is empty or only whitespace error."){
   if(value.length()==0 || StringHelper::Trim(value).length()==0){
    throw DataException(message);
   }
  }  

  /// <summary>
  /// Checks that the string is not empty.
  /// </summary>
  /// <param name="value">The string to check.</param>
  /// <param name="message">The exception message to throw.</param>
  /// <exception cref="DataException">Thrown when the string is empty.</exception>
  static void NotEmpty(const std::string & value, const std::string & message="String is empty error."){
   if(value.length()==0){
    throw DataException(message);
   }
  }  

  /// <summary>
  /// Checks that the string is not empty.
  /// </summary>
  /// <param name="value">The string to check.</param>
  /// <param name="message">The exception message to throw.</param>
  /// <exception cref="DataException">Thrown when the string is empty.</exception>
  static void NotEmpty(const std::wstring & value, const std::string & message="String is empty error."){
   if(value.length()==0){
    throw DataException(message);
   }
  }

  /// <summary>
  /// Checks that a collection is not empty.
  /// </summary>
  /// <param name="count">The number of items in the collection.</param>
  /// <param name="message">The exception message to throw.</param>
  /// <exception cref="DataException">Thrown when the collection is empty.</exception>
  static void NotEmpty(int count, const std::string & message="Collection is empty error."){
   if(count<1){
    throw DataException(message);
   }
  }

  /// <summary>
  /// Checks that a character is not empty.
  /// </summary>
  /// <param name="ch">The character to check.</param>
  /// <param name="message">The exception message to throw.</param>
  /// <exception cref="DataException">Thrown when the character is empty.</exception>
  static void NotEmpty(const char ch, const std::string & message="Character is empty error."){
   if(ch==NULL){
    throw DataException(message);
   }
  }
 };

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值