Skip to content

Commit

Permalink
core, refactor: add intVal and longVal to Variant.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Oct 26, 2019
1 parent 203fe0d commit 65e7d39
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
14 changes: 13 additions & 1 deletion fibjs/include/Redis.h
Expand Up @@ -260,7 +260,19 @@ class Redis : public Redis_base {

static result_t retValue(Variant& v, bool& retVal)
{
retVal = !!(int32_t)v;
retVal = !!v.intVal();
return 0;
}

static result_t retValue(Variant& v, int32_t& retVal)
{
retVal = v.intVal();
return 0;
}

static result_t retValue(Variant& v, int64_t& retVal)
{
retVal = v.longVal();
return 0;
}

Expand Down
33 changes: 26 additions & 7 deletions fibjs/include/Variant.h
Expand Up @@ -367,11 +367,26 @@ class Variant {

operator v8::Local<v8::Value>() const;

operator int32_t() const
int32_t intVal() const
{
if (type() != VT_Integer)
return 0;
return m_Val.intVal;
if (type() == VT_Integer)
return m_Val.intVal;
if (type() == VT_Long)
return m_Val.longVal;
if (type() == VT_Number)
return m_Val.dblVal;
return 0;
}

int64_t longVal() const
{
if (type() == VT_Integer)
return m_Val.intVal;
if (type() == VT_Long)
return m_Val.longVal;
if (type() == VT_Number)
return m_Val.dblVal;
return 0;
}

bool boolVal() const
Expand All @@ -383,9 +398,13 @@ class Variant {

double dblVal() const
{
if (type() != VT_Number)
return 0;
return m_Val.dblVal;
if (type() == VT_Integer)
return m_Val.intVal;
if (type() == VT_Long)
return m_Val.longVal;
if (type() == VT_Number)
return m_Val.dblVal;
return 0;
}

void parseNumber(const char* str, int32_t len = -1);
Expand Down
2 changes: 1 addition & 1 deletion fibjs/src/net/DgramSocket.cpp
Expand Up @@ -258,7 +258,7 @@ result_t DgramSocket::bind(v8::Local<v8::Object> opts, AsyncEvent* ac)
ac->m_ctx[1] = addr;
}

int32_t port = (int32_t)ac->m_ctx[0];
int32_t port = ac->m_ctx[0].intVal();
exlib::string addr = ac->m_ctx[1].string();

return bind(port, addr, ac);
Expand Down

0 comments on commit 65e7d39

Please sign in to comment.