aboutsummaryrefslogtreecommitdiff
path: root/odb/mssql/simple-object-statements.hxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2012-06-18 09:32:09 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2012-06-18 09:32:09 +0200
commitfe625c9b53fa6b251cdc1c7ed5778ccd4dfeefa8 (patch)
treef141d7b69b9f193c7ad97c8dc7b4c3a05cb6d81a /odb/mssql/simple-object-statements.hxx
parent0ab918e8b3660aed2e24d543633da54cf698b72f (diff)
Allocate container traits lazily and only when their definition is seen
This fixes a problem with polymorphic hierarchies spread over multiple files in which case the source code for the derived class does not have the definition of the container traits for the base class. See the comment in the source code for further details.
Diffstat (limited to 'odb/mssql/simple-object-statements.hxx')
-rw-r--r--odb/mssql/simple-object-statements.hxx57
1 files changed, 55 insertions, 2 deletions
diff --git a/odb/mssql/simple-object-statements.hxx b/odb/mssql/simple-object-statements.hxx
index d4809b4..7002ecb 100644
--- a/odb/mssql/simple-object-statements.hxx
+++ b/odb/mssql/simple-object-statements.hxx
@@ -29,6 +29,58 @@ namespace odb
{
namespace mssql
{
+ // The container_statement_cache class is only defined (and used) in
+ // the generated source file. However, object_statements may be
+ // referenced from another source file in the case of a polymorphic
+ // hierarchy (though in this case the container statement cache is
+ // not used). As a result, we cannot have a by-value member and
+ // instead will store a pointer and lazily allocate the cache if
+ // and when needed. We will also need to store a pointer to the
+ // deleter function which will be initialized during allocation
+ // (at that point we know that the cache class is defined).
+ //
+ template <typename T>
+ struct container_statement_cache_ptr
+ {
+ typedef mssql::connection connection_type;
+
+ container_statement_cache_ptr (): p_ (0) {}
+ ~container_statement_cache_ptr () {if (p_ != 0) (this->*deleter_) (0);}
+
+ T&
+ get (connection_type& c)
+ {
+ if (p_ == 0)
+ allocate (&c);
+
+ return *p_;
+ }
+
+ private:
+ void
+ allocate (connection_type*);
+
+ private:
+ T* p_;
+ void (container_statement_cache_ptr::*deleter_) (connection_type*);
+ };
+
+ template <typename T>
+ void container_statement_cache_ptr<T>::
+ allocate (connection_type* c)
+ {
+ // To reduce object code size, this function acts as both allocator
+ // and deleter.
+ //
+ if (p_ == 0)
+ {
+ p_ = new T (*c);
+ deleter_ = &container_statement_cache_ptr<T>::allocate;
+ }
+ else
+ delete p_;
+ }
+
//
// Implementation for objects with object id.
//
@@ -356,7 +408,7 @@ namespace odb
container_statement_cache_type&
container_statment_cache ()
{
- return container_statement_cache_;
+ return container_statement_cache_.get (conn_);
}
public:
@@ -394,7 +446,8 @@ namespace odb
clear_delayed_ ();
private:
- container_statement_cache_type container_statement_cache_;
+ container_statement_cache_ptr<container_statement_cache_type>
+ container_statement_cache_;
image_type image_;