platform/include/pion/platform/VocabularyConfig.hpp

00001 // ------------------------------------------------------------------------
00002 // Pion is a development platform for building Reactors that process Events
00003 // ------------------------------------------------------------------------
00004 // Copyright (C) 2007-2008 Atomic Labs, Inc.  (http://www.atomiclabs.com)
00005 //
00006 // Pion is free software: you can redistribute it and/or modify it under the
00007 // terms of the GNU Affero General Public License as published by the Free
00008 // Software Foundation, either version 3 of the License, or (at your option)
00009 // any later version.
00010 //
00011 // Pion is distributed in the hope that it will be useful, but WITHOUT ANY
00012 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013 // FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for
00014 // more details.
00015 //
00016 // You should have received a copy of the GNU Affero General Public License
00017 // along with Pion.  If not, see <http://www.gnu.org/licenses/>.
00018 //
00019 
00020 #ifndef __PION_VOCABULARYCONFIG_HEADER__
00021 #define __PION_VOCABULARYCONFIG_HEADER__
00022 
00023 #include <string>
00024 #include <libxml/tree.h>
00025 #include <boost/bind.hpp>
00026 #include <boost/signal.hpp>
00027 #include <pion/PionConfig.hpp>
00028 #include <pion/PionException.hpp>
00029 #include <pion/PionHashMap.hpp>
00030 #include <pion/platform/Vocabulary.hpp>
00031 #include <pion/platform/ConfigManager.hpp>
00032 
00033 
00034 namespace pion {        // begin namespace pion
00035 namespace platform {    // begin namespace platform (Pion Platform Library)
00036 
00037 
00041 class PION_PLATFORM_API VocabularyConfig :
00042     public ConfigManager
00043 {
00044 public:
00045 
00047     class VocabularyIsLockedException : public PionException {
00048     public:
00049         VocabularyIsLockedException(const std::string& vocab_id)
00050             : PionException("Unable to modify a locked Vocabulary: ", vocab_id) {}
00051     };
00052 
00054     class MissingVocabularyException : public PionException {
00055     public:
00056         MissingVocabularyException(const std::string& config_file)
00057             : PionException("Configuration file does not define a Vocabulary: ", config_file) {}
00058     };
00059     
00061     class UpdateVocabularyException : public PionException {
00062     public:
00063         UpdateVocabularyException(const std::string& config_file)
00064             : PionException("Unable to update Vocabulary configuration file: ", config_file) {}
00065     };
00066     
00068     class EmptyVocabularyIdException : public PionException {
00069     public:
00070         EmptyVocabularyIdException(const std::string& config_file)
00071             : PionException("Vocabulary configuration file does not define a unique identifier: ", config_file) {}
00072     };
00073     
00075     class EmptyTypeException : public PionException {
00076     public:
00077         EmptyTypeException(const std::string& term_id)
00078             : PionException("Vocabulary configuration has an empty data type for Term: ", term_id) {}
00079     };
00080     
00082     class InitializeConfigException : public PionException {
00083     public:
00084         InitializeConfigException(const std::string& file_name)
00085             : PionException("Unable to initialize Vocabulary configuration file: ", file_name) {}
00086     };
00087     
00089     class AddTermConfigException : public PionException {
00090     public:
00091         AddTermConfigException(const std::string& term_id)
00092             : PionException("Unable to add a Term to the Vocabulary configuration file: ", term_id) {}
00093     };
00094 
00096     class RemoveTermConfigException : public PionException {
00097     public:
00098         RemoveTermConfigException(const std::string& term_id)
00099             : PionException("Unable to remove a Term from the Vocabulary configuration file: ", term_id) {}
00100     };
00101 
00103     class UpdateTermConfigException : public PionException {
00104     public:
00105         UpdateTermConfigException(const std::string& term_id)
00106             : PionException("Unable to update Term in the Vocabulary configuration file: ", term_id) {}
00107     };
00108     
00109     
00111     VocabularyConfig(void);
00112     
00114     virtual ~VocabularyConfig() {}
00115     
00117     virtual void createConfigFile(void);
00118     
00120     virtual void openConfigFile(void);
00121     
00123     void setId(const std::string& new_id);
00124     
00126     void setName(const std::string& new_name);
00127     
00129     void setComment(const std::string& new_comment);
00130 
00132     void setLocked(bool b);
00133     
00135     void setConfig(const xmlNodePtr config_ptr);
00136     
00142     void addTerm(const Vocabulary::Term& new_term);
00143     
00151     inline void addTerm(const std::string& term_id, const xmlNodePtr config_ptr) {
00152         Vocabulary::Term new_term(term_id);
00153         parseTermConfig(new_term, config_ptr);
00154         addTerm(new_term);
00155     }
00156 
00162     void updateTerm(const Vocabulary::Term& t);
00163 
00171     inline void updateTerm(const std::string& term_id, const xmlNodePtr config_ptr) {
00172         Vocabulary::Term updated_term(term_id);
00173         parseTermConfig(updated_term, config_ptr);
00174         updateTerm(updated_term);
00175     }
00176     
00182     void removeTerm(const std::string& term_id);
00183     
00189     inline void bind(Vocabulary& v) {
00190         m_signal_add_term.connect(boost::bind(&Vocabulary::addTerm, &v, _1));
00191         m_signal_update_term.connect(boost::bind(&Vocabulary::updateTerm, &v, _1));
00192         m_signal_remove_term.connect(boost::bind(&Vocabulary::removeTerm, &v, _1));
00193         v += m_vocabulary;
00194     }
00195 
00197     inline const std::string& getId(void) const { return m_vocabulary_id; }
00198     
00200     inline const std::string& getName(void) const { return m_name; }
00201     
00203     inline const std::string& getComment(void) const { return m_comment; }
00204     
00206     inline bool getLocked(void) const { return m_is_locked; }
00207     
00209     inline const Vocabulary& getVocabulary(void) const { return m_vocabulary; }
00210 
00212     static inline const std::string& getVocabularyElementName(void) {
00213         return VOCABULARY_ELEMENT_NAME;
00214     }
00215     
00224     static xmlNodePtr createVocabularyConfig(const char *buf, std::size_t len) {
00225         return ConfigManager::createResourceConfig(VOCABULARY_ELEMENT_NAME, buf, len);
00226     }
00227     
00236     static xmlNodePtr createTermConfig(const char *buf, std::size_t len) {
00237         return ConfigManager::createResourceConfig(TERM_ELEMENT_NAME, buf, len);
00238     }
00239     
00246     static void writeTermConfigXML(std::ostream& out, const Vocabulary::Term& t);
00247 
00255     static void parseTermConfig(Vocabulary::Term& new_term,
00256                                 const xmlNodePtr config_ptr);
00257     
00258     
00259 private:
00260 
00269     bool addNewTermTypeConfig(xmlNodePtr term_node, const Vocabulary::Term& t);
00270 
00271     
00273     static const std::string        DEFAULT_CONFIG_FILE;
00274 
00276     static const std::string        VOCABULARY_ELEMENT_NAME;
00277 
00279     static const std::string        LOCKED_ELEMENT_NAME;
00280     
00282     static const std::string        TERM_ELEMENT_NAME;
00283     
00285     static const std::string        TYPE_ELEMENT_NAME;
00286 
00288     static const std::string        SIZE_ATTRIBUTE_NAME;    
00289         
00291     static const std::string        FORMAT_ATTRIBUTE_NAME;  
00292 
00293     
00295     xmlNodePtr                      m_vocabulary_node;
00296     
00298     Vocabulary                      m_vocabulary;
00299     
00301     std::string                     m_vocabulary_id;
00302     
00304     std::string                     m_name;
00305     
00307     std::string                     m_comment;
00308     
00310     bool                            m_is_locked;
00311     
00313     boost::signal1<void,const Vocabulary::Term&>                m_signal_add_term;
00314 
00316     boost::signal1<void,const Vocabulary::Term&>                m_signal_update_term;
00317 
00319     boost::signal1<void,const std::string&>                     m_signal_remove_term;
00320 };
00321 
00322 
00323 }   // end namespace platform
00324 }   // end namespace pion
00325 
00326 #endif

Generated on Wed Apr 13 16:38:34 2011 for pion-platform by  doxygen 1.4.7