解除对基类Object依赖

This commit is contained in:
大石头 2017-02-26 15:09:02 +08:00
parent 7d36d3ce73
commit 2dd269fd9e
11 changed files with 60 additions and 65 deletions

View File

@ -23,22 +23,22 @@ void Json::Init(cstring str, int len)
Json::Json(int value)
{
}
Json::Json(bool value)
{
}
Json::Json(double value)
{
}
Json::Json(String& value)
{
}
// 值类型
@ -292,8 +292,7 @@ Json& Json::Add(cstring key, const Json& value)
s += key;
s += "\":";
//s += value;
value.ToStr(s);
s += value;
s += '}';
@ -310,15 +309,16 @@ Json& Json::Add(const Json& value)
else
s += '[';
value.ToStr(s);
s += value;
s += ']';
return *this;
}
String& Json::ToStr(String& str) const
String Json::ToString() const
{
String str;
if(_s) str += *_s;
return str;
@ -627,8 +627,9 @@ uint JObject::size() const
return _items.Count();
}
String& JObject::ToStr(String& str) const
String JObject::ToString() const
{
String str;
auto& keys = _items.Keys();
auto& vals = _items.Values();
str += '{';
@ -640,7 +641,7 @@ String& JObject::ToStr(String& str) const
str = str + '"' + key + '"' + ':';
auto& val = *(JValue*)vals[i];
val.ToStr(str);
str += val;
}
str += '}';
@ -688,23 +689,25 @@ void JArray::Add(const JValue& v)
_array.Add(&(JValue&)v);
}
String& JArray::ToStr(String& str) const
String JArray::ToString() const
{
String str;
str += '[';
for(int i=0; i<size(); i++)
{
if(i > 0) str += ',';
auto& v = (*this)[i];
v.ToStr(str);
str += v;
}
str += ']';
return str;
}
String& JValue::ToStr(String& str) const
String JValue::ToString() const
{
String str;
auto& v = *this;
switch(v.type())
{
@ -729,11 +732,11 @@ String& JValue::ToStr(String& str) const
break;
case ARRAY:
((JArray)v).ToStr(str);
str += (JArray&)v;
break;
case OBJECT:
((JObject)v).ToStr(str);
str += (JObject&)v;
break;
}

View File

@ -23,7 +23,7 @@ enum class JsonType : byte
};
// Json对象
class Json : public Object
class Json
{
public:
static Json Null;
@ -65,7 +65,7 @@ public:
// 添加数组成员
Json& Add(const Json& value);
virtual String& ToStr(String& str) const;
String ToString() const;
#if DEBUG
static void Test();
@ -100,7 +100,6 @@ class JValue;
class JObject
{
public:
JObject();
JObject(const JObject& o);
JObject(JObject&& o);
@ -119,7 +118,7 @@ public:
// 大小
uint size() const;
String& ToStr(String& str) const;
String ToString() const;
protected:
@ -131,9 +130,7 @@ protected:
class JArray
{
public:
JArray();
~JArray();
JArray(const JArray& a);
@ -151,10 +148,9 @@ public:
// 大小
uint size() const;
String& ToStr(String& str) const;
String ToString() const;
protected:
// 内部容器
List<JValue*> _array;
};
@ -207,11 +203,9 @@ public:
bool as_bool() const { return bool_v; }
String as_string() const { return string_v; }
String& ToStr(String& str) const;
String ToString() const;
protected:
double float_v;
Int64 int_v;
bool bool_v;

View File

@ -32,9 +32,7 @@ public:
// 把消息写入数据流中
virtual void Write(Stream& ms) const;
#if DEBUG
virtual String& ToStr(String& str) const;
#endif
};
#endif

View File

@ -58,7 +58,7 @@ void TinyServer::Start()
TS("TinyServer::Start");
assert(Cfg, "未指定微网服务器的配置");
// 最后倒数8KB - 倒数位置4KB 的 4KB 空间
//const uint DevAddr = 0x8000000 + (Sys.FlashSize << 10) - (8 << 10);
//const uint DevSize = 4 << 10;
@ -331,7 +331,7 @@ bool TinyServer::OnJoin(const TinyMessage& msg)
dv->Logins++;
debug_printf("\r\nTinyServer::设备第 %d 次组网 TranID=0x%04X \r\n", dv->Logins, dm.TranID);
dv->Show(true);
dv->ToString().Show(true);
// 响应
auto rs = msg.CreateReply();

View File

@ -132,9 +132,9 @@ bool Device::Valid() const
return true;
}
#if DEBUG
String& Device::ToStr(String& str) const
String Device::ToString() const
{
String str;
str += "Addr=0x";
str.Concat(Address, -16);
str += " Kind=";
@ -167,7 +167,6 @@ String& Device::ToStr(String& str) const
}
return str;
}
#endif
bool operator==(const Device& d1, const Device& d2)
{

View File

@ -19,7 +19,7 @@ union DevFlag
// 设备信息
class Device : public Object
class Device
{
public:
bool Logined; // 是否在线
@ -65,7 +65,7 @@ public:
//Buffer Config;
Device();
virtual ~Device();
~Device();
// 序列化到消息数据流
void Write(Stream& ms) const;
@ -77,9 +77,7 @@ public:
bool CanSleep() const { return SleepTime > 0; }
bool Valid() const;
#if DEBUG
virtual String& ToStr(String& str) const;
#endif
String ToString() const;
};
bool operator==(const Device& d1, const Device& d2);

View File

@ -141,7 +141,7 @@ int DevicesManagement::LoadDev()
fs = true;
}
dv->Read(ms);
dv->Show();
dv->ToString().Show();
// 加载时默认参数
dv->Logined = false;
@ -237,7 +237,7 @@ void DevicesManagement::ShowDev()
if (dv == nullptr) continue;
count++;
dv->Show();
dv->ToString().Show();
debug_printf("\r\n");
}
debug_printf("\r\nHas %d Dev \r\n", count);

View File

@ -345,7 +345,7 @@ void TokenClient::OnReceiveLocal(TokenMessage& msg, TokenController& ctrl)
{
ss = (TokenSession*)sss[i];
//ss->Stat.Show(true);
ss->Show(true);
ss->ToString().Show(true);
}
debug_printf("\r\n");
},

View File

@ -8,7 +8,7 @@
//#define MSG_DEBUG 0
// 令牌统计
class TokenStat : public Object
class TokenStat
{
public:
// 发送统计
@ -34,11 +34,11 @@ public:
int InvokeReply;
TokenStat();
virtual ~TokenStat();
~TokenStat();
void Clear();
virtual String& ToStr(String& str) const;
String ToString() const;
private:
TokenStat* _Last;
@ -573,9 +573,11 @@ void TokenStat::Clear()
InvokeReply = 0;
}
String& TokenStat::ToStr(String& str) const
String TokenStat::ToString() const
{
TS("TokenStat::ToStr");
TS("TokenStat::ToString");
String str;
/*debug_printf("this=%p _Last=%p _Total=%p ", this, _Last, _Total);
Percent().Show(true);*/
@ -592,7 +594,7 @@ String& TokenStat::ToStr(String& str) const
if (_Total)
{
str += "";
_Total->ToStr(str);
str += *_Total;
}
return str;
@ -603,15 +605,16 @@ void StatTask(void* param)
{
TS("TokenController::ShowStat");
auto st = (TokenStat*)param;
auto& st = *(TokenStat*)param;
// 这里输出 字节数 在 90-140 为了减少 new 直接使用256
char cs[256];
String str(cs, ArrayLength(cs));
str.SetLength(0);
st->ToStr(str);
//st->ToStr(str);
str += str;
str.Show(true);
st->Clear();
st.Clear();
// 向以太网广播
/*auto sock = dynamic_cast<Socket*>(Port);

View File

@ -34,8 +34,9 @@ void SessionStat::Clear()
OnPing = 0;
}
String& SessionStat::ToStr(String& str) const
String SessionStat::ToString() const
{
String str;
str = str + "Hello: " + OnHello + " Login: " + OnLogin + " Ping: " + OnPing;
return str;
}
@ -287,14 +288,15 @@ bool TokenSession::CheckExpired()
return false;
}
#if DEBUG
String& TokenSession::ToStr(String& str) const
String TokenSession::ToString() const
{
String str;
int sec = (Sys.Ms() - LastActive) / 1000UL;
str = str + Remote + " " + Name + " LastActive " + sec + "s \t";
Stat.ToStr(str);
#if DEBUG
str += Stat;
#endif
return str;
}
#endif

View File

@ -13,7 +13,7 @@
#if DEBUG
class SessionStat : public Object
class SessionStat
{
public:
ushort OnHello; // 握手次数
@ -26,13 +26,13 @@ public:
~SessionStat();
void Clear();
virtual String& ToStr(String& str) const;
String ToString() const;
};
#endif
// 令牌会话
class TokenSession :public Object
class TokenSession
{
public:
TokenClient& Client; // 客户端
@ -50,22 +50,20 @@ public:
UInt64 LastActive; // 最后活跃时间ms
#if DEBUG
static uint HisSsNum; // 历史Session个数
static uint StatShowTaskID; // 输出统计信息的任务ID
SessionStat Stat; // 统计信息
static uint HisSsNum; // 历史Session个数
static uint StatShowTaskID; // 输出统计信息的任务ID
SessionStat Stat; // 统计信息
#endif
TokenSession(TokenClient& client, TokenController& ctrl);
virtual ~TokenSession();
~TokenSession();
bool Send(TokenMessage& msg);
void OnReceive(TokenMessage& msg);
bool CheckExpired();
#if DEBUG
virtual String& ToStr(String& str) const;
#endif
String ToString() const;
private:
bool OnHello(TokenMessage& msg);