Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. C++ wrappers for C struct api

C++ wrappers for C struct api

Scheduled Pinned Locked Moved The Weird and The Wonderful
questionperformancec++jsonhelp
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kishore Jonnalagadda
    wrote on last edited by
    #1

    Hi all, I am new to this forum and generally any programming help forum for that matter. I have a case where i want to maintain both C and C++ api library. So therefore i choose to make my C++ support a thin wrapper around the C code. However, i do not know what is the most effective way to achieve this without unnecessary memory and performance overhead. Below is an example case. /*Begin Code Sample*/ /*Start time.h*/ struct Time { int seconds, int nanoseconds }; void timeAdd(struct Time* time, struct Time* delta); void timeSub(struct Time* time, struct Time* delta); /*End time.h*/ /*Start timer.h*/ struct Timer { struct Time time; void (*callback)(struct Timer *timer); }; void timerSetCallback(struct Timer *timer, void (*callback)(struct Timer *timer)); void timerStart(struct Timer *timer); void timerStop(struct Timer *timer); /*End timer.h*/ /*Start time.hxx*/ namespace C { #include "time.h" } class Time { public: Time() { m_time.seconds = 0; m_time.nanoseconds = 0; }; ~Time(){}; int seconds(){return m_time.seconds;} int nanoseconds(){return m_time.nanoseconds;} void setSeconds(int seconds){m_time.seconds = seconds}; void setNanoseconds(int nanoseconds){m_time.nanoseconds = nanoseconds}; void add(Time &time){C::timeAdd(&m_time, &time.m_time);}; void sub(Time &time){C::timeSub(&m_time, &time.m_time);}; protected: C::Time m_time; }; /*End time.hxx*/ /*Start timer.hxx*/ #include "timer.h" class Timer : public Time { public: Timer():Time(){m_timer.callback = &Timer::callback;}; ~Timer(){C::timerStop(&m_timer);}; start(){m_timer.time = m_time; C::timerStart(&m_timer);}; stop(){C::timerStop(&m_timer);}; protected: static void callback(struct Timer *timer); C::Timer m_timer; }; /*End Code Sample*/ Now while this works for this simple case, it has a few drawbacks like the additional memory used to allocate m_time in class Time when all of struct Time api's would have even worked for struct Timer and hence struct Time m_time could have been "replaced" by struct Timer m_time but instead both exist in class Timer. It also is the simpler of the cases where the struct sizes are relatively small and the call to Timer::start() was easily managed by a copy of the struct. So now, my question is how can c++ wrappers be written for such cases without the additional memory overhead? Given their similarity, I definitely want class Timer to be a subclass of class Time! However, i do have the flexi

    M D P K 4 Replies Last reply
    0
    • K Kishore Jonnalagadda

      Hi all, I am new to this forum and generally any programming help forum for that matter. I have a case where i want to maintain both C and C++ api library. So therefore i choose to make my C++ support a thin wrapper around the C code. However, i do not know what is the most effective way to achieve this without unnecessary memory and performance overhead. Below is an example case. /*Begin Code Sample*/ /*Start time.h*/ struct Time { int seconds, int nanoseconds }; void timeAdd(struct Time* time, struct Time* delta); void timeSub(struct Time* time, struct Time* delta); /*End time.h*/ /*Start timer.h*/ struct Timer { struct Time time; void (*callback)(struct Timer *timer); }; void timerSetCallback(struct Timer *timer, void (*callback)(struct Timer *timer)); void timerStart(struct Timer *timer); void timerStop(struct Timer *timer); /*End timer.h*/ /*Start time.hxx*/ namespace C { #include "time.h" } class Time { public: Time() { m_time.seconds = 0; m_time.nanoseconds = 0; }; ~Time(){}; int seconds(){return m_time.seconds;} int nanoseconds(){return m_time.nanoseconds;} void setSeconds(int seconds){m_time.seconds = seconds}; void setNanoseconds(int nanoseconds){m_time.nanoseconds = nanoseconds}; void add(Time &time){C::timeAdd(&m_time, &time.m_time);}; void sub(Time &time){C::timeSub(&m_time, &time.m_time);}; protected: C::Time m_time; }; /*End time.hxx*/ /*Start timer.hxx*/ #include "timer.h" class Timer : public Time { public: Timer():Time(){m_timer.callback = &Timer::callback;}; ~Timer(){C::timerStop(&m_timer);}; start(){m_timer.time = m_time; C::timerStart(&m_timer);}; stop(){C::timerStop(&m_timer);}; protected: static void callback(struct Timer *timer); C::Timer m_timer; }; /*End Code Sample*/ Now while this works for this simple case, it has a few drawbacks like the additional memory used to allocate m_time in class Time when all of struct Time api's would have even worked for struct Timer and hence struct Time m_time could have been "replaced" by struct Timer m_time but instead both exist in class Timer. It also is the simpler of the cases where the struct sizes are relatively small and the call to Timer::start() was easily managed by a copy of the struct. So now, my question is how can c++ wrappers be written for such cases without the additional memory overhead? Given their similarity, I definitely want class Timer to be a subclass of class Time! However, i do have the flexi

      M Offline
      M Offline
      MidwestLimey
      wrote on last edited by
      #2

      Kishore Jonnalagadda wrote:

      However, i do have the flexibility to modify both the C and C++ code but i need both the C and C++ code to be equally capable, clean and efficient. How can i do it?

      Posting in the correct forum would be a good start. Try here[^] Prepare to be flamed.


      I'm largely language agnostic


      After a while they all bug me :doh:


      1 Reply Last reply
      0
      • K Kishore Jonnalagadda

        Hi all, I am new to this forum and generally any programming help forum for that matter. I have a case where i want to maintain both C and C++ api library. So therefore i choose to make my C++ support a thin wrapper around the C code. However, i do not know what is the most effective way to achieve this without unnecessary memory and performance overhead. Below is an example case. /*Begin Code Sample*/ /*Start time.h*/ struct Time { int seconds, int nanoseconds }; void timeAdd(struct Time* time, struct Time* delta); void timeSub(struct Time* time, struct Time* delta); /*End time.h*/ /*Start timer.h*/ struct Timer { struct Time time; void (*callback)(struct Timer *timer); }; void timerSetCallback(struct Timer *timer, void (*callback)(struct Timer *timer)); void timerStart(struct Timer *timer); void timerStop(struct Timer *timer); /*End timer.h*/ /*Start time.hxx*/ namespace C { #include "time.h" } class Time { public: Time() { m_time.seconds = 0; m_time.nanoseconds = 0; }; ~Time(){}; int seconds(){return m_time.seconds;} int nanoseconds(){return m_time.nanoseconds;} void setSeconds(int seconds){m_time.seconds = seconds}; void setNanoseconds(int nanoseconds){m_time.nanoseconds = nanoseconds}; void add(Time &time){C::timeAdd(&m_time, &time.m_time);}; void sub(Time &time){C::timeSub(&m_time, &time.m_time);}; protected: C::Time m_time; }; /*End time.hxx*/ /*Start timer.hxx*/ #include "timer.h" class Timer : public Time { public: Timer():Time(){m_timer.callback = &Timer::callback;}; ~Timer(){C::timerStop(&m_timer);}; start(){m_timer.time = m_time; C::timerStart(&m_timer);}; stop(){C::timerStop(&m_timer);}; protected: static void callback(struct Timer *timer); C::Timer m_timer; }; /*End Code Sample*/ Now while this works for this simple case, it has a few drawbacks like the additional memory used to allocate m_time in class Time when all of struct Time api's would have even worked for struct Timer and hence struct Time m_time could have been "replaced" by struct Timer m_time but instead both exist in class Timer. It also is the simpler of the cases where the struct sizes are relatively small and the call to Timer::start() was easily managed by a copy of the struct. So now, my question is how can c++ wrappers be written for such cases without the additional memory overhead? Given their similarity, I definitely want class Timer to be a subclass of class Time! However, i do have the flexi

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        You should have started by reading the header of this forum, specifically where, in bold red letters, it says: Do not post programming questions in this forum.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008

        J 1 Reply Last reply
        0
        • K Kishore Jonnalagadda

          Hi all, I am new to this forum and generally any programming help forum for that matter. I have a case where i want to maintain both C and C++ api library. So therefore i choose to make my C++ support a thin wrapper around the C code. However, i do not know what is the most effective way to achieve this without unnecessary memory and performance overhead. Below is an example case. /*Begin Code Sample*/ /*Start time.h*/ struct Time { int seconds, int nanoseconds }; void timeAdd(struct Time* time, struct Time* delta); void timeSub(struct Time* time, struct Time* delta); /*End time.h*/ /*Start timer.h*/ struct Timer { struct Time time; void (*callback)(struct Timer *timer); }; void timerSetCallback(struct Timer *timer, void (*callback)(struct Timer *timer)); void timerStart(struct Timer *timer); void timerStop(struct Timer *timer); /*End timer.h*/ /*Start time.hxx*/ namespace C { #include "time.h" } class Time { public: Time() { m_time.seconds = 0; m_time.nanoseconds = 0; }; ~Time(){}; int seconds(){return m_time.seconds;} int nanoseconds(){return m_time.nanoseconds;} void setSeconds(int seconds){m_time.seconds = seconds}; void setNanoseconds(int nanoseconds){m_time.nanoseconds = nanoseconds}; void add(Time &time){C::timeAdd(&m_time, &time.m_time);}; void sub(Time &time){C::timeSub(&m_time, &time.m_time);}; protected: C::Time m_time; }; /*End time.hxx*/ /*Start timer.hxx*/ #include "timer.h" class Timer : public Time { public: Timer():Time(){m_timer.callback = &Timer::callback;}; ~Timer(){C::timerStop(&m_timer);}; start(){m_timer.time = m_time; C::timerStart(&m_timer);}; stop(){C::timerStop(&m_timer);}; protected: static void callback(struct Timer *timer); C::Timer m_timer; }; /*End Code Sample*/ Now while this works for this simple case, it has a few drawbacks like the additional memory used to allocate m_time in class Time when all of struct Time api's would have even worked for struct Timer and hence struct Time m_time could have been "replaced" by struct Timer m_time but instead both exist in class Timer. It also is the simpler of the cases where the struct sizes are relatively small and the call to Timer::start() was easily managed by a copy of the struct. So now, my question is how can c++ wrappers be written for such cases without the additional memory overhead? Given their similarity, I definitely want class Timer to be a subclass of class Time! However, i do have the flexi

          P Offline
          P Offline
          Paul Conrad
          wrote on last edited by
          #4

          No programming questions in this particular forum :mad:

          "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

          1 Reply Last reply
          0
          • K Kishore Jonnalagadda

            Hi all, I am new to this forum and generally any programming help forum for that matter. I have a case where i want to maintain both C and C++ api library. So therefore i choose to make my C++ support a thin wrapper around the C code. However, i do not know what is the most effective way to achieve this without unnecessary memory and performance overhead. Below is an example case. /*Begin Code Sample*/ /*Start time.h*/ struct Time { int seconds, int nanoseconds }; void timeAdd(struct Time* time, struct Time* delta); void timeSub(struct Time* time, struct Time* delta); /*End time.h*/ /*Start timer.h*/ struct Timer { struct Time time; void (*callback)(struct Timer *timer); }; void timerSetCallback(struct Timer *timer, void (*callback)(struct Timer *timer)); void timerStart(struct Timer *timer); void timerStop(struct Timer *timer); /*End timer.h*/ /*Start time.hxx*/ namespace C { #include "time.h" } class Time { public: Time() { m_time.seconds = 0; m_time.nanoseconds = 0; }; ~Time(){}; int seconds(){return m_time.seconds;} int nanoseconds(){return m_time.nanoseconds;} void setSeconds(int seconds){m_time.seconds = seconds}; void setNanoseconds(int nanoseconds){m_time.nanoseconds = nanoseconds}; void add(Time &time){C::timeAdd(&m_time, &time.m_time);}; void sub(Time &time){C::timeSub(&m_time, &time.m_time);}; protected: C::Time m_time; }; /*End time.hxx*/ /*Start timer.hxx*/ #include "timer.h" class Timer : public Time { public: Timer():Time(){m_timer.callback = &Timer::callback;}; ~Timer(){C::timerStop(&m_timer);}; start(){m_timer.time = m_time; C::timerStart(&m_timer);}; stop(){C::timerStop(&m_timer);}; protected: static void callback(struct Timer *timer); C::Timer m_timer; }; /*End Code Sample*/ Now while this works for this simple case, it has a few drawbacks like the additional memory used to allocate m_time in class Time when all of struct Time api's would have even worked for struct Timer and hence struct Time m_time could have been "replaced" by struct Timer m_time but instead both exist in class Timer. It also is the simpler of the cases where the struct sizes are relatively small and the call to Timer::start() was easily managed by a copy of the struct. So now, my question is how can c++ wrappers be written for such cases without the additional memory overhead? Given their similarity, I definitely want class Timer to be a subclass of class Time! However, i do have the flexi

            K Offline
            K Offline
            Kishore Jonnalagadda
            wrote on last edited by
            #5

            My apologies for being on the wrong forum. I have posted again at http://www.codeproject.com/script/Forums/View.aspx?fid=1647

            Cheers! Kishore

            1 Reply Last reply
            0
            • D Dave Kreskowiak

              You should have started by reading the header of this forum, specifically where, in bold red letters, it says: Do not post programming questions in this forum.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008

              J Offline
              J Offline
              jayart
              wrote on last edited by
              #6

              Probably the bold text should be in all capital and font size 36! :laugh:

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups