platform/include/pion/platform/Vocabulary.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_VOCABULARY_HEADER__
00021 #define __PION_VOCABULARY_HEADER__
00022 
00023 #include <string>
00024 #include <vector>
00025 #include <boost/shared_ptr.hpp>
00026 #include <pion/PionConfig.hpp>
00027 #include <pion/PionException.hpp>
00028 #include <pion/PionHashMap.hpp>
00029 
00030 
00031 namespace pion {        // begin namespace pion
00032 namespace platform {    // begin namespace platform (Pion Platform Library)
00033 
00034 
00038 class PION_PLATFORM_API Vocabulary
00039 {
00040 public:
00041 
00043     typedef unsigned long       TermRef;
00044     
00046     static const TermRef        UNDEFINED_TERM_REF;
00047     
00049     enum DataType {
00050         TYPE_NULL = 0,          
00051         TYPE_INT8,              
00052         TYPE_UINT8,             
00053         TYPE_INT16,             
00054         TYPE_UINT16,            
00055         TYPE_INT32,             
00056         TYPE_UINT32,            
00057         TYPE_INT64,             
00058         TYPE_UINT64,            
00059         TYPE_FLOAT,             
00060         TYPE_DOUBLE,            
00061         TYPE_LONG_DOUBLE,       
00062         TYPE_SHORT_STRING,      
00063         TYPE_STRING,            
00064         TYPE_LONG_STRING,       
00065         TYPE_DATE_TIME,         
00066         TYPE_DATE,              
00067         TYPE_TIME,              
00068         TYPE_CHAR,              
00069         TYPE_BLOB,              
00070         TYPE_ZBLOB,             
00071         TYPE_OBJECT             
00072     };
00073     
00075     struct Term {
00077         Term(void)
00078             : term_ref(UNDEFINED_TERM_REF),
00079             term_type(TYPE_NULL), term_size(0)
00080             {}
00082         Term(const std::string& id)
00083             : term_id(id), term_ref(UNDEFINED_TERM_REF),
00084             term_type(TYPE_NULL), term_size(0)
00085             {}
00087         Term(const Term& t)
00088             : term_id(t.term_id), term_ref(t.term_ref),
00089             term_comment(t.term_comment), term_type(t.term_type),
00090             term_size(t.term_size), term_format(t.term_format)
00091             {}
00093         inline Term& operator=(const Term& t) {
00094             term_id = t.term_id;
00095             term_ref = t.term_ref;
00096             term_comment = t.term_comment;
00097             term_type = t.term_type;
00098             term_size = t.term_size;
00099             term_format = t.term_format;
00100             return *this;
00101         }
00103         std::string             term_id;
00105         TermRef                 term_ref;
00107         std::string             term_comment;
00109         DataType                term_type;
00111         size_t                  term_size;
00113         std::string             term_format;
00114     };
00115     
00117     typedef boost::shared_ptr<Term>     TermPtr;
00118     
00120     class EmptyTermIdException : public std::exception {
00121     public:
00122         virtual const char* what() const throw() {
00123             return "Tried adding a vocabulary term with an empty identifier";
00124         }
00125     };
00126     
00128     class UnknownDataTypeException : public PionException {
00129     public:
00130         UnknownDataTypeException(const std::string& data_type)
00131             : PionException("Could not parse unknown data type: ", data_type) {}
00132     };
00133 
00135     class DuplicateTermException : public PionException {
00136     public:
00137         DuplicateTermException(const std::string& term_id)
00138             : PionException("Tried adding a duplicate term to the Vocabulary: ", term_id) {}
00139     };
00140     
00142     class TermNotFoundException : public PionException {
00143     public:
00144         TermNotFoundException(const std::string& term_id)
00145             : PionException("Unable to find Term identifier: ", term_id) {}
00146     };
00147 
00149     class TermNoLongerDefinedException : public PionException {
00150     public:
00151         TermNoLongerDefinedException(const std::string& term_id)
00152             : PionException("Term was removed from Vocabulary while still in-use: ", term_id) {}
00153     };
00154 
00155     
00157     ~Vocabulary() {}
00158         
00160     Vocabulary(void);
00161     
00163     Vocabulary(const Vocabulary& v);
00164     
00166     inline size_t size(void) const { return m_num_terms; }  
00167     
00174     inline const Term& operator[](const TermRef& term_ref) const {
00175         PION_ASSERT(term_ref <= m_num_terms);
00176         return *(m_ref_map[term_ref]);
00177     }
00178     
00185     inline TermRef findTerm(const std::string& term_id) const {
00186         TermRef term_ref = UNDEFINED_TERM_REF;
00187         TermStringMap::const_iterator i = m_uri_map.find(term_id);
00188         if (i != m_uri_map.end())
00189             term_ref = i->second->term_ref;
00190         return term_ref;
00191     }
00192 
00199     inline const Term& findTerm(const TermRef& term_ref) const {
00200         return *m_ref_map[(term_ref > m_num_terms ? UNDEFINED_TERM_REF : term_ref)];
00201     }
00202 
00209     TermRef addTerm(const Term& t);
00210     
00216     void removeTerm(const std::string& term_id);
00217     
00223     void updateTerm(const Term& t);
00224 
00230     void refreshTerm(Term& t) const;
00231     
00239     const Vocabulary& operator+=(const Vocabulary& v);
00240     
00247     static DataType parseDataType(std::string str);
00248     
00255     static std::string getDataTypeAsString(const DataType type);
00256     
00257     
00258 private:
00259 
00261     Vocabulary& operator=(const Vocabulary& v);
00262 
00263 
00265     typedef PION_HASH_MAP<std::string, TermPtr, PION_HASH_STRING >  TermStringMap;
00266     
00268     typedef std::vector<TermPtr>                    TermRefMap;
00269     
00270     
00272     TermRefMap                      m_ref_map;
00273 
00275     TermStringMap                   m_uri_map;
00276     
00278     TermRef                         m_num_terms;
00279 };
00280 
00281 
00283 typedef boost::shared_ptr<Vocabulary>   VocabularyPtr;
00284 
00285 
00286 }   // end namespace platform
00287 }   // end namespace pion
00288 
00289 #endif

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