trajectory_id.h
Go to the documentation of this file.
00001 #ifndef TRAJECTORY_ID_H
00002 #define TRAJECTORY_ID_H
00003 
00004 #include <iostream>
00005 
00006 #include <boost/thread/mutex.hpp>
00007 
00008 namespace descartes_core
00009 {
00010 namespace detail
00011 {
00021 template <typename T>
00022 struct IdGenerator;
00023 
00028 template <>
00029 struct IdGenerator<uint64_t>
00030 {
00031   typedef uint64_t value_type;
00032 
00033   static value_type make_nil()
00034   {
00035     return 0;
00036   }
00037 
00038   static value_type make_id()
00039   {
00040     boost::unique_lock<boost::mutex> scoped_lock(counter_mutex_);
00041     return counter_++;
00042   }
00043 
00044   static bool is_nil(value_type id)
00045   {
00046     return id == 0;
00047   }
00048 
00049 private:
00050   // Initialized to 1
00051   static value_type counter_;
00052   static boost::mutex counter_mutex_;
00053 };
00054 }
00055 
00060 template <typename T>
00061 class TrajectoryID_
00062 {
00063 public:
00064   typedef T value_type;
00065 
00069   TrajectoryID_(value_type id) : id_(id)
00070   {
00071   }
00072 
00077   TrajectoryID_() : id_(detail::IdGenerator<value_type>::make_nil())
00078   {
00079   }
00080 
00084   bool is_nil() const
00085   {
00086     return detail::IdGenerator<value_type>::is_nil(id_);
00087   }
00088 
00092   value_type value() const
00093   {
00094     return id_;
00095   }
00096 
00100   static TrajectoryID_<value_type> make_id()
00101   {
00102     return TrajectoryID_<value_type>(detail::IdGenerator<value_type>::make_id());
00103   }
00104 
00108   static TrajectoryID_<value_type> make_nil()
00109   {
00110     return TrajectoryID_<value_type>(detail::IdGenerator<value_type>::make_nil());
00111   }
00112 
00113 private:
00114   value_type id_;
00115 };
00116 
00118 // Helper Functions //
00120 
00121 template <typename T>
00122 inline bool operator==(TrajectoryID_<T> lhs, TrajectoryID_<T> rhs)
00123 {
00124   return lhs.value() == rhs.value();
00125 }
00126 
00127 template <typename T>
00128 inline bool operator!=(TrajectoryID_<T> lhs, TrajectoryID_<T> rhs)
00129 {
00130   return !(lhs == rhs);
00131 }
00132 
00133 template <typename T>
00134 inline bool operator<(TrajectoryID_<T> lhs, TrajectoryID_<T> rhs)
00135 {
00136   return lhs.value() < rhs.value();
00137 }
00138 
00139 template <typename T>
00140 inline std::ostream& operator<<(std::ostream& os, TrajectoryID_<T> id)
00141 {
00142   os << "ID" << id.value();
00143   return os;
00144 }
00145 
00146 typedef TrajectoryID_<uint64_t> TrajectoryID;
00147 
00148 }  // end namespace descartes_core
00149 
00150 #endif


descartes_core
Author(s): Dan Solomon
autogenerated on Thu Jun 6 2019 21:35:59