checkGuard class

来源:互联网 发布:e宠商城淘宝店 编辑:程序博客网 时间:2024/05/29 13:53
 

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);
   }
  }
 };

原创粉丝点击