473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

detecting basic types

Hi All,

Is it possible to detect (at compile-time) if an item is a basic type (int,
float, double,...) or an instance of a class/struct?

I'm thinking something like: (obviosly this isn't working...)

template<typename T>
CMyFileClass::Serialize(T &x)
{
#if IS_BASIC_TYPE(T)
// handle basic type
#else
// handle a class inherited from my CSerializable class
#endif
}
BTW, I'm writing serialization of data for a gameproject....

Thanks for any help!
Lasse
Jul 22 '05 #1
6 1782
"Lasse Skyum" <no spam> wrote in message
news:3f***********************@dread16.news.tele.d k...
Hi All,

Is it possible to detect (at compile-time) if an item is a basic type (int, float, double,...) or an instance of a class/struct?


Andrei Alexandrescu is adressing this issue in his book "Modern C++ design",
see section 2.10.2 "Detection of Fundamental Types".

Thierry
Jul 22 '05 #2
In article <3f***********************@dread16.news.tele.dk> ,
"Lasse Skyum" <no spam> wrote:
Hi All,

Is it possible to detect (at compile-time) if an item is a basic type (int,
float, double,...) or an instance of a class/struct?

I'm thinking something like: (obviosly this isn't working...)

template<typename T>
CMyFileClass::Serialize(T &x)
{
#if IS_BASIC_TYPE(T)
// handle basic type
#else
// handle a class inherited from my CSerializable class
#endif
}


You might be interested in the type traits library at boost:

http://www.boost.org/libs/type_traits/index.htm

This library has been submitted for standardization and accepted into
the first library technical report:

http://std.dkuug.dk/jtc1/sc22/wg21/d...2003/n1424.htm

A close facsimile of this lib has shipped with Metrowerks CodeWarrior
for the past 3 years. It lives in namespace Metrowerks in the header
<msl_utility>.

These libs might be used like:

#include <msl_utility>

using namespace Metrowerks;

struct CSerializable {};

template <class T>
struct CMyFileClass
{
void Serialize(T& x);
private:
void Serialize(T& x, int2type<false>);
void Serialize(T& x, int2type<true>);
};

template<typename T>
inline
void
CMyFileClass<T>::Serialize(T &x)
{
Serialize(x, int2type<is_arithmetic<T>::value>());
}

template<typename T>
void
CMyFileClass<T>::Serialize(T &x, int2type<true>)
{
// handle basic type
}

template<typename T>
void
CMyFileClass<T>::Serialize(T &x, int2type<false>)
{
compile_assert<is_convertible<T, CSerializable>::value>
must_be_convertible_to_CSerializable;
// handle a class inherited from my CSerializable class
}

I wrote this using the Metrowerks tools so that I could check it before
I posted. The boost or std::tr1 interface would be very close.

-Howard
Jul 22 '05 #3
Lasse Skyum wrote:
Hi All,

Is it possible to detect (at compile-time) if an item is a basic type (int,
float, double,...) or an instance of a class/struct?


There's a *great* treatment of this topic in the Josuttis templates book:

http://www.josuttis.com/tmplbook/tmplbook.html

Basically, you make a "traits" class to hold compile-time info about
each type, and specialize as needed. Making complicated decisions based
on the traits info is certainly possible; try googling for
"metaprogramming."

Good luck,
Jeff

Jul 22 '05 #4
Thanks a lot guys, I've solved it now!

By looking at some documents about "meta-programming" I worked something
together.

--
Lasse

Hi All,

Is it possible to detect (at compile-time) if an item is a basic type (int, float, double,...) or an instance of a class/struct?

I'm thinking something like: (obviosly this isn't working...)

template<typename T>
CMyFileClass::Serialize(T &x)
{
#if IS_BASIC_TYPE(T)
// handle basic type
#else
// handle a class inherited from my CSerializable class
#endif
}
BTW, I'm writing serialization of data for a gameproject....

Thanks for any help!
Lasse

Jul 22 '05 #5
Lasse Skyum wrote:
Thanks a lot guys, I've solved it now!

By looking at some documents about "meta-programming" I worked something
together.

--
Lasse


Would you care to post it?

Jul 22 '05 #6
> Would you care to post it?

Sure thing, please let me know if you have any additional ideas!
// This is the base-class for my Input-streams
class CZS_IStream
{
public:
virtual int ReadBytes(void *pBytes,int iByteCount)=0;

template<typename T>
bool Read(T &v)
{
CZS_Serializer<T> s; // This is where the trick happens...
return s.Read(this,v);
}

};

// Every time data is read/written it goes through a version of
CZS_Serializer.
// This is the "default" one if none other is defined.
template<typename T>
class CZS_Serializer
{
public:
bool Read(CZS_IStream *pS,T &v){return v.Serialize_Read(pS);}
bool Write(CZS_OStream *pS,T &v){return v.Serialize_Write(pS);}
};

// All basic types are registered as "binary-types" by implementing a
CZS_Serializer for them, that
// uses ReadBytes and WriteBytes instead of the
Serialize_Read/Serialize_Write functions.

#define ZS_REGISTER_BINARYTYPE(CAClass) \
template<>class CZS_Serializer<CAClass> \
{ \
public: \
bool Read(CZS_IStream *pS,T &v){return
pS->ReadBytes(&v,sizeof(T))==sizeof(T);} \
bool Write(CZS_OStream *pS,const T &v){return
pS->WriteBytes(&v,sizeof(T));} \
}; \

ZS_REGISTER_BINARYTYPE(char)
ZS_REGISTER_BINARYTYPE(unsigned char)
ZS_REGISTER_BINARYTYPE(signed char)
ZS_REGISTER_BINARYTYPE(int)
ZS_REGISTER_BINARYTYPE(unsigned int)
ZS_REGISTER_BINARYTYPE(long)
ZS_REGISTER_BINARYTYPE(unsigned long)
ZS_REGISTER_BINARYTYPE(__int8)
ZS_REGISTER_BINARYTYPE(__int16)
ZS_REGISTER_BINARYTYPE(__int32)
ZS_REGISTER_BINARYTYPE(__int64)
ZS_REGISTER_BINARYTYPE(float)
ZS_REGISTER_BINARYTYPE(double)
ZS_REGISTER_BINARYTYPE(long double)
ZS_REGISTER_BINARYTYPE(bool)
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Jay | last post by:
I'm sure this is a really dumb question, but how do you detect a variable type in Python? For example, I want to know if the variable "a" is a list of strings or a single string. How do I do...
10
by: Frances Del Rio | last post by:
pls, why is this not working? <SCRIPT language=JavaScript type="text/javascript"> var br = '<SCRIPT language=Javascript' br += 'src="js_pop.js" type="text/javascript">' br += '</SCRIPT>' var...
12
by: jyu.james | last post by:
I'm trying to detect reads of uninitialized global variables (that are declared in one file, and used in another as an extern). I know that ANSI C initializes all global variables to 0, however,...
14
by: luis | last post by:
Are basic types (int, long, ...) objetcs or not? I read that in C# all are objects including basic types, derived from Object class. Then in msdn documentation says that boxing converts basic...
25
by: junky_fellow | last post by:
Is there any way by which the overflow during addition of two integers may be detected ? eg. suppose we have three unsigned integers, a ,b, c. we are doing a check like if ((a +b) > c) do...
3
by: regtrashcan | last post by:
I have a webpage that detects whether Shockwave Player is installed and the version number. The javascript/vbscript that I use has worked fine until the latest release of the Shockwave Player. I am...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
13
by: John W. | last post by:
Hi there, Does anyone know if there's a (simple) way to detect whether an arbitrary "System.Type" originates from the .NET framework opposed to a user-defined assembly. Thanks.
15
by: RobG | last post by:
When using createEvent, an eventType parameter must be provided as an argument. This can be one of those specified in DOM 2 or 3 Events, or it might be a proprietary eventType. My problem is...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.