ctkVersionRange.cpp

Go to the documentation of this file.
00001 /*=============================================================================
00002 
00003   Library: CTK
00004 
00005   Copyright (c) 2010 German Cancer Research Center,
00006     Division of Medical and Biological Informatics
00007 
00008   Licensed under the Apache License, Version 2.0 (the "License");
00009   you may not use this file except in compliance with the License.
00010   You may obtain a copy of the License at
00011 
00012     http://www.apache.org/licenses/LICENSE-2.0
00013 
00014   Unless required by applicable law or agreed to in writing, software
00015   distributed under the License is distributed on an "AS IS" BASIS,
00016   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017   See the License for the specific language governing permissions and
00018   limitations under the License.
00019 
00020 =============================================================================*/
00021 
00022 #include "ctkVersionRange_p.h"
00023 
00024 #include <stdexcept>
00025 
00026 
00027   const ctkVersionRange& ctkVersionRange::defaultVersionRange()
00028   {
00029     static ctkVersionRange defaultVR;
00030     return defaultVR;
00031   }
00032 
00033 
00034   ctkVersionRange::ctkVersionRange(const QString& vr)
00035   {
00036     bool op = vr.startsWith("(");
00037     bool ob = vr.startsWith("[");
00038 
00039     if (op || ob) {
00040       bool cp = vr.endsWith(")");
00041       bool cb = vr.endsWith("]");
00042       int comma = vr.indexOf(',');
00043 
00044       if (comma > 0 && (cp || cb))
00045       {
00046         low = new ctkVersion(vr.mid(1, comma-1).trimmed());
00047         high = new ctkVersion(vr.mid(comma+1, vr.length()-comma-2).trimmed());
00048         lowIncluded = ob;
00049         highIncluded = cb;
00050       }
00051       else
00052       {
00053         throw std::invalid_argument("Illegal version range: " + vr.toStdString());
00054       }
00055     }
00056     else
00057     {
00058       low = new ctkVersion(vr);
00059       high = 0;
00060       lowIncluded = true;
00061       highIncluded = false;
00062     }
00063   }
00064 
00065   ctkVersionRange::ctkVersionRange()
00066   {
00067     low = new ctkVersion(ctkVersion::emptyVersion());
00068     high = 0;
00069     lowIncluded = true;
00070     highIncluded = false;
00071   }
00072 
00073   ctkVersionRange::~ctkVersionRange()
00074   {
00075     delete low;
00076     delete high;
00077   }
00078 
00079   bool ctkVersionRange::isSpecified() const
00080   {
00081     return !(*this == defaultVersionRange());
00082   }
00083 
00084   bool ctkVersionRange::withinRange(const ctkVersion& ver) const
00085   {
00086     if (*this == defaultVersionRange())
00087     {
00088       return true;
00089     }
00090     int c = low->compare(ver);
00091 
00092     if (c < 0 || (c == 0 && lowIncluded))
00093     {
00094       if (!high)
00095       {
00096         return true;
00097       }
00098       c = high->compare(ver);
00099       return c > 0 || (c == 0 && highIncluded);
00100     }
00101     return false;
00102   }
00103 
00104   bool ctkVersionRange::withinRange(const ctkVersionRange& range) const
00105   {
00106     if (*this == range) {
00107       return true;
00108     }
00109     int c = low->compare(*range.low);
00110 
00111     if (c < 0 || (c == 0 && lowIncluded == range.lowIncluded))
00112     {
00113       if (!high)
00114       {
00115         return true;
00116       }
00117       c = high->compare(*range.high);
00118       return c > 0 || (c == 0 && highIncluded == range.highIncluded);
00119     }
00120     return false;
00121   }
00122 
00123   int ctkVersionRange::compare(const ctkVersionRange& obj) const
00124   {
00125     return low->compare(*obj.low);
00126   }
00127 
00128   QString ctkVersionRange::toString() const
00129   {
00130     if (high)
00131     {
00132       QString res;
00133       if (lowIncluded)
00134       {
00135         res += '[';
00136       }
00137       else
00138       {
00139         res += '(';
00140       }
00141       res += low->toString() + "," + high->toString();
00142       if (highIncluded)
00143       {
00144         res += ']';
00145       }
00146       else
00147       {
00148         res += ')';
00149       }
00150       return res;
00151     }
00152     else
00153     {
00154       return low->toString();
00155     }
00156   }
00157 
00158   bool ctkVersionRange::operator==(const ctkVersionRange& r) const
00159   {
00160     if (*low == *(r.low))
00161     {
00162       if (high)
00163       {
00164         return (*high == *(r.high))  &&
00165           (lowIncluded == r.lowIncluded) &&
00166           (highIncluded == r.highIncluded);
00167       }
00168       else
00169       {
00170         return true;
00171       }
00172     }
00173     return false;
00174 
00175 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines