Storage policies |
Classes that inherit from value are automatically garbage collected (using
the intrusive_ptr smart pointer). This is very convenient, as the user is
liberated
from the responsibility to think about memory management. When dealing with
other classes, the user must specify how CliPP should hande allocation and
deallocation for those classes. This is done by specifying a storage_policy to
the class.
All storage policies needs to follow the pattern below:
struct some_storage_policy : storage_policy_tag
{
template<typename T>
struct apply {
typedef <some_type> type;
};
typedef <some_tag> create_tag;
};
CliPP has two storage policies today. default_storage_policy and direct_storage_policy.
1. default_storage_policy is the default storage policy for all classes inheriting from value use.
struct default_storage_policy : storage_policy_tag
{
template<typename T>
struct apply {
typedef T type;
};
typedef detail::constructor_tag create_tag;
};
The main characteristics for this storage policy are:
2. direct_storage_policy can be used when wrapping small objects on the
stack.
struct direct_storage_policy : storage_policy_tag
{
template<typename T>
struct apply {
typedef class_wrapper<T> type;
};
typedef detail::constructor_no_new_tag create_tag;
};
The main characteristics for this storage policy are:
This is a very simple storage policy. class_wrapper<T> stores
instances of T by value, making memory management as simple as possible.
Here is the source for class_wrapper:
template<typename T>
class class_wrapper : public object {
public:
class_wrapper(const T& rhs) : object_(rhs) {}
virtual ~class_wrapper() {}
operator T&() {return object_;}
static void init(context* c) {}
private:
T object_;
};
Copyright © 2003-2005 Peder Holt
Permission to copy, use, modify, sell and distribute this document
is granted provided this copyright notice appears in all copies. This document
is provided "as is" without express or implied warranty, and with
no claim as to its suitability for any purpose.