Project

General

Profile

Actions

Creation of a new type for Scol » History » Revision 1

Revision 1/3 | Next »
ModularMix, 10/14/2011 05:41 PM


Creation of a new type for Scol

Similarly to the functions, it is possible to extend Scol language by creating new data types using the integration of a new C/C++ plugin.

Definition of "Bloc" type

We will create a new Scol type, which will store a name and an integer value. We will also have to define the getters and setters for both properties. As we will implement this new type using a C++ class, we'll also define default constructor and destructor.
Let's create a new C++ header file named Bloc.h. Copy/paste the following code into it :

 // Include providing MAX_PATH define
 #include <windows.h>

 /*!
  * Bloc class. A simple class that has a name and a value associated with it.
  * It allows simple operations like getting and setting its attributes.
  *
  **/
 class Bloc
 {
 private:
    int value;                //!< The value of the bloc
    char name[MAX_PATH];    //!< The name of the bloc

 public:
    int getValue();
    char* getName();
    void setName(char*);
    void setValue(int);
    Bloc(void);
    ~Bloc(void);
 };

Now, let's create the source file (Bloc.cpp) in which we will define all the functions. There is nothing specific to explain about this source code, it's pretty easy to understand it.

 #include "Bloc.h" 
 #include <string.h>
 #include <stdio.h>

 /*!
 * \brief Bloc constructor
 * 
 */
 Bloc::Bloc(void)
 {
    value=0;
    strcpy_s(name,"SuperBloc");
 }

 /*!
 * \brief Bloc destructor
 * 
 */
 Bloc::~Bloc(void)
 {
 }

 /*!
 * \brief Gives the bloc value
 * \return The bloc value 
 */
 int Bloc::getValue()
 {
    return value;
 }

 /*!
 * \brief Gives the bloc name
 * \return The bloc name
 * 
 */
 char * Bloc::getName()
 {
    return name;
 }

 /*!
 * \brief Set the bloc value
 * \param myValue : The new value 
 */
 void Bloc::setValue(int myValue)
 {
    value = myValue;
 }

 /*!
 * \brief Set the bloc name
 * \param myValue : The new name 
 */
 void Bloc::setName(char* myName)
 {
    strncpy_s(name, myName, MAX_PATH-1);
 }

Updated by ModularMix over 12 years ago · 1 revisions