CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM

How to deal with fields of unknown type

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   May 20, 2008, 09:27
Default Hello, maybe you can sugges
  #1
kar
Senior Member
 
Kārlis Repsons
Join Date: Mar 2009
Location: Latvia
Posts: 111
Rep Power: 17
kar is on a distinguished road
Hello,

maybe you can suggest a good solution for this problem: let F be an array of names and pointers (void*) to fields like volScalarField, surfaceVectorField etc; when adding a new entry to F, pointer type should be somehow memorised such a way, that it is as easy as possible to deal with that actual type of field later through pointer.

For example, I could use

volScalarField& f;
string typeName__ = f.type();,

but that means, whenever I need to do something, I need to build a ton of switches depending on typeName__. That is bad. Especially if I know, there is type() for each pointer...

A template solution?
kar is offline   Reply With Quote

Old   May 20, 2008, 10:24
Default And one more thing worth to me
  #2
kar
Senior Member
 
Kārlis Repsons
Join Date: Mar 2009
Location: Latvia
Posts: 111
Rep Power: 17
kar is on a distinguished road
And one more thing worth to mention:
if template function is in *.so, where it is not called, it simply is not defined for an external function call. That takes dirty work-around...
I would appreciate some words of wisdom here.
kar is offline   Reply With Quote

Old   May 20, 2008, 13:21
Default Hi Karlis! (Somebody of the
  #3
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Hi Karlis!

(Somebody of the people with real knowledge: please correct me if I'm talking nonsense)

- your example doesn't make sense, because once f is a reference to volScalarField you already now what f.type() will be. The only common parent-class for all GeometricFields is IOobject where you can achieve something similar with f.headerClassName()
- I think what you are looking for is runtime polymorphism. The problem is that templates are "compile time" polymorph (in other words the same pointer can not point on a volScalarField and a volVectorField AND know that they are some kind of GeometricField
- the best solution is using templated functions/methods, Have a look at the sources for reconstructPar, how something like that is done (don'T know what you're trying to do)

Bernhard
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Old   May 20, 2008, 13:21
Default There is already a mechanism t
  #4
Senior Member
 
Eugene de Villiers
Join Date: Mar 2009
Posts: 725
Rep Power: 21
eugene is on a distinguished road
There is already a mechanism that handles all eligible objects as regIOobjects instead of their own types. Check the store() member functions in regIOobject and the lookup functions in objectRegistry to see how it is done.
eugene is offline   Reply With Quote

Old   May 20, 2008, 15:33
Default Ok, thank you, this info can b
  #5
kar
Senior Member
 
Kārlis Repsons
Join Date: Mar 2009
Location: Latvia
Posts: 111
Rep Power: 17
kar is on a distinguished road
Ok, thank you, this info can be useful too.
Looks like I wanted to squeeze too much out of g++ compiler + wasn't sure what I want.
What is done here is writing a class for easy dynamic field management, that also reads and registers fields by writing far less C++ code, once that class is written.

One thing I still want to know about this: is it possible to write and compile library with function or class templates without ever calling any of them within library code, but using afterwards?
kar is offline   Reply With Quote

Old   May 20, 2008, 16:11
Default Answer: Yes, it is possible, B
  #6
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Answer: Yes, it is possible, BUT you've got to instantiate them explicitly in one of the object files of the library: http://www.devx.com/tips/Tip/12652
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Old   May 20, 2008, 16:32
Default So one should use: template
  #7
kar
Senior Member
 
Kārlis Repsons
Join Date: Mar 2009
Location: Latvia
Posts: 111
Rep Power: 17
kar is on a distinguished road
So one should use:

template int min<int> (int, int); //function template instantiation
template class vector<string> //class template explicit instantiation

I appreciate.
kar is offline   Reply With Quote

Old   May 22, 2008, 06:17
Default But may I extend the topic abo
  #8
kar
Senior Member
 
Kārlis Repsons
Join Date: Mar 2009
Location: Latvia
Posts: 111
Rep Power: 17
kar is on a distinguished road
But may I extend the topic about runtime function calls to classes of varying implementation?
Basic idea is: there exist N>1 classes with common functions of identical names, which are differently implemented. How to call those functions, if classes aren't the same and pointer selection happens in runtime?
kar is offline   Reply With Quote

Reply


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 On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
TurbDyMFoam Unknown dynamicFvMesh type mixerFvMesh david OpenFOAM Running, Solving & CFD 11 February 16, 2009 16:30
Unknown error sivakumar OpenFOAM Pre-Processing 9 September 9, 2008 12:53
Unknown faceTetPolyPatchCellDecomp chris1980 OpenFOAM Running, Solving & CFD 2 September 4, 2006 06:01
UNKNOWN ERROR Robbie FLUENT 0 July 15, 2005 19:27
Unknown Error Robbie FLUENT 0 July 12, 2005 17:05


All times are GMT -4. The time now is 18:30.