cdlcompilertoolkit/src/CdlTkPackage.cpp
changeset 0 f58d6ec98e88
equal deleted inserted replaced
-1:000000000000 0:f58d6ec98e88
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 #include "CdlCompilerToolkit/CdlTkInstance.h"
       
    18 #include "CdlTkPriv.h"
       
    19 #include <sstream>
       
    20 using namespace std;
       
    21 
       
    22 namespace CdlCompilerToolkit {
       
    23 
       
    24 //
       
    25 // PackageErr
       
    26 //
       
    27 
       
    28 class PackageErr : public CdlCompilerToolkitErr
       
    29 	{
       
    30 public:
       
    31 	void Show(ostream& aStream) const;
       
    32 	};
       
    33 
       
    34 void PackageErr::Show(ostream& aStream) const
       
    35 	{
       
    36 	aStream << "Interface does not define a package";
       
    37 	}
       
    38 
       
    39 
       
    40 //
       
    41 // CCdlTkPackage::CRef
       
    42 //
       
    43 
       
    44 CCdlTkPackage::CRef::CRef()
       
    45 : iId(KCdlTkGetInstanceIdFromHostDll), iUid(0)
       
    46 	{
       
    47 	}
       
    48 
       
    49 const string& CCdlTkPackage::CRef::LocalInstanceName() const
       
    50 	{
       
    51 	return iLocalInstanceName;
       
    52 	}
       
    53 
       
    54 void CCdlTkPackage::CRef::SetLocalInstanceName(const string& aLocalInstanceName)
       
    55 	{
       
    56 	iLocalInstanceName = aLocalInstanceName;
       
    57 	}
       
    58 
       
    59 int CCdlTkPackage::CRef::Id() const
       
    60 	{
       
    61 	return iId;
       
    62 	}
       
    63 
       
    64 void CCdlTkPackage::CRef::SetId(int aId)
       
    65 	{
       
    66 	iId = aId;
       
    67 	}
       
    68 
       
    69 int CCdlTkPackage::CRef::Uid() const
       
    70 	{
       
    71 	return iUid;
       
    72 	}
       
    73 
       
    74 void CCdlTkPackage::CRef::SetUid(int aUid)
       
    75 	{
       
    76 	iUid = aUid;
       
    77 	}
       
    78 
       
    79 const string& CCdlTkPackage::CRef::DllName() const
       
    80 	{
       
    81 	return iDllName;
       
    82 	}
       
    83 
       
    84 void CCdlTkPackage::CRef::SetDllName(const string& aDllName)
       
    85 	{
       
    86 	iDllName = aDllName;
       
    87 	}
       
    88 
       
    89 const string& CCdlTkPackage::CRef::DllSourcePath() const
       
    90 	{
       
    91 	return iDllSourcePath;
       
    92 	}
       
    93 
       
    94 void CCdlTkPackage::CRef::SetDllSourcePath(const string& aDllSourcePath)
       
    95 	{
       
    96 	iDllSourcePath = aDllSourcePath;
       
    97 	}
       
    98 
       
    99 const string& CCdlTkPackage::CRef::DllInstName() const
       
   100 	{
       
   101 	return iDllInstName;
       
   102 	}
       
   103 
       
   104 void CCdlTkPackage::CRef::SetDllInstName(const string& aDllInstName)
       
   105 	{
       
   106 	iDllInstName = aDllInstName;
       
   107 	}
       
   108 
       
   109 
       
   110 
       
   111 //
       
   112 // CCdlTkPackage
       
   113 //
       
   114 
       
   115 CCdlTkPackage::CCdlTkPackage(const CCdlTkInterface& aInterface)
       
   116 : CCdlTkInstance(aInterface)
       
   117 	{
       
   118 	iContentsImpl = Impl().Find(KPackageContentsApi);
       
   119 	if (!iContentsImpl)
       
   120 		throw PackageErr();
       
   121 	GenerateImpl();
       
   122 	}
       
   123 
       
   124 CCdlTkPackage::~CCdlTkPackage()
       
   125 	{
       
   126 	}
       
   127 
       
   128 void CCdlTkPackage::AddLocalContent(const CCdlTkInstance& aInstance)
       
   129 	{
       
   130 	CRef ref;
       
   131 	ref.SetLocalInstanceName(aInstance.Name());
       
   132 	iContents.push_back(ref);
       
   133 	GenerateImpl();
       
   134 	}
       
   135 
       
   136 void CCdlTkPackage::AddLocalContent(const string& aLocalInstanceName)
       
   137 	{
       
   138 	CRef ref;
       
   139 	ref.SetLocalInstanceName(aLocalInstanceName);
       
   140 	iContents.push_back(ref);
       
   141 	GenerateImpl();
       
   142 	}
       
   143 
       
   144 void CCdlTkPackage::AddContent(const CCdlTkInstance& aInstance, const CCdlTkDll& aDll, const std::string& aDllSourcePath)
       
   145 	{
       
   146 	CRef ref;
       
   147 	ref.SetId(aInstance.Id());
       
   148 	ref.SetUid(aInstance.Interface().Header().Uid());
       
   149 	ref.SetDllName(aDll.Name());
       
   150 	ref.SetDllSourcePath(aDllSourcePath);
       
   151 	ref.SetDllInstName(aInstance.DllInstanceName());
       
   152 	iContents.push_back(ref);
       
   153 	GenerateImpl();
       
   154 	}
       
   155 
       
   156 void CCdlTkPackage::AddContent(int aId, int aUid, const string& aDllName)
       
   157 	{
       
   158 	CRef ref;
       
   159 	ref.SetId(aId);
       
   160 	ref.SetUid(aUid);
       
   161 	ref.SetDllName(aDllName);
       
   162 	iContents.push_back(ref);
       
   163 	GenerateImpl();
       
   164 	}
       
   165 
       
   166 void CCdlTkPackage::AddExternalContent(const string& aInstanceName, const string& aDllSourcePath, const string& aDllName)
       
   167 	{
       
   168 	CRef ref;
       
   169 	ref.SetDllInstName(aInstanceName);
       
   170 	ref.SetDllName(aDllName);
       
   171 	ref.SetDllSourcePath(aDllSourcePath);
       
   172 	iContents.push_back(ref);
       
   173 	GenerateImpl();
       
   174 	}
       
   175 
       
   176 const CCdlTkPackage::CRefs& CCdlTkPackage::Contents() const
       
   177 	{
       
   178 	return iContents;
       
   179 	}
       
   180 
       
   181 const string KArrayStart = "\
       
   182 \n\
       
   183 CDL_ARRAY_START(TCdlRef, contents)\n\
       
   184 \t{\n";
       
   185 
       
   186 const string KArrayEnd = "\
       
   187 \t}\n\
       
   188 CDL_ARRAY_END(TCdlRef, contents);\n";
       
   189 
       
   190 void CCdlTkPackage::GenerateImpl()
       
   191 	{
       
   192 	string defn;
       
   193 
       
   194 	if (iContents.empty())
       
   195 		{
       
   196 		defn = "EMPTY_CDL_ARRAY(TCdlRef, contents)\n";
       
   197 		}
       
   198 	else
       
   199 		{
       
   200 		set<string> dllNames;
       
   201 		AddIncludesAndBuildDllSet(defn, dllNames);
       
   202 		AddDllNameLits(defn, dllNames);
       
   203 		CdlTkUtil::AppendString(defn, KArrayStart);
       
   204 		AddContents(defn);
       
   205 		CdlTkUtil::AppendString(defn, KArrayEnd);
       
   206 		}
       
   207 
       
   208 	iContentsImpl->SetDefinition(defn);
       
   209 	}
       
   210 
       
   211 const string KInclude = "#include \"$NAME\"\n";
       
   212 
       
   213 void CCdlTkPackage::AddIncludesAndBuildDllSet(string& aDefn, set<string>& aDllNames)
       
   214 	{
       
   215 	for (CRefs::iterator pRef = iContents.begin(); pRef != iContents.end(); ++pRef)
       
   216 		{
       
   217 		CRef& ref = *pRef;
       
   218 		string locInst = CdlTkUtil::ToLower(ref.LocalInstanceName());
       
   219 		if (locInst.empty())
       
   220 			{
       
   221 			// if it's not a local instance, add its dll name to the set
       
   222 			string dllName = ref.DllName();
       
   223 			if (!dllName.empty())
       
   224 				aDllNames.insert(dllName);
       
   225 
       
   226 			// if the instance id comes from its host DLL, #include the host DLL ids
       
   227 			if (ref.Id() == KCdlTkGetInstanceIdFromHostDll)
       
   228 				{
       
   229 				string include = CdlTkUtil::ToLower(ref.DllSourcePath() + KDllInstHeader);
       
   230 				CdlTkUtil::AppendString(aDefn, CdlTkUtil::Replace("$NAME", include, KInclude));
       
   231 				include = CdlTkUtil::ToLower(ref.DllSourcePath() + ref.DllInstName() + ".h");
       
   232 				CdlTkUtil::AppendString(aDefn, CdlTkUtil::Replace("$NAME", include, KInclude));
       
   233 				}
       
   234 			}
       
   235 		else
       
   236 			{
       
   237 			// #include local instances
       
   238 			CdlTkUtil::AppendString(aDefn, CdlTkUtil::Replace("$NAME", locInst+".h", KInclude));
       
   239 			}
       
   240 		}
       
   241 	}
       
   242 
       
   243 const string KDllLit = "_LIT(_content_DLL_$CPP_NAME, \"$DLL_NAME\");\n";
       
   244 
       
   245 void CCdlTkPackage::AddDllNameLits(string& aDefn, set<string>& aDllNames)
       
   246 	{
       
   247 	// add literals for all of the DLL names
       
   248 	for (set<string>::iterator pDll = aDllNames.begin(); pDll != aDllNames.end(); ++pDll)
       
   249 		{
       
   250 		CdlTkUtil::AppendString(aDefn, 
       
   251 			CdlTkUtil::Replace("$CPP_NAME", CdlTkUtil::ToCpp(*pDll), 
       
   252 				CdlTkUtil::Replace("$DLL_NAME", *pDll, KDllLit)));
       
   253 		}
       
   254 	}
       
   255 
       
   256 const string KLocRef = "\tLOCAL_CDL_REF($NAME),\n";
       
   257 
       
   258 void CCdlTkPackage::AddContents(string& aDefn)
       
   259 	{
       
   260 	for (CRefs::iterator pRef = iContents.begin(); pRef != iContents.end(); ++pRef)
       
   261 		{
       
   262 		string locInst = pRef->LocalInstanceName();
       
   263 		if (locInst.empty())
       
   264 			{
       
   265 			// if it's not a local instance use {} intialisation
       
   266 			string id;
       
   267 			string uid;
       
   268 			if (pRef->Id() == KCdlTkGetInstanceIdFromHostDll)
       
   269 				{
       
   270 				id = pRef->DllInstName() + "::KCdlInstanceId";
       
   271 				uid = pRef->DllInstName() + "::KCdlInterfaceUidValue";
       
   272 				}
       
   273 			else
       
   274 				{
       
   275 				id = CdlTkUtil::IntToString(pRef->Id());
       
   276 				uid = pRef->Uid();
       
   277 				}
       
   278 
       
   279 			stringstream ref;
       
   280 			ref << "\t{" << id << ", " << uid << ", ";
       
   281 
       
   282 			// empty DLL name implies that the instance is in the same DLL,
       
   283 			// use use a null pointer, otherwise use the literal
       
   284 			string dllName = pRef->DllName();
       
   285 			if (dllName.empty())
       
   286 				ref << "NULL";
       
   287 			else
       
   288 				ref << "LIT_AS_DESC_PTR(_content_DLL_" << CdlTkUtil::ToCpp(dllName) << ")";
       
   289 			ref << "},\n";
       
   290 
       
   291 			CdlTkUtil::AppendString(aDefn, ref.str());
       
   292 			}
       
   293 		else
       
   294 			{
       
   295 			// local instances use the LOCAL_CDL_REF macro.
       
   296 			CdlTkUtil::AppendString(aDefn, CdlTkUtil::Replace("$NAME", CdlTkUtil::ToCpp(locInst), KLocRef));
       
   297 			}
       
   298 		}
       
   299 	}
       
   300 
       
   301 
       
   302 }	// end of namespace CdlCompilerToolkit