Changeset 566

Show
Ignore:
Timestamp:
06/23/08 15:54:22 (5 months ago)
Author:
weyrick
Message:

possible way to check types at runtime

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/rphp/runtime/var-test.cpp

    r562 r566  
    1515    public: 
    1616        php_hash(int xvar) : x(xvar) { } 
    17          
     17 
    1818        php_hash(const php_hash& h) { 
    1919            x = h.x; 
     
    3131typedef boost::variant< int, std::string, double, php_hash> pvar; 
    3232 
     33typedef enum { PVAR_INT, PVAR_DOUBLE, PVAR_STRING, PVAR_HASH } pvartype; 
     34 
    3335class my_visitor : public boost::static_visitor<int> 
    3436{ 
     
    3941        return i; 
    4042    } 
    41      
     43 
    4244    int operator()(double i) const 
    4345    { 
     
    4547        return 0; 
    4648    } 
    47      
     49 
    4850    int operator()(const std::string & str) const 
    4951    { 
     
    5153        return str.length(); 
    5254    } 
    53      
     55 
    5456    int operator()(const php_hash & h) const 
    5557    { 
     
    5759        return h.getx(); 
    5860    } 
    59      
     61 
    6062}; 
     63 
     64class type_checker : public boost::static_visitor<int> 
     65{ 
     66public: 
     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 
    6189 
    6290// driver 
     
    6593    pvar u; 
    6694    u = "hello world there"; 
    67      
    68     std::cout << u << std::endl;  
     95 
     96    std::cout << u << std::endl; 
    6997    int result = boost::apply_visitor( my_visitor(), u ); 
    7098 
    7199    u = 15; 
    72      
     100 
    73101    std::cout << u << std::endl; 
    74102    result = boost::apply_visitor( my_visitor(), u ); 
    75      
     103 
    76104    u = 2.3123; 
    77      
     105 
    78106    std::cout << u << std::endl; 
    79107    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    } 
    80121 
    81122    php_hash h(5); 
     
    83124    u = h; 
    84125    std::cout << u << std::endl; 
    85      
     126 
    86127    result = boost::apply_visitor( my_visitor(), u ); 
    87      
     128 
    88129    //std::cout << result << std::endl; // output: 11 (i.e., length of "hello world") 
    89      
     130 
    90131}