L:/channel/channel/include/LinearIdTrait.h

Go to the documentation of this file.
00001 
00002 // Copyright (c) 2005, 2006 Yigong Liu
00003 // Permission to use, copy, modify, distribute and sell this software for any 
00004 //     purpose is hereby granted without fee, provided that the above copyright 
00005 //     notice appear in all copies and that both that copyright notice and this 
00006 //     permission notice appear in supporting documentation.
00007 // The author makes no representations about the 
00008 //     suitability of this software for any purpose. It is provided "as is" 
00009 //     without express or implied warranty.
00011 
00012 #ifndef _LINEAR_IDTRAIT_H_
00013 #define _LINEAR_IDTRAIT_H_
00014 
00015 #include <sstream>
00016 #include <string>
00017 #include "ace/CDR_Stream.h"
00018 
00025 namespace channel {
00026 
00028   template <class IdType> class  IdTrait;
00029 
00031   template <>
00032     class  IdTrait<int> {
00033     public:
00035     enum {
00037       CHANNEL_CONN_MSG=1000,
00038       CHANNEL_DISCONN_MSG,
00039       INIT_SUBSCRIPTION_INFO_MSG,
00040       INIT_PUBLICATION_INFO_MSG,
00041       SUBSCRIPTION_INFO_MSG,
00042       UNSUBSCRIPTION_INFO_MSG,
00043       PUBLICATION_INFO_MSG,
00044       UNPUBLICATION_INFO_MSG,
00046       APP_MSG_BEGIN=2000
00047     };
00048 
00049     typedef int IdType;
00050 
00052     static bool eq(const IdType &id1, const IdType &id2)
00053       { return id1 == id2; }
00054     static bool lt(const IdType &id1, const IdType &id2)
00055       { return id1 < id2; }
00056 
00057     //compiler doesnt allow references here
00058     //static bool match(const IdType &id1, const IdType &id2)
00059     static bool match(const IdType id1, const IdType id2)
00060       { return id1 == id2; }
00061 
00063     static std::string idToString(const IdType &id) {
00064       std::ostringstream os;
00065       os << id;
00066       return os.str();
00067     }
00068 
00070     static int size(const int &id2) {
00071       ACE_UNUSED_ARG(id2);
00072       return sizeof(int);
00073     }
00074 
00076     static int marshal(ACE_OutputCDR &cdr, const int &id)
00077       {
00078         cdr << ACE_CDR::Long(id);
00079         return cdr.good_bit();
00080       }
00081     static int demarshal(ACE_InputCDR &cdr, int &id)
00082       {
00083         ACE_CDR::Long d;
00084         cdr >> d;
00085         id = (int)d;
00086         return cdr.good_bit ();
00087       }
00088   };
00089 
00091   template <>
00092     class  IdTrait<std::string> {
00093     public:
00095 
00096     static std::string CHANNEL_CONN_MSG;
00097     static std::string CHANNEL_DISCONN_MSG;
00098     static std::string INIT_SUBSCRIPTION_INFO_MSG;
00099     static std::string INIT_PUBLICATION_INFO_MSG;
00100     static std::string SUBSCRIPTION_INFO_MSG;
00101     static std::string UNSUBSCRIPTION_INFO_MSG;
00102     static std::string PUBLICATION_INFO_MSG;
00103     static std::string UNPUBLICATION_INFO_MSG;
00104 
00105     typedef std::string IdType;
00106 
00108     static bool eq(const std::string &id1, const std::string &id2)
00109       { return id1 == id2; }
00110     static bool lt(const std::string &id1, const std::string &id2)
00111       { return id1 < id2; }
00112 
00113     //compiler doesnt allow references here
00114     //static bool match(const std::string &id1, const std::string &id2)
00115     static bool match(const std::string id1, const std::string id2)
00116       { return id1 == id2; }
00117 
00118     static std::string idToString(const std::string &id) {
00119       return id;
00120     }
00121 
00122     static int size(const std::string &id2) {
00123       return id2.length();
00124     }
00125 
00127     static int marshal(ACE_OutputCDR &cdr, const std::string &id)
00128       {
00129         int len = id.length()+1; //add 1 for '\0' added by c_str()
00130         cdr << ACE_CDR::Long (len);
00131         cdr.write_char_array(id.c_str(),len);
00132         return cdr.good_bit();
00133       }
00134     static int demarshal(ACE_InputCDR &cdr, std::string &id)
00135       {
00136         ACE_CDR::Long d;
00137         cdr >> d;
00138         char *data = new char[d];
00139         cdr.read_char_array(data, d);
00140         id = data;
00141         delete[] data;
00142         return cdr.good_bit();
00143       }
00144     
00145   };
00146 
00150 
00151   enum MessageFamily {
00152     SYSTEM_MESSAGE,
00153     APPLICATION_MESSAGE
00154   };
00155 
00156   struct StructId {
00157     MessageFamily family;
00158     int type;
00159     bool operator< (const StructId &id) const {
00160       if (family < id.family || (family == id.family && type < id.type))
00161         return true;
00162       return false;
00163     }
00164     bool operator== (const StructId &id) const {
00165       if (family == id.family && type == id.type)
00166         return true;
00167       return false;
00168     }
00169     bool operator!= (const StructId &id) const {
00170       if (family != id.family || type != id.type)
00171         return true;
00172       return false;
00173     }
00174   };
00175 
00177   template <>
00178     class IdTrait<StructId> {
00179     public:
00181 
00182     static StructId CHANNEL_CONN_MSG;
00183     static StructId CHANNEL_DISCONN_MSG;
00184     static StructId INIT_SUBSCRIPTION_INFO_MSG;
00185     static StructId INIT_PUBLICATION_INFO_MSG;
00186     static StructId SUBSCRIPTION_INFO_MSG;
00187     static StructId UNSUBSCRIPTION_INFO_MSG;
00188     static StructId PUBLICATION_INFO_MSG;
00189     static StructId UNPUBLICATION_INFO_MSG;
00190 
00191     typedef StructId IdType;
00192 
00194     static bool eq(const StructId &id1, const StructId &id2)
00195       { return id1 == id2; }
00196     static bool lt(const StructId &id1, const StructId &id2)
00197       { return id1 < id2; }
00198 
00199     //compiler doesnt allow references here
00200     //static bool match(const StructId &id1, const StructId &id2)
00201     static bool match(const StructId id1, const StructId id2)
00202       { return id1 == id2; }
00203 
00204     static std::string idToString(const StructId &id) {
00205       std::ostringstream os;
00206       os << "[Family:" << id.family << ", Type:" << id.type <<"]";
00207       return os.str();
00208     }
00209 
00210     static int size(const StructId &id2) {
00211       ACE_UNUSED_ARG(id2);
00212       return sizeof(StructId);
00213     }
00214 
00216     static int marshal(ACE_OutputCDR &cdr, const StructId &id)
00217       {
00218         cdr << ACE_CDR::Long (id.family);
00219         cdr << ACE_CDR::Long (id.type);
00220         return cdr.good_bit();
00221       }
00222     static int demarshal(ACE_InputCDR &cdr, StructId &id)
00223       {
00224         ACE_CDR::Long d;
00225         cdr >> d;
00226         id.family = (MessageFamily) d;
00227         cdr >> d;
00228         id.type = (int) d;
00229         return cdr.good_bit();
00230       }    
00231   };
00232 
00233 
00234 };
00235 
00236 #endif

Generated on Mon Feb 27 19:59:21 2006 for channel by  doxygen 1.4.6-NO