Project

General

Profile

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

ModularMix, 10/14/2011 05:41 PM

1 1 ModularMix
h1. Creation of a new type for Scol
2 1 ModularMix
3 1 ModularMix
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.
4 1 ModularMix
5 1 ModularMix
6 1 ModularMix
h2. Definition of "Bloc" type
7 1 ModularMix
8 1 ModularMix
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.
9 1 ModularMix
Let's create a new C++ header file named *Bloc.h*. Copy/paste the following code into it :
10 1 ModularMix
<pre>
11 1 ModularMix
 // Include providing MAX_PATH define
12 1 ModularMix
 #include <windows.h>
13 1 ModularMix
 
14 1 ModularMix
 /*!
15 1 ModularMix
  * Bloc class. A simple class that has a name and a value associated with it.
16 1 ModularMix
  * It allows simple operations like getting and setting its attributes.
17 1 ModularMix
  *
18 1 ModularMix
  **/
19 1 ModularMix
 class Bloc
20 1 ModularMix
 {
21 1 ModularMix
 private:
22 1 ModularMix
    int value;	            //!< The value of the bloc
23 1 ModularMix
    char name[MAX_PATH];    //!< The name of the bloc
24 1 ModularMix
 
25 1 ModularMix
 public:
26 1 ModularMix
    int getValue();
27 1 ModularMix
    char* getName();
28 1 ModularMix
    void setName(char*);
29 1 ModularMix
    void setValue(int);
30 1 ModularMix
    Bloc(void);
31 1 ModularMix
    ~Bloc(void);
32 1 ModularMix
 };
33 1 ModularMix
</pre>
34 1 ModularMix
35 1 ModularMix
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.
36 1 ModularMix
<pre>
37 1 ModularMix
 #include "Bloc.h"
38 1 ModularMix
 #include <string.h>
39 1 ModularMix
 #include <stdio.h>
40 1 ModularMix
 
41 1 ModularMix
 /*!
42 1 ModularMix
 * \brief Bloc constructor
43 1 ModularMix
 * 
44 1 ModularMix
 */
45 1 ModularMix
 Bloc::Bloc(void)
46 1 ModularMix
 {
47 1 ModularMix
    value=0;
48 1 ModularMix
    strcpy_s(name,"SuperBloc");
49 1 ModularMix
 }
50 1 ModularMix
 
51 1 ModularMix
 /*!
52 1 ModularMix
 * \brief Bloc destructor
53 1 ModularMix
 * 
54 1 ModularMix
 */
55 1 ModularMix
 Bloc::~Bloc(void)
56 1 ModularMix
 {
57 1 ModularMix
 }
58 1 ModularMix
 
59 1 ModularMix
 /*!
60 1 ModularMix
 * \brief Gives the bloc value
61 1 ModularMix
 * \return The bloc value 
62 1 ModularMix
 */
63 1 ModularMix
 int Bloc::getValue()
64 1 ModularMix
 {
65 1 ModularMix
    return value;
66 1 ModularMix
 }
67 1 ModularMix
 
68 1 ModularMix
 /*!
69 1 ModularMix
 * \brief Gives the bloc name
70 1 ModularMix
 * \return The bloc name
71 1 ModularMix
 * 
72 1 ModularMix
 */
73 1 ModularMix
 char * Bloc::getName()
74 1 ModularMix
 {
75 1 ModularMix
    return name;
76 1 ModularMix
 }
77 1 ModularMix
 
78 1 ModularMix
 /*!
79 1 ModularMix
 * \brief Set the bloc value
80 1 ModularMix
 * \param myValue : The new value 
81 1 ModularMix
 */
82 1 ModularMix
 void Bloc::setValue(int myValue)
83 1 ModularMix
 {
84 1 ModularMix
    value = myValue;
85 1 ModularMix
 }
86 1 ModularMix
 
87 1 ModularMix
 /*!
88 1 ModularMix
 * \brief Set the bloc name
89 1 ModularMix
 * \param myValue : The new name 
90 1 ModularMix
 */
91 1 ModularMix
 void Bloc::setName(char* myName)
92 1 ModularMix
 {
93 1 ModularMix
    strncpy_s(name, myName, MAX_PATH-1);
94 1 ModularMix
 }
95 1 ModularMix
</pre>