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 
00011 namespace detail
00012 {
00013 
00023 template<typename T>
00024 struct IdGenerator;
00025 
00030 template<>
00031 struct IdGenerator<uint64_t>
00032 {
00033   typedef uint64_t value_type;
00034 
00035   static value_type make_nil()
00036   {
00037     return 0;
00038   }
00039 
00040   static value_type make_id()
00041   {
00042     boost::unique_lock<boost::mutex> scoped_lock (counter_mutex_);
00043     return counter_++;
00044   }
00045 
00046   static bool is_nil(value_type id)
00047   {
00048     return id == 0;
00049   }
00050 
00051 private:
00052   // Initialized to 1
00053   static value_type counter_;
00054   static boost::mutex counter_mutex_;
00055 };
00056 
00057 }
00058 
00063 template<typename T>
00064 class TrajectoryID_
00065 {
00066 public:
00067   typedef T value_type;
00068 
00072   TrajectoryID_(value_type id)
00073     : id_(id)
00074   {}
00075 
00080   TrajectoryID_()
00081     : id_(detail::IdGenerator<value_type>::make_nil())
00082   {}
00083 
00087   bool is_nil() const { return detail::IdGenerator<value_type>::is_nil(id_); }
00088 
00092   value_type value() const { return id_; }
00093 
00097   static TrajectoryID_<value_type> make_id()
00098   {
00099     return TrajectoryID_<value_type>( detail::IdGenerator<value_type>::make_id() );
00100   }
00101 
00105   static TrajectoryID_<value_type> make_nil()
00106   {
00107     return TrajectoryID_<value_type>( detail::IdGenerator<value_type>::make_nil() );
00108   }
00109 
00110 private:
00111   value_type id_;  
00112 };
00113 
00115 // Helper Functions //
00117 
00118 template<typename T>
00119 inline bool operator==(TrajectoryID_<T> lhs, TrajectoryID_<T> rhs)
00120 {
00121   return lhs.value() == rhs.value();
00122 }
00123 
00124 template<typename T>
00125 inline bool operator!=(TrajectoryID_<T> lhs, TrajectoryID_<T> rhs)
00126 {
00127   return !(lhs == rhs);
00128 }
00129 
00130 template<typename T>
00131 inline bool operator<(TrajectoryID_<T> lhs, TrajectoryID_<T> rhs)
00132 {
00133   return lhs.value() < rhs.value();
00134 }
00135 
00136 template<typename T>
00137 inline std::ostream& operator<<(std::ostream& os, TrajectoryID_<T> id)
00138 {
00139   os << "ID" << id.value();
00140   return os;
00141 }
00142 
00143 typedef TrajectoryID_<uint64_t> TrajectoryID;
00144 
00145 } // end namespace descartes_core
00146 
00147 
00148 #endif


descartes_core
Author(s): Dan Solomon
autogenerated on Wed Aug 26 2015 11:21:21