1 /*------------------------------------------------------------------------------
  2 Name:      Prop.h
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Holding data for a property
  6 Version:   $Id: Prop.h 12937 2004-11-24 20:15:11Z ruff $
  7 ------------------------------------------------------------------------------*/
  8 
  9 /**
 10  * Helper class to hold properties.
 11  * In some cases it is needed to know how a certain property has been defined. For example if it has been
 12  * expressively set by the user at the command line, in the configuration file, or if it is the hardcoded 
 13  * default. 
 14  * @author xmlBlaster@marcelruff.info
 15  * @author laghi@swissinfo.org
 16  */
 17 
 18 #ifndef _UTIL_PROP_H
 19 #define _UTIL_PROP_H
 20 
 21 #include <util/Property.h>
 22 
 23 
 24 
 25 namespace org { namespace xmlBlaster { namespace util {
 26 
 27 enum prop_enum {
 28    CREATED_BY_DEFAULT,
 29    CREATED_BY_JVMENV,
 30    CREATED_BY_PROPFILE,
 31    CREATED_BY_CMDLINE,
 32    CREATED_BY_SETTER
 33 };
 34 
 35 typedef enum prop_enum PropEnum;
 36 
 37 template <class T> class Dll_Export Prop
 38 {
 39 private:
 40    T        value_;
 41    PropEnum origin_;
 42 
 43 public:
 44    
 45    Prop(const T& value=T(), PropEnum origin=CREATED_BY_DEFAULT)
 46    {
 47       value_  = value;
 48       origin_ = origin;
 49    }
 50 
 51    Prop(const Prop& prop) 
 52    {
 53       value_ = prop.value_;
 54       origin_ = prop.origin_;
 55    }
 56 
 57    Prop& operator =(const Prop& prop) 
 58    {
 59       value_ = prop.value_;
 60       origin_ = prop.origin_;
 61       return *this;
 62    }
 63 
 64    bool setValue(Property& prop, const std::string& propName)
 65    {
 66       if (origin_ >= CREATED_BY_SETTER) return false;
 67       if (prop.getTypedProperty(propName, value_, false)) {
 68          origin_ = CREATED_BY_CMDLINE;
 69          return true;
 70       }
 71       if (origin_ > CREATED_BY_JVMENV) return false;
 72       if (prop.getTypedProperty(propName, value_, true)) return true;
 73       return false;
 74    }
 75 
 76    bool setValue(const T& value, PropEnum origin=CREATED_BY_DEFAULT)
 77    {
 78       if (origin < origin_) return false;
 79       origin_ = origin;
 80       value_ = value;
 81       return true;
 82    }
 83 
 84    PropEnum getOrigin() const
 85    {
 86       return origin_;
 87    }
 88   
 89    T getValue() const 
 90    {
 91       return value_;
 92    }
 93 
 94    /**
 95     * Is unmanipulated default value?
 96     */
 97    const bool isModified() const
 98    {
 99       return origin_ != CREATED_BY_DEFAULT;
100    }
101 
102    /**
103     * writes out the prop as an xml. If forceWrite is set to 'true', then it is always written out, otherwise
104     * it is only written out when different from the default. If it is the default, an empty std::string is
105     * returned.
106     */ 
107 /*
108    toXml(const std::string&  name, const std::string& offset="", bool forceWrite=false)
109    {
110       if (origin_ > CREATED_BY_DEFAULT || forceWrite) {
111          return "<" + name + ">" +  .... (not finished yet);
112       }
113    }
114 */   
115 };
116 
117 }}} // namespaces
118 
119 #endif


syntax highlighted by Code2HTML, v. 0.9.1