All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
DateTimeNode.h
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
DateTimeNode.h
DateTimeNode Class Definition
Qore Programming Language
Copyright 2003 - 2013 David Nichols
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _QORE_DATETIMENODE_H
#define _QORE_DATETIMENODE_H
#include <qore/AbstractQoreNode.h>
#include <qore/DateTime.h>
class qore_date_private;
class DateTimeNode : public SimpleValueQoreNode, public DateTime {
private:
DLLLOCAL DateTimeNode(const DateTime *);
DLLLOCAL DateTimeNode& operator=(const DateTimeNode &);
DLLEXPORT virtual bool getAsBoolImpl() const;
DLLEXPORT virtual int getAsIntImpl() const;
DLLEXPORT virtual int64 getAsBigIntImpl() const;
DLLEXPORT virtual double getAsFloatImpl() const;
DLLLOCAL DateTimeNode(qore_date_private *n_priv);
protected:
DLLEXPORT virtual ~DateTimeNode();
public:
DLLEXPORT DateTimeNode(bool r = false);
DLLEXPORT DateTimeNode(int n_year, int n_month, int n_day, int n_hour = 0, int n_minute = 0, int n_second = 0, short n_ms = 0, bool n_relative = false);
DLLEXPORT DateTimeNode(int64 seconds);
DLLEXPORT DateTimeNode(int64 seconds, int ms);
DLLEXPORT DateTimeNode(const char *date);
DLLEXPORT DateTimeNode(const AbstractQoreZoneInfo *zone, const char *date);
DLLEXPORT DateTimeNode(struct tm *tms);
DLLEXPORT DateTimeNode(const DateTimeNode &dt);
DLLEXPORT DateTimeNode(const DateTime &dt);
DLLEXPORT virtual QoreString *getStringRepresentation(bool &del) const;
DLLEXPORT virtual void getStringRepresentation(QoreString &str) const;
DLLEXPORT virtual DateTime *getDateTimeRepresentation(bool &del) const;
DLLEXPORT virtual void getDateTimeRepresentation(DateTime &dt) const;
DLLEXPORT virtual QoreString *getAsString(bool &del, int foff, ExceptionSink *xsink) const;
DLLEXPORT virtual int getAsString(QoreString &str, int foff, ExceptionSink *xsink) const;
DLLEXPORT virtual class AbstractQoreNode *realCopy() const;
DLLEXPORT virtual bool is_equal_soft(const AbstractQoreNode *v, ExceptionSink *xsink) const;
DLLEXPORT virtual bool is_equal_hard(const AbstractQoreNode *v, ExceptionSink *xsink) const;
DLLEXPORT virtual const char *getTypeName() const;
DLLEXPORT DateTimeNode *copy() const;
DLLEXPORT DateTimeNode *add(const DateTime *dt) const;
DLLEXPORT DateTimeNode *subtractBy(const DateTime *dt) const;
DLLEXPORT DateTimeNode *unaryMinus() const;
DLLLOCAL static const char *getStaticTypeName() {
return "date";
}
DLLLOCAL static qore_type_t getStaticTypeCode() {
return NT_DATE;
}
DLLEXPORT static DateTimeNode *getDateFromISOWeek(int year, int week, int day, ExceptionSink *xsink);
DLLEXPORT static DateTimeNode *makeAbsolute(const AbstractQoreZoneInfo *n_zone, int n_year, int n_month, int n_day, int n_hour = 0, int n_minute = 0, int n_second = 0, int n_us = 0);
DLLEXPORT static DateTimeNode *makeAbsolute(const AbstractQoreZoneInfo *zone, int64 seconds, int us = 0);
DLLEXPORT static DateTimeNode *makeAbsoluteLocal(const AbstractQoreZoneInfo *zone, int64 seconds, int us = 0);
DLLEXPORT static DateTimeNode *makeRelative(int n_year, int n_month, int n_day, int n_hour = 0, int n_minute = 0, int n_second = 0, int n_us = 0);
};
DLLEXPORT extern DateTimeNode *ZeroDate;
class DateTimeValueHelper {
private:
const DateTime *dt;
bool del;
DLLLOCAL DateTimeValueHelper(const DateTimeValueHelper&); // not implemented
DLLLOCAL DateTimeValueHelper& operator=(const DateTimeValueHelper&); // not implemented
DLLLOCAL void *operator new(size_t); // not implemented, make sure it is not new'ed
public:
DLLLOCAL DateTimeValueHelper(const AbstractQoreNode *n) {
// optmization without virtual function call for most common case
if (n) {
if (n->getType() == NT_DATE) {
dt = reinterpret_cast<const DateTimeNode *>(n);
del = false;
}
else
dt = n->getDateTimeRepresentation(del);
}
else {
dt = ZeroDate;
del = false;
}
}
DLLLOCAL ~DateTimeValueHelper() {
if (del)
delete const_cast< DateTime *>(dt);
}
DLLLOCAL const DateTime *operator->() { return dt; }
DLLLOCAL const DateTime *operator*() { return dt; }
};
class DateTimeNodeValueHelper {
private:
DateTimeNode *dt;
bool temp;
DLLLOCAL DateTimeNodeValueHelper(const DateTimeNodeValueHelper&); // not implemented
DLLLOCAL DateTimeNodeValueHelper& operator=(const DateTimeNodeValueHelper&); // not implemented
DLLLOCAL void *operator new(size_t); // not implemented, make sure it is not new'ed
public:
DLLLOCAL DateTimeNodeValueHelper(const AbstractQoreNode *n) {
if (!n) {
dt = ZeroDate;
temp = false;
return;
}
// optmization without virtual function call for most common case
if (n->getType() == NT_DATE) {
dt = const_cast< DateTimeNode *>(reinterpret_cast<const DateTimeNode *>(n));
temp = false;
return;
}
dt = new DateTimeNode();
n->getDateTimeRepresentation(*dt);
temp = true;
}
DLLLOCAL ~DateTimeNodeValueHelper() {
if (dt && temp)
dt->deref();
}
DLLLOCAL const DateTimeNode *operator->() { return dt; }
DLLLOCAL const DateTimeNode *operator*() { return dt; }
DLLLOCAL DateTimeNode *getReferencedValue() {
if (temp)
temp = false;
else if (dt)
dt->ref();
return dt;
}
};
#endif