Code and Stuff
Social and stuff!
  • MOOSE

The MOOSE API (Part 1)

3/21/2014

0 Comments

 
Over the past few weeks, I have been working with the MOOSE framework, creating sample classes to understand the way they work.
Here, I will detail some of my example classes, to give some idea of what all are required to be done.

All of my work on these example classes are derivatives of the example provided as part of the MOOSE tutorial docs.

The first one was a class that has one data member 'x'. It doesn't do anything with this member though - pretty much as much of a dummy class as you can get! Nevertheless, it shows the way a normal C class can be elevated to the status of a MOOSE class with initCinfo.

Here is the header file:
class Example {

   private:
       int x_;

   public:

       int getX() const;
       void setX( int x );

       static const Cinfo *initCinfo();

}; 
It looks fairly similar to a C class with a few important additions. Firstly, the private variable x is always accompanied by get and set functions (getX() and setX()). Secondly, there is a function called initCinfo(), which provides the required functionality in order for MOOSE to recognize Example as a class.

This is the code for initCinfo, which forms the bulk of example.cpp:
const Cinfo* Example::initCinfo(){
   
   static ValueFinfo< Example, int > x(
       "x",
       "An example field of an example class",
       &Example::setX,
       &Example::getX
   );
   static Finfo *exampleFinfos[] = { &x };
   static Cinfo exampleCinfo(
       "Example",              // The name of the class in python
       Neutral::initCinfo(),   
       exampleFinfos,          // The array of Finfos created above
       1,                      // The number of Finfos
       new Dinfo< Example >()  // The class Example itself
   );
   
   return &exampleCinfo;
}
Before we go into the above code, we need to understand a little more about Moose Classes. Variables and functions in Moose Classes are stored in special fields called Finfos. They are of 3 main types - ValueFinfo, SrcFinfo and DestFinfo, although many others exist.
  • ValueFinfos store a single value. Moose creates a large number of helper functions implicitly for each ValueFinfo. These functions help with getting and setting values.
  • SrcFinfos are used as a connection point for the object to communicate with other objects. As you may have guessed, SrcFinfos can be used to send out data to other objects via messages.
  • DstFinfos are the points of reception of messages. They take a function of the class as parameter and work as a callback. Thus, when a DestFinfo receives a message, it simply calls the associated function and provides it access to the contents of the message.

Let's see how the example class above uses these Finfos. In this case, only one ValueFinfo has been used. No SrcFinfos or DstFinfos. the decleration consists of the name of the variable, a brief summary (the DocString), the set method and the get method.

After that, there is a definition of exampleFinfos, which is a list of all the addresses of every Finfo. This will go into exampleCinfo, defined immediately after.

exampleCinfo makes the class available to the rest of Moose. The first parameter is the name of the class. This is followed by the initializer for the parent class (the default parent class for a new class in Neutral). This is followed by the pointer pointing to the list of Finfos and the number of Finfos present in the list.

The rest of example.cpp is just the definitions for getX and setX:
int Example::getX() const
{
   return x_;
}

void Example::setX(int x)
{
   x_ = x;
}
Also make sure you incude header.h and example.h in the file.

That's everything there is to know about this very basic example class. In the next post, I will go into some more details about the Moose API by explaining another example class which has a few more features.
0 Comments



Leave a Reply.

    Vivek Vidyasagaran

    Participant, Google Summer of Code 2014

    Archives

    August 2014
    July 2014
    June 2014
    April 2014
    March 2014

    Categories

    All

    RSS Feed

Powered by Create your own unique website with customizable templates.