software-desc.hh 11.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
linphone
Copyright (C) 2013 Belledonne Communications SARL
Simon Morlat (simon.morlat@linphone.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 20 21 22 23
*/

#ifndef software_desc_hh
#define software_desc_hh

24
#include <functional>
25 26 27 28
#include <iostream>
#include <list>
#include <map>
#include <algorithm>
29
#include <string>
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

#include <sys/types.h>
#include <sys/stat.h>
#include <cstring>



class Type{
public:
	enum BasicType{
		Void,
		Boolean,
		Integer,
		Float,
		String,
		Enum,
		Class,
47 48
		Callback,
		Array
49 50
	};
	static const char *sBasicTypeNames[];
51
	static Type* addType(BasicType bt, const std::string &name){
52 53 54 55 56 57 58 59 60
		Type* ret;
		if ((ret=mTypes[name])==0){
			//cout<<"Adding new "<<sBasicTypeNames[(int)bt]<<" type '"<<name<<"'"<<endl;
			ret=mTypes[name]=new Type(bt,name);
		}else if (bt!=Class){
			ret->mBasic=bt;
		}
		return ret;
	}
61 62
	static Type *getType(const std::std::string &tname){
		if (tname.find("(")!=std::string::npos) return NULL; //arrives when parsing function pointer declared inside function prototype
63 64
		if (strstr(tname.c_str(),"char")!=0 && strchr(tname.c_str(),'*')!=0){
			return &sStringType;
65
		}else if (tname.find("int")!=std::string::npos){
66
			return &sIntegerType;
67
		}else if (tname.find("size_t")!=std::string::npos){
68
			return &sIntegerType;
69
		}else if (tname.find("float")!=std::string::npos){
70
			return &sFloatType;
71
		}else if (tname.find("bool_t")!=std::string::npos){
72
			return &sBooleanType;
73
		}else if (tname.find("void")!=std::string::npos){
74
			return &sVoidType;
75
		}else if (tname.find("enum")!=std::string::npos){
76
			return addType(Enum,tname.c_str()+strlen("enum "));
77
		}else if (tname.find("MSList")!=std::string::npos){
78
			return &sArrayType;
79 80
		}else{/*an object?*/
			
81
			std::string tmp=tname;
82 83 84 85 86
			size_t pos;
			
			/*really ugly and slow*/
			
			pos=tmp.find('*');
87
			if (pos!=std::string::npos)
88 89 90
				tmp.erase(pos,1);
			
			pos=tmp.find("const");
91
			if (pos!=std::string::npos)
92 93
				tmp.erase(pos,strlen("const"));
			
94
			while ((pos=tmp.find(' '))!=std::string::npos){
95 96 97 98
				tmp.erase(pos,1);
			}
			return addType(Class,tmp);
		}
99
		std::cerr<<"Unhandled type name"<<tname<<std::endl;
100 101
		return NULL;
	}
102
	const std::string &getName()const{
103 104 105 106 107 108 109
		return mName;
	}
	BasicType getBasicType()const{
		return mBasic;
	}
private:
	BasicType mBasic;
110
	std::string mName;
111 112 113 114 115 116 117
	Type(BasicType basic, const std::string &tname="") : mBasic(basic), mName(tname){
	}
	static Type sStringType;
	static Type sIntegerType;
	static Type sVoidType;
	static Type sBooleanType;
	static Type sFloatType;
118
	static Type sArrayType;
119
	static std::map<std::string,Type*> mTypes;
120 121 122 123 124 125
};



class Argument{
public:
126
	Argument(Type *type, const std::string &argname, bool isConst, bool isPointer) : mType(type), mName(argname), mConst(isConst), mPointer(isPointer){
127 128 129 130 131 132 133 134
		if (!isPointer) mConst=false;
	}
	Type *getType()const{
		return mType;
	}
	bool isConst()const{
		return mConst;
	}
135
	const std::string &getName()const{
136 137 138 139 140
		return mName;
	}
	bool isPointer()const{
		return mPointer;
	}
141
	const std::string &getHelp()const{
142 143
		return mHelp;
	}
144
	void setHelp(const std::string &help){
145 146 147 148 149
		mHelp=help;
	}
private:
	
	Type *mType;
150 151
	std::string mName;
	std::string mHelp;
152 153 154 155 156 157 158 159 160 161 162
	bool mConst;
	bool mPointer;
};

class Method{
public:
	enum PropertyBehaviour{
		None,
		Read,
		Write
	};
163
	Method(const std::string &uid, Argument* return_arg, const std::string &name, const list<Argument*> &args, bool isConst, bool isStatic, bool isCallback=false){
164 165 166 167 168 169
		mUid=uid;
		mReturn=return_arg;
		mName=name;
		mArgs=args;
		mConst=isConst;
		mStatic=isStatic;
170
		mIsCallback=isCallback;
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
		analyseProperties();
	}
	void setHelp(const std::string &help){
		mHelp=help;
	}
	Argument *getReturnArg()const{
		return mReturn;
	}
	const string &getName()const{
		return mName;
	}
	const list<Argument*> &getArgs()const {
		return mArgs;
	}
	bool isConst()const{
		return mConst;
	}
	bool isStatic()const{
		return mStatic;
	}
191 192 193
	bool isCallback()const{
		return mIsCallback;
	}
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	const string &getHelp(){
		return mHelp;
	}
	PropertyBehaviour getPropertyBehaviour()const{
		return mPropertyBehaviour;
	}
	const string &getPropertyName()const{
		return mPropertyName;
	}
private:
	void analyseProperties(){
		size_t enabled_pos;
		mPropertyBehaviour=None;
		
		if (mName.find("get")==0 && mArgs.size()==0){
			mPropertyName=mName.substr(3,string::npos);
			if (!mPropertyName.empty()){
211 212 213 214 215 216
				mPropertyName[0]=tolower(mPropertyName[0]);
				mPropertyBehaviour=Read;
			}
		}else if (mName.find("is")==0 && mArgs.size()==0){
			mPropertyName=mName.substr(2,string::npos);
			if (!mPropertyName.empty()){
217 218 219 220 221 222 223
				mPropertyName[0]=tolower(mPropertyName[0]);
				mPropertyBehaviour=Read;
			}
		}else if (mName.find("enable")==0 && mArgs.size()==1){
			mPropertyName=mName.substr(6,string::npos);
			if (!mPropertyName.empty()){
				mPropertyName[0]=tolower(mPropertyName[0]);
224
				mPropertyName+="Enabled";
225 226 227 228 229 230 231 232 233 234 235 236 237
				mPropertyBehaviour=Write;
			}
		}else if (mName.find("set")==0 && mArgs.size()==1){
			mPropertyName=mName.substr(3,string::npos);
			if (!mPropertyName.empty()){
				mPropertyName[0]=tolower(mPropertyName[0]);
				mPropertyBehaviour=Write;
			}
		}else if ((enabled_pos=mName.rfind("Enabled"))!=string::npos && mArgs.size()==0){
			size_t goodpos=mName.size()-7;
			if (enabled_pos==goodpos){
				mPropertyName=mName.substr(0,goodpos);
				if (!mPropertyName.empty()){
238
					mPropertyName+="Enabled";
239 240 241 242
					mPropertyBehaviour=Read;
				}
			}
		}
243
		if (mPropertyBehaviour==None) {
244
			mPropertyName="";
245 246 247 248
			if (mName.find("create")==0) {
				mName="new"+mName.substr(6,string::npos);
			}
		}
249 250 251 252 253 254 255 256 257 258
	}
	string mUid;
	Argument *mReturn;
	string mName;
	list<Argument*> mArgs;
	string mHelp;
	string mPropertyName; /*if it can be a property*/
	PropertyBehaviour mPropertyBehaviour;
	bool mConst;
	bool mStatic;
259
	bool mIsCallback;
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
};

class Property{
public:
	enum Attribute{
		ReadOnly,
		ReadWrite
	};
	Property(Attribute attr, const string &name, Type *type, const string &help) : mAttr(attr), mName(name), mType(type), mHelp(help){
	}
	const string &getName()const{
		return mName;
	}
	const string &getHelp()const{
		return mHelp;
	}
	void setHelp(const string &help){
		mHelp=help;
	}
	Attribute getAttribute()const{
		return mAttr;
	}
	void setAttribute(Attribute attr){
		mAttr=attr;
	}
	Type* getType()const{
		return mType;
	}
private:
	Attribute mAttr;
	string mName;
	Type *mType;
	string mHelp;
};

Simon Morlat's avatar
Simon Morlat committed
295 296
class ConstField{
public:
297
	ConstField(Type *type, const string &name, int value) : mType(type), mName(name), mValue(value){
Simon Morlat's avatar
Simon Morlat committed
298 299 300 301 302 303 304 305 306 307 308 309 310
	}
	void setHelp(const string & help){
		mHelp=help;
	}
	const string &getHelp()const{
		return mHelp;
	}
	const string & getName()const{
		return mName;
	}
	Type *getType()const{
		return mType;
	}
311
	int getValue()const{
Simon Morlat's avatar
Simon Morlat committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
		return mValue;
	}
	static string getCommonPrefix(list<ConstField *> fields){
		if (fields.size()<2) return "";
		list<ConstField*>::iterator it;
		string prefix=fields.front()->getName();
		int prefixsize;
		
		for (prefixsize=prefix.size();prefixsize>0;prefixsize--){
			bool isMatching=true;
			prefix=prefix.substr(0,prefixsize);
			
			for(it=fields.begin();it!=fields.end();++it){
				ConstField *cf=*it;
				if (prefix != cf->getName().substr(0,prefixsize)){
					isMatching=false;
					break;
				}
			}
			if (isMatching){
332
				//cout<<"enum prefix: "<<prefix<<endl;
Simon Morlat's avatar
Simon Morlat committed
333 334 335 336 337 338 339 340
				return prefix;
			}
		}
		return "";
	}
private:
	Type *mType;
	string mName;
341
	int mValue;
Simon Morlat's avatar
Simon Morlat committed
342 343 344 345 346 347 348 349 350 351 352 353
	string mHelp;
};

template <typename _type>
struct name_matcher{
	name_matcher(const string &name) : mName(name){}
	bool operator()(_type *cf){
		return cf->getName()==mName;
	}
	string mName;
};

354 355 356 357 358 359 360 361 362 363 364 365
/*actually a class or an enum*/
class Class{
public:
	Class(const std::string &name): mName(name){
	}
	Type::BasicType getType(){
		return Type::getType(mName)->getBasicType();
	}
	void addMethod(Method *method){
		if (mMethods.find(method->getName())==mMethods.end())
			mMethods.insert(make_pair(method->getName(),method));
	}
Simon Morlat's avatar
Simon Morlat committed
366 367 368 369 370
	void addConstField(ConstField *field){
		list<ConstField*>::iterator it=find_if(mConstFields.begin(),mConstFields.end(),name_matcher<ConstField>(field->getName()));
		if (it==mConstFields.end())
			mConstFields.push_back(field);
	}
371 372 373
	void setHelp(const std::string &help){
		mHelp=help;
	}
Simon Morlat's avatar
Simon Morlat committed
374
	list<Method*> getMethods()const{
375 376 377 378 379 380 381
		list<Method*> ret;
		map<string,Method*>::const_iterator it;
		for(it=mMethods.begin();it!=mMethods.end();++it){
			ret.push_back((*it).second);
		}
		return ret;
	}
Simon Morlat's avatar
Simon Morlat committed
382 383 384
	const list<ConstField*> &getConstFields()const{
		return mConstFields;
	}
385 386 387 388 389 390
	const string &getName()const{
		return mName;
	}
	const string &getHelp()const{
		return mHelp;
	}
Simon Morlat's avatar
Simon Morlat committed
391
	list<Property*> getProperties(){
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
		list<Property*> ret;
		map<string,Property*>::const_iterator it;
		for(it=mProperties.begin();it!=mProperties.end();++it){
			ret.push_back((*it).second);
		}
		return ret;
	}
	void computeProperties(){
		map<string,Method*>::const_iterator it;
		Property *prop;
		for (it=mMethods.begin();it!=mMethods.end();++it){
			Method *m=(*it).second;
			if (m->getPropertyBehaviour()==Method::Read){
				prop=mProperties[m->getPropertyName()];
				if (prop==NULL){
					prop=new Property(Property::ReadOnly,m->getPropertyName(),m->getReturnArg()->getType(), m->getHelp());
					mProperties[m->getPropertyName()]=prop;
				}
			}else if (m->getPropertyBehaviour()==Method::Write){
				prop=mProperties[m->getPropertyName()];
				if (prop==NULL){
					prop=new Property(Property::ReadWrite,m->getPropertyName(),m->getArgs().front()->getType(), m->getHelp());
					mProperties[m->getPropertyName()]=prop;
				}else{
					prop->setHelp(m->getHelp());
					prop->setAttribute(Property::ReadWrite);
				}
			}
		}
	}
private:
	map<string,Method*> mMethods;
	map<string,Property*> mProperties;
Simon Morlat's avatar
Simon Morlat committed
425
	list<ConstField*> mConstFields;
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
	string mName;
	string mHelp;
};

class Project{
public:
	Project(const string &name="wrapper") : mName(name){
	}
	Class *getClass(const std::string &name){
		Class *ret;
		if ((ret=mClasses[name])==NULL){
			ret=mClasses[name]=new Class(name);
		}
		return ret;
	}
	list<Class*> getClasses()const{
		list<Class*> ret;
		map<string,Class*>::const_iterator it;
		for(it=mClasses.begin();it!=mClasses.end();++it){
			ret.push_back((*it).second);
		}
		return ret;
	}
	const string &getName()const{
		return mName;
	}
	void analyse(){
		list<Class*> classes=getClasses();
		for_each(classes.begin(),classes.end(),mem_fun(&Class::computeProperties));
	}
456 457 458 459 460 461 462 463
	void addCallback(Method *callback){
		list<Method*>::iterator it=find_if(mCallbacks.begin(),mCallbacks.end(),name_matcher<Method>(callback->getName()));
		if (it==mCallbacks.end())
			mCallbacks.push_back(callback);
	}
	const list<Method*> &getCallbacks()const{
		return mCallbacks;
	}
464 465
private:
	map<string,Class*> mClasses;
466
	list<Method*> mCallbacks;
467 468 469 470
	string mName;
};

#endif