few more fixes and refactoring

This commit is contained in:
aleks-f
2013-02-02 15:10:20 -06:00
parent 911f22508e
commit ba9d8574d3
2 changed files with 49 additions and 40 deletions

View File

@@ -87,7 +87,7 @@ public:
{ {
if(!empty()) if(!empty())
{ {
if(flag()) if(isLocal())
content()->~Placeholder(); content()->~Placeholder();
else else
delete content(); delete content();
@@ -102,7 +102,7 @@ public:
/// Any a = 13; /// Any a = 13;
/// Any a = string("12345"); /// Any a = string("12345");
{ {
this->~Any(); if (isLocal()) this->~Any();
construct(value); construct(value);
return *this; return *this;
} }
@@ -110,7 +110,7 @@ public:
Any& operator = (Any value) Any& operator = (Any value)
/// Assignment operator for Any. /// Assignment operator for Any.
{ {
this->~Any(); if (isLocal()) this->~Any();
construct(value); construct(value);
return *this; return *this;
} }
@@ -118,8 +118,8 @@ public:
bool empty() const bool empty() const
/// Returns true if the Any is empty. /// Returns true if the Any is empty.
{ {
//return !(flags() & ASSIGNED); char buf[POCO_SMALL_OBJECT_SIZE] = { 0 };
return _placeholder.pHolder == 0; return 0 == std::memcmp(_placeholder.holder, buf, POCO_SMALL_OBJECT_SIZE);
} }
const std::type_info & type() const const std::type_info & type() const
@@ -142,7 +142,7 @@ private:
} }
virtual const std::type_info & type() const = 0; virtual const std::type_info & type() const = 0;
virtual Placeholder * clone(Placeholder* pMem = 0) const = 0; virtual void clone(Placeholder**) const = 0;
}; };
template<typename ValueType> template<typename ValueType>
@@ -158,11 +158,23 @@ private:
return typeid(ValueType); return typeid(ValueType);
} }
virtual Placeholder * clone(Placeholder* pMem = 0) const static void setFlag(Placeholder** pMem, unsigned char val)
{ {
return pMem && (sizeof(_held) <= POCO_SMALL_OBJECT_SIZE) ? reinterpret_cast<unsigned char*>(pMem)[POCO_SMALL_OBJECT_SIZE] = val;
new (pMem) Holder(_held) : }
new Holder(_held);
virtual void clone(Placeholder** ppMem) const
{
if ((sizeof(Holder<ValueType>) <= POCO_SMALL_OBJECT_SIZE))
{
new (ppMem) Holder(_held);
setFlag(ppMem, 1);
}
else
{
*ppMem = new Holder(_held);
setFlag(ppMem, 0);
}
} }
ValueType _held; ValueType _held;
@@ -173,10 +185,8 @@ private:
Placeholder* content() const Placeholder* content() const
{ {
if (empty()) return 0; if(isLocal())
return reinterpret_cast<Placeholder*>(&(_placeholder.holder));
if(flag())
return (Placeholder *) &(_placeholder.holder);
else else
return _placeholder.pHolder; return _placeholder.pHolder;
} }
@@ -187,27 +197,26 @@ private:
if (sizeof(Holder<ValueType>) <= POCO_SMALL_OBJECT_SIZE) if (sizeof(Holder<ValueType>) <= POCO_SMALL_OBJECT_SIZE)
{ {
new (_placeholder.holder) Holder<ValueType>(value); new (_placeholder.holder) Holder<ValueType>(value);
flag() = 1; isLocal() = 1;
} }
else else
{ {
_placeholder.pHolder = new Holder<ValueType>(value); _placeholder.pHolder = new Holder<ValueType>(value);
flag() = 0; isLocal() = 0;
} }
} }
void construct(const Any & other) void construct(const Any & other)
{ {
if(other.flag()) if(other.empty())
{ erase(_placeholder.holder);
other.content()->clone((Placeholder*) &_placeholder.holder); else
flag() = 1; other.content()->clone(&_placeholder.pHolder);
return; }
}
else if(!(other.empty()))
_placeholder.pHolder = other._placeholder.pHolder->clone();
flag() = 0; inline static void erase(unsigned char* pHolder, std::size_t sz= POCO_SMALL_OBJECT_SIZE + 1)
{
std::memset(pHolder, 0, sz);
} }
union PH union PH
@@ -219,14 +228,14 @@ private:
{ {
PH () PH ()
{ {
std::memset(holder, 0, sizeof(PH)); erase(holder);
} }
Placeholder* pHolder; Placeholder* pHolder;
mutable unsigned char holder[POCO_SMALL_OBJECT_SIZE + 1]; mutable unsigned char holder[POCO_SMALL_OBJECT_SIZE + 1];
} _placeholder; } _placeholder;
unsigned char& flag() const unsigned char& isLocal() const
{ {
return _placeholder.holder[POCO_SMALL_OBJECT_SIZE]; return _placeholder.holder[POCO_SMALL_OBJECT_SIZE];
} }

View File

@@ -84,18 +84,18 @@ protected:
loadConfiguration(); // load default configuration files, if present loadConfiguration(); // load default configuration files, if present
Application::initialize(self); Application::initialize(self);
_icmpClient.pingBegin += Delegate<Ping, ICMPEventArgs>(this, &Ping::onBegin); _icmpClient.pingBegin += delegate(this, &Ping::onBegin);
_icmpClient.pingReply += Delegate<Ping, ICMPEventArgs>(this, &Ping::onReply); _icmpClient.pingReply += delegate(this, &Ping::onReply);
_icmpClient.pingError += Delegate<Ping, ICMPEventArgs>(this, &Ping::onError); _icmpClient.pingError += delegate(this, &Ping::onError);
_icmpClient.pingEnd += Delegate<Ping, ICMPEventArgs>(this, &Ping::onEnd); _icmpClient.pingEnd += delegate(this, &Ping::onEnd);
} }
void uninitialize() void uninitialize()
{ {
_icmpClient.pingBegin -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onBegin); _icmpClient.pingBegin -= delegate(this, &Ping::onBegin);
_icmpClient.pingReply -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onReply); _icmpClient.pingReply -= delegate(this, &Ping::onReply);
_icmpClient.pingError -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onError); _icmpClient.pingError -= delegate(this, &Ping::onError);
_icmpClient.pingEnd -= Delegate<Ping, ICMPEventArgs>(this, &Ping::onEnd); _icmpClient.pingEnd -= delegate(this, &Ping::onEnd);
Application::uninitialize(); Application::uninitialize();
} }