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

Generated on Fri Feb 12 20:05:29 2016 for OpenDDS by  doxygen 1.4.7