Qore Programming Language  0.8.3
include/qore/QoreStringNode.h (4311)
00001 /* -*- mode: c++; indent-tabs-mode: nil -*- */
00002 /*
00003   QoreStringNode.h
00004 
00005   QoreStringNode Class Definition
00006 
00007   Qore Programming Language
00008   
00009   Copyright 2003 - 2011 David Nichols
00010 
00011   This library is free software; you can redistribute it and/or
00012   modify it under the terms of the GNU Lesser General Public
00013   License as published by the Free Software Foundation; either
00014   version 2.1 of the License, or (at your option) any later version.
00015 
00016   This library is distributed in the hope that it will be useful,
00017   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019   Lesser General Public License for more details.
00020 
00021   You should have received a copy of the GNU Lesser General Public
00022   License along with this library; if not, write to the Free Software
00023   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00024 */
00025 
00026 #ifndef _QORE_QORESTRINGNODE_H
00027 
00028 #define _QORE_QORESTRINGNODE_H
00029 
00030 #include <qore/AbstractQoreNode.h>
00031 #include <qore/QoreString.h>
00032 
00034 
00040 class QoreStringNode : public SimpleValueQoreNode, public QoreString {
00041 private:
00043    DLLLOCAL QoreStringNode(QoreString *str);
00044 
00046    DLLLOCAL QoreStringNode& operator=(const QoreStringNode&);
00047 
00048    DLLLOCAL virtual bool getAsBoolImpl() const;
00049    DLLLOCAL virtual int getAsIntImpl() const;
00050    DLLLOCAL virtual int64 getAsBigIntImpl() const;
00051    DLLLOCAL virtual double getAsFloatImpl() const;
00052 
00053 protected:
00055    DLLEXPORT virtual ~QoreStringNode();
00056 
00057 public:
00059    DLLEXPORT QoreStringNode();
00060 
00062 
00066    DLLEXPORT QoreStringNode(const char *str, const QoreEncoding *enc = QCS_DEFAULT);
00067 
00069 
00072    DLLEXPORT QoreStringNode(const QoreString &str);
00073 
00075 
00078    DLLEXPORT QoreStringNode(const QoreStringNode &str);
00079 
00081 
00085    DLLEXPORT QoreStringNode(const std::string &str, const QoreEncoding *enc = QCS_DEFAULT);
00086 
00087    // copies binary object and makes a base64-encoded string out of it
00088    DLLEXPORT QoreStringNode(const BinaryNode *b);
00089 
00091 
00097    DLLEXPORT QoreStringNode(char *nbuf, qore_size_t nlen, qore_size_t nallocated, const QoreEncoding *enc);
00098 
00100    DLLEXPORT QoreStringNode(const char *str, qore_size_t len, const QoreEncoding *new_qorecharset = QCS_DEFAULT);
00101 
00102    // creates a string from a single character
00103    DLLEXPORT QoreStringNode(char c);
00104 
00106 
00112    DLLEXPORT int getAsString(QoreString &str, int format_offset, ExceptionSink *xsink) const;
00113 
00115 
00122    DLLEXPORT QoreString *getAsString(bool &del, int format_offset, ExceptionSink *xsink) const;
00123 
00125 
00129    DLLEXPORT virtual QoreString *getStringRepresentation(bool &del) const;
00130 
00132 
00135    DLLEXPORT virtual void getStringRepresentation(QoreString &str) const;
00136 
00138 
00142    DLLEXPORT virtual DateTime *getDateTimeRepresentation(bool &del) const;
00143 
00145 
00148    DLLEXPORT virtual void getDateTimeRepresentation(DateTime &dt) const;
00149 
00151    DLLEXPORT virtual AbstractQoreNode *realCopy() const;
00152 
00154 
00158    DLLEXPORT virtual bool is_equal_soft(const AbstractQoreNode *v, ExceptionSink *xsink) const;
00159 
00161 
00166    DLLEXPORT virtual bool is_equal_hard(const AbstractQoreNode *v, ExceptionSink *xsink) const;
00167 
00169    DLLEXPORT virtual const char *getTypeName() const;
00170 
00172 
00177    DLLEXPORT QoreStringNode *convertEncoding(const QoreEncoding *nccs, ExceptionSink *xsink) const;
00178 
00180 
00185    DLLEXPORT QoreStringNode *substr(qore_offset_t offset, ExceptionSink *xsink) const;
00186 
00188 
00194    DLLEXPORT QoreStringNode *substr(qore_offset_t offset, qore_offset_t length, ExceptionSink *xsink) const;
00195 
00197    DLLEXPORT QoreStringNode *reverse() const;
00198 
00199    // copy function
00200    DLLEXPORT QoreStringNode *copy() const;
00201 
00203    DLLEXPORT static QoreStringNode *createAndConvertEncoding(const char *str, const QoreEncoding *from, const QoreEncoding *to, ExceptionSink *xsink);
00204 
00206    DLLEXPORT QoreStringNode *parseBase64ToString(ExceptionSink *xsink) const;
00207 
00209    DLLEXPORT QoreStringNode *stringRefSelf() const;
00210 
00212    DLLLOCAL QoreStringNode(const char *str, const QoreEncoding *from, const QoreEncoding *to, ExceptionSink *xsink);
00213 
00215 
00220    DLLEXPORT QoreStringNode *extract(qore_offset_t offset, ExceptionSink *xsink);
00221 
00223 
00229    DLLEXPORT QoreStringNode *extract(qore_offset_t offset, qore_offset_t length, ExceptionSink *xsink);
00230 
00232 
00239    DLLEXPORT QoreStringNode *extract(qore_offset_t offset, qore_offset_t length, const AbstractQoreNode *strn, ExceptionSink *xsink);
00240 
00242    DLLLOCAL QoreStringNode(struct qore_string_private *p);
00243 
00244    DLLLOCAL static const char *getStaticTypeName() {
00245       return "string";
00246    }
00247 
00249    DLLLOCAL virtual AbstractQoreNode *parseInit(LocalVar *oflag, int pflag, int &lvids, const QoreTypeInfo *&typeInfo);
00250 };
00251 
00252 extern QoreStringNode *NullString;
00253 
00255 
00261 class QoreStringValueHelper {
00262 private:
00263    QoreString *str;
00264    bool del;
00265 
00267    DLLLOCAL QoreStringValueHelper(const QoreStringValueHelper&); // not implemented
00268 
00270    DLLLOCAL QoreStringValueHelper& operator=(const QoreStringValueHelper&); // not implemented
00271 
00273    DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
00274 
00275 public:
00277    DLLLOCAL QoreStringValueHelper(const AbstractQoreNode *n) {
00278       if (n) {
00279          //optimization to remove the need for a virtual function call in the most common case
00280          if (n->getType() == NT_STRING) {
00281             del = false;
00282             str = const_cast<QoreStringNode *>(reinterpret_cast<const QoreStringNode *>(n));
00283          }
00284          else
00285             str = n->getStringRepresentation(del);
00286       }
00287       else {
00288          str = NullString;
00289          del = false;
00290       }
00291    }
00292 
00294 
00306    DLLLOCAL QoreStringValueHelper(const AbstractQoreNode *n, const QoreEncoding *enc, ExceptionSink *xsink) {
00307       if (n) {
00308          //optimization to remove the need for a virtual function call in the most common case
00309          if (n->getType() == NT_STRING) {
00310             del = false;
00311             str = const_cast<QoreStringNode *>(reinterpret_cast<const QoreStringNode *>(n));
00312          }
00313          else
00314             str = n->getStringRepresentation(del);
00315          if (str->getEncoding() != enc) {
00316             QoreString *t = str->convertEncoding(enc, xsink);
00317             if (!t)
00318                return;
00319             if (del)
00320                delete str;
00321             str = t;
00322             del = true;
00323          }
00324       }
00325       else {
00326          str = NullString;
00327          del = false;
00328       }
00329    }
00330 
00332    DLLLOCAL ~QoreStringValueHelper() {
00333       if (del)
00334          delete str;
00335    }
00336 
00338 
00341    DLLLOCAL const QoreString *operator->() { return str; }
00342 
00344 
00347    DLLLOCAL const QoreString *operator*() { return str; }
00348 
00350 
00353    DLLLOCAL QoreString *giveString() {
00354       if (!str)
00355          return 0;
00356       if (!del)
00357          return str->copy();
00358 
00359       QoreString *rv = str;
00360       del = false;
00361       str = 0;
00362       return rv;
00363    }
00364 
00366    DLLLOCAL bool is_temp() const {
00367       return del;
00368    }
00369 };
00370 
00372 
00379 class QoreStringNodeValueHelper {
00380 private:
00381    QoreStringNode *str;
00382    bool temp;
00383 
00385    DLLLOCAL QoreStringNodeValueHelper(const QoreStringNodeValueHelper&);
00386 
00388    DLLLOCAL QoreStringNodeValueHelper& operator=(const QoreStringNodeValueHelper&);
00389 
00391    DLLLOCAL void* operator new(size_t);
00392 
00393 public:
00394    DLLLOCAL QoreStringNodeValueHelper(const AbstractQoreNode *n) {
00395       if (!n) {
00396          str = NullString;
00397          temp = false;
00398          return;
00399       }
00400 
00401       qore_type_t ntype = n->getType();
00402       if (ntype == NT_STRING) {
00403          str = const_cast<QoreStringNode *>(reinterpret_cast<const QoreStringNode *>(n));
00404          temp = false;
00405       }
00406       else {
00407          str = new QoreStringNode();
00408          n->getStringRepresentation(*(static_cast<QoreString *>(str)));
00409          temp = true;
00410       }
00411    }
00412 
00414    DLLLOCAL ~QoreStringNodeValueHelper() {
00415       if (temp)
00416          str->deref();
00417    }
00418 
00420 
00423    DLLLOCAL const QoreStringNode *operator->() { return str; }
00424 
00426 
00429    DLLLOCAL const QoreStringNode *operator*() { return str; }
00430 
00432 
00436    DLLLOCAL QoreStringNode *getReferencedValue() {
00437       if (temp)
00438          temp = false;
00439       else if (str)
00440          str->ref();
00441       return str;
00442    }
00443 };
00444 
00445 #include <qore/ReferenceHolder.h>
00446 
00448 
00451 typedef SimpleRefHolder<QoreStringNode> QoreStringNodeHolder;
00452 
00453 extern QoreString NothingTypeString;
00454 
00456 
00465 class QoreNodeAsStringHelper {
00466 private:
00467    QoreString *str;
00468    bool del;
00469 
00471    DLLLOCAL QoreNodeAsStringHelper(const QoreNodeAsStringHelper&); // not implemented
00472 
00474    DLLLOCAL QoreNodeAsStringHelper& operator=(const QoreNodeAsStringHelper&); // not implemented
00475 
00477    DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
00478 
00479 public:
00481    DLLLOCAL QoreNodeAsStringHelper(const AbstractQoreNode *n, int format_offset, ExceptionSink *xsink) {
00482       if (n)
00483          str = n->getAsString(del, format_offset, xsink);
00484       else {
00485          str = &NothingTypeString;
00486          del = false;
00487       }
00488    }
00489 
00491    DLLLOCAL ~QoreNodeAsStringHelper() {
00492       if (del)
00493          delete str;
00494    }
00495 
00497 
00500    DLLLOCAL const QoreString *operator->() { return str; }
00501 
00503 
00506    DLLLOCAL const QoreString *operator*() { return str; }
00507 
00509 
00512    DLLLOCAL QoreString *giveString() {
00513       if (!str)
00514          return 0;
00515       if (!del)
00516          return str->copy();
00517 
00518       QoreString *rv = str;
00519       del = false;
00520       str = 0;
00521       return rv;
00522    }
00523 };
00524 
00525 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines