digitalmars.D - Translate C++ to D
- Luca Lupo (28/28) Jan 08 2008 Someone known convert this c++ code to D?
- torhu (7/10) Jan 08 2008 Most of what you need to know is here:
- Jason House (30/33) Jan 08 2008 Below is my cut at it. Note that I made a few assumptions that may or m...
- Bill Baxter (8/15) Jan 08 2008 std.slist is in D2 only (but you can the D1 port from std2).
- BLS (7/46) Jan 08 2008 Or simply use :
- bearophile (5/7) Jan 08 2008 But is this true?
- Sean Kelly (3/9) Jan 08 2008 It's possible, but DMD does not currently do this.
- Jason House (3/16) Jan 10 2008 Silly me; that's what I get for reading the online docs... That's also
- Luca Lupo (160/160) Jan 10 2008 Thanks to everybody for the help, I have converted the other files .h to...
- bearophile (4/5) Jan 10 2008 What do you need them for? :-)
- Janice Caron (4/9) Jan 10 2008 Luca, bearophile's question was rhetorical. D does not require include
- bearophile (5/8) Jan 10 2008 Yes, sorry, in the future I'll try to avoid such answers.
- Luca Lupo (74/74) Jan 10 2008 I would like to thank all the answers, you are very available. Now I hav...
- doob (18/118) Jan 10 2008 I think the best would be if read through the language specification
- Luca Lupo (41/41) Jan 11 2008 So, I have converted the -> with the . , but I must make this also for:
- doob (27/35) Jan 11 2008 You don't write classname::function, you put all the functions in the
- Luca Lupo (47/47) Jan 11 2008 Thanks,
- Janice Caron (4/4) Jan 11 2008 Instead of
- Luca Lupo (82/82) Jan 11 2008 So this files is correctly converted from .cpp to .d??
- bearophile (7/11) Jan 11 2008 I think it may be necessary a cast there:
- Luca Lupo (233/233) Jan 11 2008 So I have finish conversion but there are problems:
- bearophile (4/6) Jan 11 2008 I suggest you to do some of your own homework, and read some docs. I don...
- doob (18/236) Jan 10 2008 D don't need any kind of header files, you just put the implementation
- BLS (12/13) Jan 08 2008 May I offer you a porting C++ to D link :
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
Luca Lupo wrote:Someone known convert this c++ code to D? TANKS SO MUCHMost 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
Luca Lupo Wrote:Someone known convert this c++ code to D? TANKS SO MUCHBelow 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
Jason House wrote:Luca Lupo Wrote: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? --bbSomeone known convert this c++ code to D? TANKS SO MUCHBelow 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
Jan 08 2008
Jason House schrieb:Luca Lupo Wrote: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 :) BjoernSomeone known convert this c++ code to D? TANKS SO MUCHBelow 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
Jason House:// If no overloading by derived classes, // compiler will make non-virtualBut is this true? (I did some benchmarks, and to me it seems false for the current DMD 1.x.) Bye, bearophile
Jan 08 2008
bearophile wrote:Jason House:It's possible, but DMD does not currently do this. Sean// If no overloading by derived classes, // compiler will make non-virtualBut is this true? (I did some benchmarks, and to me it seems false for the current DMD 1.x.)
Jan 08 2008
Sean Kelly wrote:bearophile wrote:Silly me; that's what I get for reading the online docs... That's also where I got the Slist thing too.Jason House:It's possible, but DMD does not currently do this. Sean// If no overloading by derived classes, // compiler will make non-virtualBut is this true? (I did some benchmarks, and to me it seems false for the current DMD 1.x.)
Jan 10 2008
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
Luca Lupo:I would known if also the file include of D have the extension.hWhat do you need them for? :-) Bye, bearophile
Jan 10 2008
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.hWhat do you need them for? :-) Bye, bearophile
Jan 10 2008
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 .dYes, 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
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
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
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
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?? ThanksYou 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
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
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
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
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
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
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
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 ThanksD 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
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 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