Sierra Toolkit Version of the Day
Option.hpp
00001 #ifndef STK_UTIL_DIAG_Option_h
00002 #define STK_UTIL_DIAG_Option_h
00003 
00004 #include <map>
00005 
00006 namespace stk {
00007 namespace diag {
00008 
00009 
00010 
00011 typedef unsigned long OptionMask;
00012 
00018 struct OptionDescription
00019 {
00024   virtual ~OptionDescription()
00025   {}
00026 
00037   virtual std::ostream &describe(std::ostream &os) const = 0;
00038 };
00039 
00040 
00047 struct Option : public OptionDescription
00048 {
00053   Option()
00054     : m_name(""),
00055       m_envName(""),
00056       m_description(""),
00057       m_value(""),
00058       m_subOptionDescription(0)
00059   {}
00060 
00081   Option(const std::string &name, const std::string &env_name, const std::string &value = std::string(),
00082          const std::string &description = "No description available",
00083          const OptionDescription *sub_option_description = 0)
00084     : m_name(name),
00085       m_envName(env_name),
00086       m_description(description),
00087       m_value(value),
00088       m_subOptionDescription(sub_option_description)
00089   {}
00090 
00098   Option(const Option &option)
00099     : m_name(option.m_name),
00100       m_envName(option.m_envName),
00101       m_description(option.m_description),
00102       m_value(option.m_value),
00103       m_subOptionDescription(option.m_subOptionDescription)
00104   {}
00105 
00106 
00114   Option &operator=(const Option &option)
00115   {
00116     m_name = option.m_name;
00117     m_envName = option.m_envName;
00118     m_description = option.m_description;
00119     m_value = option.m_value;
00120     m_subOptionDescription = option.m_subOptionDescription;
00121     return *this;
00122   }
00123 
00128   virtual ~Option()
00129   {}
00130 
00131   const std::string &getName() const {
00132     return m_name;
00133   }
00134 
00135   const std::string &getValue() const {
00136     return m_value;
00137   }
00138 
00149   virtual std::ostream &describe(std::ostream &os) const;
00150 
00151   operator std::string &() {
00152     return m_value;
00153   }
00154 
00155   std::string      m_name;      
00156   std::string      m_envName;    
00157   std::string      m_description;    
00158   std::string      m_value;    
00159   const OptionDescription *  m_subOptionDescription;  
00160 };
00161 
00162 
00167 struct OptionMaskName
00168 {
00173   OptionMaskName()
00174     : m_name(""),
00175       m_mask(0),
00176       m_description("")
00177   {}
00178 
00186   OptionMaskName(const std::string &name, const OptionMask &mask, const std::string &description = "No description available")
00187     : m_name(name),
00188       m_mask(mask),
00189       m_description(description)
00190   {}
00191 
00196   virtual ~OptionMaskName()
00197   {}
00198 
00205   bool operator<(const OptionMaskName &o) const {
00206     return m_name < o.m_name;
00207   }
00208 
00215   virtual std::ostream &describe(std::ostream &os) const;
00216 
00217   std::string    m_name;
00218   OptionMask    m_mask;
00219   std::string    m_description;
00220 };
00221 
00222 
00227 class OptionMaskNameMap: public std::map<std::string, OptionMaskName>
00228 {
00229 public:
00237   void mask(const std::string &name, const OptionMask l_mask, const std::string &description) {
00238     iterator it = find(name);
00239     if (it == end())
00240       insert(std::make_pair(name, OptionMaskName(name, l_mask, description)));
00241     else {
00242       (*it).second.m_mask = l_mask;
00243       (*it).second.m_description = description;
00244     }
00245   }
00246 };
00247 
00248 
00258 class OptionMaskParser : public OptionDescription
00259 {
00260 public:
00261   typedef OptionMask Mask;    
00262 
00263 public:
00268   OptionMaskParser()
00269     : m_optionMaskNameMap(),
00270       m_optionMask(0),
00271       m_status(true)
00272   {}
00273 
00278   virtual ~OptionMaskParser()
00279   {}
00280 
00281   const OptionMaskNameMap &getOptionMaskNameMap() const {
00282     return m_optionMaskNameMap;
00283   }
00284 
00292   virtual Mask parse(const char *mask) const;
00293 
00303   virtual void parseArg(const std::string &name, const std::string &arg) const;
00304 
00315   Mask &operator[](const std::string &name) {
00316     return m_optionMaskNameMap[name].m_mask;
00317   }
00318 
00332   void mask(const std::string &name, const Mask l_mask, const std::string &description) {
00333     m_optionMaskNameMap.mask(name, l_mask, description);
00334   }
00335 
00342   std::ostream &describe(std::ostream &os) const;
00343 
00344 protected:
00345   OptionMaskNameMap    m_optionMaskNameMap;  
00346   mutable OptionMask    m_optionMask;    
00347   mutable bool      m_status;    
00348 };
00349 
00350 } // namespace diag
00351 } // namespace stk
00352 
00353 namespace sierra {
00354 typedef stk::diag::OptionMask OptionMask;
00355 typedef stk::diag::OptionDescription OptionDescription;
00356 typedef stk::diag::Option Option;
00357 typedef stk::diag::OptionMaskName OptionMaskName;
00358 typedef stk::diag::OptionMaskNameMap OptionMaskNameMap;
00359 typedef stk::diag::OptionMaskParser OptionMaskParser;
00360 } // namespace sierra
00361 
00362 #endif // STK_UTIL_DIAG_Option_h
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines