View mode: basic / threaded / horizontal-split · Log in · Help
January 08, 2008
Translate C++ to D
Someone known convert this c++ code to D?

TANKS SO MUCH

#include<list>
using namespace std;

#include"Observer.h"
#include"Post.h"

class Subject{
     private:
     public:
            list<Observer*> _observer;
            virtual ~Subject();
            virtual void Attach(Observer *);
            virtual void Detach(Observer *);
            virtual void Notify();  
  protected:
            Subject(); 
     };

class Blog : public Subject{
     private:
             string Blog_name;
             list<Post> l_post;
     public:
            Blog(string );
            void Notify(); 
            void NewPost(Post);
            string GetName();
            void print();
     };
January 08, 2008
Re: Translate C++ to D
Luca Lupo wrote:
> Someone known convert this c++ code to D?
> 
> TANKS SO MUCH

Most of what you need to know is here:
http://www.digitalmars.com/d/1.0/class.html

Please try yourself, and then ask again if you have trouble. ;)

D's standard library has no linked list, but you can probably use the 
built-in dynamic arrays to begin with, then convert to linked list as 
needed.
January 08, 2008
Re: Translate C++ to D
Luca Lupo Wrote:

> Someone known convert this c++ code to D?
> 
> TANKS SO MUCH

Below is my cut at it.  Note that I made a few assumptions that may or may not be correct for you.  I used a singly linked list from the phobos library instead of std.list.  Also, I don't know what Observer and Post are.  I don't know what the right way to use them are.  For example, if Observer is a class, there's no need to use Observer*.  Also, I don't know if Post is getting copied when passed into the newPost function.  Depending on what's going on there, that function signature may need to change


import std.slist;
import Observer;
import Post;

class Subject{
      private:
      public:
              // Functions are virtual by default
              Slist!(Observer *) _observer; // Observer or Observer*?
              ~Subject();
              void Attach(Observer *); // Observer or Observer*?
              void Detach(Observer *); // Observer or Observer*?
              void Notify();  
      protected:
              this();
      }

class Blog : public Subject{
      private:
              string Blog_name;
              Slist!(Post) l_post;
      public:
              // If no overloading by derived classes, 
              // compiler will make non-virtual
             Blog(string );
             void Notify(); 
             void NewPost(Post); // Post? const Post? in Post?
             string GetName();
             void print();
      };
January 08, 2008
std.slist? (Re: Translate C++ to D)
Jason House wrote:
> Luca Lupo Wrote:
> 
>> Someone known convert this c++ code to D?
>>
>> TANKS SO MUCH
> 
> Below is my cut at it.  Note that I made a few assumptions that may or may not be correct for you.  I used a singly linked list from the phobos library instead of std.list.  Also, I don't know what Observer and Post are.  I don't know what the right way to use them are.  For example, if Observer is a class, there's no need to use Observer*.  Also, I don't know if Post is getting copied when passed into the newPost function.  Depending on what's going on there, that function signature may need to change

std.slist is in D2 only (but you can the D1 port from std2).

More importantly, though, the last news about it was that Andrei said it 
wasn't ready and told Brad to pull it from the release.  I haven't heard 
that that has changed, and there was no mention of slist in the last D 
change log, so I was thinking it was still not officially released.

Anyone know for sure?

--bb
January 08, 2008
Re: Translate C++ to D
Luca Lupo schrieb:
> Someone known convert this c++ code to D?

May I offer you a porting C++ to D link :
http://www.prowiki.org/wiki4d/wiki.cgi?PortingFromCxx

Regarding the observer pattern, respective GoF pattern in general have a 
look at :
http://www.dofactory.com/Patterns/Patterns.aspx#list

All GoF pattern are (clean) implemented in C#, the translation into D is 
trivial. So porting the observer pattern into D should not take you more 
the 3 minutes.

Oh, in case that you prefer STL like containers, have a look at the 
indigo library.  at dsource

HTH Bjoern
January 08, 2008
Re: Translate C++ to D
Jason House schrieb:
> Luca Lupo Wrote:
> 
>> Someone known convert this c++ code to D?
>>
>> TANKS SO MUCH
> 
> Below is my cut at it.  Note that I made a few assumptions that may or may not be correct for you.  I used a singly linked list from the phobos library instead of std.list.  Also, I don't know what Observer and Post are.  I don't know what the right way to use them are.  For example, if Observer is a class, there's no need to use Observer*.  Also, I don't know if Post is getting copied when passed into the newPost function.  Depending on what's going on there, that function signature may need to change
> 
> 
> import std.slist;
> import Observer;
> import Post;
> 
> class Subject{
>        private:
>        public:
>                // Functions are virtual by default
>                Slist!(Observer *) _observer; // Observer or Observer*?
>                ~Subject();
>                void Attach(Observer *); // Observer or Observer*?
>                void Detach(Observer *); // Observer or Observer*?
>                void Notify();  
>        protected:
>                this();
>        }
> 
> class Blog : public Subject{
>        private:
>                string Blog_name;
>                Slist!(Post) l_post;
>        public:
>                // If no overloading by derived classes, 
>                // compiler will make non-virtual
>               Blog(string );
>               void Notify(); 
>               void NewPost(Post); // Post? const Post? in Post?
>               string GetName();
>               void print();
>        };
Or simply use :
Observer[] _observer;
I mean in this case a reasonable solution.
Beside
Oberser is good enough(tm) otherwise u have a sort of Double whopper :)
Bjoern
January 08, 2008
Re: Translate C++ to D
Jason House:
>                // If no overloading by derived classes, 
>                // compiler will make non-virtual

But is this true?
(I did some benchmarks, and to me it seems false for the current DMD 1.x.)

Bye,
bearophile
January 08, 2008
Re: Translate C++ to D
bearophile wrote:
> Jason House:
>>                // If no overloading by derived classes, 
>>                // compiler will make non-virtual
> 
> But is this true?
> (I did some benchmarks, and to me it seems false for the current DMD 1.x.)

It's possible, but DMD does not currently do this.


Sean
January 10, 2008
Re: Translate C++ to D
Sean Kelly wrote:

> bearophile wrote:
>> Jason House:
>>>                // If no overloading by derived classes,
>>>                // compiler will make non-virtual
>> 
>> But is this true?
>> (I did some benchmarks, and to me it seems false for the current DMD
>> 1.x.)
> 
> It's possible, but DMD does not currently do this.
> 
> 
> Sean

Silly me; that's what I get for reading the online docs...  That's also
where I got the Slist thing too.
January 10, 2008
Re: Translate C++ to D
Thanks to everybody for the help, I have converted the other files .h to D I post it now, somebody can said me if it's well?

Thanks

//Observer.h

#include<string>
using namespace std;

class Subject;

class Observer{
     public:
            virtual ~Observer();
            virtual void Update(Subject* the_change_subject) = 0;
     protected:
            Observer();
     };

class Subscriber : public Observer{
     private:
            string indirizzo_e_mail;
     public:
            void Update(Subject* );
     };

class SubscriberG : public Subscriber{
     private:
             string indirizzo_e_mail_G;
     public:
            SubscriberG(string );
            void Update(Subject* );
     };

class SubscriberS : public Subscriber{
     private:
             string indirizzo_e_mail_S;
             string name_author;
     public:
            SubscriberS(string ,string);
            void Update(Subject* );
     };





//Observer_D.h.

import string;


class Subject;

class Observer{
     public:
            ~Observer();
            void Update(Subject* the_change_subject) = 0;
     protected:
            this();
     };

class Subscriber : public Observer{
     private:
            string indirizzo_e_mail;
     public:
            void Update(Subject* );
     };

class SubscriberG : public Subscriber{
     private:
             string indirizzo_e_mail_G;
     public:
            SubscriberG(string );
            void Update(Subject* );
     };

class SubscriberS : public Subscriber{
     private:
             string indirizzo_e_mail_S;
             string name_author;
     public:
            SubscriberS(string ,string);
            void Update(Subject* );
     };






//Post.h

#include<string>
using namespace std;

class Post{
     private:
            string Title;
            string Text;
            string Author;
     public:
            Post(string ,string ,string);
            void print();
            string print_Title();
            void print_Line();
            string print_Author();
            string print_Text();
     };





//Post_d.h

import string;


class Post{
     private:
            string Title;
            char *Text;
            string Author;
     public:
            Post(string ,char* ,string);
            void print();
            string print_Title();
            void print_Line();
            string print_Author();
            string print_Text();
     };






//Subject.h

#include<list>
#include"Observer.h"
#include"Post.h"
using namespace std;

class Subject{
     private:

     public:
            list<Observer*> _observer;
            virtual ~Subject();
            virtual void Attach(Observer *);
            virtual void Detach(Observer *);
            virtual void Notify();  //Per tutti gli oggetti nella lista chiama Update()
  protected:
            Subject(); //Puo essere chiamata SOLO dalle classi derivate
     };

class Blog : public Subject{
     private:
             string Blog_name;
             list<Post> l_post;
     public:
            Blog(string );
            void Notify();  //Per tutti gli oggetti nella lista chiama Update()
            void NewPost(Post);
            string GetName();
            void GetText();
            void GetTitle();  //Titolo dell ultimo post
            string GetAuthor();
            void GetLine();
     };




//Subject_d.h

import std.slist;
import Observer;
import Post;

class Subject{
      private:
      public:
              // Functions are virtual by default
              Slist!(Observer *) _observer; // is Observer*
              ~Subject();
              void Attach(Observer *); // is Observer*
              void Detach(Observer *); // is Observer*
              void Notify();  
      protected:
              this();
      }

class Blog : public Subject{
      private:
              string Blog_name;
              Slist!(Post) l_post;
      public:
              // If no overloading by derived classes, 
              // compiler will make non-virtual
             Blog(string );
             void Notify(); 
             void NewPost(Post); // is Post
             string GetName();
             void GetText();
             void GetTitle();  
             string GetAuthor();
             void GetLine();  
      };




I would known if also the file include of D have the extension.h

Thanks
« First   ‹ Prev
1 2 3
Top | Discussion index | About this forum | D home