LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-27-2006, 09:19 AM   #1
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
class declared in lib, defined in implementation (C++)


I'm currently working on a lib that provides a hierarchical data structure to applications. The library defines object and structure behavior, but can't define communication between objects because of the potentially diverse implementations. To facilitate this, I've included virtual functions taking pointers/references to a declared but undefined class so that the lib objects have communications functions available to the user. Example:
Code:
//library

class Message;
//Implementation-defined

struct MessageRecipient
{
        virtual
                bool
        SendMessage(Message&) = 0;
};
Code:
//program

#include <library.hpp>

class Message {};

class MyObject :
        public MessageRecipient
{
                bool
        SendMessage(Message&);
};
This allows the user to implement the interface of 'Message' as they see fit; if I were to make it an abstract class then the communication potential of a 'MessageRecipient' would be restricted to that interface only.

The library provides a default class definition (derived from std::string) available by including an additional header, but the user still has the option to define their own. Aside from possible misuse (such as two object files using different class definitions), are there any problems with this technique?
ta0kira

Last edited by ta0kira; 09-27-2006 at 09:20 AM.
 
Old 09-27-2006, 10:04 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Personally I would use an abstract class/struct or a templated class/struct.
 
Old 09-27-2006, 12:21 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
Yes, I considered those, but each have their own problems with this lib:

Abstract: Ideal source code solution, but very restrictive as far as interface. The library doesn't implement the messages at all, so it couldn't presume to know what type of interface would be most suitable. As an API strategy, I've also tried to reduce abstract functions to those actually called by the library.

Template: Can't be compiled into a shared library, and the library doesn't have any templates otherwise. This lib has 4 classes as mentioned (input, output, data, message), which would make each element require 4 template args. Each resulting program should only require a single definition for each of these classes.

The implementation-defined classes can be as simple as strings or as complex as polymorphic classes with virtual functions. This makes interface definition impossible.

The other options are 'void*' or a visitor pattern. 'void*' is much more shaky than what I'm doing now, and a visitor pattern would leave the same interface definition problem with the visitor class.

The restriction is that the structural elements are linked and provide pointers to each other via prev/next type functions. The returned type must be a pointer to the same type of object, requiring the in/out/message/etc. functions to be built into the element class itself.
ta0kira
 
Old 09-27-2006, 03:45 PM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
i would say an abstract Message, and MessageRecip as a template class taking the correct form of message.. however if you do not want to use templates you could define Message as abstract and have the user derive their own Message type from it. it would still be passed as a generic Message type to the MessRecp, then the MessRecp would have to cast the message to the appropriate Message type (where the template *should* be)..

does that make any sense.. i have a hard time thinking it if im not coding it..
 
Old 09-28-2006, 12:03 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
I know what you're saying xhi. If I'm going to require the resulting program to static_cast, however, I might as well make it an empty class (since the lib has no interface requirement), or give it a template function:
Code:
//Library

struct Message
{
        template <class Type>
                Type
        *Cast() { return static_cast <Type*> (this); }
};

//User's Implementation

class MyMessage :
        public Message
{};

//Main Program

int main()
{
        MyMessage Msg;

        Message *Generic = &Msg; //<-- simulates function call with 'Message*' argument
        MyMessage *Specific = Generic->Cast <MyMessage> (); //<-- sumulated function body
}
I don't see any advantage over allowing the user to define the class 'Message' in this case. If the user knows a 'Message*' argument will always have 'MyMessage' underlying (even if that iself is abstract), then they might as well be able to define 'Message' as they would 'MyMessage'.

Another option I thought of was an object cache where the argument would be a numeric identifier (probably unsigned int.) The user's element would then manage accessing the correct object, managed by a cache also in their program, using the numeric argument to denote the correct object.

So, it seems I have 6 alternatives to what I'm doing (between all of my responses), but each add some frailty or superfluous code over what I'm doing now. The problem with the way I do it is the user may have multiple definitions of the class within their program, which could happen in any situation otherwise. I think that's a fair trade-off. As a lib user, would any of you prefer one of these other methods over being able to define the class yourself?
ta0kira
 
Old 09-28-2006, 08:13 PM   #6
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
yeh it seems that you have plenty of solutions available. and no, i really cant say that the method i tried to describe has any advantages over the other ones suggested.

now that i have studied your work a little more i dont think that i as a user of the lib would have any problem with what you are doing, kind of has a java feel to it.

> As a lib user, would any of you prefer one of these other methods over being able to define the class yourself?

at this point i cant say that i would rather have one over the other, i am used to having a abstract/virtual messaging heirarchy, but i would be interested in see how this works out for you. definately an interesting idea.
 
Old 09-29-2006, 08:10 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
Thanks. It seems to be working out so far, and I haven't had any "funny feelings" about it yet. I'll just have to emphasize this point in the documentation.
ta0kira
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
kdevelop error "Cannot find implementation class for form" J3N7iL Linux - Software 0 06-28-2006 01:11 AM
Which C++ editor in Linux has the class view/class browser feature imaginationworks Programming 7 05-22-2006 12:09 AM
Program defined class--Java Fredstar Programming 3 02-28-2006 05:07 PM
Unable to execute code in Implementation class when a button is clicked Gnarg Programming 2 12-01-2005 07:36 AM
Java: Overriding a class in /usr/java1.3/jre/lib/ext ??? stefanlasiewski Programming 1 08-11-2005 07:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration