platform/include/pion/platform/Codec.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_CODEC_HEADER__
00021 #define __PION_CODEC_HEADER__
00022 
00023 #include <istream>
00024 #include <ostream>
00025 #include <libxml/tree.h>
00026 #include <boost/shared_ptr.hpp>
00027 #include <pion/PionConfig.hpp>
00028 #include <pion/PionException.hpp>
00029 #include <pion/platform/Event.hpp>
00030 #include <pion/platform/Vocabulary.hpp>
00031 #include <pion/platform/PlatformPlugin.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 Codec
00042     : public PlatformPlugin
00043 {
00044 public:
00045 
00047     class EmptyEventException : public PionException {
00048     public:
00049         EmptyEventException(const std::string& codec_id)
00050             : PionException("Codec configuration does not define an event type: ", codec_id) {}
00051     };
00052 
00054     class UnknownTermException : public PionException {
00055     public:
00056         UnknownTermException(const std::string& event_type)
00057             : PionException("Codec configuration references an unknown event type: ", event_type) {}
00058     };
00059 
00061     class NotAnObjectException : public PionException {
00062     public:
00063         NotAnObjectException(const std::string& event_type)
00064             : PionException("Codec configuration defines a non-object event type: ", event_type) {}
00065     };
00066 
00068     class WrongEventTypeException : public PionException {
00069     public:
00070         WrongEventTypeException(void)
00071             : PionException("Event is not of the expected type") {}
00072     };
00073 
00075     Codec(void) {}
00076     
00078     virtual ~Codec() {}
00079     
00081     virtual const std::string& getContentType(void) const = 0;
00082     
00088     virtual boost::shared_ptr<Codec> clone(void) const = 0;
00089 
00096     virtual void write(std::ostream& out, const Event& e) = 0;
00097 
00104     virtual void finish(std::ostream& out) = 0;
00105 
00113     virtual bool read(std::istream& in, Event& e) = 0;
00114 
00122     virtual void setConfig(const Vocabulary& v, const xmlNodePtr config_ptr);
00123     
00130     virtual void updateVocabulary(const Vocabulary& v);
00131 
00140     inline EventPtr read(std::istream& in, EventFactory& f) {
00141         EventPtr event_ptr(f.create(getEventType()));
00142         if (! read(in, *event_ptr))
00143             event_ptr.reset();
00144         return event_ptr;
00145     }
00146     
00148     inline Event::EventType getEventType(void) const { return m_event_term.term_ref; }
00149 
00150     
00151 protected:
00152     
00154     inline void copyCodec(const Codec& c) {
00155         copyPlugin(c);
00156         m_event_term = c.m_event_term;
00157     }
00158 
00159     
00160 private:
00161     
00163     static const std::string        EVENT_ELEMENT_NAME;
00164 
00165     
00167     Vocabulary::Term                m_event_term;
00168 };
00169 
00170 
00172 typedef boost::shared_ptr<Codec>    CodecPtr;
00173     
00174     
00175 //
00176 // The following symbols must be defined for any Codecs that you would
00177 // like to be able to load dynamically using the CodecFactory::loadCodec()
00178 // function.  These are not required for any Codecs that you only want to link
00179 // directly into your programs.
00180 //
00181 // Make sure that you replace "CODEC" with the name of your derived class.
00182 // This name must also match the name of the object file (excluding the
00183 // extension).  These symbols must be linked into your Codec's object file,
00184 // not included in any headers that it may use (declarations are OK in headers
00185 // but not the definitions).
00186 //
00187 // The "pion_create" function is used to create new instances of your Codec.
00188 // The "pion_destroy" function is used to destroy instances of your Codec.
00189 //
00190 // extern "C" PION_PLUGIN_API Codec *pion_create_CODEC(void) {
00191 //      return new CODEC;
00192 // }
00193 //
00194 // extern "C" PION_PLUGIN_API void pion_destroy_CODEC(CODEC *codec_ptr) {
00195 //      delete codec_ptr;
00196 // }
00197 //
00198 
00199     
00200 }   // end namespace platform
00201 }   // end namespace pion
00202 
00203 #endif

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