COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libcoopy_core/Property.cpp
Go to the documentation of this file.
00001 #include <coopy/Property.h>
00002 #include <stdlib.h>
00003 
00004 using namespace coopy::store;
00005 using namespace std;
00006 
00007 namespace coopy {
00008   namespace store {
00009     class IntValue;
00010     class BooleanValue;
00011     class StringValue;
00012   }
00013 }
00014 
00015 Property Property::nullProperty;
00016 
00017 Property& Value::asMap() {
00018   return Property::getNullProperty();
00019 }
00020 
00021 const Property& Value::asMap() const {
00022   return Property::getNullProperty();
00023 }
00024 
00025 class coopy::store::IntValue : public Value {
00026 public:
00027   int x;
00028   IntValue(int x): x(x) {}
00029 
00030   virtual bool isInt() const { return true; }
00031   virtual bool isNull() const { return false; }
00032   virtual int asInt() const { return x; }  
00033   virtual bool asBoolean() const { return (x!=0); }  
00034 
00035   virtual std::string toString() const {
00036     char buf[256];
00037     sprintf(buf,"%d",x);
00038     return buf;
00039   }
00040 };
00041 
00042 class coopy::store::BooleanValue : public Value {
00043 public:
00044 
00045   bool x;
00046   BooleanValue(bool x): x(x) {}
00047 
00048   virtual bool isBoolean() const { return true; }
00049   virtual bool isNull() const { return false; }
00050   virtual int asInt() const { return x?1:0; }  
00051   virtual bool asBoolean() const { return x; }  
00052 
00053   virtual std::string toString() const {
00054     return x?"True":"False";
00055   }
00056 };
00057 
00058 class coopy::store::StringValue : public Value {
00059 public:
00060   string x;
00061   StringValue(string x): x(x) {}
00062 
00063   virtual bool isString() const { return true; }
00064   virtual bool isNull() const { return false; }
00065   virtual std::string asString() const { return x; }  
00066   virtual int asInt() const { 
00067     if (x=="true") return 1;
00068     if (x=="True") return 1;
00069     if (x=="TRUE") return 1;
00070     return atoi(x.c_str());
00071   }  
00072 
00073   virtual bool asBoolean() const { 
00074     return asInt()!=0;
00075   }
00076 
00077   virtual std::string toString() const {
00078     return "[" + x + "]";
00079   }
00080 };
00081 
00082 bool PolyValue::setInt(int x) {
00083   setNull();
00084   value = new IntValue(x);
00085   if (value!=NULL) value->addReference();
00086   return value!=NULL;
00087 }
00088 
00089 bool PolyValue::setBoolean(bool x) {
00090   setNull();
00091   value = new BooleanValue(x);
00092   if (value!=NULL) value->addReference();
00093   return value!=NULL;
00094 }
00095 
00096 bool PolyValue::setString(const char *str) {
00097   setNull();
00098   value = new StringValue(str);
00099   if (value!=NULL) value->addReference();
00100   return value!=NULL;
00101 }
00102 
00103 bool PolyValue::setString(const std::string& str) {
00104   setNull();
00105   value = new StringValue(str);
00106   if (value!=NULL) value->addReference();
00107   return value!=NULL;
00108 }
00109 
00110 bool PolyValue::setNull() {
00111   clear();
00112   return true;
00113 }
00114 
00115 bool PolyValue::setMap() {
00116   setNull();
00117   value = new Property();
00118   if (value!=NULL) value->addReference();
00119   return value!=NULL;
00120 }
00121 
00122 std::string Property::toString() const {
00123   std::string result = "";
00124   for (std::map<std::string,PolyValue>::const_iterator it = data.begin();
00125        it != data.end(); 
00126        it++) {
00127     result += "(";
00128     result += it->first;
00129     result += " ";
00130     result += it->second.toString();
00131     result += ") ";
00132   }
00133   return result;
00134 }
00135 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines