POV-Ray : Newsgroups : povray.programming : Object oriented POV scene language? Server Time
28 Jul 2024 20:25:21 EDT (-0400)
  Object oriented POV scene language? (Message 23 to 32 of 72)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>
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

From: Thorsten Froehlich
Subject: Re: Object oriented POV scene language?
Date: 25 Jul 2000 10:44:22
Message: <397da7c6@news.povray.org>
In article <397d8cb8@news.povray.org> , Warp <war### [at] tagpovrayorg>  wrote:

>   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);

For the swap function this is to long, yes, but what about classes similar
to ones link vector<>, list<> or map<> templates in C++?  While in many
cases good old void pointers will do, it is possible to use macros instead.

Anyway, my point was not that templates are easy and good to do in C, just
that it is possible with a little bit of work to get a lot of features of
C++ in C - and the three lines would be exactly what it would takes to
implement a vector<> template of any class in 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: Disnel
Subject: Re: Object oriented POV scene language?
Date: 26 Jul 2000 11:23:50
Message: <397F0286.85776D5C@itam.cas.cz>
Chris Huff wrote:
> 
> In article <3975DC6C.3C023FF2@itam.cas.cz>, Disnel <dis### [at] itamcascz>
> wrote:
> 
> > 2) No one talked about virtual methods, although its big advantage, I
> > think.
> 
> I don't think they would apply to POV-Script...their main use is when
> referring to objects by pointers, and POV-Script doesn't have pointers.
> I think that *all* methods would behave "virtual", separate bindings
> would be useless in POV-Script.

But what are POV #declares? They can be taken as references, I think.
You are right with virtual methods, all would behave "virtual"

> 
> > This is true when you don't allow changing objects after they have been
> > parsed, but I think, that can be one great advantage, mainly in
> > persistent animations.
> 
> I don't see what you mean...why would changing the object be a problem?

For example I have two spheres and my new object, which connects
these two spheres with cylinder. When one sphere was changed during
animation, my connecting object need to be notified about it.
Where it contains only copies of sphere, it knows nothing about
change.

> If you mean adding members to an existing variable, then don't do that!
> It would be like trying to add variables and methods to a C++ class.
> Just make a new object from that one, which has the additional members
> you need.

I don't mean anything else.

> If you mean modifying an object which has been placed in the scene, and
> isn't a variable, then (again) don't do that! Modify the object, and
> *then* put it in the scene. This will work with persistent variables
> just fine...

Here we don't understand: you are thinking about object oriented
preprocesor and I'am thinking about object oriented scene during
rendering and animation, I'am right?

> 
> --
> Christopher James Huff - Personal e-mail: chr### [at] maccom
> TAG(Technical Assistance Group) e-mail: chr### [at] tagpovrayorg
> Personal Web page: http://homepage.mac.com/chrishuff/
> TAG Web page: http://tag.povray.org/


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.