Please excuse the length of this post, I am unfortunately long-winded,
and don't know how to make my postings more brief.
I have a C++ class library (and application generator, called
Yacc++(r) and the Language Objects Library) that I have converted over
to C#. It works okay. However, in the C# version, one has to build
the class library into the generated application, because I haven't
structured this one thing right. I would like to fix that obvious
hole for my clients, so that it works "just like" the C++ version.
(Well, there will always be some differences, for example the C#
version is nicely garbage collected while in the C++ version, that is
done in the C++ style.)
Ok, here is a summary of the key problem.
There is this class "yy_ref_obj" which is an application specific
class. That is each application run through the generator gets its
own custom yy_ref_obj class that is tailored to that application.
There are some other classes, like a "yy_psr_dflt_obj" which are also
generated as part of the application. In fact, the yy_psr_dflt_obj
class is actually the factory class, for the yy_psr_dflt_obj class.
That is all the yy_ref_obj objects are created by calls to functions
in the yy_psr_dflt_obj class.
However, there are common methods and members that each
yy_psr_dflt_obj class use, these are encapsulated in classes like
yy_psr_obj which is a base class for the yy_psr_dflt_obj class and is
one of the classes I would like to put into the class library.
The problem is that the base class has some arrays of yy_ref_objs and
member functions which return elements of those arrays. In C++, I use
pointers to yy_ref_objs to resolve this problem, an array of
yy_ref_objs in C++ is simply a pointer to the first yy_ref_obj in the
array. Thus, the base yy_psr_obj class can use a "forward declared"
(opaque) version of a yy_ref_obj (i.e. class yy_ref_obj;) to declare
the functions which are often virtual and defined in the derived
yy_psr_dflt_obj class when they need to return actual yy_ref_objs in
any case. What makes this work in C++ is that anytime one has a need
of a real yy_ref_obj, one has brought in an application specific
header file and the code can determine what the application specific
layout is. If one has only the "headers" for the class library
loaded, one cannot see into a yy_ref_obj, so one cannot tell its size
or shape and one doesn't care. (if this were a functional programming
group, I would explain that the library uses parametric polymorphism
(i.e. the parameter is strongly typed, but the library is agnostic to
what that type actually is), if that helps anyone understand the
problem.)
However, I don't know the best way to approach the same problem in C#.
The current solution use the yy_ref_obj class from the application in
the library code, couples the two pieces of code together too
strongly. I have thought about just using the "Object" class, but I
really want uses in the application, which call functions in the
library to get back application specific objects and not have to cast
them. I have also thought of using an interface class (e.g. a
yy_ref_ptr class) in the library code and having something that
implicitly converts a yy_ref_ptr to a yy_ref_obj, but I'm not sure of
the C# way of expressing that idea.
Note, I do have this application generation program (Yacc++)
involved, so I can generate classes and member functions that do the
real work (with yy_ref_objs) in the application context. I just want
to keep the non-application specific pieces in the library as much as
possible.
Note the current code is targeted to VS 2003, but I can target VS 2005
if that will help.
Thanks for any help or suggestions,
-Chris
************************************************** ***************************
Chris Clark Internet : co*****@world.std.com
Compiler Resources, Inc. Web Site : http://world.std.com/~compres
23 Bailey Rd voice : (508) 435-5016
Berlin, MA 01503 USA fax : (978) 838-0263 (24 hours)
------------------------------------------------------------------------------