A
Angus
Hello
I am modelling a telephony item, called a connection id, which is made
up of two components. A thing called a Call id and a device id. Both
the Call ID and the Device ID could be either a string or a integer.
My initial design used a union to store the data (a union was used for
performance/code size reasons over templates). Here is a rough
outline of the code:
class ConnectionID : public ref_type
{
public:
typedef ref_ptr<ConnectionID> ptr_type;
ConnectionID();
ConnectionID(int callid, int deviceid);
ConnectionID(int callid, const char* szDeviceID);
ConnectionID(const char* szCallID, int deviceid);
ConnectionID(const char* szCallID, const char* szDeviceID);
~ConnectionID();
enum DataType { type_none, type_int, type_charp };
//CallID
string getCallID(string& callid) const;
long getCallID(long callid) const;
void setCallID(const char* callid);
void setCallID(long callid);
DataType getCallIDType() const;
//DeviceID
string getDeviceID(string& callid) const;
long getDeviceID(long devid) const;
void setDeviceID(const char* devid);
void setDeviceID(long devid);
DataType getDeviceIDType() const;
protected:
union{
long nCallid;
char* szCallid;
}m_callid;
DataType m_CallIDType;
union{
long nDeviceID;
char* szDeviceID;
}m_DeviceID;
DataType m_DeviceIDType;
};
///Device
class DeviceID : public ref_type
{
public:
typedef ref_ptr<DeviceID> ptr_type;
DeviceID();
DeviceID(int deviceid);
DeviceID(const char* szDeviceID);
~DeviceID();
enum DataType { type_none, type_int, type_charp };
//DeviceID
string getDeviceID(string& devid) const;
long getDeviceID(long devid) const;
void setDeviceID(const char* devid);
void setDeviceID(long devid);
DataType getDeviceIDType() const;
protected:
union{
long nDeviceID;
char* szDeviceID;
}m_DeviceID;
DataType m_DeviceIDType;
};
You cannot use a class with a constructor with a union so that is why
I used a char*.
What I would like to do is somehow hide the union and allow users to
set and get using either a std::string or an int. I have other places
where a union with other types are required and using class objects
would be more convenient.
How could I hide the union/char* implementation from the user of the
class? But still benefit from the union?
Any assistance would be most appreciated.
Angus
I am modelling a telephony item, called a connection id, which is made
up of two components. A thing called a Call id and a device id. Both
the Call ID and the Device ID could be either a string or a integer.
My initial design used a union to store the data (a union was used for
performance/code size reasons over templates). Here is a rough
outline of the code:
class ConnectionID : public ref_type
{
public:
typedef ref_ptr<ConnectionID> ptr_type;
ConnectionID();
ConnectionID(int callid, int deviceid);
ConnectionID(int callid, const char* szDeviceID);
ConnectionID(const char* szCallID, int deviceid);
ConnectionID(const char* szCallID, const char* szDeviceID);
~ConnectionID();
enum DataType { type_none, type_int, type_charp };
//CallID
string getCallID(string& callid) const;
long getCallID(long callid) const;
void setCallID(const char* callid);
void setCallID(long callid);
DataType getCallIDType() const;
//DeviceID
string getDeviceID(string& callid) const;
long getDeviceID(long devid) const;
void setDeviceID(const char* devid);
void setDeviceID(long devid);
DataType getDeviceIDType() const;
protected:
union{
long nCallid;
char* szCallid;
}m_callid;
DataType m_CallIDType;
union{
long nDeviceID;
char* szDeviceID;
}m_DeviceID;
DataType m_DeviceIDType;
};
///Device
class DeviceID : public ref_type
{
public:
typedef ref_ptr<DeviceID> ptr_type;
DeviceID();
DeviceID(int deviceid);
DeviceID(const char* szDeviceID);
~DeviceID();
enum DataType { type_none, type_int, type_charp };
//DeviceID
string getDeviceID(string& devid) const;
long getDeviceID(long devid) const;
void setDeviceID(const char* devid);
void setDeviceID(long devid);
DataType getDeviceIDType() const;
protected:
union{
long nDeviceID;
char* szDeviceID;
}m_DeviceID;
DataType m_DeviceIDType;
};
You cannot use a class with a constructor with a union so that is why
I used a char*.
What I would like to do is somehow hide the union and allow users to
set and get using either a std::string or an int. I have other places
where a union with other types are required and using class objects
would be more convenient.
How could I hide the union/char* implementation from the user of the
class? But still benefit from the union?
Any assistance would be most appreciated.
Angus