Line data Source code
1 : /* 2 : * ALMA - Atacama Large Millimeter Array 3 : * (c) European Southern Observatory, 2002 4 : * (c) Associated Universities Inc., 2002 5 : * Copyright by ESO (in the framework of the ALMA collaboration), 6 : * Copyright by AUI (in the framework of the ALMA collaboration), 7 : * All rights reserved. 8 : * 9 : * This library is free software; you can redistribute it and/or 10 : * modify it under the terms of the GNU Lesser General Public 11 : * License as published by the Free software Foundation; either 12 : * version 2.1 of the License, or (at your option) any later version. 13 : * 14 : * This library is distributed in the hope that it will be useful, 15 : * but WITHOUT ANY WARRANTY, without even the implied warranty of 16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 : * Lesser General Public License for more details. 18 : * 19 : * You should have received a copy of the GNU Lesser General Public 20 : * License along with this library; if not, write to the Free Software 21 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 22 : * MA 02111-1307 USA 23 : * 24 : * File Interval.h 25 : */ 26 : 27 : #ifndef Interval_CLASS 28 : #define Interval_CLASS 29 : 30 : #include <stdint.h> 31 : #include <vector> 32 : #include <iostream> 33 : #include <string> 34 : 35 : #ifndef WITHOUT_ACS 36 : #include <asdmIDLTypesC.h> 37 : #endif 38 : 39 : #include <alma/ASDM/StringTokenizer.h> 40 : #include <alma/ASDM/NumberFormatException.h> 41 : 42 : #include <alma/ASDM/EndianStream.h> 43 : 44 : namespace asdm { 45 : 46 : class Interval; 47 : Interval operator * ( int64_t , const Interval & ); 48 : std::ostream & operator << ( std::ostream &, const Interval & ); 49 : std::istream & operator >> ( std::istream &, Interval&); 50 : 51 : /** 52 : * The Interval class implements an interval of time in units 53 : * of nanoseconds. 54 : * 55 : * @version 1.00 Jan. 7, 2005 56 : * @author Allen Farris 57 : */ 58 : class Interval { 59 : friend Interval operator * ( int64_t , const Interval & ); 60 : friend std::ostream & operator << ( std::ostream &, const Interval & ); 61 : friend std::istream & operator >> ( std::istream &, Interval&); 62 : 63 : public: 64 : static int64_t fromString(const std::string&); 65 : static std::string toString(int64_t); 66 : static Interval getInterval(StringTokenizer &t); 67 : 68 : Interval(); // default constructor 69 : Interval(const Interval &); // X const X& constructor 70 : Interval(const std::string &s); 71 : #ifndef WITHOUT_ACS 72 : Interval(const asdmIDLTypes::IDLInterval &); 73 : #endif 74 : Interval(int64_t value); 75 : virtual ~Interval(); // destructor 76 : 77 : Interval& operator = (const Interval&); // assignment operator 78 : Interval& operator = (const int64_t); // assignment operator 79 : 80 : Interval& operator += (const Interval&); // assignment with arithmetic 81 : Interval& operator -= (const Interval&); // operators 82 : Interval& operator *= (const int64_t); 83 : Interval& operator /= (const int64_t); 84 : 85 : Interval operator + (const Interval&) const; // arithmetic operators 86 : Interval operator - (const Interval&) const; 87 : Interval operator * (const int64_t) const; 88 : Interval operator / (const int64_t) const; 89 : 90 : bool operator < (const Interval&) const; // comparison operators 91 : bool operator > (const Interval&) const; 92 : bool operator <= (const Interval&) const; 93 : bool operator >= (const Interval&) const; 94 : bool operator == (const Interval&) const; 95 : bool equals(const Interval&) const; 96 : bool operator != (const Interval&) const; 97 : 98 : bool isZero() const; 99 : 100 : Interval operator - () const; // unary minus 101 : Interval operator + () const; // unary plus 102 : 103 : std::string toString() const; 104 : std::string toStringI() const; 105 : 106 : /** 107 : * Write the binary representation of this into an EndianOSStream 108 : * @param eoss 109 : * 110 : */ 111 : void toBin(EndianOSStream& eoss) ; 112 : 113 : /** 114 : * Write the binary representation of a vector of Interval into a EndianOSStream. 115 : * @param interval 116 : * @param eoss 117 : */ 118 : static void toBin(std::vector<Interval> interval, EndianOSStream& eoss) ; 119 : 120 : /** 121 : * Write the binary representation of a vector of vector of Interval into a EndianOSStream. 122 : * @param interval 123 : * @param eoss 124 : */ 125 : static void toBin(std::vector<std::vector<Interval> > interval, EndianOSStream& eoss) ; 126 : 127 : /** 128 : * Read the binary representation of an Interval from an EndianIStream 129 : * and use the read value to set an Interval. 130 : * @param eis the EndianIStream to be read 131 : * @return an Interval 132 : */ 133 : static Interval fromBin(EndianIStream& eis); 134 : 135 : /** 136 : * Read the binary representation of a vector of Interval from a EndianIStream 137 : * and use the read value to set a vector of Interval. 138 : * @param eis the EndianIStream to be read 139 : * @return a vector of Interval 140 : */ 141 : static std::vector<Interval> from1DBin(EndianIStream& eis); 142 : 143 : /** 144 : * Read the binary representation of a vector of vector of Interval from a EndianIStream 145 : * and use the read value to set a vector of vector of Interval. 146 : * @param eis the EndianIStream to be read 147 : * @return a vector of vector of Interval 148 : */ 149 : static std::vector<std::vector<Interval> >from2DBin(EndianIStream& eis); 150 : 151 : operator std::string () const; 152 : int64_t get() const; 153 : #ifndef WITHOUT_ACS 154 : asdmIDLTypes::IDLInterval toIDLInterval() const; 155 : #endif 156 : static std::string unit(); 157 : 158 : private: 159 : int64_t value; 160 : 161 : }; 162 : 163 : // Interval constructors 164 4941505 : inline Interval::Interval() : value(0L) { 165 4941505 : } 166 : 167 13448654 : inline Interval::Interval(const Interval &t) : value(t.value) { 168 13448654 : } 169 : 170 : #ifndef WITHOUT_ACS 171 : inline Interval::Interval(const asdmIDLTypes::IDLInterval &l) : value(l.value) { 172 : } 173 : #endif 174 : 175 7769 : inline Interval::Interval(const std::string &s) : value(fromString(s)) { 176 7769 : } 177 : 178 50198842 : inline Interval::Interval(int64_t v) : value(v) { 179 50198842 : } 180 : 181 : // Interval destructor 182 0 : inline Interval::~Interval() { } 183 : 184 : // assignment operator 185 14723976 : inline Interval& Interval::operator = ( const Interval &t ) { 186 14723976 : value = t.value; 187 14723976 : return *this; 188 : } 189 : 190 : // assignment operator 191 4764283 : inline Interval& Interval::operator = ( const int64_t v ) { 192 4764283 : value = v; 193 4764283 : return *this; 194 : } 195 : 196 : // assignment with arithmetic operators 197 : inline Interval& Interval::operator += ( const Interval& t) { 198 : value += t.value; 199 : return *this; 200 : } 201 : 202 : inline Interval& Interval::operator -= ( const Interval& t) { 203 : value -= t.value; 204 : return *this; 205 : } 206 : 207 : inline Interval& Interval::operator *= ( const int64_t n) { 208 : value *= n; 209 : return *this; 210 : } 211 : 212 : inline Interval& Interval::operator /= ( const int64_t n) { 213 : value /= n; 214 : return *this; 215 : } 216 : 217 : // arithmetic functions 218 68728 : inline Interval Interval::operator + ( const Interval &t2 ) const { 219 68728 : Interval tmp; 220 68728 : tmp.value = value + t2.value; 221 68728 : return tmp; 222 : } 223 : 224 5616 : inline Interval Interval::operator - ( const Interval &t2 ) const { 225 5616 : Interval tmp; 226 5616 : tmp.value = value - t2.value; 227 5616 : return tmp; 228 : } 229 : inline Interval Interval::operator * ( const int64_t n) const { 230 : Interval tmp; 231 : tmp.value = value * n; 232 : return tmp; 233 : } 234 : 235 0 : inline Interval Interval::operator / ( const int64_t n) const { 236 0 : Interval tmp; 237 0 : tmp.value = value / n; 238 0 : return tmp; 239 : } 240 : 241 : // comparison operators 242 68728 : inline bool Interval::operator < (const Interval& x) const { 243 68728 : return (value < x.value); 244 : } 245 : 246 68728 : inline bool Interval::operator > (const Interval& x) const { 247 68728 : return (value > x.value); 248 : } 249 : 250 0 : inline bool Interval::operator <= (const Interval& x) const { 251 0 : return (value <= x.value); 252 : } 253 : 254 : inline bool Interval::operator >= (const Interval& x) const { 255 : return (value >= x.value); 256 : } 257 : 258 12964 : inline bool Interval::operator == (const Interval& x) const { 259 12964 : return (value == x.value); 260 : } 261 11495 : inline bool Interval::equals(const Interval& x) const { 262 11495 : return (value == x.value); 263 : } 264 : 265 0 : inline bool Interval::operator != (const Interval& x) const { 266 0 : return (value != x.value); 267 : } 268 : 269 : // unary - and + operators 270 : inline Interval Interval::operator - () const { 271 : Interval tmp; 272 : tmp.value = -value; 273 : return tmp; 274 : } 275 : 276 : inline Interval Interval::operator + () const { 277 : Interval tmp; 278 : tmp.value = value; 279 : return tmp; 280 : } 281 : 282 : // Conversion functions 283 : inline Interval::operator std::string () const { 284 : return toString(); 285 : } 286 : 287 7180 : inline std::string Interval::toString() const { 288 7180 : return toString(value); 289 : } 290 : 291 : inline std::string Interval::toStringI() const { 292 : return toString(value); 293 : } 294 : 295 31579831 : inline int64_t Interval::get() const { 296 31579831 : return value; 297 : } 298 : 299 : #ifndef WITHOUT_ACS 300 : inline asdmIDLTypes::IDLInterval Interval::toIDLInterval() const { 301 : asdmIDLTypes::IDLInterval tmp; 302 : tmp.value = value; 303 : return tmp; 304 : } 305 : #endif 306 : 307 : // Friend functions 308 : 309 : inline Interval operator * ( int64_t n, const Interval &x) { 310 : Interval tmp; 311 : tmp.value = x.value * n; 312 : return tmp; 313 : } 314 : 315 0 : inline std::ostream & operator << ( std::ostream &o, const Interval &x ) { 316 0 : o << x.value; 317 0 : return o; 318 : } 319 : 320 0 : inline std::istream & operator >> ( std::istream &i, Interval &x ) { 321 0 : i >> x.value; 322 0 : return i; 323 : } 324 : 325 : inline std::string Interval::unit() { 326 : return std::string ("nanosec"); 327 : } 328 : 329 : } // End namespace asdm 330 : 331 : #endif /* Interval_CLASS */