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