Mirror reflection library 0.5.13
|
00001 00010 #ifndef MIRROR_UTILS_BOOSTFS_FACTORY_1101122253_HPP 00011 #define MIRROR_UTILS_BOOSTFS_FACTORY_1101122253_HPP 00012 00013 #include <mirror/config.hpp> 00014 #include <mirror/utils/sdn_factory.hpp> 00015 #include <boost/filesystem.hpp> 00016 #include <boost/filesystem/fstream.hpp> 00017 00018 MIRROR_NAMESPACE_BEGIN 00019 00020 class sdn_fact_boostfs_property; 00021 class sdn_fact_boostfs_element; 00022 class sdn_fact_boostfs_range; 00023 00024 class sdn_fact_boostfs_property 00025 { 00026 private: 00027 typedef sdn_fact_boostfs_property property; 00028 typedef boost::filesystem::path bfs_path; 00029 bfs_path filepath; 00030 00031 friend class sdn_fact_boostfs_element; 00032 00033 sdn_fact_boostfs_property(const bfs_path& _path) 00034 : filepath(_path) 00035 { } 00036 00037 bool ok(void) const 00038 { 00039 return boost::filesystem::exists(filepath) && 00040 boost::filesystem::is_regular_file(filepath); 00041 } 00042 public: 00043 operator bool (void) const 00044 { 00045 return ok(); 00046 } 00047 00048 bool operator ! (void) const 00049 { 00050 return !ok(); 00051 } 00052 00053 bool has_type(const std::string& type_name) 00054 { 00055 return true; 00056 } 00057 00058 std::string text(void) 00059 { 00060 assert(ok()); 00061 boost::filesystem::ifstream f(filepath, std::ios_base::in); 00062 std::stringbuf temp; 00063 f.get(temp); 00064 return temp.str(); 00065 } 00066 }; 00067 00068 class sdn_fact_boostfs_range 00069 { 00070 private: 00071 typedef sdn_fact_boostfs_element element; 00072 typedef boost::filesystem::path bfs_path; 00073 boost::filesystem::directory_iterator cur, end; 00074 public: 00075 sdn_fact_boostfs_range(const bfs_path& _path) 00076 : cur(_path) 00077 , end() 00078 { } 00079 00080 bool empty(void) const 00081 { 00082 return cur == end; 00083 } 00084 00085 void step_front(void) 00086 { 00087 assert(!empty()); 00088 ++cur; 00089 } 00090 00091 element front(void) const; 00092 }; 00093 00094 class sdn_fact_boostfs_element 00095 { 00096 private: 00097 typedef sdn_fact_boostfs_range range; 00098 typedef sdn_fact_boostfs_element element; 00099 typedef sdn_fact_boostfs_property property; 00100 00101 typedef boost::filesystem::path bfs_path; 00102 bfs_path dirpath; 00103 00104 bool ok(void) const 00105 { 00106 return boost::filesystem::exists(dirpath) && 00107 boost::filesystem::is_directory(dirpath); 00108 } 00109 public: 00110 sdn_fact_boostfs_element(const bfs_path& _path) 00111 : dirpath(_path) 00112 { 00113 } 00114 00115 operator bool (void) const 00116 { 00117 return ok(); 00118 } 00119 00120 bool operator ! (void) const 00121 { 00122 return !ok(); 00123 } 00124 00125 bool has_type(const std::string& type_name) 00126 { 00127 assert(ok()); 00128 return true; 00129 } 00130 00131 range elements(void) const 00132 { 00133 assert(ok()); 00134 return range(dirpath); 00135 } 00136 00137 element child(const std::string& name) 00138 { 00139 assert(ok()); 00140 return element(dirpath / name); 00141 } 00142 00143 property attribute(void) 00144 { 00145 assert(ok()); 00146 return property(dirpath / "value"); 00147 } 00148 00149 property attribute(const std::string& name) 00150 { 00151 assert(!name.empty()); 00152 assert(ok()); 00153 return property(dirpath / name); 00154 } 00155 }; 00156 00157 sdn_fact_boostfs_element sdn_fact_boostfs_range::front(void) const 00158 { 00159 assert(!empty()); 00160 return sdn_fact_boostfs_element(*cur); 00161 } 00162 00163 typedef sdn_fact_traits< 00164 sdn_fact_boostfs_range, 00165 sdn_fact_boostfs_element, 00166 sdn_fact_boostfs_property 00167 > sdn_fact_boostfs_traits; 00168 00169 typedef factory_maker< 00170 sdn_fact_manuf, 00171 sdn_fact_suppl, 00172 sdn_fact_enum, 00173 sdn_fact_boostfs_traits 00174 > boostfs_factory_maker; 00175 00176 typedef sdn_fact_data< 00177 sdn_fact_boostfs_traits 00178 > boostfs_factory_input; 00179 00180 MIRROR_NAMESPACE_END 00181 00182 #endif //include guard 00183