2005/5/10

     
 

Object in ACDK

artefaktur

| Basics | Objects | Types | Hello World |


We now want to introduce to you the object classes of ACDK.


Content of this chapter:

   Traditional C++-Classes
   Derived Types
   Simple ACDK-Classes
   The ACDK class
   Interfaces
   Classes with Interfaces
   Namespaces
   Next steps



 Traditional C++-Classes

Since ACDK is still C++, you can define normal C++-classes this way:

// this is a NON ACDK class
class AClass
{
  int _val;
public:
  AClass()
    : _val(42)
  {
  }
  // a NON ACDK class can make usage of ACDK classes.
  RString get()
  {
    RStringBuffer sb = new StringBuffer();
    sb.append(_val);
    return sb->toString();
  }
};


 Derived Types


To create C++ classes, which fits into the ACDK framework (a profit from ACDK features like Gargage collection, using in collections, reflection, usable via scripting languages, etc.) the C++ class has to fullfill several conditions.

ACDK follows the rules of Java concerning the designing of classes:
  • All concrete Classes (no Interfaces) must be directly or indirectly derived from acdk::lang::Object
  • Multiple inheritence from acdk::lang::Object is not allowed.
  • Interfaces are pure abstract classes.
  • A Class can implement multiple Interfaces.

 Simple ACDK-Classes

To define ACDK-Classes, you should derive them from the class acdk::lang::Object or other ACDK-Classes:



#include <acdk.h>

// defining RAClass, AClassArray and RAClassArray
ACDK_DECL_CLASS(AClass);

class AClass
: extends acdk::lang::Object
{
  int _val;
public:
  AClass(int number)
  : Object()
  , _val(number)
  {
  }
};
RAClass aclass = new AClass(42);

// or create a specialized version of a container
#include <acdk/util/HashMap.h>

ACDK_DECL_CLASS(ASpecialMap);

class ASpecialMap
: extends acdk::util::HashMap
{
  // ....
};


 The ACDK class

Another example with header and source file:

// AClass.h
#include <acdk.h>

// defining RAClass, AClassArray and RAClassArray
ACDK_DECL_CLASS(AClass);

class AClass
: extends acdk::lang::Object
{
private:
  int _val; // basic type
  RString _label; // Member
public:
  AClass(int number = 0)
  : Object()
  , _val(number)
  , _label(new String("")) // initialize with empty String
  {
  }
  virtual RString getLabel() { return _label; }
  virtual void setLabel(IN(RString) newlabel} { _label = newlabel; }
  virtual int calcLengthOfOldLabel();
  static int calcLengthOfString(IN(RString) str);
};

// AClass.cpp
//virtual
int
AClass::calcLengthOfOldLabel()
{
  if (_label == Nil)
    return _val;
  return _val = _label->length();
}

//static
int
AClass::calcLengthOfString(IN(RString) str)
{
  RAClass aclass = new AClass();
  aclass->setLabel(str);
  return aclass->calcLengthOfOldLabel();
}


Please refer also to:

 Interfaces

Interfaces are used quite similar as in Java:

#include <acdk.h>

ACDK_DECL_INTERFACE(AInterface);

class AInterface
      ACDK_INTERFACEBASE
{
public:
  virtual void doIt(IN(RString) arg) = 0;
};

Please refer also to:  Interface Types.

 Classes with Interfaces

On given Class AClass and AInterface a Class, which implements an interface looks like this:


class BClass
: extends AClass
, implements AInterface
{
public:
  BClass()
  : AClass()
  {
  }
  virtual void doIt(RString arg)
  {
    // implementation here
  }
};


 Namespaces

ACDK utilizes the C++ namespaces to group classes into packages.


#include <acdk.h>

using namespace acdk::lang;
using namespace acdk::io;

class MyWriter
: extends acdk::io::AbstractWriter,
  implements acdk::io::Serializable
{
  // ...
};
Instead of using namespace you can use

USING_CLASS(::acdk::namespace::, ClassName);
this might be necessary with some compilers, because they don't expand the namespace accurate e.g. MS VC++.

#include <acdk.h>

USING_CLASS(::acdk::lang::, String);
USING_CLASS(::acdk::io::, File);

class MyWriter
: extends acdk::io::AbstractWriter,
  implements acdk::io::Serializable
{
  // ...
};
Please refer also to:  Namespace Types.

 Next steps

To make usage of one of the ACDK object modell meta features (like serialization, advanced garbage collecting, invokable from script code) Metainfo has to be generated for these classes.

Please read at  Metainfo how to generate and use Metainfo for your own C++ classes.


 < prevTutorial(2 / 4) next >