src_other/tuplebase/ZTBSpec.h

00001 /*  @(#) $Id: ZTBSpec.h,v 1.31 2006/11/02 22:47:47 agreen Exp $ */
00002 
00003 /* ------------------------------------------------------------
00004 Copyright (c) 2003 Andrew Green and Learning in Motion, Inc.
00005 http://www.zoolib.org
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a copy
00008 of this software and associated documentation files (the "Software"), to deal
00009 in the Software without restriction, including without limitation the rights
00010 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011 copies of the Software, and to permit persons to whom the Software is
00012 furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020 COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00021 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00022 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023 ------------------------------------------------------------ */
00024 
00025 #ifndef __ZTBSpec__
00026 #define __ZTBSpec__ 1
00027 #include "zconfig.h"
00028 
00029 #include "ZCompare.h"
00030 #include "ZRegex.h"
00031 #include "ZTextCollator.h"
00032 #include "ZTuple.h"
00033 
00034 #include <set>
00035 #include <vector>
00036 
00037 // =================================================================================================
00038 #pragma mark -
00039 #pragma mark * ZTBSpec
00040 
00042 
00043 class ZTBSpec
00044         {
00045         ZOOLIB_DEFINE_OPERATOR_BOOL_TYPES(ZTBSpec,
00046                 operator_bool_generator_type, operator_bool_type);
00047 public:
00048         class Comparator;
00049         class Criterion;
00050         class Rep;
00051 
00053         typedef std::vector<Criterion> CriterionSect;
00054 
00056         typedef std::vector<CriterionSect> CriterionUnion;
00057 
00059         enum ERel
00060                 {
00061                 eRel_Invalid,
00062                 eRel_Less,
00063                 eRel_LessEqual,
00064                 eRel_Equal,
00065                 eRel_GreaterEqual,
00066                 eRel_Greater,
00067                 eRel_Has,
00068                 eRel_HasOfType,
00069                 eRel_Lacks,
00070                 eRel_LacksOfType,
00071                 eRel_VectorContains,
00072                 eRel_StringContains,
00073                 eRel_Regex
00074                 };
00075         //@{
00079         ZTBSpec();
00080 
00082         ZTBSpec(const ZTBSpec& iOther);
00083 
00085         ZTBSpec& operator=(const ZTBSpec& iOther);
00086 
00088         ~ZTBSpec();
00090 
00091         //@{
00095         explicit ZTBSpec(bool iAny);
00096 
00098         explicit ZTBSpec(const Criterion& iCriterion);
00099         ZTBSpec(const ZTuplePropName& iPropName, ERel iRel, const ZTupleValue& iTV);
00100         ZTBSpec(const ZTuplePropName& iPropName, ERel iRel, int iStrength, const ZTupleValue& iTV);
00101         ZTBSpec(const ZTuplePropName& iPropName, Comparator iComparator, const ZTupleValue& iTV);
00102 
00104         explicit ZTBSpec(const CriterionSect& iCriterionSect);
00106 
00107         //@{
00111         explicit ZTBSpec(const ZTuple& iTuple);
00112 
00114         explicit ZTBSpec(const ZStreamR& iStreamR);
00115 
00117         ZTuple AsTuple() const;
00118 
00120         void ToStream(const ZStreamW& iStreamW) const;
00122 
00123         //@{
00126         operator operator_bool_type() const;
00127 
00128         int Compare(const ZTBSpec& iOther) const;
00129 
00130         bool operator==(const ZTBSpec& iOther) const;
00131         bool operator!=(const ZTBSpec& iOther) const;
00132 
00133         bool operator<(const ZTBSpec& iOther) const;
00134         bool operator<=(const ZTBSpec& iOther) const;
00135         bool operator>(const ZTBSpec& iOther) const;
00136         bool operator>=(const ZTBSpec& iOther) const;
00138 
00139         //@{
00143         ZTBSpec operator&(const ZTBSpec& iOther) const;
00144 
00146         ZTBSpec& operator&=(const ZTBSpec& iOther);
00147 
00149         ZTBSpec operator|(const ZTBSpec& iOther) const;
00150 
00152         ZTBSpec& operator|=(const ZTBSpec& iOther);
00154 
00156         bool Matches(const ZTuple& iTuple) const;
00157 
00159         bool IsAny() const;
00160 
00162         bool IsNone() const;
00163 
00164         //@{
00167         const CriterionUnion& GetCriterionUnion() const;
00168 
00169         void GetPropNames(std::set<std::string>& oNames) const;
00170         void GetPropNames(std::set<ZTuplePropName>& oNames) const;
00172 
00173         //@{
00181         static ZTBSpec sAny();
00182 
00184         static ZTBSpec sNone();
00185 
00187         static ZTBSpec sHas(const ZTuplePropName& iPropName);
00188 
00190         static ZTBSpec sHas(const ZTuplePropName& iPropName, ZType iType);
00191 
00193         static ZTBSpec sLacks(const ZTuplePropName& iPropName);
00194 
00196         static ZTBSpec sLacks(const ZTuplePropName& iPropName, ZType iType);
00197 
00199         static ZTBSpec sEquals(const ZTuplePropName& iPropName, const ZTupleValue& iValue);
00200 
00202         static ZTBSpec sNotEqual(const ZTuplePropName& iPropName, const ZTupleValue& iValue);
00203 
00205         static ZTBSpec sLess(const ZTuplePropName& iPropName, const ZTupleValue& iValue);
00206 
00208         static ZTBSpec sLessEqual(const ZTuplePropName& iPropName, const ZTupleValue& iValue);
00209 
00211         static ZTBSpec sGreater(const ZTuplePropName& iPropName, const ZTupleValue& iValue);
00212 
00214         static ZTBSpec sGreaterEqual(const ZTuplePropName& iPropName, const ZTupleValue& iValue);
00215 
00217         static ZTBSpec sStringContains(
00218                 const ZTuplePropName& iPropName, const std::string& iString, int iStrength);
00219 
00221         static ZTBSpec sVectorContains(const ZTuplePropName& iPropName, const ZTupleValue& iValue);
00222 
00224         static ZTBSpec sRegex(const ZTuplePropName& iPropName, const std::string& iRegex, int iStrength);
00226 
00227 private:
00228         ZTBSpec(CriterionUnion& ioCriterionUnion, bool iKnowWhatIAmDoing);
00229 
00230         void MakeFresh();
00231         ZRef<Rep> fRep;
00232         };
00233 
00234 inline bool ZTBSpec::operator==(const ZTBSpec& iOther) const
00235         { return this->Compare(iOther) == 0; }
00236 
00237 inline bool ZTBSpec::operator<(const ZTBSpec& iOther) const
00238         { return this->Compare(iOther) < 0; }
00239 
00240 namespace ZooLib {
00241 template <>
00242 inline int sCompare_T(const ZTBSpec& iL, const ZTBSpec& iR)
00243         { return iL.Compare(iR); }
00244 }
00245 
00246 // =================================================================================================
00247 #pragma mark -
00248 #pragma mark * ZTBSpec::Comparator
00249 
00251 
00252 class ZTBSpec::Comparator
00253         {
00254 public:
00255         explicit Comparator(const ZStreamR& iStreamR);
00256         Comparator();
00257 
00259         Comparator(ERel iRel, int iStrength);
00260 
00262         Comparator(ERel iRel);
00263         //@{
00266         int Compare(const Comparator& iOther) const;
00267         bool operator<(const Comparator& iOther) const;
00269 
00270         void ToStream(const ZStreamW& iStreamW) const;
00271 
00273         ERel fRel;
00274 
00276         int fStrength;
00277         };
00278 
00279 inline int ZTBSpec::Comparator::Compare(const Comparator& iOther) const
00280         {
00281         if (int compare = fRel - iOther.fRel)
00282                 return compare;
00283 
00284         return fStrength - iOther.fStrength;
00285         }
00286 
00287 inline bool ZTBSpec::Comparator::operator<(const Comparator& iOther) const
00288         {
00289         return fRel < iOther.fRel || (fRel == iOther.fRel && fStrength < iOther.fStrength);
00290         }
00291 
00292 namespace ZooLib {
00293 template <>
00294 inline int sCompare_T(const ZTBSpec::Comparator& iL, const ZTBSpec::Comparator& iR)
00295         { return iL.Compare(iR); }
00296 }
00297 
00298 // =================================================================================================
00299 #pragma mark -
00300 #pragma mark * ZTBSpec::Criterion
00301 
00303 
00304 class ZTBSpec::Criterion
00305         {
00306 private:
00307         // An unitialized Criterion has no meaning.
00308         Criterion();
00309 
00310 public:
00311         explicit Criterion(const ZTuple& iTuple);
00312         explicit Criterion(const ZStreamR& iStreamR);
00313 
00315         Criterion(const ZTuplePropName& iPropName, const Comparator& iComparator, const ZTupleValue& iTV);
00316 
00318         Criterion(const ZTuplePropName& iPropName);
00319 
00321         Criterion(const ZTuplePropName& iPropName, ZType iType);
00322 
00324         Criterion(const ZTuplePropName& iPropName, ERel iRel, const ZTupleValue& iTV);
00325 
00327         Criterion(const ZTuplePropName& iPropName, ERel iRel, int iStrength, const ZTupleValue& iTV);
00328 
00330         ZTuple AsTuple() const;
00331 
00332         void ToStream(const ZStreamW& iStreamW) const;
00333 
00335         bool Matches(const ZTuple& iTuple) const;
00336         //@{
00339         int Compare(const Criterion& iOther) const;
00340         bool operator<(const Criterion& iOther) const;
00342 
00343         const ZTuplePropName& GetPropName() const;
00344         const Comparator& GetComparator() const;
00345         const ZTupleValue& GetTupleValue() const;
00346 
00347 private:
00348         class Rep;
00349         ZRef<Rep> fRep;
00350         };
00351 
00352 namespace ZooLib {
00353 template <>
00354 inline int sCompare_T(const ZTBSpec::Criterion& iL, const ZTBSpec::Criterion& iR)
00355         { return iL.Compare(iR); }
00356 }
00357 
00358 // =================================================================================================
00359 #pragma mark -
00360 #pragma mark * ZTBSpec::Criterion::Rep
00361 
00362 class ZTBSpec::Criterion::Rep : public ZRefCounted
00363         {
00364 private:
00365         Rep();
00366 
00367 public:
00368         static bool sCheckAccessEnabled() { return false; }
00369 
00370         explicit Rep(const ZTuple& iTuple);
00371         explicit Rep(const ZStreamR& iStreamR);
00372 
00373         Rep(const ZTuplePropName& iPropName, const Comparator& iComparator, const ZTupleValue& iTV);
00374         Rep(const ZTuplePropName& iPropName);
00375         Rep(const ZTuplePropName& iPropName, ZType iType);
00376         Rep(const ZTuplePropName& iPropName, ERel iRel, const ZTupleValue& iTV);
00377         Rep(const ZTuplePropName& iPropName, ERel iRel, int iStrength, const ZTupleValue& iTV);
00378 
00379         ZTuple AsTuple() const;
00380         void ToStream(const ZStreamW& iStreamW) const;
00381 
00382 private:
00383         friend class Criterion;
00384 
00385         // It is essential that these fields remain in this order,
00386         // as they may be initialized from a stream.
00387         
00388         ZTuplePropName fPropName;
00389         Comparator fComparator;
00390         ZTupleValue fTupleValue;
00391 
00392         #if ZCONFIG_API_Enabled(Regex)
00393                 mutable ZRegex fRegex;
00394         #endif
00395         mutable ZTextCollator fTextCollator;
00396         };
00397 
00398 // =================================================================================================
00399 #pragma mark -
00400 #pragma mark * ZTBSpec inlines
00401 
00402 inline ZTBSpec::operator operator_bool_type() const
00403         { return operator_bool_generator_type::translate(!this->IsNone()); }
00404 
00405 inline bool ZTBSpec::operator!=(const ZTBSpec& iOther) const
00406         { return !(*this == iOther); }
00407 
00408 inline bool ZTBSpec::operator<=(const ZTBSpec& iOther) const
00409         { return !(iOther < *this); }
00410 
00411 inline bool ZTBSpec::operator>(const ZTBSpec& iOther) const
00412         { return iOther < *this; }
00413 
00414 inline bool ZTBSpec::operator>=(const ZTBSpec& iOther) const
00415         { return !(*this < iOther); }
00416 
00417 inline ZTBSpec ZTBSpec::sAny()
00418         { return ZTBSpec(true); }
00419 
00420 inline ZTBSpec ZTBSpec::sNone()
00421         { return ZTBSpec(); }
00422 
00423 inline ZTBSpec ZTBSpec::sHas(const ZTuplePropName& iPropName)
00424         { return ZTBSpec(iPropName, eRel_Has, ZTupleValue()); }
00425 
00426 inline ZTBSpec ZTBSpec::sHas(const ZTuplePropName& iPropName, ZType iType)
00427         { return ZTBSpec(iPropName, eRel_HasOfType, iType); }
00428 
00429 inline ZTBSpec ZTBSpec::sLacks(const ZTuplePropName& iPropName)
00430         { return ZTBSpec(iPropName, eRel_Lacks, ZTupleValue()); }
00431 
00432 inline ZTBSpec ZTBSpec::sLacks(const ZTuplePropName& iPropName, ZType iType)
00433         { return ZTBSpec(iPropName, eRel_LacksOfType, iType); }
00434 
00435 inline ZTBSpec ZTBSpec::sEquals(const ZTuplePropName& iPropName, const ZTupleValue& iValue)
00436         { return ZTBSpec(iPropName, eRel_Equal, iValue); }
00437 
00438 inline ZTBSpec ZTBSpec::sNotEqual(const ZTuplePropName& iPropName, const ZTupleValue& iValue)
00439         { return ZTBSpec(iPropName, eRel_Less, iValue) | ZTBSpec(iPropName, eRel_Greater, iValue); }
00440 
00441 inline ZTBSpec ZTBSpec::sLess(const ZTuplePropName& iPropName, const ZTupleValue& iValue)
00442         { return ZTBSpec(iPropName, eRel_Less, iValue); }
00443 
00444 inline ZTBSpec ZTBSpec::sLessEqual(const ZTuplePropName& iPropName, const ZTupleValue& iValue)
00445         { return ZTBSpec(iPropName, eRel_LessEqual, iValue); }
00446 
00447 inline ZTBSpec ZTBSpec::sGreater(const ZTuplePropName& iPropName, const ZTupleValue& iValue)
00448         { return ZTBSpec(iPropName, eRel_Greater, iValue); }
00449 
00450 inline ZTBSpec ZTBSpec::sGreaterEqual(const ZTuplePropName& iPropName, const ZTupleValue& iValue)
00451         { return ZTBSpec(iPropName, eRel_GreaterEqual, iValue); }
00452 
00453 inline ZTBSpec ZTBSpec::sStringContains(
00454         const ZTuplePropName& iPropName, const std::string& iString, int iStrength)
00455         { return ZTBSpec(iPropName, eRel_StringContains, iStrength, iString); }
00456 
00457 inline ZTBSpec ZTBSpec::sVectorContains(const ZTuplePropName& iPropName, const ZTupleValue& iValue)
00458         { return ZTBSpec(iPropName, eRel_VectorContains, iValue); }
00459 
00460 inline ZTBSpec ZTBSpec::sRegex(
00461         const ZTuplePropName& iPropName, const std::string& iRegex, int iStrength)
00462         { return ZTBSpec(iPropName, eRel_Regex, iStrength, iRegex); }
00463 
00464 // =================================================================================================
00465 #pragma mark -
00466 #pragma mark * ZTBSpec::Criterion inlines
00467 
00468 inline bool ZTBSpec::Criterion::operator<(const Criterion& iOther) const
00469         { return this->Compare(iOther) < 0; }
00470 
00471 inline const ZTuplePropName& ZTBSpec::Criterion::GetPropName() const
00472         { return fRep->fPropName; }
00473 
00474 inline const ZTBSpec::Comparator& ZTBSpec::Criterion::GetComparator() const
00475         { return fRep->fComparator; }
00476 
00477 inline const ZTupleValue& ZTBSpec::Criterion::GetTupleValue() const
00478         { return fRep->fTupleValue; }
00479 
00480 #endif // __ZTBSpec__

Generated on Thu Jul 26 11:21:57 2007 for ZooLib by  doxygen 1.4.7