首页 > 代码库 > Variants

Variants

  1 ////Variants.cpp  2   3 #include "vcf/FoundationKit/FoundationKit.h"  4 #include "vcf/FoundationKit/Dictionary.h"  5 using namespace VCF;  6   7   8   9  10 void printVariant( VariantData& v ) 11 { 12     System::print( Format("As string: {%s},  type: %d\n") % v.toString() % v.type); 13 } 14  15 void testConstructors() 16 { 17     VariantData v1("Hello"); 18     VCF_ASSERT( v1.type == pdString ); 19     printVariant(v1); 20  21     VariantData v2(L"Hello"); 22     VCF_ASSERT( v2.type == pdString ); 23     printVariant(v2); 24  25     VariantData v3(String("Hello")); 26     VCF_ASSERT( v3.type == pdString ); 27     printVariant(v3); 28  29     VariantData v4(10); 30     VCF_ASSERT( v4.type == pdInt ); 31     printVariant(v4); 32  33     VariantData v5((long)90005); 34     VCF_ASSERT( v5.type == pdLong ); 35     printVariant(v5); 36  37     VariantData v6(-9859405); 38     VCF_ASSERT( v6.type == pdInt ); 39     printVariant(v6); 40  41     VariantData v7 = (VCF::ushort)123; 42     VCF_ASSERT( v7.type == pdUShort ); 43     printVariant(v7); 44  45     VariantData v8 = (uint32)15997823; 46     VCF_ASSERT( v8.type == pdUInt ); 47     printVariant(v8); 48  49  50     VariantData v9 = 2332.003893112; 51     VCF_ASSERT( v9.type == pdDouble ); 52     printVariant(v9); 53  54     VariantData v10 = 1.0893112f; 55     VCF_ASSERT( v10.type == pdFloat ); 56     printVariant(v10); 57  58      59     VariantData v11 = true; 60     VCF_ASSERT( v11.type == pdBool ); 61     printVariant(v11); 62  63     VariantData v12 = (int64)8797852412; 64     VCF_ASSERT( v12.type == pdInt64 ); 65     printVariant(v12); 66  67     VariantData v13 = (uint64)8797852413; 68     VCF_ASSERT( v13.type == pdUInt64 ); 69     printVariant(v13); 70  71     VariantData v14 = DateTime::now(); 72     VCF_ASSERT( v14.type == pdDateTime ); 73     printVariant(v14); 74  75     DateTime dt( 1998, 8, 1, 16, 30, 0 ); 76     VariantData v15 = DateTime::now() - dt; 77     VCF_ASSERT( v15.type == pdDateTimeSpan ); 78     printVariant(v15); 79  80     dt.setMilliseconds( 0 ); 81     VariantData v16 = dt; 82     VCF_ASSERT( v16.type == pdDateTime ); 83     printVariant(v16); 84  85  86  87     char tmp[256]; 88     void* tmpPtr = tmp; 89  90     VariantData v17 = tmpPtr; 91     VCF_ASSERT( v17.type == pdVoidPointer ); 92      93     String sp = v17.toString(); 94     v17.setFromString(sp); 95  96  97     printVariant(v17);     98 } 99 100 101 void testConversions() 102 {103     VariantData v1;104     VariantData v2;105 106     v1 = "Stuff";107     printVariant(v1);108 109     v1 = 1002;110     printVariant(v1);111 112     v2 = v1;113     printVariant(v2);114 115 116     //note we don‘t support internal conversion from one117     //type to another - v1 is an int, but won‘t *yet*118     //convert automatically to a double.119     //Should it?120     double d = v1;121 122     //v2 now sees the gibberish double value123     v2 = d;124     printVariant(v2);125 126     d = 1001.223211;127     v2 = d;128     printVariant(v2);129 130     v1 = false;131     printVariant(v1);132 133     //note what currently happens. 134     //VariantData::setFromString expects the 135     //string value to be in the same existing136     //format as the variant is currently in.137     //v1 is a currently holding a bool value type138     //(pdBool), and the string that is returned139     //from v2‘s call to toString() is that of 140     //a double (pdDouble). So the boolean string141     //parsing of a double string is nonsensical142     try {143         v1.setFromString( v2.toString() );144     }145     catch ( std::exception& e ) {146         System::println( String("Error: \n") + e.what() );147     }148 149     printVariant(v1);150 }151 152 153 void testCompares()154 {155     VariantData v1(true);156     VariantData v2;157 158     v2 = true;159     160     VCF_ASSERT( v1 == v2 );161 }162 163 int main( int argc, char** argv ){164 165     FoundationKit::init( argc, argv );166     167     try {168         testConstructors();169         System::println( "testConstructors completed OK." );170 171         testConversions();172         System::println( "testConversions completed OK." );173 174         testCompares();175         System::println( "testCompares completed OK." );176 177     }178     catch ( BasicException& e ) {179         System::println( String("Error: \n") + e.getMessage() );180     }181     catch ( std::exception& e ) {182         System::println( String("Error: \n") + e.what() );183     }184 185     FoundationKit::terminate();186     return 0;187 }

 

Variants