AW: [odb-users] reuse inheritance with templated base class

Marcel Nehring mne at qosmotec.com
Thu Aug 28 07:23:32 EDT 2014


Hi Boris,

the reason for why I asked if it is possible to store different classes in the same table is that to me it looked like that it is not possible. Let's say I have two classes with two members each. One of these members is "the same" in both classes i.e. I want them to be mapped to the same column. The other member should be mapped to different columns. So that if I persist one of the classes it will use two of the table's columns and set the other one to null. In my specific case we are talking about the same template class with different template parameters/members.
However, ODB does only generate the DDL for one of the classes. So in my example I would have a table with two instead of three columns. So when you said it works, did you mean it works if I generate the table by hand?

Unfortunately there is another thing I could not solve myself. ODB supports C++11 enum classes, but when I try to compile code that uses them I get a compile error.  That is because in traits.hxx in big_int_value_traits::set_value ODB tries to assign the value 0 to an instance of my enum class. But implicit conversions are illegal for  enum classes. Searching the documentation for enum class I could not find any special treatment that is necessary to persist members of an enum class type. What am I missing here?

Thanks,
	Marcel

-----Ursprüngliche Nachricht-----
Von: Boris Kolpackov [mailto:boris at codesynthesis.com] 
Gesendet: Donnerstag, 21. August 2014 09:49
An: Marcel Nehring
Cc: odb-users at codesynthesis.com
Betreff: Re: [odb-users] reuse inheritance with templated base class

Hi Marcel,

[I've CC'ed odb-users since others may find this information useful.]

Marcel Nehring <mne at qosmotec.com> writes:

> But I have indeed discovered a few other issues/questions this morning.
> 
> 1) Trying to compile the generated ODB code gives me a C2380 error 
> with Visual Studio. To me it looks like that this might be because I 
> have a value type declared that has a member called value. If I then 
> define a class that has a member of type of this value type that is 
> also named value, then the resulting struct in the ODB code is called 
> value_type_ and inside this struct there is a typedef that is also called value_type_.

Uh, you are really pushing ODB, I like that ;-). I've fixed this, let me know if you would like the updated ODB compiler binary.


> 2) Is it somehow possible to store different classes in the same 
> database table? For example for a certain template class I would like 
> to store all of its instantiations in a single table.

Yes, you can map multiple classes to the same table:

#pragma db object table("foo")
class foo
{
  ...
};

#pragma db object table("foo")
class bar
{
  ...
};

ODB just assumes you know what you are doing ;-).


> I would  be able to figure out the correct template instantiation that 
> is needed when reading the row from the database by one of the columns 
> value.

Just to clarify, "you" here is literally "you", not ODB. You will need to somehow figure out the correct class to use before, say, calling load(). You could, for example, use a view to first load the column value and then based on that, load the correct object.

You might also want to take a look at object sections (Chapter 9). They allow you to partition a class into multiple separately loadable, updatable, etc., groups of data members. Maybe this is what you are looking for.

Also polymorphism might be related (Section 8.2).


> 3) I cannot specify different output directories for the different 
> types (*.hxx, *.sql, etc.) of generated files, can I?

No, but you can always move the files where you need to afterwards with a few shell commands.


> 4) I cannot specify different include prefixes for my own header files 
> and odb's header files, can I? For example that it results in #include 
> "inc/MyClass.h" and #include "gen/odb/MyClass-odb.h". At the moment I 
> do this by using the include-regex option.

--include-regex is the way to do it. There is no special mechanism to separate headers into "your own" and "odb's".

Boris



More information about the odb-users mailing list