lean cpp library
A lean C++ library providing efficient utility classes for high-performance C++ applications.
property_types.h
00001 /*****************************************************/
00002 /* lean Properties              (c) Tobias Zirr 2011 */
00003 /*****************************************************/
00004 
00005 #ifndef LEAN_PROPERTIES_PROPERTY_TYPES
00006 #define LEAN_PROPERTIES_PROPERTY_TYPES
00007 
00008 #include "../lean.h"
00009 #include "property.h"
00010 #include "../type_info.h"
00011 #include "../memory/default_heap.h"
00012 
00013 namespace lean
00014 {
00015 namespace properties
00016 {
00017 
00019 template <class Type, class Heap = default_heap, size_t Alignment = alignof(Type)>
00020 struct generic_property_type : public property_type
00021 {
00023     typedef Type value_type;
00025     typedef default_heap heap_type;
00027     static const size_t alignment = Alignment;
00028 
00030     size_t size(size_t count) const
00031     {
00032         return sizeof(value_type) * count;
00033     }
00035     const struct type_info& type_info() const
00036     {
00037         return get_type_info<value_type>();
00038     }
00039 
00041     void* allocate(size_t count) const
00042     {
00043         return Heap::allocate<Alignment>(size(count));
00044     }
00046     void construct(void *elements, size_t count) const
00047     {
00048         new (elements) value_type[count];
00049     }
00051     void destruct(void *elements, size_t count) const
00052     {
00053         for (size_t i = 0; i < count; ++i)
00054             static_cast<value_type*>(elements)[i].~value_type();
00055     }
00057     void deallocate(void *elements, size_t count) const
00058     {
00059         Heap::free<Alignment>(elements);
00060     }
00061 };
00062 
00064 template <class Type>
00065 LEAN_INLINE const property_type& get_property_type()
00066 {
00067     static generic_property_type<Type> type;
00068     return type;
00069 }
00070 
00071 } // namespace
00072 
00073 using properties::generic_property_type;
00074 using properties::get_property_type;
00075 
00076 } // namespace
00077 
00078 #endif