View mode: basic / threaded / horizontal-split · Log in · Help
January 11, 2008
Re: Translate C++ to D
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("");
            }
     }
January 11, 2008
Re: Translate C++ to D
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
January 11, 2008
Re: Translate C++ to D
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();  
      };
January 11, 2008
Re: Translate C++ to D
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
Next ›   Last »
1 2 3
Top | Discussion index | About this forum | D home