Changeset 566
- Timestamp:
- 06/23/08 15:54:22 (5 months ago)
- Files:
-
- trunk/rphp/runtime/var-test.cpp (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/rphp/runtime/var-test.cpp
r562 r566 15 15 public: 16 16 php_hash(int xvar) : x(xvar) { } 17 17 18 18 php_hash(const php_hash& h) { 19 19 x = h.x; … … 31 31 typedef boost::variant< int, std::string, double, php_hash> pvar; 32 32 33 typedef enum { PVAR_INT, PVAR_DOUBLE, PVAR_STRING, PVAR_HASH } pvartype; 34 33 35 class my_visitor : public boost::static_visitor<int> 34 36 { … … 39 41 return i; 40 42 } 41 43 42 44 int operator()(double i) const 43 45 { … … 45 47 return 0; 46 48 } 47 49 48 50 int operator()(const std::string & str) const 49 51 { … … 51 53 return str.length(); 52 54 } 53 55 54 56 int operator()(const php_hash & h) const 55 57 { … … 57 59 return h.getx(); 58 60 } 59 61 60 62 }; 63 64 class type_checker : public boost::static_visitor<int> 65 { 66 public: 67 pvartype operator()(int i) const 68 { 69 return PVAR_INT; 70 } 71 72 pvartype operator()(double i) const 73 { 74 return PVAR_DOUBLE; 75 } 76 77 pvartype operator()(const std::string & str) const 78 { 79 return PVAR_STRING; 80 } 81 82 pvartype operator()(const php_hash & h) const 83 { 84 return PVAR_HASH; 85 } 86 87 }; 88 61 89 62 90 // driver … … 65 93 pvar u; 66 94 u = "hello world there"; 67 68 std::cout << u << std::endl; 95 96 std::cout << u << std::endl; 69 97 int result = boost::apply_visitor( my_visitor(), u ); 70 98 71 99 u = 15; 72 100 73 101 std::cout << u << std::endl; 74 102 result = boost::apply_visitor( my_visitor(), u ); 75 103 76 104 u = 2.3123; 77 105 78 106 std::cout << u << std::endl; 79 107 result = boost::apply_visitor( my_visitor(), u ); 108 109 // type checking? 110 int pt = boost::apply_visitor( type_checker(), u ); 111 switch (pt) { 112 case PVAR_STRING: 113 std::cout << "found a string" << std::endl; 114 break; 115 case PVAR_DOUBLE: 116 std::cout << "found a float" << std::endl; 117 break; 118 default: 119 std::cout << "woops, what type was it?" << std::endl; 120 } 80 121 81 122 php_hash h(5); … … 83 124 u = h; 84 125 std::cout << u << std::endl; 85 126 86 127 result = boost::apply_visitor( my_visitor(), u ); 87 128 88 129 //std::cout << result << std::endl; // output: 11 (i.e., length of "hello world") 89 130 90 131 }
