Skip to content

Commit

Permalink
move impl of tstring database to tstring.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed Jun 25, 2014
1 parent 4637f44 commit 09e2ce0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
34 changes: 10 additions & 24 deletions src/shared_object.hpp
Expand Up @@ -15,8 +15,12 @@
#ifndef SHARED_OBJECT_HPP_INCLUDED
#define SHARED_OBJECT_HPP_INCLUDED

#include <climits>
#include <algorithm>
#include <cassert>
#include <climits>
#include <cstddef>
#include <ostream>

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
Expand All @@ -30,18 +34,14 @@ struct shared_node {
static const unsigned long max_count = ULONG_MAX;
};

template <typename T>
struct hasher {
size_t operator()(const T& o) {
return hash_value(o.val);
}
};

template <typename T, typename node = shared_node<T> >
class shared_object {
public:
typedef T type;

static const node* insert_into_index(const node &);
static void erase_from_index(const node *);

shared_object() : val_(NULL) { set(T()); }

explicit shared_object(const T &o) : val_(NULL) { set(o); }
Expand Down Expand Up @@ -76,7 +76,7 @@ class shared_object {
if (valid() && o == get()) return;
clear();

val_ = &*index().insert(node(o)).first;
val_ = insert_into_index(node(o)); //&*index().insert(node(o)).first;
val_->count++;

assert((val_->count) < (node::max_count));
Expand All @@ -97,20 +97,6 @@ class shared_object {

protected:

typedef boost::multi_index_container<
node,
boost::multi_index::indexed_by<
boost::multi_index::hashed_unique<
BOOST_MULTI_INDEX_MEMBER(node, T, val)
>
>
> hash_map;

typedef typename hash_map::template nth_index<0>::type hash_index;

static hash_map& map() { static hash_map* map = new hash_map; return *map; }
static hash_index& index() { return map().template get<0>(); }

const node* val_;

bool valid() const {
Expand All @@ -121,7 +107,7 @@ class shared_object {
if (!valid()) return;
val_->count--;

if (val_->count == 0) index().erase(index().find(val_->val));
if (val_->count == 0) erase_from_index(val_); //index().erase(index().find(val_->val));
val_ = NULL;
}

Expand Down
38 changes: 38 additions & 0 deletions src/tstring.cpp
Expand Up @@ -28,6 +28,10 @@
#include "log.hpp"
#include <boost/functional/hash.hpp>

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>

static lg::log_domain log_config("config");
#define LOG_CF LOG_STREAM(info, log_config)
#define ERR_CF LOG_STREAM(err, log_config)
Expand Down Expand Up @@ -508,3 +512,37 @@ std::ostream& operator<<(std::ostream& stream, const t_string_base& string)
return stream;
}

/**
* shared_object<tstring_base> implementation of hash database
*/

template <typename T, typename node = shared_node<T> >
struct types {
typedef boost::multi_index_container<
node,
boost::multi_index::indexed_by<
boost::multi_index::hashed_unique<
BOOST_MULTI_INDEX_MEMBER(node, T, val)
>
>
> hash_map;

typedef typename hash_map::template nth_index<0>::type hash_index;
};

static types<t_string_base>::hash_map& map() { static types<t_string_base>::hash_map* map = new types<t_string_base>::hash_map; return *map; }
static types<t_string_base>::hash_index& index() { return map().get<0>(); }

typedef shared_node<t_string_base> node;

template<>
const node* shared_object<t_string_base>::insert_into_index(const node & n)
{
return &*index().insert(n).first;
}

template<>
void shared_object<t_string_base>::erase_from_index(const node * ptr)
{
index().erase(index().find(ptr->val));
}

0 comments on commit 09e2ce0

Please sign in to comment.