[Assorted-commits] SF.net SVN: assorted:[1168] cpp-commons/trunk
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-02-06 00:40:04
|
Revision: 1168 http://assorted.svn.sourceforge.net/assorted/?rev=1168&view=rev Author: yangzhang Date: 2009-02-06 00:39:52 +0000 (Fri, 06 Feb 2009) Log Message: ----------- - use shared ptrs in st_multichannel - added overload randint(min,max) - tweaked README Modified Paths: -------------- cpp-commons/trunk/README cpp-commons/trunk/src/commons/rand.h cpp-commons/trunk/src/commons/st/st.h Modified: cpp-commons/trunk/README =================================================================== --- cpp-commons/trunk/README 2009-02-06 00:36:31 UTC (rev 1167) +++ cpp-commons/trunk/README 2009-02-06 00:39:52 UTC (rev 1168) @@ -11,7 +11,7 @@ Here are some of the features present in the library: - C functions for string manipulation -- RAII utilities, such as for closing file descriptors +- RAII utilities, such as for closing file descriptors and `finally` objects - `array`: thin wrapper around arrays (`scoped_array` + size) - `pool`: fixed-size object pools - bit manipulation Modified: cpp-commons/trunk/src/commons/rand.h =================================================================== --- cpp-commons/trunk/src/commons/rand.h 2009-02-06 00:36:31 UTC (rev 1167) +++ cpp-commons/trunk/src/commons/rand.h 2009-02-06 00:39:52 UTC (rev 1168) @@ -24,7 +24,7 @@ }; /** - * Generate a random int up to but excluding max. + * Generate a random int from 0 up to but excluding max. * \param[in] max Must be greater than one. */ inline int @@ -98,10 +98,20 @@ // // OK, this path isn't taking us anywhere. Let's go back to the original // formula. Sure, it exceeds max. But what if we just wrap it back around - // when it does? We can do so using %max. Success! + // when it does? We can do so using % max. Success! return static_cast<int>( random() / ( RAND_MAX / max ) % max ); } + + /** + * Use randint() to return a number in [min, max) if max > min + 1 (i.e. if + * there is more than one choice), otherwise return min. + */ + inline int + randint(int min, int max) + { + return max > min + 1 ? min + randint(max - min) : min; + } } #endif Modified: cpp-commons/trunk/src/commons/st/st.h =================================================================== --- cpp-commons/trunk/src/commons/st/st.h 2009-02-06 00:36:31 UTC (rev 1167) +++ cpp-commons/trunk/src/commons/st/st.h 2009-02-06 00:39:52 UTC (rev 1168) @@ -189,6 +189,8 @@ bool b; }; + void toggle(st_bool& b) { if (b) b.reset(); else b.set(); } + /** * Wraps st_mutex_* errno-functions with exceptions and cleans up on * destruction. @@ -223,8 +225,9 @@ empty_.signal(); } T take() { - while (q_.empty()) + while (q_.empty()) { empty_.wait(); + } T x = front(); q_.pop(); return x; @@ -241,24 +244,23 @@ /** * An unbounded FIFO multi-cast channel, i.e. publish-subscribe. - * \todo Use shared_ptr. */ template <typename T> class st_multichannel { public: void push(const T &x) { - foreach (st_channel<T> *q, qs) { + foreach (shared_ptr<st_channel<T> > q, qs) { q->push(x); } } st_channel<T> &subscribe() { - st_channel<T>* q = new st_channel<T>; + shared_ptr<st_channel<T> > q(new st_channel<T>); qs.push_back(q); return *q; } private: - vector<st_channel<T>*> qs; + vector<shared_ptr<st_channel<T> > > qs; }; /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |