c++ - Extensible parser design -


i have class target "filetype" enum holds kinds of files parser needs know (currently source, header, resource). i'd parsing function able generic like:

if( token == some_known_filetype )     put_nexttoken_in_the_list_for_that_filetype(); else     return an_error(); 

but there's catch: i'd able extend known filetypes subclasses of target, extend enum in correct way, see base enum class inheritance how did this. don't want modify above code, generically extend target half. c++0x may required , welcome.

thanks!

update: upon trying explain here , posting reduced class declarations, realised design broken, , tried push specialization of filetype deep in class structure. wanted 1 place store full list of known types, in trying so, accidentally forced design have access list in 2 places @ time. realise list of filetypes should keywords source, header, etc. read, , handled *generically thereon. store full list in 1 place, , access list through "huge" enum later on. std::map<filetype, std::set<std::string> > pops head logical choice here, instead of seperately named set each specific filetype. braincandy in responses though! thoughts still welcome.

i believe not right approach, that's belief. in different way. take step , trying achieve, want control behavior based on incoming (input parameter) value. lets have classes: filea fileb ... file_type holds type of file. use factory control available list of files (which can change based on registration of different files).

class filea {     void register_type();  // register factory. };  class fileb.. //main code class filefactorydelegator {     ...     delegatecontrol (filetype file_type) {         //validate file_type.         file_types[file_type]->performfileoperation (..);     } }; 

instead of if-else loop.

file_factory.delegatecontrol (token); 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -