2016-07-04 5 views
1

Ich versuche ODB ORM und ich muss bei einer Schnittstelle bleiben, also muss ich ein const Objekt nehmen und es bestehen. Ich bin mir nicht sicher, ob die ODB-API es erlaubt, ein const-Objekt beizubehalten, weil ein Teil dafür vorbereitet zu sein scheint, aber es funktioniert nicht.Kann ich ein const-Objekt in ODB an db.persist übergeben?

Ich erhalte diesen Fehler von gcc hier:

void OdbReportRW::write_object(const MyObject &my_object) 
{ 
    odb::core::transaction t{db->begin()}; 
    db->persist(my_object); 
    t.commit(); 
} 

dies ist der Fehler, die ich denke, es sagt, dass my_object nicht const sein sollte:

In file included from /usr/local/include/odb/database.hxx:632:0, 
from odb_report.hpp:21, 
from src/vcf/odb_report.cpp:18: 
/usr/local/include/odb/database.txx: In instantiation of ‘typename odb::object_traits<T>::id_type odb::database::persist_(T&) [with T = const MyObject; odb::database_id DB = (odb::database_id)5u; typename odb::object_traits<T>::id_type = long unsigned int]’: 
/usr/local/include/odb/database.ixx:167:45: required from ‘typename odb::object_traits<T>::id_type odb::database::persist(const T&) [with T = MyObject; typename odb::object_traits<T>::id_type = long unsigned int]’ 
src/vcf/odb_report.cpp:134:26: required from here 
/usr/local/include/odb/database.txx:38:39: error: no matching function for call to ‘odb::object_traits_impl<MyObject, (odb::database_id)5u>::persist(odb::database&, const MyObject&)’ 
object_traits::persist (*this, obj); 
^ 
/usr/local/include/odb/database.txx:38:39: note: candidate is: 
In file included from src/vcf/odb_report.cpp:27:0: 
my-object-error-odb.hxx:247:5: note: static void odb::access::object_traits_impl<MyObject, (odb::database_id)1u>::persist(odb::database&, odb::access::object_traits<MyObject>::object_type&) 
persist (database&, object_type&); 
^ 
my-object-odb.hxx:247:5: note: no known conversion for argument 2 from ‘const MyObject’ to ‘odb::access::object_traits<MyObject>::object_type& {aka MyObject&}’ 

gleiche Fehler mit Klirren, ein Bit mehr deskriptiv:

In file included from src/vcf/odb_report.cpp:18: 
In file included from inc/vcf/odb_report.hpp:21: 
In file included from /usr/local/include/odb/database.hxx:632: 
/usr/local/include/odb/database.txx:38:36: error: binding of reference to type 'object_type' (aka 'MyObject') to a value of type 'const MyObject' drops qualifiers 
    object_traits::persist (*this, obj); 
            ^~~ 
/usr/local/include/odb/database.ixx:167:12: note: in instantiation of function template specialization 'odb::database::persist_<const MyObject, 5>' requested here 
    return persist_<const T, id_common> (obj); 
     ^
src/vcf/odb_report.cpp:134:13: note: in instantiation of function template specialization 'odb::database::persist<MyObject>' requested here 
     db->persist(my_object); 
      ^
inc/vcf/error-odb.hxx:247:37: note: passing argument to parameter here 
    persist (database&, object_type&); 
            ^

Allerdings kann ich in der Datenbank-Schnittstelle (von ODB), dass beide Typen sehen von persistieren vorgesehen sind: Referenz- und konstante Referenz (und andere mit Zeigern):

// in odb/database.hxx 
template <typename T> 
typename object_traits<T>::id_type 
persist (T& object); 

template <typename T> 
typename object_traits<T>::id_type 
persist (const T& object); 

ich sah, dass ein ähnlicher Fehler ausgelöst wird (find, nicht persist), wenn es in der Klasse kein Default-Konstruktor ist zu beharren (MyObject hier), aber es ist da, also ist es nicht das Problem. Ich habe bereits überprüft, dass der Standardkonstruktor nur eine zusätzliche Methode find im generierten Code generiert.

Es funktionierte Entfernen der const Specifier in meiner write_object Methode, aber wie gesagt, ich muss an einer Schnittstelle bleiben.

Irgendwelche Ideen, um ein const Objekt zu erhalten?

Antwort

1

Lese gründlicher die Dokumentation ich diese gefunden (http://www.codesynthesis.com/products/odb/doc/manual.xhtml#3.8):

Die ersten persistieren() Funktion erwartet ein konstanter Verweis auf eine Instanz beibehalten wird. Die zweite Funktion erwartet einen konstanten Objektzeiger. Beide Funktionen können nur für Objekte mit anwendungsspezifischen Objekt-IDs verwendet werden (Abschnitt 14.4.2, "auto").

in die Tat, ich wurde mit den auto Spezifizierer für Datenbank-behandelt ids:

// class MyObject  
#pragma db id auto 
unsigned long id_; 

So scheint es, dass ich nicht in der gleichen Zeit die Auto-ID und die konstante Referenz bestehen bleiben kann.