File: GraphClass.mc

package info (click to toggle)
openc%2B%2B 2.5.3-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 3,268 kB
  • ctags: 7,251
  • sloc: cpp: 30,583; ansic: 16,718; makefile: 336; asm: 209; tcl: 126; sh: 3
file content (196 lines) | stat: -rw-r--r-- 4,123 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* -*-Mode: C++;-*-
 *
 *  GraphClass.mc
 *  Author : Renaud Pawlak (pawlak@ecoledoc.lip6.fr)
 *
 *  If you find out some problems in this sample, just email me :
 *  pawlak@ecoledoc.lip6.fr / pawlak@cnam.fr
 */

#include "GraphClass.h"
#include <fstream.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>

char *outname = "gclass-test.wish";

// Initiatization of static slots...

ofstream* GraphClass::fsp;
int GraphClass::nofClasses = 0;

// Redefinition of Class::Initialize()

bool GraphClass::Initialize()
{
    ChangeDefaultMetaclass("GraphClass");

    /* ofstream should be instantiated and fsp should be initialized
       here since Initialize() may be invoked before initializing
       static members. */
    fsp = new ofstream(outname);

    ofstream& f = *fsp;
    FILE *base;
    char ch;
    char *buf;    
    int lbase;

    if(!(base = fopen("baseScript","r")))
	cerr << "Unable to find base tk script...\n";
    fseek(base, 0L, SEEK_END);
    lbase = ftell(base);
    fseek(base, 0L, SEEK_SET);
    buf = (char*)malloc(lbase);
    fread(buf, sizeof(char), lbase-1, base);
    f << buf << endl;
    free(buf);
    fclose(base);
    return true;
}

// Redefinition of Class::TranslateClass()

void GraphClass::TranslateClass(Environment* env)
{
    Ptree *pt = Name();
    Member m;
    int i;
    bool written;
    TypeInfo type;
    ofstream& f = *fsp;

    // call the 'makeClass' tk routine...
    f << "makeClass " << (nofClasses*200)%900 << " 0 ";
    nofClasses++;
    pt->Write(f);

    // create public members...
    f << " {";
    i=1; written=FALSE;
    while(NthMember(i,m)) {
	if(m.IsPublic()) {
	    if(written) f << " ";
	    f << "{";
	    m.Name()->Write(f);
	    if(m.IsFunction()) {
		f << "(";
		m.ArgumentList()->Write(f);
		f << ")";
	    } else {      
		f << " : ";
		m.Signature(type);
		if(type.IsReferenceType() || type.IsPointerType())
		    type.Dereference();
		type.FullTypeName()->Write(f);
	    }
	    f << "}";
	    written=TRUE;
	}
	i++;
    }

    // create protected members...
    f << "} {";
    i=1; written=FALSE;
    while(NthMember(i,m)) {
	if(m.IsProtected()) {
	    if(written) f << " ";
	    f << "{";
	    m.Name()->Write(f);
	    if(m.IsFunction()) {
		f << "(";
		m.ArgumentList()->Write(f);
		f << ")";
	    } else {      
		f << " : ";
		m.Signature(type);
		if(type.IsReferenceType() || type.IsPointerType())
		    type.Dereference();
		type.FullTypeName()->Write(f);
	    }
	    f << "}";
	    written=TRUE;
	}
	i++;
    }

    // create private members...
    f << "} {";
    i=1; written=FALSE;
    while(NthMember(i,m)) {
	if(m.IsPrivate()) {
	    if(written) f << " ";
	    f << "{";
	    m.Name()->Write(f);
	    if(m.IsFunction()) {
		f << "(";
		m.ArgumentList()->Write(f);
		f << ")";
	    } else {      
		f << " : ";
		m.Signature(type);
		if(type.IsReferenceType() || type.IsPointerType())
		    type.Dereference();
		type.FullTypeName()->Write(f);
	    }
	    f << "}";
	    written=TRUE;
	}
	i++;
    }
    f << "}\n";
}

// Redefinition of Class::FinalizeClass()

Ptree *GraphClass::FinalizeClass()
{
    ClassArray classes;
    int nclasses = InstancesOf("GraphClass", classes);
    Member member;
    int nmember, relatedClass;
    TypeInfo type;
    ofstream& f = *fsp;

    // create relations if needed...
    if(!nclasses)
	return nil;

    for(int i = 0; i < nclasses; i++) {
	nmember = 1;
	while(classes[i]->NthMember(nmember,member)) {
	    member.Signature(type);
	    if(relatedClass=SearchClass(type,classes))
		f << "makeRelation " << i+1 << " " << relatedClass << "\n";
	    nmember++;
	}
    }
    f.close();
#if !defined(_MSC_VER)
    chmod(outname, S_IREAD|S_IEXEC);
#endif
    cout << "'" << outname << "'" << " has been sucessfully generated...\n";
    return nil;
}  

int GraphClass::SearchClass(TypeInfo& type,ClassArray& classes)
{
    int nclasses=classes.Number();

    if(type.IsPointerType() || type.IsReferenceType())
	type.Dereference();

    for(int i=0;i<nclasses;i++)
	if(classes[i] == type.ClassMetaobject())
	    return i+1;

    return 0;
}