www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Translate C++ to D

reply Luca Lupo <luca.lupo hotmail.it> writes:
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();
      };
Jan 08 2008
next sibling parent torhu <no spam.invalid> writes:
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.
Jan 08 2008
prev sibling next sibling parent reply Jason House <jason.james.house gmail.com> writes:
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(); };
Jan 08 2008
next sibling parent Bill Baxter <dnewsgroup billbaxter.com> writes:
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
Jan 08 2008
prev sibling next sibling parent BLS <nanali nospam-wanadoo.fr> writes:
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(); };

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
Jan 08 2008
prev sibling next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
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
Jan 08 2008
parent reply Sean Kelly <sean f4.ca> writes:
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
Jan 08 2008
next sibling parent reply Jason House <jason.james.house gmail.com> writes:
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.
Jan 10 2008
parent reply Luca Lupo <luca.lupo hotmail.it> writes:
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
Jan 10 2008
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Luca Lupo:
 I would known if also the file include of D have the extension.h

What do you need them for? :-) Bye, bearophile
Jan 10 2008
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Janice Caron:
 Luca, bearophile's question was rhetorical. D does not require include
 files. They're just not needed. You only have to write the source
 files - and they will have an extension of .d

Yes, sorry, in the future I'll try to avoid such answers. Related: the -H compiler flag generates 'header' files, thy may be useful for larger programs, but the compiler generates them for you. Bye, bearophile
Jan 10 2008
prev sibling parent reply Luca Lupo <luca.lupo hotmail.it> writes:
I would like to thank all the answers, you are very available. Now I have to
start converting the files. Cpp and I hope that you can continue to help.

This is the first file: observer.cpp, any suggestions?

//Observer.cpp

#include<iostream>
#include<string> 
#include"Subject.h" 
using namespace std;

Observer::~Observer()
{}

Observer::Observer()
{}

SubscriberG::SubscriberG(string a){
     indirizzo_e_mail_G = a;
}

SubscriberS::SubscriberS(string a,string b){
     indirizzo_e_mail_S = a;
     name_author = b;
}


void Observer::Update(Subject* b)
{}

void Subscriber::Update(Subject* b)
{}

void SubscriberS::Update(Subject* b){
     if (name_author == ((Blog *)b)->GetAuthor()){
        cout<<"Subscriber tipo S->"<<"Name blog: "<<((Blog
*)b)->GetName()<<endl;
        ((Blog *)b)->GetTitle();
        ((Blog *)b)->GetText();
        cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor();
        cout<<endl<<endl;
     }
}

void SubscriberG::Update(Subject* b){
     cout<<"Subscriber tipo G->"<<"Name blog: "<<((Blog *)b)->GetName()<<endl;
     ((Blog *)b)->GetTitle();
     ((Blog *)b)->GetLine();
     cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor();
     cout<<endl<<endl;
}



//Observer.d

import std.string;
import Subject

Observer::~this() //distructor for observer
{}

Observer::this() // costructor for observer
{}

SubscriberG::this(string a) //contructor for subscriberG
{indirizzo_e_mail_G = a;}

SubscriberS::this(string a,string b) //contructor for subscriberS
{
    indirizzo_e_mail_S = a;
    name_author = b;
}


void Observer::Update(Subject* b) //it's equal? for me yes!
{}

void Subscriber::Update(Subject* b) //it's equal? for me yes!
{}

void SubscriberS::Update(Subject* b) //Help??
{
    if(name_author == ((Blog *)b)->GetAuthor()){
cout<<"Subscriber tipo S->"<<"Name blog: "<<((Blog *)b)->GetName()<<endl;
((Blog *)b)->GetTitle();
((Blog *)b)->GetText();
cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor();
                                               }
cout<<endl<<endl;
}

void SubscriberG::Update(Subject* b) //Help??
{
cout<<"Subscriber tipo G->"<<"Name blog: "<<((Blog *)b)->GetName()<<endl;
((Blog *)b)->GetTitle();
((Blog *)b)->GetLine();
cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor();
cout<<endl<<endl;
}
Jan 10 2008
parent reply doob <doobnet gmail.com> writes:
Luca Lupo wrote:
 I would like to thank all the answers, you are very available. Now I have to
start converting the files. Cpp and I hope that you can continue to help.
 
 This is the first file: observer.cpp, any suggestions?
 
 //Observer.cpp
 
 #include<iostream>
 #include<string> 
 #include"Subject.h" 
 using namespace std;
 
 Observer::~Observer()
 {}
 
 Observer::Observer()
 {}
 
 SubscriberG::SubscriberG(string a){
      indirizzo_e_mail_G = a;
 }
 
 SubscriberS::SubscriberS(string a,string b){
      indirizzo_e_mail_S = a;
      name_author = b;
 }
 
 
 void Observer::Update(Subject* b)
 {}
 
 void Subscriber::Update(Subject* b)
 {}
 
 void SubscriberS::Update(Subject* b){
      if (name_author == ((Blog *)b)->GetAuthor()){
         cout<<"Subscriber tipo S->"<<"Name blog: "<<((Blog
*)b)->GetName()<<endl;
         ((Blog *)b)->GetTitle();
         ((Blog *)b)->GetText();
         cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor();
         cout<<endl<<endl;
      }
 }
 
 void SubscriberG::Update(Subject* b){
      cout<<"Subscriber tipo G->"<<"Name blog: "<<((Blog *)b)->GetName()<<endl;
      ((Blog *)b)->GetTitle();
      ((Blog *)b)->GetLine();
      cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor();
      cout<<endl<<endl;
 }
 
 
 
 //Observer.d
 
 import std.string;
 import Subject
 
 Observer::~this() //distructor for observer
 {}
 
 Observer::this() // costructor for observer
 {}
 
 SubscriberG::this(string a) //contructor for subscriberG
 {indirizzo_e_mail_G = a;}
 
 SubscriberS::this(string a,string b) //contructor for subscriberS
 {
     indirizzo_e_mail_S = a;
     name_author = b;
 }
 
 
 void Observer::Update(Subject* b) //it's equal? for me yes!
 {}
 
 void Subscriber::Update(Subject* b) //it's equal? for me yes!
 {}
 
 void SubscriberS::Update(Subject* b) //Help??
 {
     if(name_author == ((Blog *)b)->GetAuthor()){
 cout<<"Subscriber tipo S->"<<"Name blog: "<<((Blog *)b)->GetName()<<endl;
 ((Blog *)b)->GetTitle();
 ((Blog *)b)->GetText();
 cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor();
                                                }
 cout<<endl<<endl;
 }
 
 void SubscriberG::Update(Subject* b) //Help??
 {
 cout<<"Subscriber tipo G->"<<"Name blog: "<<((Blog *)b)->GetName()<<endl;
 ((Blog *)b)->GetTitle();
 ((Blog *)b)->GetLine();
 cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor();
 cout<<endl<<endl;
 }
 

I think the best would be if read through the language specification (http://www.digitalmars.com/d/1.0/lex.html) and tried to port files own your own and then ask if it's something you don't understand or need help with. Some corrections and tips: Drop all the name spaces All member functions/methods must be in the class not as free functions. Think of it as you write a class declaration in a header file but you also add the implementation. Classes are by reference in D so drop all the pointers ":: -> ." are all written as "." in D If any class has a pure virtual function make it an abstract function (put the keyword "abstract" in front of the function and drop "=0") or if all functions are pure functions then make the class an interface instead(replace the "class" keyword with the "interface" keyword) Don't put a semicolon after a class Tips: put every class in it's own module/file
Jan 10 2008
parent reply Luca Lupo <luca.lupo hotmail.it> writes:
So, I have converted the -> with the . , but I must make this also for:
example:

Observer::~this(){} ??? :: no, but .??

And Exit the cout in D?? or I must import some library??

Thanks

//Observer.d

import std.string;
import Subject

Observer::~this() //distructor for observer
{}

Observer::this() // costructor for observer
{}

SubscriberG::this(string a) //contructor for subscriberG
{indirizzo_e_mail_G = a;}

SubscriberS::this(string a,string b) //contructor for subscriberS
{
    indirizzo_e_mail_S = a;
    name_author = b;
}


void Observer::Update(Subject* b) //it's equal? for me yes!
{}

void Subscriber::Update(Subject* b) //it's equal? for me yes!
{}

void SubscriberS::Update(Subject* b) //Help??
{
    if(name_author == ((Blog *)b).GetAuthor()){
cout<<"Subscriber tipo S->"<<"Name blog: "<<((Blog *)b).GetName()<<endl;
((Blog *)b).GetTitle();
((Blog *)b).GetText();
cout<<"Autore Ultimo Post :"<<((Blog *)b).GetAuthor();
                                               }
cout<<endl<<endl;
}

void SubscriberG::Update(Subject* b) //Help??
{
cout<<"Subscriber tipo G->"<<"Name blog: "<<((Blog *)b).GetName()<<endl;
((Blog *)b).GetTitle();
((Blog *)b).GetLine();
cout<<"Autore Ultimo Post :"<<((Blog *)b).GetAuthor();
cout<<endl<<endl;
}
Jan 11 2008
parent reply doob <doobnet gmail.com> writes:
Luca Lupo wrote:
 So, I have converted the -> with the . , but I must make this also for:
 example:
 
 Observer::~this(){} ??? :: no, but .??
 
 And Exit the cout in D?? or I must import some library??
 
 Thanks

You don't write classname::function, you put all the functions in the class like this: class Observer { this() { // do something } foo (int i) { // do something else } } But if you want to access a static function you replace the two colons with a dot like: Observer.staticFunction "And Exit the cout in D?? or I must import some library??" what do you mean? You write to the standard output in D like this: import std.stdio; void main () { writefln("Hello D World"); // adds a new line } Again, look at the language specification http://www.digitalmars.com/d/1.0/lex.html and also the phobos (the standard library) link
Jan 11 2008
parent reply Luca Lupo <luca.lupo hotmail.it> writes:
Thanks,
So in this way?

//Observer.d

import std.stdio;
import std.string;
import Subject

class Observer{
      ~this(){} //distructor for observer
      
       this(){} // costructor for observer
       
       Update(Subject* b){}
       }

class Subscriber{
      Update(Subject* b);
      }

class SubscriberG{
      this (string a){
           indirizzo_e_mail_G = a;
           }
      
      Update (Subject* b){
             cout<<"Subscriber tipo G->"<<"Name blog: "<<((Blog
*)b).GetName()<<endl;
             ((Blog *)b).GetTitle();
             ((Blog *)b).GetLine();
             cout<<"Autore Ultimo Post :"<<((Blog *)b).GetAuthor();
             cout<<endl<<endl;
             }
      }
      
class SubscriberS{
      this (string a, string b){
           indirizzo_e_mail_S = a;
           name_author = b;
           }

      Update (Subject* b){
             if (name_author == ((Blog *)b).GetAuthor()){
                cout<<"Subscriber tipo S->"<<"Name blog: "<<((Blog
*)b).GetName()<<endl;
                ((Blog *)b).GetTitle();
                ((Blog *)b).GetText();
                cout<<"Autore Ultimo Post :"<<((Blog *)b).GetAuthor();
                }
             cout<<endl<<endl;
             }
      }




but is possibile import the standard library of cpp so I can use cout?

Thanks
Jan 11 2008
next sibling parent Luca Lupo <luca.lupo hotmail.it> writes:
So this files is correctly converted from .cpp to .d??

//Observer.cpp

#include<iostream>
#include<string>
#include"Subject.h"
using namespace std;

Observer::~Observer()
{}

Observer::Observer()
{}

SubscriberG::SubscriberG(string a){
     indirizzo_e_mail_G = a;
}

SubscriberS::SubscriberS(string a,string b){
     indirizzo_e_mail_S = a;
     name_author = b;
}


void Observer::Update(Subject* b)
{}

void Subscriber::Update(Subject* b)
{}

void SubscriberS::Update(Subject* b){
     if (name_author == ((Blog *)b)->GetAuthor()){
        cout<<"Subscriber tipo S->"<<"Name blog: "<<((Blog
*)b)->GetName()<<endl;
        ((Blog *)b)->GetTitle();
        ((Blog *)b)->GetText();
        cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor();
        cout<<endl<<endl;
     }
}

void SubscriberG::Update(Subject* b){
     cout<<"Subscriber tipo G->"<<"Name blog: "<<((Blog *)b)->GetName()<<endl;
     ((Blog *)b)->GetTitle();
     ((Blog *)b)->GetLine();
     cout<<"Autore Ultimo Post :"<<((Blog *)b)->GetAuthor();
     cout<<endl<<endl;
}




//Observer.d

import std.stdio;
import std.string;
import Subject

class Observer{
      ~this(){} //distructor for observer
      
       this(){} // costructor for observer
       
       Update(Subject* b){}
       }

class Subscriber{
      Update(Subject* b);
      }

class SubscriberG{
      this (string a){
           indirizzo_e_mail_G = a;
           }
      
      Update (Subject* b){
             writefln("Subscriber tipo G->", "Name blog: ", ((Blog
*)b).GetName());
             ((Blog *)b).GetTitle();
             ((Blog *)b).GetLine();
             writefln("Autore Ultimo Post: ",((Blog *)b).GetAuthor());
             writefln("");
             writefln("");
             }
      }
      
class SubscriberS{
      this (string a, string b){
           indirizzo_e_mail_S = a;
           name_author = b;
           }

      Update (Subject* b){
             if (name_author == ((Blog *)b).GetAuthor()){
                writefln("Subscriber tipo S->", "Name blog: ", ((Blog
*)b).GetName());
                ((Blog *)b).GetTitle();
                ((Blog *)b).GetText();
                writefln("Autore ultimo Post: ",((Blog *)b).GetAuthor());
                }
             writefln("");
             writefln("");
             }
      }
Jan 11 2008
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Janice Caron:
 Instead of
     cout<<"Subscriber tipo G->"<<"Name blog: "<<((Blog *)b).GetName()<<endl;
 do
     writefln("Subscriber tipo G->", "Name blog: ", ((Blog *)b).GetName());

I think it may be necessary a cast there: writefln("Subscriber tipo G->Name blog: ", (cast(Blog*)b).GetName ); But be careful because the first string can't contain spurious %. D 2.x has write/writeln to solve that (silly) bug. Anyway, I suggest against this kind of translation, I suggest an incremental way of doing such thing. That is translate a tiny working piece of C++ code, make it work, and then add more and more code, keeping it more or less working... unit tests help a lot. This isn't always possible, but doing otherwise is a good way to put lot of bugs in the translated code. Bye, bearophile
Jan 11 2008
parent reply Luca Lupo <luca.lupo hotmail.it> writes:
So I have finish conversion but there are problems:

Exist the iterator in D and the functions name_list.push_back(element) or
name_list.erase(iterator) ???



//Observer.d

import std.stdio;
import std.string;
import Subject

class Observer{
      ~this(){} //distructor for observer
      
       this(){} // costructor for observer
       
       Update(Subject* b){}
       }

class Subscriber{
      Update(Subject* b);
      }

class SubscriberG{
      this (string a){
           indirizzo_e_mail_G = a;
           }
      
      Update (Subject* b){
             writefln("Subscriber tipo G->", "Name blog: ", ((Blog
*)b).GetName()); //maybe needed a cast
             ((Blog *)b).GetTitle();
             ((Blog *)b).GetLine();
             writefln("Autore Ultimo Post: ",((Blog *)b).GetAuthor()); //maybe
needed a cast
             writefln("");
             writefln("");
             }
      }
      
class SubscriberS{
      this (string a, string b){
           indirizzo_e_mail_S = a;
           name_author = b;
           }

      Update (Subject* b){
             if (name_author == ((Blog *)b).GetAuthor()){
                writefln("Subscriber tipo S->", "Name blog: ", ((Blog
*)b).GetName()); //maybe needed a cast
                ((Blog *)b).GetTitle();
                ((Blog *)b).GetText();
                writefln("Autore ultimo Post: ",((Blog *)b).GetAuthor());
//maybe needed a cast
                }
             writefln("");
             writefln("");
             }
      }





//Observer_D.h.

import std.string;


class Subject;

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

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

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

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





//Post.d

import std.stdio;
import std.string;
import Subject;
import Post;

class Post{
      this (string title, string text, string author){
           Title=title;
           Text=text;
           Author=author;
           }
           
      print (){
            writefln("Title :", Title, "Text :", Text, "Author :", Author)endl;
            }

      print_Title (){
                  return Title;
                  }
                  
      print_Author (){
                   return Author;
                   }
      
      print_Line (){
                 writefln(Text.substr(0,10));
                 }

      print_Text (){
                 return Text;
                 }
      }




//Post_d.h

import std.string;


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




//Subject.d

import std.stdio;
import std.string;
import Subject


class Subject{
      this(){}
      
      ~this(){}
      
      Attach (Observer* o){
             _observer.push_back(o);
             }
             
      Detach (Observer* o){
             list<Observer*>::iterator i;
             for (i=_observer.begin() ; i!=_observer.end() ; i++){
                 if (*i == o ){
                 _observer.erase(i);
                 break;
                 }
             }
      }
      
      Notify(){}
      }
      

class Blog{
      this (string a){
           Blog_name=a;
           Subject sub1();
           }
           
      Notify (){
             list<Observer*>::iterator i;
             for (i=_observer.begin() ; i!=_observer.end() ; i++){
                 (*i).Update(this);
                 }
             }
      
      NewPost (Post p){
              writefln("---------E' stato inserito un nuovo Post---------");
              l_post.push_back(p);
              Notify();
              }
              
      GetName (){
              return Blog_name;
              }
              
      GetTitle (){
               list<Post>::iterator i;
               for (i=l_post.begin() ; i!=l_post.end() ; i++);
               i--;
               writefln("Titolo ultimo post: ", i.print_Title());
               }
               
      GetText (){
              list<Post>::iterator i;
              for (i=l_post.begin() ; i!=l_post.end() ; i++);
              i--;
              writefln("Testo Intero :", i.print_Text());
              }
              
      GetAuthor (){
                list<Post>::iterator i;
                for (i=l_post.begin() ; i!=l_post.end() ; i++);
                i--;
                return i.print_Author();
                }
                
      GetLine (){
              list<Post>::iterator i;
              for (i=l_post.begin() ; i!=l_post.end() ; i++);
              i--;
              writefln("Primi 10 caratteri ultimo post: ");
              i.print_Line();
              }
      }




//Subject_d.h

import std.slist;
import Observer;
import Post;

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

class Blog : public Subject{
       private:
               string Blog_name;
               Slist!(Post) l_post;
       public:
               // If no overloading by derived classes, 
               // compiler will make non-virtual
              this(string ); //costruttore
              void Notify(); 
              void NewPost(Post); // is Post
              string GetName();
              void GetText();
              void GetTitle();  
              string GetAuthor();
              void GetLine();  
       };
Jan 11 2008
parent bearophile <bearophileHUGS lycos.com> writes:
Luca Lupo:
 So I have finish conversion but there are problems:
 Exist the iterator in D and the functions name_list.push_back(element) or
name_list.erase(iterator) ???

I suggest you to do some of your own homework, and read some docs. I don't think you will be able to debug your code soon with that silly style of code translation. You may want to try to actually run bits of code, like the cast I have suggested you. Bye, bearophile
Jan 11 2008
prev sibling parent doob <doobnet gmail.com> writes:
Luca Lupo wrote:
 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

D don't need any kind of header files, you just put the implementation in one .d file. All you .d files need a module declaration in the beginning of the file, "module filename", you can also but them in packages/directories, "module directory.filename" Change all imports from "string" to "std.string" Classes in D are by references, therefore you don't need pointers to the classes Constructors are written as "this()" and destructors "~this()" not the class names You don't need to write "public" when you extend a class Why suddenly the "char *" in Post and not "string" You can't write "void Update(Subject* the_change_subject) = 0;" the " = 0" part. My C++ knowledge is not the best but that class should maybe be an interface or at least the function should be abstract "std.slist" is D 2 only and D 2 is not stable, if you use D 1 maybe it's enough with D's built in dynamic arrays.
Jan 10 2008
prev sibling parent "Janice Caron" <caron800 googlemail.com> writes:
Instead of

    cout<<"Subscriber tipo G->"<<"Name blog: "<<((Blog *)b).GetName()<<endl;

do

    writefln("Subscriber tipo G->", "Name blog: ", ((Blog *)b).GetName());
Jan 11 2008
prev sibling parent "Janice Caron" <caron800 googlemail.com> writes:
Luca, bearophile's question was rhetorical. D does not require include
files. They're just not needed. You only have to write the source
files - and they will have an extension of .d

On 1/10/08, bearophile <bearophileHUGS lycos.com> wrote:
 Luca Lupo:
 I would known if also the file include of D have the extension.h

What do you need them for? :-) Bye, bearophile

Jan 10 2008
prev sibling parent BLS <nanali nospam-wanadoo.fr> writes:
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
Jan 08 2008