/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/

#ifndef _SCTS_COMMONELEMENTS_H
#define _SCTS_COMMONELEMENTS_H
#include "SctsPlatformLib.h"
#ifdef __SCTS_USING_RTK__

#include "smldtd.h"
#include "smlmetinfdtd.h"
#include "smldevinfdtd.h"
#include "sml.h"
#include "mgrutil.h"

#define SCTSRTKSETPCDATALIST(PCDATA,SCTSPCDATA)\
	SCTSASSERT(rawData);\
	if (PCDATA) smlFreePcdataList(PCDATA);\
	if (!SCTSPCDATA)\
	{\
		PCDATA=0;\
		return;\
	}\
	PCDATA=SCTSPCDATA->rawData;

#define SCTSRTKGETPCDATALIST(PCDATA,SCTSPCDATA)\
	if (rawData && PCDATA)\
	SCTSPCDATA.rawData=PCDATA;\
	else\
		SCTSPCDATA.rawData=0;\
	return SCTSPCDATA;\

#define SCTSRTKSETPCDATA(PCDATA,SCTSPCDATA)\
	SCTSASSERT(rawData);\
	if (PCDATA) smlFreePcdata(PCDATA);\
	if (!SCTSPCDATA || !SCTSPCDATA->rawData)\
	{\
		PCDATA=0;\
		return;\
	}\
	PCDATA=SCTSPCDATA->rawData;

#define SCTSRTKGETPCDATA(PCDATA,SCTSPCDATA)\
	if (rawData && PCDATA)\
		SCTSPCDATA.rawData=PCDATA;\
	else\
		SCTSPCDATA.rawData=0;\
	return SCTSPCDATA;\

#define SCTSRTKSETMETA(PCDATA,SCTSMETA)\
	SCTSASSERT(rawData);\
	if (PCDATA) smlFreePcdata(PCDATA);\
	if (!SCTSMETA)\
	{\
		PCDATA=0;\
		return;\
	}\
	PCDATA=(SmlPcdataPtr_t)sctsMalloc(sizeof(SmlPcdata_t));\
	PCDATA->contentType=SML_PCDATA_EXTENSION;\
	PCDATA->extension=SML_EXT_METINF;\
	PCDATA->content=SCTSMETA->rawData;
	
#define SCTSRTKGETMETA(PCDATA,SCTSMETA)\
	if (rawData && PCDATA && PCDATA->extension==SML_EXT_METINF)\
		SCTSMETA.rawData=(SmlMetInfMetInfPtr_t)PCDATA->content;\
	else\
		SCTSMETA.rawData=0;\
	return SCTSMETA;

#define SCTSRTKSETTARGETSOURCE(TS,LOCURI,LOCNAME) \
	SCTSASSERT(rawData);\
	if (TS) smlFreeSourceTargetPtr(TS);\
	if (LOCURI || LOCNAME)\
		TS=SctsTargetSource(LOCURI,LOCNAME).rawData;\
	else\
		TS=0;

#define SCTSRTKGETTARGETSOURCE(TS,SCTSTS)\
	if (rawData && TS)\
		SCTSTS.rawData=TS;\
	else\
		SCTSTS.rawData=0;\
	return SCTSTS;

#define SCTSRTKSETINT(PCDATA,INT) \
	SCTSASSERT(rawData);\
	if (PCDATA) smlFreePcdata(PCDATA);\
	if(INT==-1)\
	{\
		PCDATA=0;\
		return;\
	}\
	PCDATA=sctsInt2Pcdata(INT);

#define SCTSRTKGETINT(PCDATA) \
	if (!rawData || !PCDATA) return -1;\
	return sctsPcdata2Int(PCDATA);

class SctsDevInf;

/*****************************************************************
Helper Functions/Macros
******************************************************************/

SmlPcdataPtr_t sctsInt2Pcdata(int i);
int sctsPcdata2Int(SmlPcdataPtr_t pPcdata);

/*****************************************************************
Common Element Classes
******************************************************************/

class SctsPcdata;
class SctsTargetSource;
class SctsMeta;
class SctsCred;
class SctsItem;
class SctsMapItem;
typedef class SctsMeta SctsChal;

/*****************************************************************
SctsList Definition (Template class for all lists)
******************************************************************/

template <class ListType,class ListItemType,class ListItemWrapper> class SctsList
{
private:
	typedef struct 
	{
		ListItemType *data;
		ListType *next;
	} SctsListItem;
public:
	ListType *rawData;
	bool operator!() {return (!rawData);}
	operator bool() {if (rawData) return true;return false;}
	ListItemWrapper& GetData(ListItemWrapper& listItem)
	{
		if (!rawData)
			listItem.rawData=0;
		else
			listItem.rawData=((SctsListItem *)rawData)->data;
		return listItem;
	}

	void SetData(ListItemWrapper& listItem)
	{
		SCTSASSERT(rawData);
		if (((SctsListItem *)rawData)->data)
		{
			ListItemType *temp=((SctsListItem *)rawData)->data;
			
		}
		((SctsListItem *)rawData)->data=listItem.rawData;
	}

	ListItemWrapper& GetNextData(ListItemWrapper& listItem)
	{
		if (rawData) 
		{
			rawData=rawData->next;
			return GetData(listItem);
		}
		listItem.rawData=0;
		return listItem;
	}
	
	void SetNextData(ListItemWrapper& listItem)
	{
		SctsListItem *newList;
		SCTSASSERT(rawData);
		if (!(newList=(SctsListItem *)sctsMalloc(sizeof(SctsListItem)))) return;
		newList->next=rawData->next;
		newList->data=listItem.rawData;
		rawData->next=(ListType *)newList;
		rawData=(ListType *)newList;
	}

	/* Constructors */
	SctsList(ListType *pList=0)
	{
		rawData=pList;
	}

	SctsList(ListItemWrapper& listItem)
	{
		rawData=0;
		rawData=(ListType *)sctsMalloc(sizeof(ListType));
		SCTSASSERT(rawData);
		rawData->next=0;
		((SctsListItem *)rawData)->data=listItem.rawData;
	}
};

/*****************************************************************
List classes
******************************************************************/

typedef SctsList<SmlPcdataList_t,SmlPcdata_t,SctsPcdata> SctsPcdataList;
typedef SctsList<SmlTargetRefList_t,SmlPcdata_t,SctsPcdata> SctsTargetRefList;
typedef SctsList<SmlSourceRefList_t,SmlPcdata_t,SctsPcdata> SctsSourceRefList;
typedef SctsList<SmlItemList_t,SmlItem_t,SctsItem> SctsItemList;
typedef SctsList<SmlMapItemList_t,SmlMapItem_t,SctsMapItem> SctsMapItemList;
typedef SctsList<SmlSourceList_t,SmlSource_t,SctsTargetSource> SctsSourceList;

/*****************************************************************
Note :
All List class wrappers are designed to be used as iterators.
A call to GetNextItem causes the List object to wrap the next item 
and then call GetItem..
******************************************************************/

class SctsPcdata
{

public:
	SmlPcdataPtr_t rawData;
	bool operator!();
	operator bool();
	operator char *();
	friend bool operator==(SctsPcdata&,char *);
	friend bool operator==(SctsPcdata&,unsigned int);
	friend bool operator==(SctsPcdata&,SctsPcdata&);
	SctsPcdata& operator=(SctsPcdata&);
	int GetLength();
	bool IsUndef();
	bool IsMeta();
	bool IsString();
	void Clear();
	SctsMeta& GetMeta(SctsMeta&);
	char *GetString();
	int GetInt();

	/* Constructors */
	SctsPcdata(int);
	SctsPcdata(char *);
	SctsPcdata(char *,unsigned int size,SctsEncoding encoding);
	SctsPcdata(SctsMeta&);
	SctsPcdata(SmlPcdataPtr_t pPcdata=0);
};

/*****************************************************************/

class SctsTargetSource
{
public:
	sml_source_or_target_s *rawData;
	bool operator!();
	operator bool();
	void SetLocURI(SctsPcdata *);
	SctsPcdata& GetLocURI(SctsPcdata&);
	void SetLocName(SctsPcdata *);
	SctsPcdata& GetLocName(SctsPcdata &);
	
	/* Constructors */
	SctsTargetSource(char *pLocURI,char *pLocName=0);
	SctsTargetSource(sml_source_or_target_s *pTargetSource=0);
};

/*****************************************************************/

class SctsMeta
{
public:
	SmlMetInfMetInfPtr_t rawData;
	bool operator!();
	operator bool();
	void SetType(SctsPcdata *);
	SctsPcdata& GetType(SctsPcdata&);
	void SetFormat(SctsPcdata *);
	SctsPcdata& GetFormat(SctsPcdata&);
	void SetMark(SctsPcdata *);
	SctsPcdata& GetMark(SctsPcdata&);
	void SetVersion(SctsPcdata *);
	SctsPcdata& GetVersion(SctsPcdata&);
	void SetNextNonce(SctsPcdata *);
	SctsPcdata& GetNextNonce(SctsPcdata&);
	void SetAnchorLast(SctsPcdata *);
	SctsPcdata& GetAnchorLast(SctsPcdata&);
	void SetAnchorNext(SctsPcdata *);
	SctsPcdata& GetAnchorNext(SctsPcdata&);
	void SetMemFreeMem(int);
	int GetMemFreeMem();
	void SetMemFreeID(int);
	int GetMemFreeID();
	void SetMaxMsgSize(int);
	int GetMaxMsgSize();
	void SetMaxObjSize(int);
	int GetMaxObjSize();
	void SetSize(int);
	int GetSize();
	void SetEMI(SctsPcdataList *);
	SctsPcdataList& GetEMI(SctsPcdataList&);
	bool IsMemSharedMem();
	void IsMemSharedMem(bool);

	/* Constructors */
	SctsMeta(SmlMetInfMetInfPtr_t);
	SctsMeta();
};

/*****************************************************************/

class SctsCred
{
public:
	SmlCredPtr_t rawData;
	bool operator!();
	operator bool();
	void SetMeta(SctsMeta *);
	SctsMeta& GetMeta(SctsMeta&);
	void SetData(SctsPcdata *);
	SctsPcdata& GetData(SctsPcdata&);

	/* Constructors */
	SctsCred(SmlCredPtr_t=0);
	SctsCred(SctsMeta *pMeta,SctsPcdata *pPcdata);
};

/*****************************************************************/

class SctsItem
{
public:
	SmlItemPtr_t rawData;
	bool operator!();
	operator bool();
	void SetMeta(SctsMeta *);
	SctsMeta& GetMeta(SctsMeta&);
	void SetData(SctsPcdata *);
	SctsPcdata& GetData(SctsPcdata&);
	void SetSource(char *pLocURI,char *pLocName=0);
	SctsTargetSource& GetSource(SctsTargetSource&);
	void SetTarget(char *pLocURI,char *pLocName=0);
	SctsTargetSource& GetTarget(SctsTargetSource&);
	void IsChunked(bool);
	bool IsChunked();

	/* Constructors */
	SctsItem(SmlItemPtr_t=0);
	SctsItem(char *pSourceLocURI,char *pSourceLocName,char *pTargetLocURI,char *pTargetLocName,SctsMeta *pMeta,SctsPcdata *pData,bool isChunked=false);
};

/*****************************************************************/

class SctsMapItem
{
public:
	SmlMapItemPtr_t rawData;
	bool operator!();
	operator bool();
	void SetSource(char *pLocURI,char *pLocName=0);
	SctsTargetSource& GetSource(SctsTargetSource&);
	void SetTarget(char *pLocURI,char *pLocName=0);
	SctsTargetSource& GetTarget(SctsTargetSource&);

	/* Constructors */
	SctsMapItem(SmlMapItemPtr_t=0);
	SctsMapItem(char *pSourceLocURI,char *pSourceLocName,char *pTargetLocURI,char *pTargetLocName);
};

#endif
#endif
