added DynamicAny::convert<>()

This commit is contained in:
Guenter Obiltschnig 2007-04-26 06:24:35 +00:00
parent 80fc258f6d
commit 4cf5e8ac12
14 changed files with 323 additions and 50 deletions

View File

@ -1,9 +1,9 @@
//
// DynamicAny.h
//
// $Id: //poco/Main/Foundation/include/Poco/DynamicAny.h#4 $
// $Id: //poco/Main/Foundation/include/Poco/DynamicAny.h#6 $
//
// Library: Poco
// Library: Foundation
// Package: Core
// Module: DynamicAny
//
@ -36,11 +36,11 @@
//
#ifndef Poco_DynamicAny_INCLUDED
#define Poco_DynamicAny_INCLUDED
#ifndef Foundation_DynamicAny_INCLUDED
#define Foundation_DynamicAny_INCLUDED
#include "Poco/Poco.h"
#include "Poco/Foundation.h"
#include "Poco/DynamicAnyHolder.h"
#include <typeinfo>
@ -146,6 +146,21 @@ public:
_pHolder->convert(result);
return result;
}
template <typename T>
const T& extract() const
/// Returns a const reference to the actual value.
///
/// Must be instantiated with the exact type of
/// the stored value, otherwise a BadCastException
/// is thrown.
{
DynamicAnyHolderImpl<T>* pHolder = dynamic_cast<DynamicAnyHolderImpl<T>*>(_pHolder);
if (pHolder)
return pHolder->value();
else
throw BadCastException();
}
template <typename T>
DynamicAny& operator = (const T& other)
@ -167,4 +182,4 @@ private:
} // namespace Poco
#endif // Poco_DynamicAny_INCLUDED
#endif // Foundation_DynamicAny_INCLUDED

View File

@ -1,9 +1,9 @@
//
// DynamicAnyHolder.h
//
// $Id: //poco/Main/Foundation/include/Poco/DynamicAnyHolder.h#4 $
// $Id: //poco/Main/Foundation/include/Poco/DynamicAnyHolder.h#6 $
//
// Library: Poco
// Library: Foundation
// Package: Core
// Module: DynamicAnyHolder
//
@ -36,11 +36,11 @@
//
#ifndef Poco_DynamicAnyHolder_INCLUDED
#define Poco_DynamicAnyHolder_INCLUDED
#ifndef Foundation_DynamicAnyHolder_INCLUDED
#define Foundation_DynamicAnyHolder_INCLUDED
#include "Poco/Poco.h"
#include "Poco/Foundation.h"
#include "Poco/NumberFormatter.h"
#include "Poco/NumberParser.h"
#include "Poco/String.h"
@ -210,6 +210,10 @@ class DynamicAnyHolderImpl: public DynamicAnyHolder
/// NotImplementedException (if the specialization for a type does not exist)
/// RangeException (if an attempt is made to assign a numeric value outside of the target min/max limits
/// SyntaxException (if an attempt is made to convert a string containing non-numeric characters to number)
///
/// All specializations must additionally implement a public member function:
/// const T& value() const
/// returning a const reference to the actual stored value.
{
public:
DynamicAnyHolderImpl()
@ -383,6 +387,11 @@ public:
{
return new DynamicAnyHolderImpl(_val);
}
const Int8& value() const
{
return _val;
}
private:
Int8 _val;
@ -476,6 +485,11 @@ public:
return new DynamicAnyHolderImpl(_val);
}
const Int16& value() const
{
return _val;
}
private:
Int16 _val;
};
@ -568,6 +582,11 @@ public:
return new DynamicAnyHolderImpl(_val);
}
const Int32& value() const
{
return _val;
}
private:
Int32 _val;
};
@ -660,6 +679,11 @@ public:
return new DynamicAnyHolderImpl(_val);
}
const Int64& value() const
{
return _val;
}
private:
Int64 _val;
};
@ -752,6 +776,11 @@ public:
return new DynamicAnyHolderImpl(_val);
}
const UInt8& value() const
{
return _val;
}
private:
UInt8 _val;
};
@ -844,6 +873,11 @@ public:
return new DynamicAnyHolderImpl(_val);
}
const UInt16& value() const
{
return _val;
}
private:
UInt16 _val;
};
@ -936,6 +970,11 @@ public:
return new DynamicAnyHolderImpl(_val);
}
const UInt32& value() const
{
return _val;
}
private:
UInt32 _val;
};
@ -1028,6 +1067,11 @@ public:
return new DynamicAnyHolderImpl(_val);
}
const UInt64& value() const
{
return _val;
}
private:
UInt64 _val;
};
@ -1120,6 +1164,11 @@ public:
return new DynamicAnyHolderImpl(_val);
}
const bool& value() const
{
return _val;
}
private:
bool _val;
};
@ -1213,6 +1262,11 @@ public:
return new DynamicAnyHolderImpl(_val);
}
const float& value() const
{
return _val;
}
private:
float _val;
};
@ -1312,6 +1366,11 @@ public:
return new DynamicAnyHolderImpl(_val);
}
const double& value() const
{
return _val;
}
private:
double _val;
};
@ -1404,6 +1463,11 @@ public:
return new DynamicAnyHolderImpl(_val);
}
const char& value() const
{
return _val;
}
private:
char _val;
};
@ -1514,6 +1578,11 @@ public:
return new DynamicAnyHolderImpl(_val);
}
const std::string& value() const
{
return _val;
}
private:
std::string _val;
};
@ -1609,6 +1678,11 @@ public:
return new DynamicAnyHolderImpl(_val);
}
const long& value() const
{
return _val;
}
private:
long _val;
};
@ -1701,6 +1775,11 @@ public:
return new DynamicAnyHolderImpl(_val);
}
const unsigned long& value() const
{
return _val;
}
private:
unsigned long _val;
};
@ -1712,4 +1791,4 @@ private:
} // namespace Poco
#endif // Poco_DynamicAnyHolder_INCLUDED
#endif // Foundation_DynamicAnyHolder_INCLUDED

View File

@ -1,9 +1,9 @@
//
// SharedMemory.h
//
// $Id: //poco/Main/Foundation/include/Poco/SharedMemory.h#4 $
// $Id: //poco/Main/Foundation/include/Poco/SharedMemory.h#5 $
//
// Library: Poco
// Library: Foundation
// Package: Processes
// Module: SharedMemory
//
@ -36,11 +36,11 @@
//
#ifndef Poco_SharedMemory_INCLUDED
#define Poco_SharedMemory_INCLUDED
#ifndef Foundation_SharedMemory_INCLUDED
#define Foundation_SharedMemory_INCLUDED
#include "Poco/Poco.h"
#include "Poco/Foundation.h"
#include <algorithm>
#include <cstddef>
@ -128,4 +128,4 @@ inline void SharedMemory::swap(SharedMemory& other)
} // namespace Poco::Poco
#endif // Poco_SharedMemory_INCLUDED
#endif // Foundation_SharedMemory_INCLUDED

View File

@ -1,9 +1,9 @@
//
// SharedMemoryImpl.h
//
// $Id: //poco/Main/Foundation/include/Poco/SharedMemory_DUMMY.h#3 $
// $Id: //poco/Main/Foundation/include/Poco/SharedMemory_DUMMY.h#4 $
//
// Library: Poco
// Library: Foundation
// Package: Processes
// Module: SharedMemoryImpl
//
@ -36,11 +36,11 @@
//
#ifndef Poco_SharedMemoryImpl_INCLUDED
#define Poco_SharedMemoryImpl_INCLUDED
#ifndef Foundation_SharedMemoryImpl_INCLUDED
#define Foundation_SharedMemoryImpl_INCLUDED
#include "Poco/Poco.h"
#include "Poco/Foundation.h"
#include "Poco/SharedMemory.h"
#include "Poco/RefCountedObject.h"
@ -107,4 +107,4 @@ inline char* SharedMemoryImpl::end() const
} // namespace Poco
#endif // Poco_SharedMemoryImpl_INCLUDED
#endif // Foundation_SharedMemoryImpl_INCLUDED

View File

@ -1,9 +1,9 @@
//
// SharedMemoryImpl.h
//
// $Id: //poco/Main/Foundation/include/Poco/SharedMemory_POSIX.h#4 $
// $Id: //poco/Main/Foundation/include/Poco/SharedMemory_POSIX.h#5 $
//
// Library: Poco
// Library: Foundation
// Package: Processes
// Module: SharedMemoryImpl
//
@ -36,11 +36,11 @@
//
#ifndef Poco_SharedMemoryImpl_INCLUDED
#define Poco_SharedMemoryImpl_INCLUDED
#ifndef Foundation_SharedMemoryImpl_INCLUDED
#define Foundation_SharedMemoryImpl_INCLUDED
#include "Poco/Poco.h"
#include "Poco/Foundation.h"
#include "Poco/SharedMemory.h"
#include "Poco/RefCountedObject.h"
@ -122,4 +122,4 @@ inline char* SharedMemoryImpl::end() const
} // namespace Poco
#endif // Poco_SharedMemoryImpl_INCLUDED
#endif // Foundation_SharedMemoryImpl_INCLUDED

View File

@ -1,9 +1,9 @@
//
// SharedMemoryImpl.h
//
// $Id: //poco/Main/Foundation/include/Poco/SharedMemory_WIN32.h#3 $
// $Id: //poco/Main/Foundation/include/Poco/SharedMemory_WIN32.h#4 $
//
// Library: Poco
// Library: Foundation
// Package: Processes
// Module: SharedMemoryImpl
//
@ -36,8 +36,8 @@
//
#ifndef Poco_SharedMemoryImpl_INCLUDED
#define Poco_SharedMemoryImpl_INCLUDED
#ifndef Foundation_SharedMemoryImpl_INCLUDED
#define Foundation_SharedMemoryImpl_INCLUDED
#include "Poco/Poco.h"
@ -122,4 +122,4 @@ inline char* SharedMemoryImpl::end() const
} // namespace Poco
#endif // Poco_SharedMemoryImpl_INCLUDED
#endif // Foundation_SharedMemoryImpl_INCLUDED

View File

@ -1,9 +1,9 @@
//
// DynamicAny.cpp
//
// $Id: //poco/Main/Foundation/src/DynamicAny.cpp#2 $
// $Id: //poco/Main/Foundation/src/DynamicAny.cpp#3 $
//
// Library: Poco
// Library: Foundation
// Package: Core
// Module: DynamicAny
//

View File

@ -1,9 +1,9 @@
//
// DynamicAnyHolder.cpp
//
// $Id: //poco/Main/Foundation/src/DynamicAnyHolder.cpp#1 $
// $Id: //poco/Main/Foundation/src/DynamicAnyHolder.cpp#2 $
//
// Library: Poco
// Library: Foundation
// Package: Core
// Module: DynamicAnyHolder
//

View File

@ -1,9 +1,9 @@
//
// SharedMemory.cpp
//
// $Id: //poco/Main/Foundation/src/SharedMemory.cpp#5 $
// $Id: //poco/Main/Foundation/src/SharedMemory.cpp#6 $
//
// Library: Poco
// Library: Foundation
// Package: Processes
// Module: SharedMemory
//

View File

@ -1,9 +1,9 @@
//
// SharedMemoryImpl.cpp
//
// $Id: //poco/Main/Foundation/src/SharedMemory_DUMMY.cpp#2 $
// $Id: //poco/Main/Foundation/src/SharedMemory_DUMMY.cpp#3 $
//
// Library: Poco
// Library: Foundation
// Package: Processes
// Module: SharedMemoryImpl
//

View File

@ -1,9 +1,9 @@
//
// SharedMemoryImpl.cpp
//
// $Id: //poco/Main/Foundation/src/SharedMemory_POSIX.cpp#8 $
// $Id: //poco/Main/Foundation/src/SharedMemory_POSIX.cpp#9 $
//
// Library: Poco
// Library: Foundation
// Package: Processes
// Module: SharedMemoryImpl
//

View File

@ -1,9 +1,9 @@
//
// SharedMemoryImpl.cpp
//
// $Id: //poco/Main/Foundation/src/SharedMemory_WIN32.cpp#4 $
// $Id: //poco/Main/Foundation/src/SharedMemory_WIN32.cpp#5 $
//
// Library: Poco
// Library: Foundation
// Package: Processes
// Module: SharedMemoryImpl
//

View File

@ -1,7 +1,7 @@
//
// pocomsg.mc[.h]
//
// $Id: //poco/Main/Foundation/src/pocomsg.mc#7 $
// $Id: //poco/Main/Foundation/src/pocomsg.h#20 $
//
// The Poco message source/header file.
//

View File

@ -1,7 +1,7 @@
//
// DynamicAnyTest.cpp
//
// $Id: //poco/Main/Foundation/testsuite/src/DynamicAnyTest.cpp#5 $
// $Id: //poco/Main/Foundation/testsuite/src/DynamicAnyTest.cpp#6 $
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
@ -107,6 +107,18 @@ void DynamicAnyTest::testInt8()
std::string t2;
a2.convert(t2);
assert (s1 == t2);
Int8 value = a1.extract<Int8>();
assert (value == 32);
try
{
Int16 value2 = a1.extract<Int16>();
fail("bad cast - must throw");
}
catch (Poco::BadCastException&)
{
}
}
@ -166,6 +178,18 @@ void DynamicAnyTest::testInt16()
std::string t2;
a2.convert(t2);
assert (s1 == t2);
Int16 value = a1.extract<Int16>();
assert (value == 32);
try
{
Int32 value2 = a1.extract<Int32>();
fail("bad cast - must throw");
}
catch (Poco::BadCastException&)
{
}
}
@ -225,6 +249,18 @@ void DynamicAnyTest::testInt32()
std::string t2;
a2.convert(t2);
assert (s1 == t2);
Int32 value = a1.extract<Int32>();
assert (value == 32);
try
{
Int16 value2 = a1.extract<Int16>();
fail("bad cast - must throw");
}
catch (Poco::BadCastException&)
{
}
}
@ -284,6 +320,18 @@ void DynamicAnyTest::testInt64()
std::string t2;
a2.convert(t2);
assert (s1 == t2);
Int64 value = a1.extract<Int64>();
assert (value == 32);
try
{
Int16 value2 = a1.extract<Int16>();
fail("bad cast - must throw");
}
catch (Poco::BadCastException&)
{
}
}
@ -343,6 +391,18 @@ void DynamicAnyTest::testUInt8()
std::string t2;
a2.convert(t2);
assert (s1 == t2);
UInt8 value = a1.extract<UInt8>();
assert (value == 32);
try
{
Int16 value2 = a1.extract<Int16>();
fail("bad cast - must throw");
}
catch (Poco::BadCastException&)
{
}
}
@ -402,6 +462,18 @@ void DynamicAnyTest::testUInt16()
std::string t2;
a2.convert(t2);
assert (s1 == t2);
UInt16 value = a1.extract<UInt16>();
assert (value == 32);
try
{
Int16 value2 = a1.extract<Int16>();
fail("bad cast - must throw");
}
catch (Poco::BadCastException&)
{
}
}
@ -461,6 +533,18 @@ void DynamicAnyTest::testUInt32()
std::string t2;
a2.convert(t2);
assert (s1 == t2);
UInt32 value = a1.extract<UInt32>();
assert (value == 32);
try
{
Int16 value2 = a1.extract<Int16>();
fail("bad cast - must throw");
}
catch (Poco::BadCastException&)
{
}
}
@ -520,6 +604,18 @@ void DynamicAnyTest::testUInt64()
std::string t2;
a2.convert(t2);
assert (s1 == t2);
UInt64 value = a1.extract<UInt64>();
assert (value == 32);
try
{
Int16 value2 = a1.extract<Int16>();
fail("bad cast - must throw");
}
catch (Poco::BadCastException&)
{
}
}
@ -579,6 +675,18 @@ void DynamicAnyTest::testBool()
std::string t2;
a2.convert(t2);
assert (s1 == t2);
bool value = a1.extract<bool>();
assert (value);
try
{
Int16 value2 = a1.extract<Int16>();
fail("bad cast - must throw");
}
catch (Poco::BadCastException&)
{
}
}
@ -638,6 +746,18 @@ void DynamicAnyTest::testChar()
std::string t2;
a2.convert(t2);
assert (s1 == t2);
char value = a1.extract<char>();
assert (value == ' ');
try
{
Int16 value2 = a1.extract<Int16>();
fail("bad cast - must throw");
}
catch (Poco::BadCastException&)
{
}
}
@ -697,6 +817,18 @@ void DynamicAnyTest::testFloat()
std::string t2;
a2.convert(t2);
assert (s1 == t2);
float value = a1.extract<float>();
assert (value == 32.0f);
try
{
Int16 value2 = a1.extract<Int16>();
fail("bad cast - must throw");
}
catch (Poco::BadCastException&)
{
}
}
@ -756,6 +888,18 @@ void DynamicAnyTest::testDouble()
std::string t2;
a2.convert(t2);
assert (s1 == t2);
double value = a1.extract<double>();
assert (value == 32.0);
try
{
Int16 value2 = a1.extract<Int16>();
fail("bad cast - must throw");
}
catch (Poco::BadCastException&)
{
}
}
@ -810,8 +954,19 @@ void DynamicAnyTest::testString()
assert (s11 == 32.0);
assert (s12);
assert (s13 == '3');
}
const std::string& value = a1.extract<std::string>();
assert (value == "32");
try
{
Int16 value2 = a1.extract<Int16>();
fail("bad cast - must throw");
}
catch (Poco::BadCastException&)
{
}
}
void DynamicAnyTest::testLong()
@ -870,6 +1025,18 @@ void DynamicAnyTest::testLong()
std::string t2;
a2.convert(t2);
assert (s1 == t2);
long value = a1.extract<long>();
assert (value == 32);
try
{
Int16 value2 = a1.extract<Int16>();
fail("bad cast - must throw");
}
catch (Poco::BadCastException&)
{
}
}
@ -929,6 +1096,18 @@ void DynamicAnyTest::testULong()
std::string t2;
a2.convert(t2);
assert (s1 == t2);
unsigned long value = a1.extract<unsigned long>();
assert (value == 32);
try
{
Int16 value2 = a1.extract<Int16>();
fail("bad cast - must throw");
}
catch (Poco::BadCastException&)
{
}
}