POV-Ray : Newsgroups : povray.programming : Object oriented POV scene language? Server Time
28 Jul 2024 14:33:51 EDT (-0400)
  Object oriented POV scene language? (Message 21 to 30 of 72)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>
From: Warp
Subject: Re: Object oriented POV scene language?
Date: 24 Jul 2000 05:47:02
Message: <397c1096@news.povray.org>
David <dav### [at] maccom> wrote:
:> Uh, ever hear of structs?
:     Can a struct have functions as well is vars? Can a struct have protected
: members? Can a struct have inheritance? Are structs encapsulated? Ect.

  The fact that the language doesn't directly support modules (and that's
what we are talking about here; you have so far mentioned no truely OO
programming feature) it doesn't mean that you can't use structures as if
they were modules.
  You can have member variables and member functions (either you just call
the proper function giving the struct as parameter, as C++ internally does,
or you put function pointers inside the struct).
  This, of course, looks ugly, but when used properly it makes little
difference compared to a truely modular language (like C++).

  And I would like to emphasize that you keep talking about object-oriented
programming, and still you haven't mentioned ANY feature of object-oriented
languages (inheritance, dynamic binding, overloading...). You just keep
talking about modules.
  For your information, the povray source code even uses some kind of
inheritance (and even dynamic binding, I think), which makes it very OO-like.
It looks ugly in C, but it does it.

-- 
main(i,_){for(_?--i,main(i+2,"FhhQHFIJD|FQTITFN]zRFHhhTBFHhhTBFysdB"[i]
):5;i&&_>1;printf("%s",_-70?_&1?"[]":" ":(_=0,"\n")),_/=2);} /*- Warp -*/


Post a reply to this message

From: Warp
Subject: Re: Object oriented POV scene language?
Date: 24 Jul 2000 05:51:30
Message: <397c11a2@news.povray.org>
David <dav### [at] maccom> wrote:
:     Now, how can you get OOP is C?

  I know several programs that use OOP in C. The most obvious one is povray.
To mention another one, the GTK library uses OOP as well.
  They both use modules, inheritance and dynamic binding. It looks quite
ugly in C, but they use them.

-- 
main(i,_){for(_?--i,main(i+2,"FhhQHFIJD|FQTITFN]zRFHhhTBFHhhTBFysdB"[i]
):5;i&&_>1;printf("%s",_-70?_&1?"[]":" ":(_=0,"\n")),_/=2);} /*- Warp -*/


Post a reply to this message

From: Thorsten Froehlich
Subject: Re: Object oriented POV scene language?
Date: 24 Jul 2000 08:52:43
Message: <397c3c1b@news.povray.org>
In article <B5A11995.1927%dav### [at] maccom> , David <dav### [at] maccom>
wrote:

>    Can a struct have functions as well is vars? Can a struct have protected
> members? Can a struct have inheritance? Are structs encapsulated? Ect.

You can have all of it, also it obviously won't look the same.  Take a look
at the operating system you are running (Mac OS, I presume).  In particular,
look at the Window Manager. There is (in reverse order for better reading):

struct WindowRecord {
 GrafPort       port;
 short        windowKind;
 Boolean       visible;
 ...
};

struct GrafPort {
 short        device;
 BitMap        portBits;
 Rect        portRect;
 ...
};

struct BitMap {
 Ptr        baseAddr;
 short        rowBytes;
 Rect        bounds;
};


Now, as C++ classes this could look like:

class WindowRecord {
private:
 GrafPort       port;
 short        windowKind;
 Boolean       visible;
 ...
};

class GrafPort {
private:
 short        device;
 BitMap        portBits;
 Rect        portRect;
 ...
};

class BitMap {
private:
 Ptr        baseAddr;
 short        rowBytes;
 Rect        bounds;
};

typedef WindowRecord *      WindowPtr;


Instead of having functions in here, this example shows that you do _not_
even need function pointers (also C++ does it this way) to get OOP.  All you
need is a well defined interface, and every call then just does this:

WindowPtr win = NewWindow(...);

SetWindowTitle(win, "My Title");

DisposeWindow(win);


While in C++ this would look like:

Window *win = new Window(...);

win->SetTitle("My title");

delete win;


If you want private data members, and as a good OOP principle is to disallow
access to all direct data members anyway, and to use accessor functions you
would get:

// in the "public" header, you do this
typedef struct OpaqueWindowPtr *      WindowRef;
// while in the private header you would do this
typedef WindowRecord *      WindowPtr;
// this allows you to have both working, it is just a matter of a few simple
// "tricks" in C


WindowRef win = NewWindow(...);

SetWindowTitle(win, "My Title");

DisposeWindow(win);


Now, all you functions need to do is to cast the win pointer they receive to
WindowPtr and they can use it as they did before.  By not exporting function
prototypes of functions, you make these functions "private".  In C you could
always do so by declaring a function "static".

As you can see, most OOP support is "included" in C, and additionally, the
code above was (like the original Mac OS) done in Pascal!  And the above
code is actual code, the first part as it has been until now, and the later
(windowRef) as it is when using the Carbon APIs.

The difference and reason for C++ to exists is to make these "features" of C
more accessible and integrated into the language, which allows greater
flexibility and speed.  Of course there are templates and multiple
inheritance in C++, but with the simple C preprocessors directives, even
templates are possible.  The only thing really missing is operator
overloading...

Anyway, this discussion has little to do with OOP, more with the
implementation of support for OOP in various languages - in some (like Java)
OOP is nearly enforced, while others like C leave it to the programmer to
write in good OOP style.   BTW, the Java Virtual Machine is not written in
C++, but C...


      Thorsten


____________________________________________________
Thorsten Froehlich, Duisburg, Germany
e-mail: tho### [at] trfde

Visit POV-Ray on the web: http://mac.povray.org


Post a reply to this message

From: Warp
Subject: Re: Object oriented POV scene language?
Date: 24 Jul 2000 10:22:27
Message: <397c5123@news.povray.org>
Thorsten Froehlich <tho### [at] trfde> wrote:
: but with the simple C preprocessors directives, even
: templates are possible.

  Are you sure? How do you make this extremely simple template in C:

template<typename T>
void swap(T& x, T& y)
{
  T tmp = x;
  x = y;
  y = tmp;
}

  Yes, you can make that code in a #define macro, but the problem is where
are you going to the the type for 'tmp'?

-- 
main(i,_){for(_?--i,main(i+2,"FhhQHFIJD|FQTITFN]zRFHhhTBFHhhTBFysdB"[i]
):_;i&&_>1;printf("%s",_-70?_&1?"[]":" ":(_=0,"\n")),_/=2);} /*- Warp -*/


Post a reply to this message

From: Ron Parker
Subject: Re: Object oriented POV scene language?
Date: 24 Jul 2000 10:37:25
Message: <slrn8nols3.a61.ron.parker@fwi.com>
On 24 Jul 2000 10:22:27 -0400, Warp wrote:
>  Yes, you can make that code in a #define macro, but the problem is where
>are you going to the the type for 'tmp'?

Make it one of the parameters to the macro?

-- 
Ron Parker   http://www2.fwi.com/~parkerr/traces.html
My opinions.  Mine.  Not anyone else's.


Post a reply to this message

From: Ron Parker
Subject: Re: Object oriented POV scene language?
Date: 24 Jul 2000 10:48:11
Message: <slrn8nomg9.a61.ron.parker@fwi.com>
On 24 Jul 2000 10:37:25 -0400, Ron Parker wrote:
>On 24 Jul 2000 10:22:27 -0400, Warp wrote:
>>  Yes, you can make that code in a #define macro, but the problem is where
>>are you going to the the type for 'tmp'?
>
>Make it one of the parameters to the macro?

Better yet (untested):

#define SWAP(a,b) {                \
  char tmp[sizeof(a)];             \
  assert( sizeof(a) == sizeof(b)); \
  memcpy( tmp, &a, sizeof(a));     \
  memcpy( &a, &b, sizeof(a));      \
  memcpy( &b, tmp, sizeof(a));     \
  }

This does assume that your dialect of C allows you to declare variables
at the start of any block rather than just at the top of the function, 
but I don't see any way of doing it without stipulating that.  It looks
like tmp has a dynamic size, but since sizeof is a compile-time operator,
it really doesn't.

-- 
Ron Parker   http://www2.fwi.com/~parkerr/traces.html
My opinions.  Mine.  Not anyone else's.


Post a reply to this message

From: Thorsten Froehlich
Subject: Re: Object oriented POV scene language?
Date: 24 Jul 2000 11:27:20
Message: <397c6058@news.povray.org>
In article <397c5123@news.povray.org> , Warp <war### [at] tagpovrayorg>  wrote:

>  Are you sure? How do you make this extremely simple template in C:
>
> template<typename T>
> void swap(T& x, T& y)
> {
>   T tmp = x;
>   x = y;
>   y = tmp;
> }

I would do it with include files that are not protected from
multiple-inclusion.  It could look like this:

FILE: mytmpl.h

void TEMPLATE_swap(T& x, T& y);

void TEMPLATE_swap(TEMPLATE_T& x, TEMPLATE_T& y)
// OK, there are no references in C...
{
  TEMPLATE_T tmp = x;
  x = y;
  y = tmp;
}


FILE: mymain.c

#define T int
#define TEMPLATE_swap swap1
#include "mytmpl.h"
#undef TEMPLATE_swap
#undef T

#define T char *
#define TEMPLATE_swap swap2
#include "mytmpl.h"
#undef TEMPLATE_swap
#undef T


While this is more work than just two functions, it does exactly the same.

However, if you have complex "classes" (structs) you only have to create a
new class name but can reuse the code without having to rename each
function.


     Thorsten


____________________________________________________
Thorsten Froehlich, Duisburg, Germany
e-mail: tho### [at] trfde

Visit POV-Ray on the web: http://mac.povray.org


Post a reply to this message

From: Warp
Subject: Re: Object oriented POV scene language?
Date: 25 Jul 2000 05:12:13
Message: <397d59ed@news.povray.org>
Ok, let's take this a step further:

template<typename T1, typename T2>
void swap(T1& x, T2& y)
{
    T1 tmp = x;
    x = y;
    y = tmp;
}

  And suppose we have:

class B;

class A
{
 public:
    A& operator=(const B&);
}

class B
{
 public:
    B& operator=(const A&);
}

int main()
{
    A a;
    B b;
    swap(a,b);
}

-- 
main(i,_){for(_?--i,main(i+2,"FhhQHFIJD|FQTITFN]zRFHhhTBFHhhTBFysdB"[i]
):_;i&&_>1;printf("%s",_-70?_&1?"[]":" ":(_=0,"\n")),_/=2);} /*- Warp -*/


Post a reply to this message

From: Thorsten Froehlich
Subject: Re: Object oriented POV scene language?
Date: 25 Jul 2000 06:33:52
Message: <397d6d10@news.povray.org>
In article <397d59ed@news.povray.org> , Warp <war### [at] tagpovrayorg>  wrote:

>   Ok, let's take this a step further:

OK, keep in mind that I pointed out before "The only thing really missing is
operator overloading...".  So a trick is needed to replace your overloaded
operator.  Of course this trick won't work with built-in types and two
versions of your template will be needed, one for classes and one for
built-in types!


FILE: mytmpl.h

void TEMPLATE_swap(T1& x, T2& y);

void TEMPLATE_swap(TEMPLATE_T1& x, TEMPLATE_T2& y)
// OK, there are no references in C...
{
  TEMPLATE_T1 tmp = x; // may need "copy" constructor replacement here

  x.assignment(&x, y);
  y.assignment(&y, tmp);
}


FILE: mymain.c

// First, every "class" gets "assignment" member functions:

struct A
{
    void (*assignment)(A *this, B& b);
};

struct B
{
    void (*assignment)(B *this, A& a);
};


#define T1 A
#define T2 B
#define TEMPLATE_swap swap_AB
#include "mytmpl.h"
#undef T1
#undef T2

int main(void)
{
    A a;
    B b;

    swap_AB(a,b);

    return 0;
}



____________________________________________________
Thorsten Froehlich, Duisburg, Germany
e-mail: tho### [at] trfde

Visit POV-Ray on the web: http://mac.povray.org


Post a reply to this message

From: Warp
Subject: Re: Object oriented POV scene language?
Date: 25 Jul 2000 08:48:56
Message: <397d8cb8@news.povray.org>
Thorsten Froehlich <tho### [at] trfde> wrote:
: #define T1 A
: #define T2 B
: #define TEMPLATE_swap swap_AB
: #include "mytmpl.h"
: #undef T1
: #undef T2

  What is shorter, to write the 6 lines of code above for every pair of
structs you use, or just write the swap function directly?

  Perhaps in this case it could be better to have a 'swap' method inside
the classes/structs, so you could write:

  a.swap(b);

-- 
main(i,_){for(_?--i,main(i+2,"FhhQHFIJD|FQTITFN]zRFHhhTBFHhhTBFysdB"[i]
):_;i&&_>1;printf("%s",_-70?_&1?"[]":" ":(_=0,"\n")),_/=2);} /*- Warp -*/


Post a reply to this message

<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.