aboutsummaryrefslogtreecommitdiff
path: root/common/session/custom/session.hxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2013-01-16 16:31:29 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2013-01-16 16:31:29 +0200
commit856a438b33184959b64864f1afdf5a6a2fd6b0d2 (patch)
treeb1fd1d9c616262d8d62e4431469f74e5e74d4fe1 /common/session/custom/session.hxx
parent5cf30ccfe764701f549e4152ad312187221f5285 (diff)
Make session cache management functions static, add notifications
Diffstat (limited to 'common/session/custom/session.hxx')
-rw-r--r--common/session/custom/session.hxx64
1 files changed, 43 insertions, 21 deletions
diff --git a/common/session/custom/session.hxx b/common/session/custom/session.hxx
index bb60a4b..3b1789a 100644
--- a/common/session/custom/session.hxx
+++ b/common/session/custom/session.hxx
@@ -26,14 +26,13 @@ private:
session (const session&);
session& operator= (const session&);
- // Current session interface.
+ // Session for the current thread. This can be implemented in pretty
+ // much any way that makes sense to the application. It can be a global
+ // session as we have here. In multi-threaded applications we could use
+ // TLS instead.
//
public:
- static bool
- has_current ();
-
- static session&
- current ();
+ static session* current;
// Change tracking interface.
//
@@ -61,23 +60,30 @@ private:
mark () = 0;
};
+ enum object_state
+ {
+ tracking, // Tracking any modifications by storing the original copy.
+ changed, // Known to be changed.
+ flushed // Flushed but not yet committed/rolled back.
+ };
+
template <typename T>
- struct object_state
+ struct object_data
{
typedef typename odb::object_traits<T>::pointer_type pointer_type;
explicit
- object_state (pointer_type o): obj (o), flushed_ (false) {}
+ object_data (pointer_type o): obj (o), state (tracking) {}
pointer_type obj;
pointer_type orig;
- bool flushed_;
+ object_state state;
};
template <typename T>
struct object_map: object_map_base,
std::map<typename odb::object_traits<T>::id_type,
- object_state<T> >
+ object_data<T> >
{
virtual void
flush (odb::database&);
@@ -95,37 +101,53 @@ public:
typedef object_map<T> map;
typedef typename map::iterator iterator;
- position () {}
+ position (): map_ (0) {}
position (map& m, const iterator& p): map_ (&m), pos_ (p) {}
map* map_;
iterator pos_;
};
+ // Cache management.
+ //
template <typename T>
- position<T>
+ static position<T>
insert (odb::database&,
const typename odb::object_traits<T>::id_type&,
const typename odb::object_traits<T>::pointer_type&);
template <typename T>
+ static typename odb::object_traits<T>::pointer_type
+ find (odb::database&, const typename odb::object_traits<T>::id_type&);
+
+ template <typename T>
+ static void
+ erase (const position<T>& p)
+ {
+ if (p.map_ != 0)
+ p.map_->erase (p.pos_);
+ }
+
+ // Notifications.
+ //
+ template <typename T>
static void
- initialize (const position<T>&);
+ persist (const position<T>& p)
+ {
+ load (p);
+ }
template <typename T>
- typename odb::object_traits<T>::pointer_type
- find (odb::database&, const typename odb::object_traits<T>::id_type&) const;
+ static void
+ load (const position<T>&);
template <typename T>
- void
- erase (odb::database&, const typename odb::object_traits<T>::id_type&);
+ static void
+ update (odb::database&, const T&);
template <typename T>
static void
- erase (const position<T>& p)
- {
- p.map_->erase (p.pos_);
- }
+ erase (odb::database&, const typename odb::object_traits<T>::id_type&);
private:
typedef std::map<const std::type_info*,