Shorthand是一个强大的脚本语言
源代码在线查看: value.h
#ifndef __value_h
#define __value_h
///////////////////////////////////////////////////////////////////////////////
// $Header: /shorthand/src/value.h 6 2/08/03 6:40a Arm $
//-----------------------------------------------------------------------------
// Project: ShortHand interpreter
// Author: Andrei Remenchuk
//-----------------------------------------------------------------------------
// value.h: ShortHand value class
///////////////////////////////////////////////////////////////////////////////
#include
#include "array.h"
#include "cstring.h"
#include "datetime.h"
class ShhObject;
class ShhArray;
class ShhHash;
/**
* ShortHand value type
*/
typedef enum
{
SHH_INT=0,
SHH_FLOAT=2,
SHH_DATE=3,
SHH_STRING=4,
SHH_OBJECT=5,
SHH_ARRAY=6,
SHH_HASH=7,
} ShhType;
/**
* Primitive ShortHand value
*/
struct ShhValue
{
int m_type;
union {
int u_int;
const char* u_string;
double u_float;
ShhObject* u_object;
ShhArray* u_array;
ShhHash* u_hash;
unsigned int u_date[2];
} m_value;
static ShhValue null();
static ShhValue EMPTY;
ShhValue() : m_type(SHH_STRING) { m_value.u_string = ""; }
ShhValue(int value) : m_type(SHH_INT) { m_value.u_int = value; }
ShhValue(const char* value) : m_type(SHH_STRING) { if (value == NULL) value = ""; m_value.u_string = value; }
ShhValue(ShhObject* value) : m_type(SHH_OBJECT) { m_value.u_object = value; }
ShhValue(double value) : m_type(SHH_FLOAT) { m_value.u_float = value; }
ShhValue(const datetime& dt) : m_type(SHH_DATE) { dt.export_jdx(m_value.u_date); }
ShhValue(const ShhValue& other)
{
m_type = other.m_type;
m_value = other.m_value;
}
bool isEmpty() const
{
if (m_type == SHH_FLOAT) return m_value.u_float == 0.0;
else return m_value.u_int == 0;
}
ShhValue* clone() const
{
return new ShhValue(*this);
}
const ShhValue& operator = (const ShhValue& other)
{
m_type = other.m_type;
m_value = other.m_value;
return *this;
}
const ShhValue& operator = (int x)
{
m_type = SHH_INT;
m_value.u_int = x;
return *this;
}
const ShhValue& operator = (const char* s)
{
m_type = SHH_STRING;
m_value.u_string = s;
return *this;
}
const ShhValue& operator = (ShhObject* o)
{
m_type = SHH_OBJECT;
m_value.u_object = o;
return *this;
}
const ShhValue& operator = (const datetime& dt)
{
dt.export_jdx((unsigned int*)m_value.u_date);
m_type = SHH_DATE;
return *this;
}
const ShhValue& setDate(const datetime& dt)
{
dt.export_jdx((unsigned int*)m_value.u_date);
m_type = SHH_DATE;
return *this;
}
const ShhValue& setFloat(double x)
{
m_type = SHH_FLOAT;
m_value.u_float = x;
return *this;
}
const ShhValue& setInt(int x)
{
m_type = SHH_INT;
m_value.u_int = x;
return *this;
}
const ShhValue& setArray(ShhArray* array)
{
m_type = SHH_ARRAY;
m_value.u_array = array;
return *this;
}
const ShhValue& setHash(ShhHash* hash)
{
m_type = SHH_HASH;
m_value.u_hash = hash;
return *this;
}
int toInt() const;
void toString(string& s) const;
void toDate(datetime& dt) const;
double toFloat() const;
const char* toSZ(string& backupBuffer) const;
ShhObject* toObject() const;
ShhArray* toArray() const { return isArray() ? m_value.u_array : NULL; }
ShhHash* toHash() const { return isHash() ? m_value.u_hash : NULL; }
bool isString() const { return m_type == SHH_STRING; }
bool isFloat() const { return m_type == SHH_FLOAT; }
bool isInt() const { return m_type == SHH_INT; }
bool isObject() const { return m_type == SHH_OBJECT || m_type == SHH_ARRAY || m_type == SHH_HASH; }
bool isDate() const { return m_type == SHH_DATE; }
bool isArray() const { return m_type == SHH_ARRAY; }
bool isHash() const { return m_type == SHH_HASH; }
bool isLikeNumber() const;
bool isLikeInt() const;
bool isLikeFloat() const;
const char* getTypeName() const;
};
class ShhValueList : public array
{
public:
ShhValueList() {}
int hasIndex(int index) const { return index >= 0 && index < size(); }
};
ShhValue operator + (const ShhValue& a, const ShhValue& b);
ShhValue operator - (const ShhValue& a, const ShhValue& b);
ShhValue operator * (const ShhValue& a, const ShhValue& b);
ShhValue operator / (const ShhValue& a, const ShhValue& b);
bool operator == (const ShhValue& a, const ShhValue& b);
bool operator != (const ShhValue& a, const ShhValue& b);
bool operator < (const ShhValue& a, const ShhValue& b);
bool operator > (const ShhValue& a, const ShhValue& b);
bool operator bool operator >= (const ShhValue& a, const ShhValue& b);
#endif // __value_h