Properties.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00020 #ifndef Properties_h
00021 #define Properties_h
00022 
00023 
00024 #include <string>
00025 #include <vector>
00026 #include <map>
00027 
00028 
00029 namespace RTC
00030 {
00085   class Properties
00086   {
00087   public:
00111     Properties(const char* key = "", const char* value = "");
00112     
00134     Properties(std::map<std::string, std::string>& defaults);
00135     
00194     Properties(const char* defaults[], long num = LONG_MAX);
00195     
00212     Properties(const Properties& prop);
00213     
00231     Properties& operator=(const Properties& prop);
00232     
00244     virtual ~Properties();
00245     
00246     //============================================================
00247     // public functions
00248     //============================================================
00249     
00267     inline const char* getName() const          {return name.c_str();}
00268     
00286     inline const char* getValue() const         {return value.c_str();}
00287     
00305     inline const char* getDefaultValue() const {return default_value.c_str();}
00306     
00324     inline const std::vector<Properties*>& getLeaf() const {return leaf;}
00325     
00343     inline const Properties* getRoot() const    {return root;}
00344     
00374     const std::string& getProperty(const std::string& key) const;
00375     
00404     const std::string& getProperty(const std::string& key,
00405                                    const std::string& def) const;
00406     
00435     const std::string& operator[](const std::string& key) const;
00436     
00469     std::string& operator[](const std::string& key);
00470     
00495     const std::string& getDefault(const std::string& key) const;
00496     
00525     std::string setProperty(const std::string& key, const std::string& value);
00526     
00550     std::string setDefault(const std::string& key, const std::string& value);
00551     
00579     void setDefaults(const char* defaults[], long num = LONG_MAX);
00580     
00581     //============================================================
00582     // load and save functions
00583     //============================================================
00605     void list(std::ostream& out);
00606     
00733     void load(std::istream& inStream);
00734     
00760     void save(std::ostream& out, const std::string& header);
00761     
00844     void store(std::ostream& out, const std::string& header);
00845     
00846     //============================================================
00847     // other util functions
00848     //============================================================
00874     std::vector<std::string> propertyNames() const;
00875     
00893     int size() const;
00894     
00916     Properties* getNode(const std::string& key) const;
00917     
00944     bool createNode(const char* key)
00945     {
00946       Properties* p(getNode(key));
00947       if (p != NULL) return false;
00948       (*this)[key] = "";
00949       return true;
00950     }
00951     
00975     Properties* removeNode(const char* leaf_name);
00976     
01000     Properties* hasKey(const char* key) const;
01001     
01009     void clear();
01010     
01032     Properties& operator<<(const Properties& prop);
01033     
01034   protected:
01071     static void splitKeyValue(const std::string& str, std::string& key,
01072                               std::string& value);
01073     
01105     static bool split(const std::string& str, const char delim,
01106                       std::vector<std::string>& value);
01107     
01140     static Properties* _getNode(std::vector<std::string>& keys,
01141                                 std::vector<Properties*>::size_type index,
01142                                 const Properties* curr);
01143     
01165     static void _propertiyNames(std::vector<std::string>& names,
01166                                 std::string curr_name,
01167                                 const Properties* curr);
01168     
01190     static void _store(std::ostream& out, std::string curr_name,
01191                        Properties* curr);
01192     
01220     static std::ostream& _dump(std::ostream& out, const Properties& curr,
01221                                int index);
01222     
01246     static std::string indent(int index);
01247     
01248   private:
01249     std::string name;
01250     std::string value;
01251     std::string default_value;
01252     Properties* root;
01253     std::vector<Properties*> leaf;
01254     const std::string m_empty;
01255     friend std::ostream& operator<<(std::ostream& lhs, const Properties& rhs);
01256   };   // class Properties
01257 };     // namespace RTC  
01258 #endif // Properties_h
01259 

Generated on Thu May 29 15:03:33 2008 for OpenRTM by  doxygen 1.5.3