I've got the following code, trying to access functions that go beyond my base class..
When I do the compiler states it doesn't see my derived class functions such as my public member `TestCPPClient` nor `querySymbols` function.. leading me to believe it can only see the Super class.. is my factory not setup correctly?
error: cannot dynamic_cast ‘& proxy_conn’ (of type ‘class cpool::ConnectionPool::ConnectionProxy*’) to type ‘class TMAConnection&’ (source is not of class type)
48 | TMAConnection* x = dynamic_cast<TMAConnection&>(&proxy_conn);
| ^
#pragma once
#ifndef TMAConnection_H
#define TMAConnection_H
#include "TMAConnection.h"
#include <connection-pool/pool.h>
#include "tma-client/TestCppClient.h"
class TMAConnection final : public cpool::Connection {
public:
TMAConnection(constchar* hostname, constint port){
this->gateway_host=hostname;
this->gateway_port=port;
}
bool heart_beat() override { return connected; }
bool is_healthy() override { return connected; }
bool connect() override {
connected = true;
return connected;
}
void disconnect() override {
connected = false;
}
// cannot call this function
void querySymbols(std::vector<tmaapithrift::ContractDescription> & empty_contract_vect, std::vector<std::string> & queries_list) {
this->tma_client.query_matching_tickers(empty_contract_vect, queries_list);
std::vector<std::string> empty_vec;
}
TestCppClient tma_client; // cannot see this
private:
TMAConnection(){ }
friend cpool::ConnectionPoolFactory<TMAConnection>;
bool connected = false;
constchar* gateway_host;
int gateway_port;
uint16_t tma_client_id;
};
template <>
class cpool::ConnectionPoolFactory<TMAConnection> {
public:
static std::unique_ptr<cpool::ConnectionPool> create( const std::uint16_t num_connections, constchar* tma_gw_host, constint tma_gw_port) {
std::vector< std::unique_ptr<cpool::Connection> > connections;
for ( std::uint16_t k = 0; k < num_connections; ++k ) {
// cannot use std::make_unique, because constructor is hidden
connections.emplace_back( std::unique_ptr<TMAConnection>( new TMAConnection{tma_gw_host, tma_gw_port} ) );
}
return std::unique_ptr<cpool::ConnectionPool>( new cpool::ConnectionPool{std::move( connections )} );
}
};
#endif
**Main.cpp**
auto tma_conn_pool = cpool::ConnectionPoolFactory<TMAConnection>::create(32, target_gateway_host, gateway_port);
I store the reference to this `tma_conn_pool` throughout my server code as
cpool::ConnectionPool * const tma_conn_pool;
When I tried adding Templates to the ConnectionPool pointer i get more errors that this is not a template..
All other things with `override` work as expected with my customized code from the derived class `TMAConnection` .. just nothing beyond `override` functions... any hints appreciated.. I am guessing I need to modify the Factory ?
I believe I need a pointer, as the design of the connection pool keeps ownership of the objects to gives me via create()
I am going to see if I need to simply modify the factory... or in fact modify the entire library to fit my needs.. but I think I am just needing to understand how to get the pointer of the derived class instead of cpool::ConnectionPool::ConnectionProxy
EDIT:
here's my attempts to modify to cast to a pointer ..
1 2 3 4 5 6 7
cpool::ConnectionPool::ConnectionProxy proxy_conn = this->tma_conn_pool->get_connection();
TMAConnection* x = dynamic_cast<TMAConnection*>(&proxy_conn);
/home/server.cpp: In member function ‘std::vector<std::__cxx11::basic_string<char> > DataFetcher::process_data(std::vector<std::__cxx11::basic_string<char> >, int, int)’:
/home/server.cpp:48:64: error: cannot dynamic_cast ‘& proxy_conn’ (of type ‘class cpool::ConnectionPool::ConnectionProxy*’) to type ‘class TMAConnection*’ (source type is not polymorphic)
48 | TMAConnection* x = dynamic_cast<TMAConnection*>(&proxy_conn);
According to the documentation I think it's supposed to be this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
cpool::ConnectionPool::ConnectionProxy proxy_conn = this->tma_conn_pool->get_connection();
TMAConnection* x = dynamic_cast<TMAConnection&>(&proxy_conn);if(proxy_conn.valid()) // Check validity
{
if(proxy_conn->is_healthy()) // Check connection
{
// In order to access your specific connection you can use this:
TMAConnection& x = dynamic_cast<TMAConnection&>(*proxy_conn); // Note that this may throw
x.querySymbols(empty_contract_vect, sub_batch_vector);
}
else
std::cout << "Problem with connection\n"; // Or proxy_conn->connect()
}
else
std::cout << "Invalid connection\n";
this->tma_conn_pool->release_connection(std::move(proxy_conn));
An interface that requires a [dynamic_]cast seems to be rather flawed...