Skip to content

Instantly share code, notes, and snippets.

@mpenick
Last active May 4, 2016 15:39
Show Gist options
  • Select an option

  • Save mpenick/1382e1b5a992a75515103dc32f724ed8 to your computer and use it in GitHub Desktop.

Select an option

Save mpenick/1382e1b5a992a75515103dc32f724ed8 to your computer and use it in GitHub Desktop.
template<class T, void (*FreeFunction)(T*)>
class Deleter {
public:
void operator()(T* object) {
if (object) {
FreeFunction(object);
}
}
};
template<class T, void (*FreeFunction)(T*)>
class Object {
public:
typedef Deleter<T, FreeFunction> Deleter;
typedef SmartPtr<T, Deleter> Ptr;
Object() { }
Object(T* object)
: object_(object) { }
T* get() {
check_null();
return object_.get();
}
const T* get() const {
check_null();
return object_.get();
}
protected:
void check_null() const {
if (!object) {
throw Exception("Attempted to use null object");
}
}
private:
Ptr object_;
};
typedef Object<CassCollection, cass_collection_free> Collection;
class Future : public Object<CassFuture, cass_future_free> {
public:
Future(CassFuture* future)
: Object(future) { }
CassError error_code() {
return cass_future_error_code(get());
}
};
Collection c1(cass_collection_new(CASS_COLLECTION_TYPE_LIST, 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment