ValidType.hpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
44 #ifndef VALIDTYPE_HPP
45 #define VALIDTYPE_HPP
46 
47 #include <ostream>
48 
49 #include "Exception.hpp"
50 
51 namespace gnsstk
52 {
53 
54  // Note that the regular operators don't have to be defined
55  // because of the conversion operator. This allows
56  // ValidType<int> p=1;
57  // p+=1;
58  // to use the regular int operators.
59  // Also note that the exception is declaired outside of the
60  // template class so there will only be one exception for all
61  // instantiations
62 
64 
65  template <class T>
66  class ValidType
67  {
68  public:
69  ValidType(const T& v):value(v),valid(true){}
70  ValidType():value(0),valid(false){}
71 
72  ValidType& operator=(const T& v) noexcept
73  { this->valid = true; this->value = v; return *this; }
74 
75  ValidType& operator+=(const T& r) noexcept{value+=r; return *this;}
76  ValidType& operator-=(const T& r) noexcept{value-=r; return *this;}
77 
82  operator T() const
83  {
84  if (!this->is_valid()) throw InvalidValue();
85  return value;
86  }
87 
88  bool operator==(const ValidType& r) const
89  {
90  return ((!this->valid && !r.valid) ||
91  (this->valid && r.valid && this->value == r.value));
92  }
93  bool operator!=(const ValidType& r) const
94  { return !(*this == r); }
95 
96  bool is_valid() const { return valid; }
97  T get_value() const { return value; }
98 
99  void set_valid(const bool& v) noexcept
100  { valid=v; }
101 
102  private:
103  T value;
104  bool valid;
105  };
106 
117 
118 
119  template <class T>
120  std::ostream& operator<<(std::ostream& s, const ValidType<T>& r) noexcept
121  {
122  if (r.is_valid())
123  s << r.get_value();
124  else
125  s << "Unknown";
126  return s;
127  }
128 
129 }
130 
131 #endif
gnsstk::ValidType
Definition: ValidType.hpp:66
gnsstk::vchar
ValidType< char > vchar
Definition: ValidType.hpp:109
gnsstk::ValidType::operator-=
ValidType & operator-=(const T &r) noexcept
Definition: ValidType.hpp:76
gnsstk::ValidType::valid
bool valid
Definition: ValidType.hpp:104
gnsstk::ValidType::get_value
T get_value() const
Definition: ValidType.hpp:97
gnsstk::vfloat
ValidType< float > vfloat
Definition: ValidType.hpp:107
gnsstk::vdouble
ValidType< double > vdouble
Definition: ValidType.hpp:108
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::Exception
Definition: Exception.hpp:151
gnsstk::NEW_EXCEPTION_CLASS
NEW_EXCEPTION_CLASS(FileSpecException, gnsstk::Exception)
gnsstk::ValidType::operator==
bool operator==(const ValidType &r) const
Definition: ValidType.hpp:88
gnsstk::vuint
ValidType< unsigned int > vuint
Definition: ValidType.hpp:115
gnsstk::operator<<
std::ostream & operator<<(std::ostream &s, const ObsEpoch &oe) noexcept
Definition: ObsEpochMap.cpp:54
gnsstk::ValidType::is_valid
bool is_valid() const
Definition: ValidType.hpp:96
gnsstk::vlong
ValidType< long > vlong
Definition: ValidType.hpp:112
gnsstk::vuchar
ValidType< unsigned char > vuchar
Definition: ValidType.hpp:113
gnsstk::vushort
ValidType< unsigned short > vushort
Definition: ValidType.hpp:114
gnsstk::ValidType::operator!=
bool operator!=(const ValidType &r) const
Definition: ValidType.hpp:93
gnsstk::vint
ValidType< int > vint
Definition: ValidType.hpp:111
gnsstk::ValidType::ValidType
ValidType(const T &v)
Definition: ValidType.hpp:69
Exception.hpp
gnsstk::ValidType::ValidType
ValidType()
Definition: ValidType.hpp:70
gnsstk::vshort
ValidType< short > vshort
Definition: ValidType.hpp:110
gnsstk::vulong
ValidType< unsigned long > vulong
Definition: ValidType.hpp:116
gnsstk::ValidType::value
T value
Definition: ValidType.hpp:103
gnsstk::ValidType::operator=
ValidType & operator=(const T &v) noexcept
Definition: ValidType.hpp:72
gnsstk::ValidType::set_valid
void set_valid(const bool &v) noexcept
Definition: ValidType.hpp:99
gnsstk::ValidType::operator+=
ValidType & operator+=(const T &r) noexcept
Definition: ValidType.hpp:75


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:42