Skip to content

Commit

Permalink
Merge pull request #12 from sy-c/master
Browse files Browse the repository at this point in the history
added overloaded DataBlockContainer constructor with release callback
  • Loading branch information
sy-c authored Mar 15, 2018
2 parents ef8c21e + 942a986 commit afab08b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
8 changes: 8 additions & 0 deletions include/Common/DataBlockContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <stdlib.h>
#include <memory>
#include <functional>

// A container class for data blocks.
// In particular, allows to take care of the block release after use.
Expand All @@ -17,8 +18,15 @@ class DataBlockContainer {
virtual ~DataBlockContainer();
DataBlock *getData();

using ReleaseCallback = std::function<void(void)>;
// NB: may use std::bind to add extra arguments

// this constructor allows to specify a callback which is invoked when container is destroyed
DataBlockContainer(ReleaseCallback callback, DataBlock *v_data=NULL);

protected:
DataBlock *data;
ReleaseCallback releaseCallback;
};


Expand Down
9 changes: 8 additions & 1 deletion src/DataBlockContainer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@

// base DataBlockContainer class

DataBlockContainer::DataBlockContainer(DataBlock *v_data) : data(v_data) {
DataBlockContainer::DataBlockContainer(DataBlock *v_data) : data(v_data), releaseCallback(nullptr) {
}

DataBlockContainer::DataBlockContainer(ReleaseCallback v_callback, DataBlock *v_data)
: data(v_data), releaseCallback(v_callback) {
}

DataBlockContainer::~DataBlockContainer() {
if (releaseCallback != nullptr) {
releaseCallback();
}
}

DataBlock * DataBlockContainer::getData() {
Expand Down
9 changes: 9 additions & 0 deletions test/testMemPool.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
/// \author Sylvain Chapeland, CERN

#include "Common/MemPool.h"
#include "Common/DataBlockContainer.h"

#include <stdlib.h>
#include <stdio.h>
#include <vector>
Expand Down Expand Up @@ -81,6 +83,13 @@ int main() {
printf("%d errors\n",nErr);
}

DataBlockContainer *bc=nullptr;
auto releaseCallback = [bc] (void) -> void {
printf("release callback for %p\n",bc);
return;
};
bc=new DataBlockContainer(releaseCallback,nullptr);
delete bc;

printf("Delete pool\n");
delete mp;
Expand Down

0 comments on commit afab08b

Please sign in to comment.