Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

Properties.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00019 /*
00020  * $Log: Properties.h,v $
00021  * Revision 1.7  2007/04/24 01:24:32  n-ando
00022  * createNode() was added.
00023  *
00024  * Revision 1.6  2007/04/23 04:56:43  n-ando
00025  * Some fixes about const.
00026  *
00027  * Revision 1.5  2007/04/13 18:14:07  n-ando
00028  * Now Properties class is implemented as hierarchical data structure
00029  * instead of std::map based data structure.
00030  *
00031  * Revision 1.4  2007/01/06 17:59:23  n-ando
00032  * getProperty() is now const function.
00033  *
00034  * Revision 1.3  2006/10/17 10:10:21  n-ando
00035  * Some escape/unescape related bugs are fixed.
00036  * The following some functions were moved to StringUtil.h
00037  * - eraseHeadBlank(), eraseTailBlank(), replaceString()
00038  *
00039  * Revision 1.2  2006/10/13 03:50:00  n-ando
00040  * The bug that white space delimiter did not work properly was fixed.
00041  *
00042  * Revision 1.1  2006/09/11 18:09:57  n-ando
00043  * Java compatible properties class.
00044  *
00045  *
00046  */
00047 
00048 #ifndef Properties_h
00049 #define Properties_h
00050 
00051 
00052 #include <string>
00053 #include <vector>
00054 #include <map>
00055 
00056 
00057 namespace RTC
00058 {
00115   class Properties
00116   {
00117   public:
00134     Properties(const char* key = "", const char* value = "");
00135     
00136     
00153     Properties(std::map<std::string, std::string>& defaults);
00154     
00155     
00202     Properties(const char* defaults[], long num = LONG_MAX);
00203     
00204     
00221     Properties(const Properties& prop);
00222 
00223 
00239     Properties& operator=(const Properties& prop);
00240     
00241 
00253     virtual ~Properties();
00254     
00255 
00256     //============================================================
00257     // public functions
00258     //============================================================
00259     
00260 
00261     inline const char* getName() const          {return name.c_str();}
00262     inline const char* getVlue() const         {return value.c_str();}
00263     inline const char* getDefaultValue() const {return default_value.c_str();}
00264     inline const std::vector<Properties*>& getLeaf() const {return leaf;}
00265     inline const Properties* getRoot() const    {return root;}
00266 
00294     const std::string& getProperty(const std::string& key) const;
00295     
00296 
00326     const std::string& getProperty(const std::string& key,
00327                              const std::string& def) const;
00328 
00356     const std::string& operator[](const std::string& key) const;
00357 
00358 
00388     std::string& operator[](const std::string& key);
00389 
00390     
00398     const std::string& getDefault(const std::string& key) const;
00399 
00400     
00427     std::string setProperty(const std::string& key, const std::string& value);
00428 
00429 
00437     std::string setDefault(const std::string& key, const std::string& value);
00438     
00439 
00447     void setDefaults(const char* defaults[], long num = LONG_MAX);
00448 
00449     
00450     //============================================================
00451     // load and save functions
00452     //============================================================
00474     void list(std::ostream& out);
00475     
00476     
00635     void load(std::istream& inStream);
00636     
00637     
00661     void save(std::ostream& out, const std::string& header);
00662     
00663     
00744     void store(std::ostream& out, const std::string& header);
00745 
00746     
00747     //============================================================
00748     // other util functions
00749     //============================================================
00775     std::vector<std::string> propertyNames() const;
00776 
00777 
00785     int size() const;
00786 
00787 
00795     Properties* getNode(const std::string& key) const;
00796     bool createNode(const char* key)
00797     {
00798       Properties* p(getNode(key));
00799       if (p != NULL) return false;
00800       (*this)[key] = "";
00801       return true;
00802     }
00803 
00811     Properties* removeNode(const char* leaf_name);
00812 
00813 
00821     Properties* hasKey(const char* key) const;
00822 
00823 
00831     void clear();
00832 
00833 
00841     Properties& operator<<(const Properties& prop);
00842     
00843 
00844   protected:
00845     static void splitKeyValue(const std::string& str, std::string& key,
00846                               std::string& value);
00847 
00848     static bool split(const std::string& str, const char delim,
00849                       std::vector<std::string>& value);
00850 
00851     static Properties* _getNode(std::vector<std::string>& keys,
00852                                 std::vector<Properties*>::size_type index,
00853                                 const Properties* curr);
00854 
00855     static void _propertiyNames(std::vector<std::string>& names,
00856                                 std::string curr_name,
00857                                 const Properties* curr);
00858 
00859     static void _store(std::ostream& out, std::string curr_name,
00860                        Properties* curr);
00861 
00862     static std::ostream& _dump(std::ostream& out, const Properties& curr,
00863                                int index);
00864 
00865     static std::string indent(int index);
00866     
00867   private:
00868     std::string name;
00869     std::string value;
00870     std::string default_value;
00871     Properties* root;
00872     std::vector<Properties*> leaf;
00873     const std::string m_empty;
00874     friend std::ostream& operator<<(std::ostream& lhs, const Properties& rhs);
00875 
00876   };   // class Properties
00877 };     // namespace RTC  
00878 #endif // Properties_h
00879   

Generated on Fri Oct 5 05:16:00 2007 for OpenRTM by  doxygen 1.4.1