|
Qore Programming Language
0.8.3
|
00001 /* -*- mode: c++; indent-tabs-mode: nil -*- */ 00002 /* 00003 QoreHashNode.h 00004 00005 Qore Programming Language 00006 00007 Copyright 2003 - 2011 David Nichols 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Lesser General Public 00011 License as published by the Free Software Foundation; either 00012 version 2.1 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00022 */ 00023 00024 #ifndef _QORE_QOREHASHNODE_H 00025 00026 #define _QORE_QOREHASHNODE_H 00027 00028 #include <qore/AbstractQoreNode.h> 00029 #include <qore/common.h> 00030 00031 class HashMember; 00032 class LocalVar; 00033 00035 00041 class QoreHashNode : public AbstractQoreNode { 00042 friend class HashIterator; 00043 friend class ConstHashIterator; 00044 friend class HashAssignmentHelper; 00045 friend class hash_assignment_priv; 00046 friend class qore_object_private; 00047 00048 private: 00050 DLLLOCAL QoreHashNode(const QoreHashNode&); 00051 00053 DLLLOCAL QoreHashNode& operator=(const QoreHashNode&); 00054 00055 protected: 00057 struct qore_hash_private *priv; 00058 00060 00066 DLLEXPORT virtual bool derefImpl(ExceptionSink *xsink); 00067 00069 00072 DLLEXPORT virtual AbstractQoreNode *evalImpl(ExceptionSink *xsink) const; 00073 00075 00078 DLLLOCAL virtual AbstractQoreNode *evalImpl(bool &needs_deref, ExceptionSink *xsink) const; 00079 00081 DLLLOCAL virtual int64 bigIntEvalImpl(ExceptionSink *xsink) const; 00082 00084 DLLLOCAL virtual int integerEvalImpl(ExceptionSink *xsink) const; 00085 00087 DLLLOCAL virtual bool boolEvalImpl(ExceptionSink *xsink) const; 00088 00090 DLLLOCAL virtual double floatEvalImpl(ExceptionSink *xsink) const; 00091 00093 00096 DLLEXPORT virtual ~QoreHashNode(); 00097 00098 public: 00100 DLLEXPORT QoreHashNode(); 00101 00103 00109 DLLEXPORT virtual int getAsString(QoreString &str, int foff, ExceptionSink *xsink) const; 00110 00112 00119 DLLEXPORT virtual QoreString *getAsString(bool &del, int foff, ExceptionSink *xsink) const; 00120 00122 00124 DLLEXPORT virtual AbstractQoreNode *realCopy() const; 00125 00127 00131 DLLEXPORT virtual bool is_equal_soft(const AbstractQoreNode *v, ExceptionSink *xsink) const; 00132 00134 00138 DLLEXPORT virtual bool is_equal_hard(const AbstractQoreNode *v, ExceptionSink *xsink) const; 00139 00141 00143 DLLEXPORT virtual const char *getTypeName() const; 00144 00146 00148 DLLLOCAL static const char *getStaticTypeName() { 00149 return "hash"; 00150 } 00151 00153 00155 DLLEXPORT QoreHashNode *hashRefSelf() const; 00156 00158 00160 DLLEXPORT const char *getFirstKey() const; 00161 00163 00165 DLLEXPORT const char *getLastKey() const; 00166 00168 00172 DLLEXPORT AbstractQoreNode *getKeyValueExistence(const char *key, bool &exists); 00173 00175 00179 DLLEXPORT const AbstractQoreNode *getKeyValueExistence(const char *key, bool &exists) const; 00180 00182 00189 DLLEXPORT AbstractQoreNode *getKeyValueExistence(const QoreString *key, bool &exists, ExceptionSink *xsink); 00190 00192 00199 DLLEXPORT const AbstractQoreNode *getKeyValueExistence(const QoreString *key, bool &exists, ExceptionSink *xsink) const; 00200 00202 00208 DLLEXPORT AbstractQoreNode *getKeyValue(const QoreString *key, ExceptionSink *xsink); 00209 00211 00217 DLLEXPORT AbstractQoreNode *getKeyValue(const QoreString &key, ExceptionSink *xsink); 00218 00220 00226 DLLEXPORT const AbstractQoreNode *getKeyValue(const QoreString *key, ExceptionSink *xsink) const; 00227 00229 00232 DLLEXPORT AbstractQoreNode *getKeyValue(const char *key); 00233 00235 00238 DLLEXPORT const AbstractQoreNode *getKeyValue(const char *key) const; 00239 00241 00245 DLLEXPORT int64 getKeyAsBigInt(const char *key, bool &found) const; 00246 00248 00252 DLLEXPORT bool getKeyAsBool(const char *key, bool &found) const; 00253 00255 00257 DLLEXPORT QoreHashNode *copy() const; 00258 00260 00268 DLLEXPORT AbstractQoreNode **getKeyValuePtr(const QoreString *key, ExceptionSink *xsink); 00269 00271 00276 DLLEXPORT AbstractQoreNode **getKeyValuePtr(const char *key); 00277 00279 00286 DLLEXPORT AbstractQoreNode **getExistingValuePtr(const QoreString *key, ExceptionSink *xsink); 00287 00289 00293 DLLEXPORT AbstractQoreNode **getExistingValuePtr(const char *key); 00294 00296 00301 DLLEXPORT void merge(const QoreHashNode *h, ExceptionSink *xsink); 00302 00304 00310 DLLEXPORT void setKeyValue(const QoreString *key, AbstractQoreNode *value, ExceptionSink *xsink); 00311 00313 00319 DLLEXPORT void setKeyValue(const QoreString &key, AbstractQoreNode *value, ExceptionSink *xsink); 00320 00322 00328 DLLEXPORT void setKeyValue(const char *key, AbstractQoreNode *value, ExceptionSink *xsink); 00329 00331 00337 DLLEXPORT AbstractQoreNode *swapKeyValue(const QoreString *key, AbstractQoreNode *value, ExceptionSink *xsink); 00338 00340 00345 DLLEXPORT AbstractQoreNode *swapKeyValue(const char *key, AbstractQoreNode *value); 00346 00348 00354 DLLEXPORT AbstractQoreNode *swapKeyValue(const char *key, AbstractQoreNode *value, ExceptionSink *xsink); 00355 00357 00362 DLLEXPORT void deleteKey(const QoreString *key, ExceptionSink *xsink); 00363 00365 00369 DLLEXPORT void deleteKey(const char *key, ExceptionSink *xsink); 00370 00372 00376 DLLEXPORT AbstractQoreNode *takeKeyValue(const QoreString *key, ExceptionSink *xsink); 00377 00379 00382 DLLEXPORT AbstractQoreNode *takeKeyValue(const char *key); 00383 00385 00388 DLLEXPORT QoreListNode *getKeys() const; 00389 00391 00394 DLLEXPORT bool compareSoft(const QoreHashNode *h, ExceptionSink *xsink) const; 00395 00397 00400 DLLEXPORT bool compareHard(const QoreHashNode *h, ExceptionSink *xsink) const; 00401 00403 00405 DLLEXPORT qore_size_t size() const; 00406 00408 00410 DLLEXPORT bool empty() const; 00411 00413 00417 DLLEXPORT bool existsKey(const char *key) const; 00418 00420 00423 DLLEXPORT bool existsKeyValue(const char *key) const; 00424 00426 00430 DLLEXPORT void removeKey(const QoreString *key, ExceptionSink *xsink); 00431 00433 00437 DLLEXPORT void removeKey(const char *key, ExceptionSink *xsink); 00438 00440 DLLLOCAL virtual AbstractQoreNode *parseInit(LocalVar *oflag, int pflag, int &lvids, const QoreTypeInfo *&typeInfo); 00441 00442 DLLLOCAL QoreHashNode(bool ne); 00443 DLLLOCAL void clear(ExceptionSink *xsink); 00444 00446 DLLLOCAL void clearNeedsEval(); 00447 00449 DLLLOCAL void setNeedsEval(); 00450 00451 DLLLOCAL AbstractQoreNode *evalKeyValue(const QoreString *key, ExceptionSink *xsink) const; 00452 00453 // returns a new hash consisting of just the members of value_list 00454 DLLLOCAL QoreHashNode *getSlice(const QoreListNode *value_list, ExceptionSink *xsink) const; 00455 00456 // "key" is always passed in the default character encoding 00457 DLLLOCAL AbstractQoreNode *getReferencedKeyValue(const char *key) const; 00458 00459 // "key" is always passed in the default character encoding 00460 DLLLOCAL AbstractQoreNode *getReferencedKeyValue(const char *key, bool &exists) const; 00461 00462 DLLLOCAL AbstractQoreNode *getFirstKeyValue() const; 00463 00464 DLLLOCAL static void doDuplicateKeyWarning(const char *key); 00465 }; 00466 00467 #include <qore/ReferenceHolder.h> 00468 00470 00473 typedef ReferenceHolder<QoreHashNode> QoreHashNodeHolder; 00474 00476 00485 class HashIterator { 00486 friend class HashAssignmentHelper; 00487 00488 private: 00489 QoreHashNode *h; 00490 HashMember *ptr; 00491 00493 DLLLOCAL HashIterator(const HashIterator&); 00494 00496 DLLLOCAL HashIterator& operator=(const HashIterator&); 00497 00499 DLLLOCAL void* operator new(size_t); 00500 00501 public: 00503 DLLEXPORT HashIterator(QoreHashNode *h); 00504 00506 DLLEXPORT HashIterator(QoreHashNode &h); 00507 00509 00512 DLLEXPORT bool next(); 00513 00515 DLLEXPORT const char *getKey() const; 00516 00518 DLLEXPORT QoreString *getKeyString() const; 00519 00521 DLLEXPORT AbstractQoreNode *getValue() const; 00522 00524 DLLEXPORT AbstractQoreNode *takeValueAndDelete(); 00525 00527 00531 DLLEXPORT void deleteKey(ExceptionSink *xsink); 00532 00534 00537 DLLEXPORT AbstractQoreNode **getValuePtr() const; 00538 00540 DLLEXPORT AbstractQoreNode *getReferencedValue() const; 00541 00543 DLLEXPORT bool first() const; 00544 00546 DLLEXPORT bool last() const; 00547 00548 //DLLEXPORT void setValue(AbstractQoreNode *val, ExceptionSink *xsink); 00549 }; 00550 00552 00561 class ConstHashIterator { 00562 private: 00563 const QoreHashNode *h; 00564 HashMember *ptr; 00565 00567 DLLLOCAL ConstHashIterator(const HashIterator&); 00568 00570 DLLLOCAL ConstHashIterator& operator=(const HashIterator&); 00571 00573 DLLLOCAL void* operator new(size_t); 00574 00575 public: 00577 DLLEXPORT ConstHashIterator(const QoreHashNode *h); 00578 00580 DLLEXPORT ConstHashIterator(const QoreHashNode &h); 00581 00583 00586 DLLEXPORT bool next(); 00587 00589 DLLEXPORT const char *getKey() const; 00590 00592 DLLEXPORT QoreString *getKeyString() const; 00593 00595 DLLEXPORT const AbstractQoreNode *getValue() const; 00596 00598 DLLEXPORT AbstractQoreNode *getReferencedValue() const; 00599 00601 DLLEXPORT bool first() const; 00602 00604 DLLEXPORT bool last() const; 00605 }; 00606 00608 class HashAssignmentHelper { 00609 private: 00611 DLLLOCAL HashAssignmentHelper(const HashAssignmentHelper&); 00612 00614 DLLLOCAL HashAssignmentHelper& operator=(const HashAssignmentHelper&); 00615 00617 DLLLOCAL void* operator new(size_t); 00618 00619 protected: 00621 struct hash_assignment_priv *priv; 00622 00623 public: 00625 00629 DLLLOCAL HashAssignmentHelper(QoreHashNode &n_h, const char *key, bool must_already_exist = false); 00630 00632 00636 DLLLOCAL HashAssignmentHelper(QoreHashNode &n_h, const std::string &key, bool must_already_exist = false); 00637 00639 00646 DLLLOCAL HashAssignmentHelper(ExceptionSink *xsink, QoreHashNode &n_h, const QoreString &key, bool must_already_exist = false); 00647 00649 00656 DLLLOCAL HashAssignmentHelper(ExceptionSink *xsink, QoreHashNode &n_h, const QoreString *key, bool must_already_exist = false); 00657 00659 00661 DLLLOCAL HashAssignmentHelper(HashIterator &hi); 00662 00664 DLLLOCAL ~HashAssignmentHelper(); 00665 00667 00669 DLLLOCAL operator bool() const; 00670 00672 00676 DLLLOCAL void assign(AbstractQoreNode *v, ExceptionSink *xsink); 00677 00679 00682 DLLLOCAL AbstractQoreNode *swap(AbstractQoreNode *v, ExceptionSink *xsink); 00683 00685 00687 DLLLOCAL AbstractQoreNode *operator*() const; 00688 }; 00689 00690 #endif // _QORE_HASH_H