fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. #define CSTR
  6. #define DSTR
  7.  
  8. using int_pair = std::pair< int, int >;
  9. //----------------------------------------------------------------------------------------
  10.  
  11. struct POINT3D
  12. {
  13. float x, y, z;
  14.  
  15. CSTR POINT3D() : x( 0 ), y( 0 ), z( 0 ) {}
  16. CSTR POINT3D( float x, float y, float z )
  17. : x( x ), y( y ), z( z ) {}
  18.  
  19. auto& operator=( const POINT3D& rhs )
  20. {
  21. x = rhs.x, y = rhs.y, z = rhs.z;
  22. return *this;
  23. }
  24.  
  25. friend std::ostream& operator<<( std::ostream& os, const POINT3D& p )
  26. {
  27. os << " x: " << p.x << std::endl;
  28. os << " y: " << p.y << std::endl;
  29. os << " z: " << p.z << std::endl;
  30. return os;
  31. }
  32. };
  33. //----------------------------------------------------------------------------------------
  34.  
  35. enum class DEVICE_TYPE
  36. {
  37. sensor,
  38. motor,
  39. };
  40.  
  41. class DEVICE_MANAGER;
  42.  
  43. ///---------------------------------------------------------------------------------------
  44.  
  45. class DEVICE
  46. {
  47. friend class DEVICE_MANAGER;
  48. private:
  49. virtual void refresh() = 0;
  50. const DEVICE_TYPE device_type;
  51.  
  52. protected:
  53. std::string name;
  54. virtual void show() const = 0;
  55.  
  56. public:
  57. CSTR DEVICE(
  58. const std::string& name,
  59. const DEVICE_TYPE device_type )
  60. : name( name )
  61. , device_type( device_type )
  62. {}
  63. };
  64. ///---------------------------------------------------------------------------------------
  65.  
  66. class DEVICE_MANAGER
  67. {
  68. private:
  69. std::vector< DEVICE* > devices;
  70.  
  71. public:
  72. CSTR DEVICE_MANAGER()
  73. {}
  74. DSTR ~DEVICE_MANAGER()
  75. {
  76. while( !devices.empty() )
  77. {
  78. delete devices.back();
  79. devices.pop_back();
  80. }
  81. }
  82.  
  83. void add( const DEVICE* device )
  84. {
  85. devices.push_back( (DEVICE*)device );
  86. }
  87.  
  88. void refresh() const
  89. {
  90. for( auto* device : devices )
  91. device->refresh();
  92. }
  93.  
  94. void show() const
  95. {
  96. for( const auto* device : devices )
  97. device->show();
  98. }
  99.  
  100. template< typename T >
  101. void set( const T& value ) const;
  102. };
  103. //----------------------------------------------------------------------------------------
  104.  
  105. template< class T >
  106. class SENSOR : public DEVICE
  107. {
  108. protected:
  109. T value;
  110.  
  111. virtual void show() const
  112. {
  113. std::cout << name << std::endl;
  114. std::cout << value << std::endl;
  115. }
  116.  
  117. public:
  118. CSTR SENSOR( DEVICE_MANAGER* owner, const std::string& name )
  119. : DEVICE( name, DEVICE_TYPE::sensor )
  120. {
  121. owner->add( ( DEVICE* )this );
  122. }
  123.  
  124. const T get() const
  125. {
  126. return value;
  127. }
  128.  
  129. void set( const T& rvalue )
  130. {
  131. value = rvalue;
  132. }
  133. };
  134. ///---------------------------------------------------------------------------------------
  135.  
  136. template< typename T >
  137. inline void DEVICE_MANAGER::set( const T& value ) const
  138. {
  139. for( auto* device : devices )
  140. {
  141. if( device->device_type == DEVICE_TYPE::sensor )
  142. ( (SENSOR< T >*)device )->set( value );
  143. }
  144. }
  145. ///---------------------------------------------------------------------------------------
  146.  
  147. class GYRO : public SENSOR< POINT3D >
  148. {
  149. protected:
  150. virtual void refresh()
  151. {
  152. value.x = (float)rand() / rand();
  153. value.y = (float)rand() / rand();
  154. value.z = (float)rand() / rand();
  155. }
  156.  
  157. public:
  158. CSTR GYRO( DEVICE_MANAGER* owner, const int counter )
  159. : SENSOR< POINT3D >( owner,
  160. std::string( "Gyro_" ) + std::to_string( counter ) ) {}
  161. };
  162. ///---------------------------------------------------------------------------------------
  163.  
  164. class ACCE : public SENSOR< POINT3D >
  165. {
  166. protected:
  167. virtual void refresh()
  168. {
  169. value.x = (float)rand();
  170. value.y = (float)rand();
  171. value.z = (float)rand();
  172. }
  173.  
  174. public:
  175. CSTR ACCE( DEVICE_MANAGER* owner, const int counter )
  176. : SENSOR< POINT3D >( owner,
  177. std::string( "Accelero_" ) + std::to_string( counter ) ) {}
  178. };
  179.  
  180. //----------------------------------------------------------------------------------------
  181.  
  182. inline std::string get_version_string( const int_pair& version )
  183. {
  184. return std::to_string( version.first ) + "." + std::to_string( version.second );
  185. }
  186.  
  187. inline std::string get_dimension_string( const int_pair& dimension )
  188. {
  189. return std::to_string( dimension.first ) + "x" + std::to_string( dimension.second );
  190. }
  191.  
  192. class SMART_PHONE
  193. {
  194. protected:
  195. DEVICE_MANAGER* sensor_manager;
  196.  
  197. virtual std::string get_model_name() const = 0;
  198. virtual std::string get_core_type() const = 0;
  199. virtual int get_core_speed_mhz() const = 0;
  200. virtual int get_core_count() const = 0;
  201. virtual int get_ram_size_gb() const = 0;
  202. virtual int get_flash_size_gb() const = 0;
  203. virtual std::string get_ext_memory_type() const { return "None"; }
  204. virtual int get_ext_memory_max_gb() const = 0;
  205. virtual std::string get_os_type() const = 0;
  206. virtual int_pair get_os_version() const = 0;
  207. virtual std::string get_display_type() const = 0;
  208. virtual int_pair get_display_matrix() const = 0;
  209. virtual int get_pixel_density_ppi() const = 0;
  210. virtual std::string get_proof_type() const { return "None"; }
  211. virtual int get_battery_cap_mah() const = 0;
  212. virtual std::string get_removable_battery() const { return "No"; }
  213.  
  214. public:
  215. CSTR SMART_PHONE()
  216. {
  217. sensor_manager = new DEVICE_MANAGER();
  218. }
  219. DSTR ~SMART_PHONE()
  220. {
  221. delete sensor_manager;
  222. }
  223.  
  224. void show_specification() const
  225. {
  226. using namespace std;
  227.  
  228. cout << "Model : " << get_model_name() << endl;
  229. cout << "Processor : " << get_core_speed_mhz() / 1000.f << "GHz "
  230. << get_core_type() << endl;
  231. cout << "Core : " << get_core_count() << endl;
  232. cout << "RAM : " << get_ram_size_gb() << "GB" << endl;
  233.  
  234. cout << "Int. storage : " << get_flash_size_gb() << "GB" << endl;
  235. cout << "Ext. storage : " << get_ext_memory_max_gb() << "GB" << endl;
  236.  
  237. cout << "OS : " << get_os_type() << " "
  238. << get_version_string( get_os_version() ) << endl;
  239. cout << "Display : " << get_display_type() << " ("
  240. << get_dimension_string( get_display_matrix() )
  241. << ")" << endl;
  242. cout << "Pixel density : " << get_pixel_density_ppi() << " PPI" << endl;
  243. cout << "Water/Dust proof : " << get_proof_type() << endl;
  244. cout << "Battery capacity : " << get_battery_cap_mah() << "mAh" << endl;
  245. cout << "Removable battery : " << get_removable_battery() << endl;
  246.  
  247. cout << endl;
  248. }
  249.  
  250. void show_sensor()
  251. {
  252. sensor_manager->show();
  253. }
  254.  
  255. void refresh()
  256. {
  257. sensor_manager->refresh();
  258. }
  259.  
  260. template< typename T >
  261. void set_sensor( const T& value )
  262. {
  263. sensor_manager->set( value );
  264. }
  265. };
  266.  
  267. //---------------------------------------------------------------------------------------
  268.  
  269. class GALAXY_S5 : public SMART_PHONE
  270. {
  271. private:
  272. static int counter;
  273. const int battery_cap;
  274.  
  275. protected:
  276. virtual std::string get_model_name() const
  277. {
  278. return "Samsung Galaxy S5";
  279. }
  280. virtual std::string get_core_type() const
  281. {
  282. return "Snapdragon 801";
  283. }
  284. virtual int get_core_speed_mhz() const
  285. {
  286. return 2500;
  287. }
  288. virtual int get_core_count() const
  289. {
  290. return 4;
  291. }
  292. virtual int get_ram_size_gb() const
  293. {
  294. return 2;
  295. }
  296. virtual int get_flash_size_gb() const
  297. {
  298. return 16;
  299. }
  300. virtual std::string get_ext_memory_type() const
  301. {
  302. return "microSD";
  303. }
  304. virtual int get_ext_memory_max_gb() const
  305. {
  306. return 128;
  307. }
  308. virtual std::string get_os_type() const
  309. {
  310. return "Android";
  311. }
  312. virtual int_pair get_os_version() const
  313. {
  314. return std::make_pair( 4, 4 );
  315. }
  316. virtual std::string get_display_type() const
  317. {
  318. return "5.1\" Super AMOLED Full HD";
  319. }
  320. virtual int_pair get_display_matrix() const
  321. {
  322. return std::make_pair( 1920, 1080 );
  323. }
  324. virtual int get_pixel_density_ppi() const
  325. {
  326. return 432;
  327. }
  328. virtual std::string get_proof_type() const
  329. {
  330. return "IP67";
  331. }
  332. virtual int get_battery_cap_mah() const
  333. {
  334. return battery_cap;
  335. }
  336. virtual std::string get_removable_battery() const
  337. {
  338. return "Yes";
  339. }
  340.  
  341. public:
  342. CSTR GALAXY_S5( const int battery_cap = 2800 )
  343. : battery_cap( battery_cap )
  344. {
  345. new GYRO( sensor_manager, 0 );
  346. new ACCE( sensor_manager, 0 );
  347. }
  348. };
  349.  
  350. int GALAXY_S5::counter = 0;
  351.  
  352. //---------------------------------------------------------------------------------------
  353.  
  354. class HTC_M8 : public SMART_PHONE
  355. {
  356. private:
  357. static int counter;
  358. const int flash_size;
  359.  
  360. protected:
  361. virtual std::string get_model_name() const
  362. {
  363. return "HTC One (M8)";
  364. }
  365. virtual std::string get_core_type() const
  366. {
  367. return "Snapdragon 801";
  368. }
  369. virtual int get_core_speed_mhz() const
  370. {
  371. return 2500;
  372. }
  373. virtual int get_core_count() const
  374. {
  375. return 4;
  376. }
  377. virtual int get_ram_size_gb() const
  378. {
  379. return 2;
  380. }
  381. virtual int get_flash_size_gb() const
  382. {
  383. return flash_size;
  384. }
  385. virtual std::string get_ext_memory_type() const
  386. {
  387. return "microSD (128GB)";
  388. }
  389. virtual int get_ext_memory_max_gb() const
  390. {
  391. return 128;
  392. }
  393. virtual std::string get_os_type() const
  394. {
  395. return "Android";
  396. }
  397. virtual int_pair get_os_version() const
  398. {
  399. return std::make_pair( 4, 4 );
  400. }
  401. virtual std::string get_display_type() const
  402. {
  403. return "5.0\" Super Clear LCD 3 Full HD";
  404. }
  405. virtual int_pair get_display_matrix() const
  406. {
  407. return std::make_pair( 1920, 1080 );
  408. }
  409. virtual int get_pixel_density_ppi() const
  410. {
  411. return 440;
  412. }
  413. virtual int get_battery_cap_mah() const
  414. {
  415. return 2600;
  416. }
  417.  
  418. public:
  419. CSTR HTC_M8( const int flash_size )
  420. : flash_size( flash_size )
  421. {
  422. new GYRO( sensor_manager, 0 );
  423. new ACCE( sensor_manager, 0 );
  424. }
  425. };
  426.  
  427. int HTC_M8::counter = 0;
  428.  
  429. //---------------------------------------------------------------------------------------
  430.  
  431. int main()
  432. {
  433. SMART_PHONE* phones[] { new GALAXY_S5, new HTC_M8( 32 ) };
  434.  
  435. for( const auto* phone : phones )
  436. phone->show_specification();
  437.  
  438. for( auto* phone : phones )
  439. {
  440. phone->refresh();
  441. phone->show_sensor();
  442. phone->set_sensor( POINT3D( 0, 0, 0 ) );
  443. phone->show_sensor();
  444. }
  445.  
  446. return 0;
  447. }
  448. //---------------------------------------------------------------------------------------
  449.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
Model              : Samsung Galaxy S5
Processor          : 2.5GHz Snapdragon 801
Core               : 4
RAM                : 2GB
Int. storage       : 16GB
Ext. storage       : 128GB
OS                 : Android 4.4
Display            : 5.1" Super AMOLED Full HD (1920x1080)
Pixel density      : 432 PPI
Water/Dust proof   : IP67
Battery capacity   : 2800mAh
Removable battery  : Yes

Model              : HTC One (M8)
Processor          : 2.5GHz Snapdragon 801
Core               : 4
RAM                : 2GB
Int. storage       : 32GB
Ext. storage       : 128GB
OS                 : Android 4.4
Display            : 5.0" Super Clear LCD 3 Full HD (1920x1080)
Pixel density      : 440 PPI
Water/Dust proof   : None
Battery capacity   : 2600mAh
Removable battery  : No

Gyro_0
 x: 2.13039
 y: 0.980787
 z: 4.61474

Accelero_0
 x: 7.19885e+08
 y: 1.64976e+09
 z: 5.96517e+08

Gyro_0
 x: 0
 y: 0
 z: 0

Accelero_0
 x: 0
 y: 0
 z: 0

Gyro_0
 x: 1.1604
 y: 1.72395
 z: 0.539157

Accelero_0
 x: 1.96751e+09
 y: 1.36518e+09
 z: 1.54038e+09

Gyro_0
 x: 0
 y: 0
 z: 0

Accelero_0
 x: 0
 y: 0
 z: 0