composite_keys.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* Boost.MultiIndex example of composite keys.
  2. *
  3. * Copyright 2003-2008 Joaquin M Lopez Munoz.
  4. * Distributed under the Boost Software License, Version 1.0.
  5. * (See accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. *
  8. * See http://www.boost.org/libs/multi_index for library home page.
  9. */
  10. #if !defined(NDEBUG)
  11. #define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING
  12. #define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE
  13. #endif
  14. #include <boost/call_traits.hpp>
  15. #include <boost/multi_index_container.hpp>
  16. #include <boost/multi_index/composite_key.hpp>
  17. #include <boost/multi_index/member.hpp>
  18. #include <boost/multi_index/ordered_index.hpp>
  19. #include <boost/next_prior.hpp>
  20. #include <boost/tokenizer.hpp>
  21. #include <functional>
  22. #include <iostream>
  23. #include <iterator>
  24. #include <map>
  25. #include <string>
  26. using namespace boost::multi_index;
  27. /* A file record maintains some info on name and size as well
  28. * as a pointer to the directory it belongs (null meaning the root
  29. * directory.)
  30. */
  31. struct file_entry
  32. {
  33. file_entry(
  34. std::string name_,unsigned size_,bool is_dir_,const file_entry* dir_):
  35. name(name_),size(size_),is_dir(is_dir_),dir(dir_)
  36. {}
  37. std::string name;
  38. unsigned size;
  39. bool is_dir;
  40. const file_entry* dir;
  41. friend std::ostream& operator<<(std::ostream& os,const file_entry& f)
  42. {
  43. os<<f.name<<"\t"<<f.size;
  44. if(f.is_dir)os<<"\t <dir>";
  45. return os;
  46. }
  47. };
  48. /* A file system is just a multi_index_container of entries with indices on
  49. * file and size. These indices are firstly ordered by directory, as commands
  50. * work on a current directory basis. Composite keys are just fine to model
  51. * this.
  52. * NB: The use of derivation here instead of simple typedef is explained in
  53. * Compiler specifics: type hiding.
  54. */
  55. struct name_key:composite_key<
  56. file_entry,
  57. BOOST_MULTI_INDEX_MEMBER(file_entry,const file_entry*,dir),
  58. BOOST_MULTI_INDEX_MEMBER(file_entry,std::string,name)
  59. >{};
  60. struct size_key:composite_key<
  61. file_entry,
  62. BOOST_MULTI_INDEX_MEMBER(file_entry,const file_entry* const,dir),
  63. BOOST_MULTI_INDEX_MEMBER(file_entry,unsigned,size)
  64. >{};
  65. /* see Compiler specifics: composite_key in compilers without partial
  66. * template specialization, for info on composite_key_result_less
  67. */
  68. typedef multi_index_container<
  69. file_entry,
  70. indexed_by<
  71. /* primary index sorted by name (inside the same directory) */
  72. ordered_unique<name_key>,
  73. /* secondary index sorted by size (inside the same directory) */
  74. ordered_non_unique<size_key>
  75. >
  76. > file_system;
  77. /* typedef's of the two indices of file_system */
  78. typedef nth_index<file_system,0>::type file_system_by_name;
  79. typedef nth_index<file_system,1>::type file_system_by_size;
  80. /* We build a rudimentary file system simulation out of some global
  81. * info and a map of commands provided to the user.
  82. */
  83. static file_system fs; /* the one and only file system */
  84. static file_system_by_name& fs_by_name=fs; /* name index to fs */
  85. static file_system_by_size& fs_by_size=get<1>(fs); /* size index to fs */
  86. static const file_entry* current_dir=0; /* root directory */
  87. /* command framework */
  88. /* A command provides an execute memfun fed with the corresponding params
  89. * (first param is stripped off as it serves to identify the command
  90. * currently being used.)
  91. */
  92. typedef boost::tokenizer<boost::char_separator<char> > command_tokenizer;
  93. class command
  94. {
  95. public:
  96. virtual ~command(){}
  97. virtual void execute(
  98. command_tokenizer::iterator tok1,command_tokenizer::iterator tok2)=0;
  99. };
  100. /* available commands */
  101. /* cd: syntax cd [.|..|<directory>] */
  102. class command_cd:public command
  103. {
  104. public:
  105. virtual void execute(
  106. command_tokenizer::iterator tok1,command_tokenizer::iterator tok2)
  107. {
  108. if(tok1==tok2)return;
  109. std::string dir=*tok1++;
  110. if(dir==".")return;
  111. if(dir==".."){
  112. if(current_dir)current_dir=current_dir->dir;
  113. return;
  114. }
  115. file_system_by_name::iterator it=fs.find(
  116. boost::make_tuple(current_dir,dir));
  117. if(it==fs.end()){
  118. std::cout<<"non-existent directory"<<std::endl;
  119. return;
  120. }
  121. if(!it->is_dir){
  122. std::cout<<dir<<" is not a directory"<<std::endl;
  123. return;
  124. }
  125. current_dir=&*it;
  126. }
  127. };
  128. static command_cd cd;
  129. /* ls: syntax ls [-s] */
  130. class command_ls:public command
  131. {
  132. public:
  133. virtual void execute(
  134. command_tokenizer::iterator tok1,command_tokenizer::iterator tok2)
  135. {
  136. std::string option;
  137. if(tok1!=tok2)option=*tok1++;
  138. if(!option.empty()){
  139. if(option!="-s"){
  140. std::cout<<"incorrect parameter"<<std::endl;
  141. return;
  142. }
  143. /* list by size */
  144. file_system_by_size::iterator it0,it1;
  145. boost::tie(it0,it1)=fs_by_size.equal_range(
  146. boost::make_tuple(current_dir));
  147. std::copy(it0,it1,std::ostream_iterator<file_entry>(std::cout,"\n"));
  148. return;
  149. }
  150. /* list by name */
  151. file_system_by_name::iterator it0,it1;
  152. boost::tie(it0,it1)=fs.equal_range(boost::make_tuple(current_dir));
  153. std::copy(it0,it1,std::ostream_iterator<file_entry>(std::cout,"\n"));
  154. }
  155. };
  156. static command_ls ls;
  157. /* mkdir: syntax mkdir <directory> */
  158. class command_mkdir:public command
  159. {
  160. public:
  161. virtual void execute(
  162. command_tokenizer::iterator tok1,command_tokenizer::iterator tok2)
  163. {
  164. std::string dir;
  165. if(tok1!=tok2)dir=*tok1++;
  166. if(dir.empty()){
  167. std::cout<<"missing parameter"<<std::endl;
  168. return;
  169. }
  170. if(dir=="."||dir==".."){
  171. std::cout<<"incorrect parameter"<<std::endl;
  172. return;
  173. }
  174. if(!fs.insert(file_entry(dir,0,true,current_dir)).second){
  175. std::cout<<"directory already exists"<<std::endl;
  176. return;
  177. }
  178. }
  179. };
  180. static command_mkdir mkdir;
  181. /* table of commands, a map from command names to class command pointers */
  182. typedef std::map<std::string,command*> command_table;
  183. static command_table cmt;
  184. int main()
  185. {
  186. /* fill the file system with some data */
  187. file_system::iterator it0,it1;
  188. fs.insert(file_entry("usr.cfg",240,false,0));
  189. fs.insert(file_entry("memo.txt",2430,false,0));
  190. it0=fs.insert(file_entry("dev",0,true,0)).first;
  191. fs.insert(file_entry("tty0",128,false,&*it0));
  192. fs.insert(file_entry("tty1",128,false,&*it0));
  193. it0=fs.insert(file_entry("usr",0,true,0)).first;
  194. it1=fs.insert(file_entry("bin",0,true,&*it0)).first;
  195. fs.insert(file_entry("bjam",172032,false,&*it1));
  196. it0=fs.insert(file_entry("home",0,true,0)).first;
  197. it1=fs.insert(file_entry("andy",0,true,&*it0)).first;
  198. fs.insert(file_entry("logo.jpg",5345,false,&*it1)).first;
  199. fs.insert(file_entry("foo.cpp",890,false,&*it1)).first;
  200. fs.insert(file_entry("foo.hpp",93,false,&*it1)).first;
  201. fs.insert(file_entry("foo.html",750,false,&*it1)).first;
  202. fs.insert(file_entry("a.obj",12302,false,&*it1)).first;
  203. fs.insert(file_entry(".bash_history",8780,false,&*it1)).first;
  204. it1=fs.insert(file_entry("rachel",0,true,&*it0)).first;
  205. fs.insert(file_entry("test.py",650,false,&*it1)).first;
  206. fs.insert(file_entry("todo.txt",241,false,&*it1)).first;
  207. fs.insert(file_entry(".bash_history",9510,false,&*it1)).first;
  208. /* fill the command table */
  209. cmt["cd"] =&cd;
  210. cmt["ls"] =&ls;
  211. cmt["mkdir"]=&mkdir;
  212. /* main looop */
  213. for(;;){
  214. /* print out the current directory and the prompt symbol */
  215. if(current_dir)std::cout<<current_dir->name;
  216. std::cout<<">";
  217. /* get an input line from the user: if empty, exit the program */
  218. std::string com;
  219. std::getline(std::cin,com);
  220. command_tokenizer tok(com,boost::char_separator<char>(" \t\n"));
  221. if(tok.begin()==tok.end())break; /* null command, exit */
  222. /* select the corresponding command and execute it */
  223. command_table::iterator it=cmt.find(*tok.begin());
  224. if(it==cmt.end()){
  225. std::cout<<"invalid command"<<std::endl;
  226. continue;
  227. }
  228. it->second->execute(boost::next(tok.begin()),tok.end());
  229. }
  230. return 0;
  231. }