xref: /trunk/main/vos/source/object.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <string.h>
29 
30 #include <rtl/alloc.h>
31 #include <rtl/memory.h>
32 
33 #include <vos/diagnose.hxx>
34 
35 #include <vos/object.hxx>
36 
37 using namespace vos;
38 
39 /////////////////////////////////////////////////////////////////////////////
40 // Object super class
41 
42 VOS_NAMESPACE(OClassInfo, vos) VOS_NAMESPACE(OObject, vos)::__ClassInfo__(VOS_CLASSNAME(OObject, vos), sizeof(VOS_NAMESPACE(OObject, vos)));
43 
44 OObject::OObject()
45 {
46 }
47 
48 OObject::OObject(const OCreateParam&)
49 {
50 }
51 
52 OObject::~OObject()
53 {
54 }
55 
56 void* OObject::operator new(size_t size)
57 {
58    void* p = rtl_allocateMemory(size);
59 
60    VOS_ASSERT(p != NULL);
61 
62    return (p);
63 }
64 
65 void* OObject::operator new(size_t, void* p)
66 {
67    return (p);
68 }
69 
70 void OObject::operator delete(void* p)
71 {
72    rtl_freeMemory(p);
73 }
74 
75 const OClassInfo& OObject::classInfo()
76 {
77 	return (__ClassInfo__);
78 }
79 
80 const OClassInfo& OObject::getClassInfo() const
81 {
82 	return (VOS_CLASSINFO(VOS_NAMESPACE(OObject, vos)));
83 }
84 
85 sal_Bool OObject::isKindOf(const OClassInfo& rClass) const
86 {
87     VOS_ASSERT(this != NULL);
88 
89     const OClassInfo& rClassThis = getClassInfo();
90 
91     return (rClassThis.isDerivedFrom(rClass));
92 }
93 
94 /////////////////////////////////////////////////////////////////////////////
95 // Basic class information
96 
97 OClassInfo::OClassInfo(const sal_Char *pClassName, sal_Int32 ObjectSize,
98 		               const OClassInfo* pBaseClass, sal_uInt32 Schema,
99 			           OObject* (SAL_CALL * fnCreateObject)(const OCreateParam&))
100 {
101 	m_pClassName  = pClassName;
102 	m_nObjectSize = ObjectSize;
103 	m_wSchema     = Schema;
104 
105 	m_pfnCreateObject = fnCreateObject;
106 
107 	m_pBaseClass = pBaseClass;
108 	m_pNextClass = NULL;
109 }
110 
111 OObject* OClassInfo::createObject(const OCreateParam& rParam) const
112 {
113 	if (m_pfnCreateObject == NULL)
114 		return NULL;
115 
116     OObject* pObject = NULL;
117 	pObject = (*m_pfnCreateObject)(rParam);
118 
119 	return (pObject);
120 }
121 
122 sal_Bool OClassInfo::isDerivedFrom(const OClassInfo& rClass) const
123 {
124 	VOS_ASSERT(this != NULL);
125 
126 	const OClassInfo* pClassThis = this;
127 
128 	while (pClassThis != NULL)
129 	{
130 		if (pClassThis == &rClass)
131 			return (sal_True);
132 
133 		pClassThis = pClassThis->m_pBaseClass;
134 	}
135 
136 	return (sal_False);      // walked to the top, no match
137 }
138 
139 const OClassInfo* OClassInfo::getClassInfo(const sal_Char* pClassName)
140 {
141 	VOS_ASSERT(pClassName != NULL);
142 
143 	const OClassInfo* pClass = &VOS_CLASSINFO(VOS_NAMESPACE(OObject, vos));
144 
145 	while (pClass != NULL)
146 	{
147 		if (strcmp(pClassName, pClass->m_pClassName) == 0)
148 			break;
149 
150 		pClass = pClass->m_pNextClass;
151 	}
152 
153 	return (pClass);
154 }
155 
156 VOS_CLASSINIT::VOS_CLASSINIT(register OClassInfo* pNewClass)
157 {
158 	VOS_ASSERT(pNewClass != NULL);
159 
160 	OClassInfo* pClassRoot = (OClassInfo*)&VOS_CLASSINFO(VOS_NAMESPACE(OObject, vos));
161 
162 	pNewClass->m_pNextClass = pClassRoot->m_pNextClass;
163 
164 	pClassRoot->m_pNextClass = pNewClass;
165 }
166