Initial commit
This commit is contained in:
commit
28285e9df3
|
@ -0,0 +1,91 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "ColoredSquare.h"
|
||||||
|
|
||||||
|
void ColoredSquare_constructor (void *this)
|
||||||
|
{
|
||||||
|
printf ("ColoredSquare_constructor (%lu) called\n",
|
||||||
|
(unsigned long) this);
|
||||||
|
Square_constructor (this);
|
||||||
|
((ColoredSquare *) this)->color = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ColoredSquare_copy (void *to, void *from)
|
||||||
|
{
|
||||||
|
printf ("ColoredSquare_copy (%lu, %lu) called\n",
|
||||||
|
(unsigned long) to,
|
||||||
|
(unsigned long) from);
|
||||||
|
Square_copy (to, from);
|
||||||
|
((ColoredSquare *) to)->color = ((ColoredSquare *) from)->color;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* ColoredSquare_clone (void *this)
|
||||||
|
{
|
||||||
|
ColoredSquare *csquare = ColoredSquare_new ();
|
||||||
|
printf ("ColoredSquare_clone (%lu) called\n",
|
||||||
|
(unsigned long) this);
|
||||||
|
ColoredSquare_copy (csquare, this);
|
||||||
|
printf ("ColoredSquare_clone (%lu) returns %lu\n",
|
||||||
|
(unsigned long) this,
|
||||||
|
(unsigned long) csquare);
|
||||||
|
|
||||||
|
return csquare;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* ColoredSquare_type (void *this)
|
||||||
|
{
|
||||||
|
static const char *csquare_type_str = "ColoredSquare";
|
||||||
|
printf ("ColoredSquare_type (%lu) called\n",
|
||||||
|
(unsigned long) this);
|
||||||
|
printf ("ColoredSquare_type (%lu) returns \"%s\"\n",
|
||||||
|
(unsigned long) this,
|
||||||
|
csquare_type_str);
|
||||||
|
return csquare_type_str;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ColoredSquare_draw (void *this)
|
||||||
|
{
|
||||||
|
printf ("ColoredSquare_draw (%lu) called\n",
|
||||||
|
(unsigned long) this);
|
||||||
|
printf ("Drawing ColoredSquare with %f side and %d color\n",
|
||||||
|
((ColoredSquare *) this)->a,
|
||||||
|
((ColoredSquare *) this)->color);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ColoredSquare_set_color (void *this, int color)
|
||||||
|
{
|
||||||
|
printf ("ColoredSquare_draw (%lu, %d) called\n",
|
||||||
|
(unsigned long) this,
|
||||||
|
color);
|
||||||
|
|
||||||
|
((ColoredSquare *) this)->color = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* public */
|
||||||
|
void* ColoredSquare_new ()
|
||||||
|
{
|
||||||
|
static ColoredSquare_interface vtable =
|
||||||
|
{
|
||||||
|
ColoredSquare_clone,
|
||||||
|
Square_destroy,
|
||||||
|
ColoredSquare_type,
|
||||||
|
ColoredSquare_draw,
|
||||||
|
Square_area,
|
||||||
|
Square_resize,
|
||||||
|
Square_diag_length,
|
||||||
|
ColoredSquare_set_color
|
||||||
|
};
|
||||||
|
ColoredSquare *square = malloc (sizeof (*square));
|
||||||
|
|
||||||
|
printf ("ColoredSquare_new () returns %lu\n",
|
||||||
|
(unsigned long) square);
|
||||||
|
|
||||||
|
square->vtable = (void *) &vtable;
|
||||||
|
|
||||||
|
/*goto end;
|
||||||
|
err:
|
||||||
|
|
||||||
|
end:*/
|
||||||
|
|
||||||
|
return square;
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
#ifndef __COLORED_SQUARE_H__
|
||||||
|
#define __COLORED_SQUARE_H__
|
||||||
|
|
||||||
|
#include "Square.h"
|
||||||
|
|
||||||
|
typedef struct ColoredSquare_data
|
||||||
|
{
|
||||||
|
Square_data;
|
||||||
|
|
||||||
|
int color;
|
||||||
|
|
||||||
|
} ColoredSquare_data;
|
||||||
|
|
||||||
|
typedef struct ColoredSquare_interface
|
||||||
|
{
|
||||||
|
Square_interface;
|
||||||
|
|
||||||
|
void (*set_color) (void *this, int color);
|
||||||
|
|
||||||
|
} ColoredSquare_interface;
|
||||||
|
|
||||||
|
void ColoredSquare_constructor (void *this);
|
||||||
|
void ColoredSquare_copy (void *to, void *from);
|
||||||
|
void* ColoredSquare_clone (void *this);
|
||||||
|
const char* ColoredSquare_type (void *this);
|
||||||
|
void ColoredSquare_draw (void *this);
|
||||||
|
void ColoredSquare_set_color (void *this, int color);
|
||||||
|
|
||||||
|
/* public */
|
||||||
|
typedef struct ColoredSquare
|
||||||
|
{
|
||||||
|
ColoredSquare_interface *vtable;
|
||||||
|
|
||||||
|
ColoredSquare_data;
|
||||||
|
|
||||||
|
} ColoredSquare;
|
||||||
|
|
||||||
|
void* ColoredSquare_new ();
|
||||||
|
|
||||||
|
#endif // __COLORED_SQUARE_H__
|
|
@ -0,0 +1,24 @@
|
||||||
|
#ifndef __FIGURE_H__
|
||||||
|
#define __FIGURE_H__
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "Object.h"
|
||||||
|
|
||||||
|
typedef struct Figure_interface
|
||||||
|
{
|
||||||
|
Object_interface;
|
||||||
|
|
||||||
|
const char* (*type) (void *this);
|
||||||
|
void (*draw) (void *this);
|
||||||
|
double (*area) (void *this);
|
||||||
|
|
||||||
|
} Figure_interface;
|
||||||
|
|
||||||
|
typedef struct Figure
|
||||||
|
{
|
||||||
|
Figure_interface *vtable;
|
||||||
|
|
||||||
|
} Figure;
|
||||||
|
|
||||||
|
#endif // __FIGURE_H__
|
|
@ -0,0 +1,125 @@
|
||||||
|
# Makefile generated by command: smake.sh -t main --cflags=-fms-extensions
|
||||||
|
# This file is generated with smake.sh.
|
||||||
|
# You can use this make file with instruction make to
|
||||||
|
# use one of build mode: debug, profile, develop, release.
|
||||||
|
# No need to call make clean if You make with other mode,
|
||||||
|
# because the Makefile containes rules for automatically clean.
|
||||||
|
# Some usage examples:
|
||||||
|
# make # default mode is debug
|
||||||
|
# CFLAGS="-O2 -march=core2 -mtune=core2 -msse4.1 -mfpmath=sse -fomit-frame-pointer -pipe" LDFLAGS="-Wl,-O1" make mode=develop
|
||||||
|
# CFLAGS="-O2 -march=amdfam10 -mtune=amdfam10 -msse4a --mfpmath=sse -fomit-frame-pointer -pipe" LDFLAGS="-Wl,-O1" make mode=profile
|
||||||
|
# CFLAGS="-O2 -march=k6-2 -mtune=k6-2 -m3dnow --mfpmath=387 -fomit-frame-pointer -pipe" LDFLAGS="-Wl,-O1" make mode=release
|
||||||
|
# Report bugs to <mecareful@gmail.com>
|
||||||
|
|
||||||
|
#_________________________________
|
||||||
|
# ENVIRONMENT |
|
||||||
|
#________________________________|
|
||||||
|
TARGET0=main
|
||||||
|
TARGETS= $(TARGET0)
|
||||||
|
CC=cc
|
||||||
|
CXX=c++
|
||||||
|
CFLAGS := -fms-extensions $(CFLAGS)
|
||||||
|
CXXFLAGS := $(CXXFLAGS)
|
||||||
|
LDFLAGS := $(LDFLAGS)
|
||||||
|
LIBS=
|
||||||
|
SRC=
|
||||||
|
INCLUDES=
|
||||||
|
#________________________________________
|
||||||
|
# BUILD SCRIPT (don't change) |
|
||||||
|
#_______________________________________|
|
||||||
|
ifeq ($(mode),)
|
||||||
|
mode = debug
|
||||||
|
endif
|
||||||
|
ifeq ($(mode),debug)
|
||||||
|
CFLAGS := -O0 -g -DDEBUG -std=c99 -pedantic -Wextra -Wconversion $(CFLAGS)
|
||||||
|
LDFLAGS := $(LDFLAGS)
|
||||||
|
endif
|
||||||
|
ifeq ($(mode),profile)
|
||||||
|
CFLAGS := -O0 -g -DDEBUG -std=c99 -p -ftest-coverage -Wcoverage-mismatch $(CFLAGS)
|
||||||
|
LDFLAGS := -g -p $(LDFLAGS)
|
||||||
|
endif
|
||||||
|
ifeq ($(mode),develop)
|
||||||
|
CFLAGS := -O2 -g -DDEBUG -std=c99 $(CFLAGS)
|
||||||
|
LDFLAGS := -O1 $(LDFLAGS)
|
||||||
|
endif
|
||||||
|
ifeq ($(mode),release)
|
||||||
|
CFLAGS := -O2 -std=c99 $(CFLAGS)
|
||||||
|
LDFLAGS := -O1 $(LDFLAGS)
|
||||||
|
endif
|
||||||
|
|
||||||
|
CFLAGS += -Wall $(INCLUDES)
|
||||||
|
LDFLAGS += -Wall $(LIBS)
|
||||||
|
|
||||||
|
all:
|
||||||
|
@make change_make_options &>/dev/null
|
||||||
|
+make $(TARGETS)
|
||||||
|
|
||||||
|
ifneq ($(mode),debug)
|
||||||
|
ifneq ($(mode),profile)
|
||||||
|
ifneq ($(mode),develop)
|
||||||
|
ifneq ($(mode),release)
|
||||||
|
@echo "Invalid build mode."
|
||||||
|
@echo "Please use 'make mode=release', 'make mode=develop', 'make mode=profile' or 'make mode=debug'"
|
||||||
|
@exit 1
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
@echo ".........................."
|
||||||
|
@echo "Building on "$(mode)" mode "
|
||||||
|
@echo "CFLAGS=$(CFLAGS)"
|
||||||
|
@echo "LDFLAGS=$(LDFLAGS)"
|
||||||
|
@echo ".........................."
|
||||||
|
|
||||||
|
OLD_BUILD_MODE=$(shell grep ^MODE make_options.out 2>/dev/null | sed 's~^MODE=~~')
|
||||||
|
OLD_BUILD_CFLAGS=$(shell grep ^CFLAGS make_options.out 2>/dev/null | sed 's~^CFLAGS=~~')
|
||||||
|
OLD_BUILD_LDFLAGS=$(shell grep ^LDFLAGS make_options.out 2>/dev/null | sed 's~^LDFLAGS=~~')
|
||||||
|
change_make_options:
|
||||||
|
ifneq ($(mode)|$(CFLAGS)|$(LDFLAGS), $(OLD_BUILD_MODE)|$(OLD_BUILD_CFLAGS)|$(OLD_BUILD_LDFLAGS))
|
||||||
|
@echo CLEANING...
|
||||||
|
@make clean
|
||||||
|
@echo "MODE=$(mode)" > make_options.out
|
||||||
|
@echo "CFLAGS=$(CFLAGS)" >> make_options.out
|
||||||
|
@echo "LDFLAGS=$(LDFLAGS)" >> make_options.out
|
||||||
|
endif
|
||||||
|
|
||||||
|
%.o :
|
||||||
|
$(CC) -c $(CFLAGS) $(SRC) -o $@ $<
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM) *.o *.out callgrind.out.* *.gcno $(TARGETS)
|
||||||
|
|
||||||
|
.PHONY: all change_make_options clean
|
||||||
|
|
||||||
|
#_________________________________
|
||||||
|
# R U L E S |
|
||||||
|
#________________________________|
|
||||||
|
target_objs0 = \
|
||||||
|
main.o \
|
||||||
|
ColoredSquare.o \
|
||||||
|
Square.o
|
||||||
|
|
||||||
|
$(TARGET0): $(target_objs0)
|
||||||
|
$(CC) $(LDFLAGS) -o $@ $(target_objs0)
|
||||||
|
|
||||||
|
|
||||||
|
main.o: \
|
||||||
|
main.c \
|
||||||
|
ColoredSquare.h \
|
||||||
|
Figure.h \
|
||||||
|
Object.h \
|
||||||
|
Square.h
|
||||||
|
|
||||||
|
ColoredSquare.o: \
|
||||||
|
ColoredSquare.c \
|
||||||
|
ColoredSquare.h \
|
||||||
|
Figure.h \
|
||||||
|
Object.h \
|
||||||
|
Square.h
|
||||||
|
|
||||||
|
Square.o: \
|
||||||
|
Square.c \
|
||||||
|
Figure.h \
|
||||||
|
Object.h \
|
||||||
|
Square.h
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
#ifndef __OBJECT_H__
|
||||||
|
#define __OBJECT_H__
|
||||||
|
|
||||||
|
typedef struct Object_interface
|
||||||
|
{
|
||||||
|
void* (*clone) (void *this);
|
||||||
|
void (*destroy) (void *this);
|
||||||
|
|
||||||
|
} Object_interface;
|
||||||
|
|
||||||
|
#endif // __OBJECT_H__
|
|
@ -0,0 +1,125 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "Square.h"
|
||||||
|
|
||||||
|
void Square_constructor (void *this)
|
||||||
|
{
|
||||||
|
printf ("Square_constructor (%lu) called\n",
|
||||||
|
(unsigned long) this);
|
||||||
|
((Square *) this)->a = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Square_destructor (void *this)
|
||||||
|
{
|
||||||
|
printf ("Square_destructor (%lu) called\n",
|
||||||
|
(unsigned long) this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Square_copy (void *to, void *from)
|
||||||
|
{
|
||||||
|
printf ("Square_copy (%lu, %lu) called\n",
|
||||||
|
(unsigned long) to,
|
||||||
|
(unsigned long) from);
|
||||||
|
((Square *) to)->a = ((Square *) from)->a;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* Square_clone (void *this)
|
||||||
|
{
|
||||||
|
Square *square = Square_new ();
|
||||||
|
printf ("Square_clone (%lu) called\n",
|
||||||
|
(unsigned long) this);
|
||||||
|
Square_copy (square, this);
|
||||||
|
printf ("Square_clone (%lu) returns %lu\n",
|
||||||
|
(unsigned long) this,
|
||||||
|
(unsigned long) square);
|
||||||
|
|
||||||
|
return square;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Square_destroy (void *this)
|
||||||
|
{
|
||||||
|
printf ("Square_destroy (%lu) called\n",
|
||||||
|
(unsigned long) this);
|
||||||
|
free (this);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* Square_type (void *this)
|
||||||
|
{
|
||||||
|
static const char *square_type_str = "Square";
|
||||||
|
printf ("Square_type (%lu) called\n",
|
||||||
|
(unsigned long) this);
|
||||||
|
printf ("Square_type (%lu) returns \"%s\"\n",
|
||||||
|
(unsigned long) this,
|
||||||
|
square_type_str);
|
||||||
|
|
||||||
|
return square_type_str;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Square_draw (void *this)
|
||||||
|
{
|
||||||
|
printf ("Square_draw (%lu) called\n",
|
||||||
|
(unsigned long) this);
|
||||||
|
printf ("Drawing Square with %f side\n",
|
||||||
|
((Square *) this)->a);
|
||||||
|
}
|
||||||
|
|
||||||
|
double Square_area (void *this)
|
||||||
|
{
|
||||||
|
double area = ((Square *)this)->a * ((Square *)this)->a;
|
||||||
|
printf ("Square_area (%lu) called\n",
|
||||||
|
(unsigned long) this);
|
||||||
|
printf ("Square_area (%lu) returns %f\n",
|
||||||
|
(unsigned long) this,
|
||||||
|
area);
|
||||||
|
|
||||||
|
return area;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Square_resize (void *this, double a)
|
||||||
|
{
|
||||||
|
printf ("Square_resize (%lu, %f) called\n",
|
||||||
|
(unsigned long) this,
|
||||||
|
a);
|
||||||
|
((Square *) this)->a = a;
|
||||||
|
}
|
||||||
|
|
||||||
|
double Square_diag_length (void *this)
|
||||||
|
{
|
||||||
|
double diag_length = ((Square *)this)->a * 1.41421356;
|
||||||
|
|
||||||
|
printf ("Square_diag_length (%lu) called\n",
|
||||||
|
(unsigned long) this);
|
||||||
|
printf ("Square_diag_length (%lu) returns %f\n",
|
||||||
|
(unsigned long) this,
|
||||||
|
diag_length);
|
||||||
|
|
||||||
|
return diag_length;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* public */
|
||||||
|
void* Square_new ()
|
||||||
|
{
|
||||||
|
static Square_interface vtable =
|
||||||
|
{
|
||||||
|
Square_clone,
|
||||||
|
Square_destroy,
|
||||||
|
Square_type,
|
||||||
|
Square_draw,
|
||||||
|
Square_area,
|
||||||
|
Square_resize,
|
||||||
|
Square_diag_length
|
||||||
|
};
|
||||||
|
Square *square = malloc (sizeof (*square));
|
||||||
|
|
||||||
|
printf ("Square_new () returns %lu\n",
|
||||||
|
(unsigned long) square);
|
||||||
|
|
||||||
|
square->vtable = (void *) &vtable;
|
||||||
|
|
||||||
|
/*goto end;
|
||||||
|
err:
|
||||||
|
|
||||||
|
end:*/
|
||||||
|
|
||||||
|
return square;
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
#ifndef __SQUARE_H__
|
||||||
|
#define __SQUARE_H__
|
||||||
|
|
||||||
|
#include "Figure.h"
|
||||||
|
|
||||||
|
typedef struct Square_data
|
||||||
|
{
|
||||||
|
double a;
|
||||||
|
|
||||||
|
} Square_data;
|
||||||
|
|
||||||
|
typedef struct Square_interface
|
||||||
|
{
|
||||||
|
Figure_interface;
|
||||||
|
|
||||||
|
void (*resize) (void *this, double a);
|
||||||
|
double (*diag_length) (void *this);
|
||||||
|
|
||||||
|
} Square_interface;
|
||||||
|
|
||||||
|
void Square_constructor (void *this);
|
||||||
|
void Square_destructor (void *this);
|
||||||
|
void Square_copy (void *to, void *from);
|
||||||
|
void* Square_clone (void *this);
|
||||||
|
void Square_destroy (void *this);
|
||||||
|
const char* Square_type (void *this);
|
||||||
|
void Square_draw (void *this);
|
||||||
|
double Square_area (void *this);
|
||||||
|
void Square_resize (void *this, double a);
|
||||||
|
double Square_diag_length (void *this);
|
||||||
|
|
||||||
|
/* public */
|
||||||
|
typedef struct Square
|
||||||
|
{
|
||||||
|
Square_interface *vtable;
|
||||||
|
|
||||||
|
Square_data;
|
||||||
|
|
||||||
|
} Square;
|
||||||
|
|
||||||
|
void* Square_new ();
|
||||||
|
|
||||||
|
#endif // __SQUARE_H__
|
|
@ -0,0 +1,38 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "ColoredSquare.h"
|
||||||
|
|
||||||
|
int main (int argc, char *argv[])
|
||||||
|
{
|
||||||
|
Figure *fig[2];
|
||||||
|
|
||||||
|
fig[0] = Square_new ();
|
||||||
|
fig[1] = ColoredSquare_new ();
|
||||||
|
|
||||||
|
((ColoredSquare *) fig[1])->vtable->set_color (fig[1], 5);
|
||||||
|
((Square *) fig[0])->vtable->resize (fig[0], 2);
|
||||||
|
((Square *) fig[1])->vtable->resize (fig[0], 3);
|
||||||
|
|
||||||
|
fig[0]->vtable->draw (fig[0]);
|
||||||
|
fig[1]->vtable->draw (fig[1]);
|
||||||
|
|
||||||
|
printf ("area(%lu) = %f\n",
|
||||||
|
(unsigned long) fig[0],
|
||||||
|
fig[0]->vtable->area (fig[0]));
|
||||||
|
printf ("area(%lu) = %f\n",
|
||||||
|
(unsigned long) fig[1],
|
||||||
|
fig[1]->vtable->area (fig[1]));
|
||||||
|
|
||||||
|
printf ("diag_length(%lu) = %f\n",
|
||||||
|
(unsigned long) fig[0],
|
||||||
|
((Square *) fig[0])->vtable->diag_length (fig[0]));
|
||||||
|
|
||||||
|
/*goto end;
|
||||||
|
err:
|
||||||
|
end:*/
|
||||||
|
fig[0]->vtable->destroy (fig[0]);
|
||||||
|
fig[1]->vtable->destroy (fig[1]);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue