coherence/lang/TypedClass.hpp

00001 /*
00002 * TypedClass.hpp
00003 *
00004 * Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.
00005 *
00006 * Oracle is a registered trademarks of Oracle Corporation and/or its
00007 * affiliates.
00008 *
00009 * This software is the confidential and proprietary information of Oracle
00010 * Corporation. You shall not disclose such confidential and proprietary
00011 * information and shall use it only in accordance with the terms of the
00012 * license agreement you entered into with Oracle.
00013 *
00014 * This notice may not be removed or altered.
00015 */
00016 #ifndef COH_TYPED_CLASS_HPP
00017 #define COH_TYPED_CLASS_HPP
00018 
00019 #include "coherence/lang/compatibility.hpp"
00020 
00021 #include "coherence/lang/Class.hpp"
00022 #include "coherence/lang/ClassLoader.hpp"
00023 #include "coherence/lang/IllegalArgumentException.hpp"
00024 
00025 #include <ostream>
00026 #include <typeinfo>
00027 
00028 COH_OPEN_NAMESPACE2(coherence,lang)
00029 
00030 
00031 /**
00032 * TypedClass provides a templated implementation of Class.
00033 *
00034 * TypedClass supports classes which include a no-argument create method.
00035 *
00036 * @author mf  2008.04.03
00037 */
00038 template<class T> class TypedClass
00039     : public class_spec<TypedClass<T>,
00040         extends<Class> >
00041     {
00042     friend class factory<TypedClass<T> >;
00043 
00044     // ----- typedefs -------------------------------------------------------
00045 
00046     public:
00047         /**
00048         * Class type.
00049         */
00050         typedef T Type;
00051 
00052     // ----- constructors ---------------------------------------------------
00053 
00054     protected:
00055         /**
00056         * Create a new TypedClass.
00057         */
00058         TypedClass<T>()
00059             : class_spec<TypedClass<T>, extends<Class> >(typeid(T))
00060             {
00061             }
00062 
00063 
00064     // ----- TypedClass interface -------------------------------------------
00065 
00066     public:
00067         /**
00068         * {@inheritDoc}
00069         */
00070         virtual Object::Handle newInstance(ObjectArray::View vaParam) const
00071             {
00072             if (NULL != vaParam)
00073                 {
00074                 COH_THROW (IllegalArgumentException::create
00075                         ("initialization parameter based instantiation in not supported on TypedClass"));
00076                 }
00077             return T::create();
00078             }
00079 
00080         /**
00081         * {@inheritDoc}
00082         */
00083         virtual const std::type_info& getTypeInfo() const
00084             {
00085             return typeid(T);
00086             }
00087 
00088         /**
00089         * {@inheritDoc}
00090         */
00091         virtual size32_t getSize() const
00092             {
00093             return (size32_t) sizeof(T);
00094             }
00095 
00096 
00097     // ----- Object interface -----------------------------------------------
00098 
00099     public:
00100         /**
00101         * {@inheritDoc}
00102         */
00103         virtual void toStream(std::ostream& out) const
00104             {
00105             out << "TypedClass<" << Class::getName() << ">";
00106             }
00107     };
00108 
00109 COH_CLOSE_NAMESPACE2
00110 
00111 #endif // COH_TYPED_CLASS_HPP
Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.