473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Conceptual function type problem

Hello. I have a strange conceptual problem I'm trying to think about.

I would like to build a library that allows the user to do the
following:

1) User defined types:
The user defines their own types as needed for their particular
application and registers them with the library.

class ClassA {};
class ClassB : public ClassA {};
class ClassC: public ClassB{};

REGISTERTYPE(ClassA);
REGISTERTYPE(ClassB);
REGISTERTYPE(ClassC);

2) User defined functions:
The user defines a set of functions as needed for their particular
application and registers them with the library.

ClassA Function1(ClassB);
ClassB Function2(ClassB);
ClassC Function3(ClassC, ClassB);
ClassC Function4(ClassC);

REGISTERFUNCTION(Function1);
REGISTERFUNCTION(Function2);
REGISTERFUNCTION(Function3);
REGISTERFUNCTION(Function4);

3) The user can then query the library for functions that return the
type it requires and call those functions and get an object of the
desired type OR a type that can be implicitly converted to the desired
type:

// User needs a ClassA object
vector<ClassAresults;
vector<something?functions = GetFunctions(some way to indicate
ClassA);
// GetFunctions should return [pointers/function objs/something else?]
for Functions 1, 2, 3 and 4
// (since a ClassA object can be obtained from them or implicitly
converted from their return types)
for(functions::const_itr itr = functions.begin(); itr !=
functions.end(); itr++)
results.push_back(CALLFUNCTION(itr));

I'm trying to develop an architecture that would permit this... I'm
thinking the registration functions would need some kind of template
metaprogramming code... could this maybe be done simply with
polymorphism and virtual functions? Boost::any was another idea I
considered.

I think I am getting stuck in two areas: trying to query for the
return type of a function and then trying to determine if that return
type can be implicitly converted to some other type (either because
the return type is a derived class of the desired type or because the
return type has an implicit conversion operator to the desired type).

Any info appreciated. Obviously not looking for code, more just tips
on how to think about this kind of a problem.

Cheers

Aug 10 '07 #1
6 1623
Hi!

cp****@gmail.com schrieb:
3) The user can then query the library for functions that return the
type it requires and call those functions and get an object of the
desired type OR a type that can be implicitly converted to the desired
type:
So how about parameters to these functions? How is the user supposed to
call the functions if she doesn't know what arguments to provide?

It seems to me you try to "register" ways (functions) which can later be
used to create certain objects. Am I right here?

Frank
Aug 10 '07 #2
cp****@gmail.com wrote:
I think I am getting stuck in two areas: trying to query for the
return type of a function and then trying to determine if that return
type can be implicitly converted to some other type (either because
have you try Boost.Function,
but it can't trait out the return type of operator function
the return type is a derived class of the desired type or because the
return type has an implicit conversion operator to the desired type).

Any info appreciated. Obviously not looking for code, more just tips
on how to think about this kind of a problem.
Aug 11 '07 #3
Barry wrote:
cp****@gmail.com wrote:
>I think I am getting stuck in two areas: trying to query for the
return type of a function and then trying to determine if that return
type can be implicitly converted to some other type (either because

have you try Boost.Function,
but it can't trait out the return type of operator function
>the return type is a derived class of the desired type or because the
return type has an implicit conversion operator to the desired type).

Any info appreciated. Obviously not looking for code, more just tips
on how to think about this kind of a problem.
Sorry my mistake,
Boost.Utility result_of
Aug 11 '07 #4
Thanks for the tips.

Part of the challenge I'm having is figuring out conceptually how to
store these objects.

I.e. I could have a vector<boost::function>, but then i have the issue
that these functions take all kinds of parameters and return whatever
types they want (as determined by the user who wrote them).

I can't use templates for this reason as well.

With regards to parameters, i need a way to basically query the
function at run time and determine what types it takes as parameters
and then lookup a function in the function library that returns the
appropriate type. I.e. the system says I need to execute Function1,
but to do so i need an object of ClassB, so let's go through all the
functions and find the ones that return an object of ClassB (Function2
in the previous example would meet this requirement) and execute that
function, take its return value and pass it to Function1.

I'm leaning towards template metaprogramming as a way to automatically
generate all possible combinations of types with the appropriate
dynamic_casts in there to handle inherited types, but I'm quite the
newb on MPL.

Pointers appreciated
On Aug 10, 10:10 pm, Barry <dh...@126.comwrote:
Barry wrote:
cpp...@gmail.com wrote:
I think I am getting stuck in two areas: trying to query for the
return type of a function and then trying to determine if that return
type can be implicitly converted to some other type (either because
have you try Boost.Function,
but it can't trait out the return type of operator function
the return type is a derived class of the desired type or because the
return type has an implicit conversion operator to the desired type).
Any info appreciated. Obviously not looking for code, more just tips
on how to think about this kind of a problem.

Sorry my mistake,
Boost.Utility result_of- Hide quoted text -

- Show quoted text -

Aug 17 '07 #5

cppnow <cp****@gmail.comwrote in message...
Thanks for the tips.
Thanks for top-posting.
Part of the challenge I'm having is figuring out conceptually how to
store these objects.
What objects?

Why not just let your users program it in C++, directly?
It sounds to me like you are trying to re-invent scripting language.
( there are already thousands of those!! )
[ sorry, just being a smart-ass. <G]
I.e. I could have a vector<boost::function>, but then i have the issue
that these functions take all kinds of parameters and return whatever
types they want (as determined by the user who wrote them).
Look up 'Multiple dispatching' (using the Visitor pattern).

There is a (somewhat) simple example in "Thinking in C++" Vol. 2.
Chapter 10: Design Patterns, section 'Multiple dispatching'.

Get "Thinking in C++", 2nd ed. Volume 1&2 by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html
FAQ http://www.parashift.com/c++-faq-lite
--
Bob R
POVrookie
Aug 17 '07 #6
Part of the challenge I'm having is figuring out conceptually how to
store these objects.

What objects?

Why not just let your users program it in C++, directly?
It sounds to me like you are trying to re-invent scripting language.
( there are already thousands of those!! )
[ sorry, just being a smart-ass. <G]
You've kind of hit the nail on the head here. The idea is that the
user provides the building blocks of the program (functions and data
types) and the library can reconfigure them as needed at run-time and
'execute' these new reconfigured 'programs'. So for example if the
user supplied two ways (functions) of creating an object (of a user-
defined type), the library might pick one at one point and another at
another point. You can think of these reconfigured 'programs' as
deferred-evaluation expressions. The user doesn't actually build the
'program' except by specifying what he wants to pop out of the
'program' and the library assembles the building blocks necessary to
produce it

Look up 'Multiple dispatching' (using the Visitor pattern).

There is a (somewhat) simple example in "Thinking in C++" Vol. 2.
Chapter 10: Design Patterns, section 'Multiple dispatching'.

Get "Thinking in C++", 2nd ed. Volume 1&2 by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html
I had a look at both. My hunch with multiple dispatching using visitor
is passing an operator object at a data object and using the double
calls to resolve against the types of both. This would mean that the
user would have to provide a version of each operator for every type
in his program. I'm more hoping the user could say "This operator
requires an object of type B, any object of a type derived from B or
any object convertible to type B" and the library would understand how
to work within the user's type-hierarchy to satisfy these
requirements.

As a more involved example, the user might provide the library with
the following types and functions:

class Q;
class A { public: A(const &Q) {// construct from a Q} };
class B : public A {};
class C { public: operator A() {// convert this C object to an A
object} };

int Builder1(const A& b); // do something with the A
B Builder2(); // the library should know that it can use this function
to build the parameter to be fed into Builder1 (since all Bs are As)
C Builder3(); // likewise with this function since all Cs can be
converted to As
Q Builder4(); // and again with this function since As can be
constructed using Qs

The user can then ask the library to use these building blocks to
produce an int. The only way to do that is with Builder1(), which
takes as input the output of one of the other builders, etc... Being
able to automatically make these determinations is the work of the
library. The library wouldn't actually build C++ expressions, but it
might pass around function objects, move data in boost::any objects,
use dynamic_cast<to handle traversing the type hierarchy, test for
convertibility using boost::is_convertible, etc., etc.

Since this line of reasoning uses compile-time information (types,
conversions, return values, etc.), I'm thinking this is a natural
application of metaprogramming, but i don't want to start a huge
effort in that direction if there's something fundamental I'm missing.

Aug 20 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: mACKnIFE | last post by:
Hi list! I've just finished reading the nice book: "Data Modeling and Relational Database Design Volume 1 - Student Guide", by Jan Speelpenning, Patrice Daux and Jeff Gallus, published by Oracle...
4
by: Daniel Ladd | last post by:
Hi, I have a problem with a conceptual graph in c++. I have a oist of structures like this: typedef struct Conceptual { char* Name;//Rappresenta la parola da mettere nel grafo Conceptual* Next;...
3
by: M Davidson | last post by:
Hello all, Thank you in advance for any guidance. (The standard form/subform won't work because my users will mess up and not enter a value for a category. I don't mean to speak ill of...
1
by: kuhni | last post by:
Hi everybody! Writing this time, I'm really desperate. Basically, I have a conceptual problem of how to solve a certain "problem" in MS Access 97. General objective: In order to categorise...
0
by: shirsoft | last post by:
I having some conceptual prob switching to c# I want to make a bitmap obj, and pass it to my func which will actually set its width, height and data ptr. But i am at loss as to how to do it ...
2
by: jason | last post by:
Please pardon my completely lack of understanding on the topic. I have a website I developed with another developer. We are both far from experts in VB.NET and OOP. We developed the site WITHOUT...
3
by: Thom Anderson | last post by:
Hi. I have been asked to create a web tool for my company. Basically an employee will use a user control (a tree view containing information about our company) and when they get to where they...
12
by: RSH | last post by:
I am still trying to grasp the use of real world Objects and how to conceptualize them using a business scenerio. What I have below is an outline that I am wrestling with trying to figure out a...
13
by: AliRezaGoogle | last post by:
Dear Members, I have a problem in the concepts of Monit.Enter and Monitor.Exit (Before everything I should say that I know how to solve this problem by using Monitor.Wait and I do not need a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.