Xavax C++ Library Class Index   FAQ   Overview

Class xavax::Object

Object is a base class that works with Class to provide metadata used to implement type casting and introspection.

Constructor Summary
Object()
         Construct an object of class Object.

Method Summary
T* cast<T>()
         Determine if a pointer to this object can be safely cast as a pointer to type T*.
const T* cast<T>() const
         Determine if a pointer to a const object can be safely cast as a pointer to type const T*.
const Class* classRecord() const
         Return the class record for this object.
const Class* ClassRecord()
         Return the class record for class Object.
Object* clone() const
         Return a new copy of this object.
Object* create()
         Create an object of this class.
long hashCode()
         Return a hash value for this object.

Macro Summary
CxDefineClass(name, parent, creator)
         Define the class record for a class deriving from Object.
CxDefineClassNM(name, parent, creator)
         Define the class record for a class with no data members deriving from Object.
CxDefineTemplateClass(name, parent, creator)
         Define the class record for a template class deriving from Object.
CxDefineTemplateClassNM(name, parent, creator)
         Define the class record for a template class with no data members deriving from Object.
CxStandardMethods(name)
         Declares the virtual and static methods and static data required by a class deriving from Object.

Related Classes
Class

Constructor Detail

Object

Object()
Construct an object.

Method Detail

cast

T* cast<T>()
Determine if a pointer to this object can be safely cast to a pointer of type T*. If this is a member of the specified class or a derived class, return this cast as T*; otherwise, return null. T must be a class derived from Object and must implement the ClassRecord method.
Returns:
this cast as T* if the cast is valid; otherwise, returns null.

cast

const T* cast() const
Determine if a pointer to a const object can be safely cast to a pointer of type const T*. If this is a member of the specified class or a derived class, return this cast as const T*; otherwise, return null. T must be a class derived from Object and must implement the ClassRecord method.
Returns:
this cast as const T* if the cast is valid; otherwise, returns null.

classRecord

const Class* classRecord() const
Return the class record for this object. This is a pure virtual method. All derived classes must implement this method.
Returns:
a pointer to the class record for this object.

ClassRecord

static const Class* ClassRecord()
Return the class record for class Object. All derived classes should implement this static method.
Note: This method name begins with an upper case character to allow the C++ compiler to distinguish the static method from the non-static method.
Returns:
a pointer to the class record for class Object.

clone

Object* clone() const
Return a new copy of this object. All derived classes must implement this pure virtual function.
Returns:
a pointer to a new copy of this object.

create

Object* create()
Create an object of this class. Derived classes should implement this method to enable the Class::create method to function. A pointer to this static method should be passed to the Class constructor when the class record is initialized.
Returns:
a pointer to a new object.

hashCode

long hashCode() const
Return a hash value for this object. The default implementation of this virtual method returns the pointer this cast as a long.
Returns:
a hash value for this object.

Macro Detail

CxDefineClass

CxDefineClass(name, parent)
Defines a class record (Class object) for a class deriving from Object and implements the classRecord and ClassRecord methods. This macro assumes the existance of a static array of member descriptors named _members. This macro is typically used near the beginning of the implementation file.
Parameters:
name - the class name.
parent - the parent class name.

CxDefineClassNM

CxDefineClassNM(name, parent)
Defines a class record (Class object) for a class with no members deriving from Object and implements the classRecord and ClassRecord methods. This macro is typically used near the beginning of the implementation file.
Parameters:
name - the class name.
parent - the parent class name.

CxDefineTemplateClass

CxDefineTemplateClass(name, parent, creator)
Defines a class record (Class object) for a template class deriving from Object and implements the classRecord and ClassRecord methods. This macro assumes the existance of a static array of member descriptors named _members. This macro is typically used near the beginning of the implementation file.
Parameters:
name - the class name.
parent - the parent class name.
creator - static function to create an object.

CxDefineClassNM

CxDefineTemplateClassNM(name, parent, creator)
Defines a class record (Class object) for a class with no members deriving from Object and implements the classRecord and ClassRecord methods. This macro is typically used near the beginning of the implementation file.
Parameters:
name - the class name.
parent - the parent class name.
creator - static function to create an object.

CxStandardMethods

CxStandardMethods(name)
Declares all of the virtual and static methods and static data required by a class deriving from Object. This macro should be the last line within the definition of a class.
Parameters:
name - the class name.

Example Code

#include Headers_h
#include Headers_h
#include Cx_h
#include Object_h
#include <iostream.h>

//
// Define classes with the following inheritance tree
// to use for testing the cast methods.
//
//         Object
//            |
//     +------+------+
//     |             |
//  Gadget         Widget
//                   |
//               Scrollbar       
//
class Widget : public Object {
public:
  Widget() {};
  ~Widget() {};
  CxStandardMethods(Widget);
};
CxDefineClassNM(Widget, Object);

class Gadget : public Object {
public:
  Gadget() {};
  ~Gadget() {};
  CxStandardMethods(Gadget);
};
CxDefineClassNM(Gadget, Object);

class Scrollbar : public Widget {
public:
  Scrollbar() {};
  ~Scrollbar() {};
  CxStandardMethods(Scrollbar);
};
CxDefineClassNM(Scrollbar, Widget);

//
// Test the cast method.  Casting up the inheritance tree
// (from Widget to Object) should pass.  Casting down
// the tree (from Widget to Scrollbar) or sideways (from
// Widget to Gadget) should fail.
//
void test1()
{
  CxTraceEnter("test1");
  Widget* wp = new Widget();
  Gadget* p1 = wp->cast<Gadget>();
  cout << "cast Widget to Gadget "
       << (p1 ? "passed" : "failed") << endl;
  Object* p2 = wp->cast<Object>();
  cout << "cast Widget to Object "
       << (p2 ? "passed" : "failed") << endl;
  Widget* p3 = wp->cast<Widget>();
  cout << "cast Widget to Widget "
       << (p3 ? "passed" : "failed") << endl;
  Scrollbar* p4 = wp->cast<Scrollbar>();
  cout << "cast Widget to Scrollbar "
       << (p4 ? "passed" : "failed") << endl;
  CxTraceReturn;
}

//
// Test the const cast method.
//
void test2()
{
  CxTraceEnter("test2");
  const Widget* wp = new Widget();
  const Gadget* p1 = wp->cast<Gadget>();
  cout << "cast const Widget to const Gadget "
       << (p1 ? "passed" : "failed") << endl;
  const Object* p2 = wp->cast<Object>();
  cout << "cast const Widget to const Object "
       << (p2 ? "passed" : "failed") << endl;
  const Widget* p3 = wp->cast<Widget>();
  cout << "cast const Widget to const Widget "
       << (p3 ? "passed" : "failed") << endl;
  const Scrollbar* p4 = wp->cast<Scrollbar>();
  cout << "cast const Widget to const Scrollbar "
       << (p4 ? "passed" : "failed") << endl;
  CxTraceReturn;
}

main(int argc, char **argv)
{
  CxTraceEnter("main");
  test1();
  test2();
  CxTraceReturn EXIT_SUCCESS;
}

Copyright © 2003 Xavax Inc. -- All Rights Reserved