Util.h

Go to the documentation of this file.
00001 /*
00002  *
00003  *
00004  * Distributed under the OpenDDS License.
00005  * See: http://www.opendds.org/license.html
00006  */
00007 
00008 #ifndef OPENDDS_DCPS_UTIL_H
00009 #define OPENDDS_DCPS_UTIL_H
00010 
00011 OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
00012 
00013 namespace OpenDDS {
00014 namespace DCPS {
00015 
00016 // bind reproduces the ACE_Hash_Map_Manager_Ex's bind behavior
00017 template <typename Container, typename FirstType, typename SecondType>
00018 int bind(
00019   Container& c,
00020   const FirstType& first,
00021   const SecondType& second)
00022 {
00023   if (c.find(first) == c.end()) {
00024     typedef typename Container::value_type container_value_type;
00025 
00026     if (c.insert(container_value_type(first, second)).second) {
00027       return 0;
00028     }
00029 
00030     return -1;
00031   }
00032 
00033   return 1;
00034 }
00035 
00036 // unbind reproduces the ACE_Hash_Map_Manager_Ex's unbind behavior
00037 template <typename Container>
00038 int unbind(
00039   Container& c,
00040   const typename Container::key_type& k,
00041   typename Container::mapped_type& v)
00042 {
00043   typename Container::const_iterator iter = c.find(k);
00044 
00045   if (iter != c.end()) {
00046     v = iter->second;
00047 
00048     if (c.erase(k) == 1) {
00049       return 0;
00050     }
00051 
00052     return -1;
00053   }
00054 
00055   return -1;
00056 }
00057 
00058 // unbind reproduces the ACE_Hash_Map_Manager_Ex's unbind behavior
00059 template <typename Container>
00060 int unbind(
00061   Container& c,
00062   const typename Container::key_type& k)
00063 {
00064   typename Container::mapped_type v;
00065   return unbind(c, k, v);
00066 }
00067 
00068 template <typename Container, typename Key>
00069 int find(
00070   Container& c,
00071   const Key& key,
00072   typename Container::mapped_type*& value)
00073 {
00074   typename Container::iterator iter =
00075     c.find(key);
00076 
00077   if (iter == c.end()) {
00078     return -1;
00079   }
00080 
00081   value = &iter->second;
00082   return 0;
00083 }
00084 
00085 template <typename Container, typename Key>
00086 int find(
00087   const Container& c,
00088   const Key& key,
00089   typename Container::mapped_type& value)
00090 {
00091   typename Container::const_iterator iter =
00092     c.find(key);
00093 
00094   if (iter == c.end()) {
00095     return -1;
00096   }
00097 
00098   value = iter->second;
00099   return 0;
00100 }
00101 
00102 template <typename Container, typename ValueType>
00103 int insert(
00104   Container& c,
00105   const ValueType& v)
00106 {
00107   if (c.find(v) == c.end()) {
00108     if (c.insert(v).second) {
00109       return 0;
00110     }
00111 
00112     return -1;
00113   }
00114 
00115   return 1;
00116 }
00117 
00118 template <typename Container, typename ValueType>
00119 int remove(
00120   Container& c,
00121   const ValueType& v)
00122 {
00123   if (c.find(v) != c.end()) {
00124     if (c.erase(v) == 1) {
00125       return 0;
00126     }
00127 
00128     return -1;
00129   }
00130 
00131   return -1;
00132 }
00133 
00134 /// std::vector-style push_back() for CORBA Sequences
00135 template <typename Seq>
00136 void push_back(Seq& seq, const typename Seq::value_type& val)
00137 {
00138   const CORBA::ULong len = seq.length();
00139   seq.length(len + 1);
00140   seq[len] = val;
00141 }
00142 
00143 
00144 } // namespace DCPS
00145 } // namespace OpenDDS
00146 
00147 OPENDDS_END_VERSIONED_NAMESPACE_DECL
00148 
00149 #endif /* OPENDDS_DCPS_UTIL_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 10 Aug 2018 for OpenDDS by  doxygen 1.6.1