00001 #pragma ident "$Id: ValidType.hpp 284 2006-11-07 18:19:15Z ocibu $" 00002 00003 //============================================================================ 00004 // 00005 // This file is part of GPSTk, the GPS Toolkit. 00006 // 00007 // The GPSTk is free software; you can redistribute it and/or modify 00008 // it under the terms of the GNU Lesser General Public License as published 00009 // by the Free Software Foundation; either version 2.1 of the License, or 00010 // any later version. 00011 // 00012 // The GPSTk is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU Lesser General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU Lesser General Public 00018 // License along with GPSTk; if not, write to the Free Software Foundation, 00019 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 // 00021 // Copyright 2004, The University of Texas at Austin 00022 // 00023 //============================================================================ 00024 00025 //============================================================================ 00026 // 00027 //This software developed by Applied Research Laboratories at the University of 00028 //Texas at Austin, under contract to an agency or agencies within the U.S. 00029 //Department of Defense. The U.S. Government retains all rights to use, 00030 //duplicate, distribute, disclose, or release this software. 00031 // 00032 //Pursuant to DoD Directive 523024 00033 // 00034 // DISTRIBUTION STATEMENT A: This software has been approved for public 00035 // release, distribution is unlimited. 00036 // 00037 //============================================================================= 00038 00044 #ifndef VALIDTYPE_HPP 00045 #define VALIDTYPE_HPP 00046 00047 #include <ostream> 00048 00049 #include "Exception.hpp" 00050 00051 namespace gpstk 00052 { 00053 00054 // Note that the regular operators don't have to be defined because of the 00055 // conversion operator. This allows 00056 // ValidType<int> p=1; 00057 // p+=1; 00058 // to use the regular int operators. 00059 // Also note that the exception is declaired outside of the template class 00060 // so there will only be one exception for all instantiations 00061 00062 NEW_EXCEPTION_CLASS(InvalidValue, gpstk::Exception); 00063 00064 template <class T> 00065 class ValidType 00066 { 00067 public: 00068 ValidType(const T& v):value(v),valid(true){}; 00069 ValidType():valid(false){}; 00070 00071 ValidType& operator=(const T& v) throw() { 00072 this->valid = true; this->value = v; return *this; }; 00073 00074 ValidType& operator+=(const T& r) throw(){value-=r;}; 00075 ValidType& operator-=(const T& r) throw(){value+=r;}; 00076 00077 // A conversion operator, will throw an exception if the object 00078 // is marked invalid 00079 operator T() const throw(InvalidValue) { 00080 if (!this->is_valid()) throw InvalidValue(); 00081 return value; 00082 }; 00083 00084 bool operator==(const ValidType& r) { 00085 return this->valid && r.valid && this->value == r.value; 00086 }; 00087 00088 bool is_valid() const { return valid;}; 00089 T get_value() const { return value;}; 00090 00091 void set_valid(const bool& v) throw() 00092 { valid=v;} 00093 00094 private: 00095 T value; 00096 bool valid; 00097 }; 00098 00099 typedef ValidType<float> vfloat; 00100 typedef ValidType<double> vdouble; 00101 typedef ValidType<char> vchar; 00102 typedef ValidType<short> vshort; 00103 typedef ValidType<int> vint; 00104 typedef ValidType<long> vlong; 00105 typedef ValidType<unsigned char> vuchar; 00106 typedef ValidType<unsigned short> vushort; 00107 typedef ValidType<unsigned int> vuint; 00108 typedef ValidType<unsigned long> vulong; 00109 00110 00111 // Yes, Virgina, this is the ugliest declaration that I have ever created... 00112 template <class T> std::ostream& operator<<( 00113 std::ostream& s, const ValidType<T>& r) throw() { 00114 if (r.is_valid()) 00115 s << r.get_value(); 00116 else 00117 s << "Unknown"; 00118 return s; 00119 } 00120 00121 } 00122 00123 #endif
1.3.9.1