Fawkes API  Fawkes Development Version
interface_info.cpp
00001 
00002 /***************************************************************************
00003  *  interface_info.h - BlackBoard Interface Info
00004  *
00005  *  Created: Mon Mar 03 15:44:46 2008
00006  *  Copyright  2006-2008  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <interface/interface_info.h>
00025 #include <interface/interface.h>
00026 #include <utils/misc/strndup.h>
00027 
00028 #include <cstdlib>
00029 #include <cstring>
00030 
00031 namespace fawkes {
00032 
00033 /** @class InterfaceInfo <interface/interface_info.h>
00034  * Interface info.
00035  * This class holds information about a specific interface.
00036  * @author Tim Niemueller
00037  */
00038 
00039 /** Constructor.
00040  * @param type type of the interface
00041  * @param id id of the interface
00042  * @param hash version hash
00043  * @param has_writer true if there is a writer, false otherwise
00044  * @param num_readers number of readers
00045  * @param serial instance serial
00046  */
00047 InterfaceInfo::InterfaceInfo(const char *type, const char *id, const unsigned char *hash,
00048                              unsigned int serial, bool has_writer, unsigned int num_readers)
00049 {
00050   __type = strndup(type, __INTERFACE_TYPE_SIZE);
00051   __id   = strndup(id, __INTERFACE_ID_SIZE);
00052   __hash = (unsigned char *)malloc(__INTERFACE_HASH_SIZE);
00053   memcpy(__hash, hash, __INTERFACE_HASH_SIZE);
00054   __has_writer = has_writer;
00055   __num_readers = num_readers;
00056   __serial = serial;
00057 }
00058 
00059 
00060 /** Copy constructor.
00061  * @param i info to copy
00062  */
00063 InterfaceInfo::InterfaceInfo(const InterfaceInfo &i)
00064 {
00065   __type = strndup(i.__type, __INTERFACE_TYPE_SIZE);
00066   __id   = strndup(i.__id, __INTERFACE_ID_SIZE);
00067   __hash = (unsigned char *)malloc(__INTERFACE_HASH_SIZE);
00068   memcpy(__hash, i.__hash, __INTERFACE_HASH_SIZE);
00069   __has_writer = i.__has_writer;
00070   __num_readers = i.__num_readers;
00071   __serial = i.__serial;
00072 }
00073 
00074 
00075 /** Destructor. */
00076 InterfaceInfo::~InterfaceInfo()
00077 {
00078   free(__type);
00079   free(__id);
00080   free(__hash);
00081 }
00082 
00083 
00084 /** Get interface type.
00085  * @return type string
00086  */
00087 const char *
00088 InterfaceInfo::type() const
00089 {
00090   return __type;
00091 }
00092 
00093 
00094 /** Get interface ID.
00095  * @return ID string
00096  */
00097 const char *
00098 InterfaceInfo::id() const
00099 {
00100   return __id;
00101 }
00102 
00103 
00104 /** Get interface version hash.
00105  * @return interface version hash
00106  */
00107 const unsigned char *
00108 InterfaceInfo::hash() const
00109 {
00110   return __hash;
00111 }
00112 
00113 
00114 /** Check if there is a writer.
00115  * @return true if there is a writer, false otherwise
00116  */
00117 bool
00118 InterfaceInfo::has_writer() const
00119 {
00120   return __has_writer;
00121 }
00122 
00123 
00124 /** Get number of readers.
00125  * @return number of readers
00126  */
00127 unsigned int
00128 InterfaceInfo::num_readers() const
00129 {
00130   return __num_readers;
00131 }
00132 
00133 
00134 /** Get interface instance serial.
00135  * @return type string
00136  */
00137 unsigned int
00138 InterfaceInfo::serial() const
00139 {
00140   return __serial;
00141 }
00142 
00143 
00144 /** < operator
00145  * This compares two interface infos with respect to the less than (<) relation
00146  * considering the type and id of an interface.
00147  * An interface info A is less than an interface info B (A < B) iff
00148  * (A.type < B.type) or ((A.type == B.type) && A.id < B.id).
00149  * @param ii interface info to compare this to
00150  * @return true if this instance is considered less than @p ii, false otherwise
00151  */
00152 bool
00153 InterfaceInfo::operator<(const InterfaceInfo &ii) const
00154 {
00155   int td = strncmp(__type, ii.__type, __INTERFACE_TYPE_SIZE);
00156   if ( td < 0 ) {
00157     return true;
00158   } else if (td > 0) {
00159     return false;
00160   } else {
00161     return (strncmp(__id, ii.__id, __INTERFACE_ID_SIZE) < 0);
00162   }
00163 }
00164 
00165 
00166 /** @class InterfaceInfoList <interface/interface_info.h>
00167  * Interface information list.
00168  * List with InterfaceInfo instances.
00169  * @author Tim Niemueller
00170  */
00171 
00172 /** Append an interface info.
00173  * @param type type of the interface
00174  * @param id id of the interface
00175  * @param hash version hash
00176  * @param has_writer true if there is a writer, false otherwise
00177  * @param num_readers number of readers
00178  * @param serial instance serial
00179  */
00180 void
00181 InterfaceInfoList::append(const char *type, const char *id, const unsigned char *hash,
00182                           unsigned int serial, bool has_writer, unsigned int num_readers)
00183 {
00184   push_back(InterfaceInfo(type, id, hash, serial, has_writer, num_readers));
00185 }
00186 
00187 } // end namespace fawkes