Skip to content

Instantly share code, notes, and snippets.

@pmelanson
Created January 29, 2013 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmelanson/4661401 to your computer and use it in GitHub Desktop.
Save pmelanson/4661401 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <boost/intrusive/list.hpp>
using namespace std;
class entity_c : public boost::intrusive::list_base_hook<> {
public:
const int isconst;
int notconst;
entity_c(int con, int notcon)
: isconst(con), notconst(notcon) {
cout << "constructed entity (" << isconst << ',' << notconst << ")\n";
}
~entity_c() {
cout << "destructed entity (" << isconst << ',' << notconst << ")\n";
}
};
typedef boost::intrusive::list<entity_c> list_t;
namespace {list_t global_list;}
//Cloner object function
struct new_cloner {
entity_c *operator()(const entity_c &clone_this)
{ return new entity_c(clone_this); }
};
//The disposer object function
struct delete_disposer {
void operator()(entity_c *delete_this)
{ delete delete_this; }
};
int main() {
int max = 5;
static vector<entity_c> values;
values.reserve(max);
int i = 1;
while(i != max) {
values.push_back(entity_c(i,i));
cout << "MADE " << values.back().isconst << "\n\n";
i++;
}
cout << "ALL CONSTRUCTED\n\n\n";
static list_t list;
cout << "INSERT INTO BASE HOOK LIST\n";
list.insert(list.begin(), values.begin(), values.end());
global_list.clone_from(list, new_cloner(), delete_disposer());
cout << global_list.back().isconst; //check that the back of the global list is the same back as the local list
cout << list.back().isconst << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment