Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 #include <typeinfo>
     17 #include <cstdio>
     18 
     19 using namespace std;
     20 
     21 class Foo
     22 {
     23 public:
     24   virtual ~Foo() { }
     25   virtual void print()
     26   {
     27     std::printf("in Foo!\n");
     28   }
     29 };
     30 
     31 class Bar: public Foo
     32 {
     33   public:
     34   void print()
     35   {
     36     std::printf("in Bar!\n");
     37   }
     38 };
     39 
     40 struct Base {};
     41 struct Derived : Base {};
     42 struct Poly_Base {virtual void Member(){}};
     43 struct Poly_Derived: Poly_Base {};
     44 
     45 #define CHECK(cond)  \
     46     do { \
     47         if (!(cond)) { \
     48             fprintf(stderr, "KO: Assertion failure: %s\n", #cond); \
     49             fail++;\
     50         }\
     51     } while (0)
     52 
     53 int main()
     54 {
     55     int  fail = 0;
     56     Foo* foo = new Bar();
     57     Bar* bar;
     58 
     59     // built-in types:
     60     int i;
     61     int * pi;
     62 
     63     CHECK(typeid(int) == typeid(i));
     64     CHECK(typeid(int*) == typeid(pi));
     65     CHECK(typeid(int) == typeid(*pi));
     66 
     67     printf("int is: %s\n", typeid(int).name());
     68     printf("  i is: %s\n", typeid(i).name());
     69     printf(" pi is: %s\n", typeid(pi).name());
     70     printf("*pi is: %s\n", typeid(*pi).name());
     71 
     72     // non-polymorphic types:
     73     Derived derived;
     74     Base* pbase = &derived;
     75 
     76     CHECK(typeid(derived) == typeid(Derived));
     77     CHECK(typeid(pbase) == typeid(Base*));
     78     CHECK(typeid(&derived) == typeid(Derived*));
     79 
     80     printf("derived is: %s\n", typeid(derived).name());
     81     printf(" *pbase is: %s\n", typeid(*pbase).name());
     82 
     83     // polymorphic types:
     84     Poly_Derived polyderived;
     85     Poly_Base* ppolybase = &polyderived;
     86 
     87     CHECK(typeid(polyderived) == typeid(Poly_Derived));
     88     CHECK(typeid(ppolybase) == typeid(Poly_Base*));
     89     CHECK(typeid(polyderived) == typeid(*ppolybase));
     90 
     91     printf("polyderived is: %s\n", typeid(polyderived).name());
     92     printf(" *ppolybase is: %s\n", typeid(*ppolybase).name());
     93 
     94     bar = dynamic_cast<Bar*>(foo);
     95     if (bar != NULL) {
     96         printf("OK: 'foo' is pointing to a Bar class instance.\n");
     97     } else {
     98         fprintf(stderr, "KO: Could not dynamically cast 'foo' to a 'Bar*'\n");
     99         fail++;
    100     }
    101 
    102     delete foo;
    103 
    104     return (fail > 0);
    105 }
    106