Skip to content

Commit

Permalink
Modifications for bounded
Browse files Browse the repository at this point in the history
git-svn-id: file:///dev/shm/somefilter.svn@610 e102df66-1e2e-11dd-9b44-c24451a4db5e
  • Loading branch information
kpu committed Aug 29, 2011
1 parent 2494105 commit 81ff96b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
29 changes: 29 additions & 0 deletions util/scoped.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <cstddef>
#include <cstdio>
#include <stdlib.h>

namespace util {

Expand Down Expand Up @@ -34,6 +35,34 @@ template <class T, class R, R (*Free)(T*)> class scoped_thing {
scoped_thing &operator=(const scoped_thing &);
};

template <class T> class scoped_malloc {
public:
scoped_malloc() : p_(NULL) {}

scoped_malloc(T *p) : p_(p) {}

~scoped_malloc() { free(p_); }

void reset(T *p) {
scoped_malloc other(p_);
p_ = p;
}

T &operator*() { return *p_; }
const T &operator*() const { return *p_; }
T &operator->() { return *p_; }
const T&operator->() const { return *p_; }

T *get() { return p_; }
const T *get() const { return p_; }

private:
T *p_;

scoped_malloc(const scoped_malloc &);
scoped_malloc &operator=(const scoped_malloc &);
};

class scoped_fd {
public:
scoped_fd() : fd_(-1) {}
Expand Down
7 changes: 7 additions & 0 deletions util/sized_iterator.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <functional>
#include <string>

#include <inttypes.h>
#include <string.h>

namespace util {

class SizedInnerIterator {
Expand Down Expand Up @@ -74,6 +77,8 @@ class SizedProxy {

typedef ProxyIterator<SizedProxy> SizedIterator;

SizedIterator SizedIt(void *ptr, std::size_t size) { return SizedIterator(SizedProxy(ptr, size)); }

// Useful wrapper for a comparison function i.e. sort.
template <class Delegate, class Proxy = SizedProxy> class SizedCompare : public std::binary_function<const Proxy &, const Proxy &, bool> {
public:
Expand All @@ -91,6 +96,8 @@ template <class Delegate, class Proxy = SizedProxy> class SizedCompare : public
bool operator()(const std::string &first, const std::string &second) const {
return delegate_(first.data(), second.data());
}

const Delegate &GetDelegate() const { return delegate_; }

private:
const Delegate delegate_;
Expand Down

0 comments on commit 81ff96b

Please sign in to comment.