move raw pointer to smart ptrs

This commit is contained in:
Dennis Luxen 2014-04-28 19:37:23 +02:00
parent d0b4ffd154
commit f02ec41fbc

View File

@ -39,6 +39,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "../Util/SimpleLogger.h" #include "../Util/SimpleLogger.h"
#include "../Util/StringUtil.h" #include "../Util/StringUtil.h"
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/unordered_map.hpp> #include <boost/unordered_map.hpp>
#include <cstdlib> #include <cstdlib>
@ -50,7 +52,7 @@ template<class DataFacadeT>
class ViaRoutePlugin : public BasePlugin { class ViaRoutePlugin : public BasePlugin {
private: private:
boost::unordered_map<std::string, unsigned> descriptorTable; boost::unordered_map<std::string, unsigned> descriptorTable;
SearchEngine<DataFacadeT> * search_engine_ptr; boost::shared_ptr<SearchEngine<DataFacadeT> > search_engine_ptr;
public: public:
explicit ViaRoutePlugin(DataFacadeT * facade) explicit ViaRoutePlugin(DataFacadeT * facade)
@ -58,16 +60,13 @@ public:
descriptor_string("viaroute"), descriptor_string("viaroute"),
facade(facade) facade(facade)
{ {
//TODO: set up an engine for each thread!! search_engine_ptr = boost::make_shared<SearchEngine<DataFacadeT> >(facade);
search_engine_ptr = new SearchEngine<DataFacadeT>(facade);
descriptorTable.emplace("json", 0); descriptorTable.emplace("json", 0);
descriptorTable.emplace("gpx" , 1); descriptorTable.emplace("gpx" , 1);
} }
virtual ~ViaRoutePlugin() { virtual ~ViaRoutePlugin() { }
delete search_engine_ptr;
}
const std::string & GetDescriptor() const { return descriptor_string; } const std::string & GetDescriptor() const { return descriptor_string; }
@ -81,19 +80,19 @@ public:
return; return;
} }
RawRouteData rawRoute; RawRouteData raw_route;
rawRoute.checkSum = facade->GetCheckSum(); raw_route.checkSum = facade->GetCheckSum();
const bool checksumOK = (routeParameters.checkSum == rawRoute.checkSum); const bool checksumOK = (routeParameters.checkSum == raw_route.checkSum);
std::vector<std::string> textCoord; std::vector<std::string> textCoord;
for(unsigned i = 0; i < routeParameters.coordinates.size(); ++i) { for(unsigned i = 0; i < routeParameters.coordinates.size(); ++i) {
if( !checkCoord(routeParameters.coordinates[i]) ) { if( !checkCoord(routeParameters.coordinates[i]) ) {
reply = http::Reply::StockReply(http::Reply::badRequest); reply = http::Reply::StockReply(http::Reply::badRequest);
return; return;
} }
rawRoute.rawViaNodeCoordinates.push_back(routeParameters.coordinates[i]); raw_route.rawViaNodeCoordinates.push_back(routeParameters.coordinates[i]);
} }
std::vector<PhantomNode> phantomNodeVector(rawRoute.rawViaNodeCoordinates.size()); std::vector<PhantomNode> phantomNodeVector(raw_route.rawViaNodeCoordinates.size());
for(unsigned i = 0; i < rawRoute.rawViaNodeCoordinates.size(); ++i) { for(unsigned i = 0; i < raw_route.rawViaNodeCoordinates.size(); ++i) {
if(checksumOK && i < routeParameters.hints.size() && "" != routeParameters.hints[i]) { if(checksumOK && i < routeParameters.hints.size() && "" != routeParameters.hints[i]) {
DecodeObjectFromBase64(routeParameters.hints[i], phantomNodeVector[i]); DecodeObjectFromBase64(routeParameters.hints[i], phantomNodeVector[i]);
if(phantomNodeVector[i].isValid(facade->GetNumberOfNodes())) { if(phantomNodeVector[i].isValid(facade->GetNumberOfNodes())) {
@ -101,93 +100,86 @@ public:
} }
} }
facade->FindPhantomNodeForCoordinate( facade->FindPhantomNodeForCoordinate(
rawRoute.rawViaNodeCoordinates[i], raw_route.rawViaNodeCoordinates[i],
phantomNodeVector[i], phantomNodeVector[i],
routeParameters.zoomLevel routeParameters.zoomLevel
); );
} }
PhantomNodes segmentPhantomNodes; PhantomNodes current_phantom_node_pair;
for(unsigned i = 0; i < phantomNodeVector.size()-1; ++i) { for (unsigned i = 0; i < phantomNodeVector.size()-1; ++i)
segmentPhantomNodes.source_phantom = phantomNodeVector[i]; {
segmentPhantomNodes.target_phantom = phantomNodeVector[i+1]; current_phantom_node_pair.source_phantom = phantomNodeVector[i];
rawRoute.segmentEndCoordinates.push_back(segmentPhantomNodes); current_phantom_node_pair.target_phantom = phantomNodeVector[i+1];
raw_route.segmentEndCoordinates.push_back(current_phantom_node_pair);
} }
if( if ((routeParameters.alternateRoute) && (1 == raw_route.segmentEndCoordinates.size()))
( routeParameters.alternateRoute ) && {
(1 == rawRoute.segmentEndCoordinates.size()) search_engine_ptr->alternative_path(raw_route.segmentEndCoordinates[0], raw_route);
) { }
search_engine_ptr->alternative_path( else
rawRoute.segmentEndCoordinates[0], {
rawRoute search_engine_ptr->shortest_path(raw_route.segmentEndCoordinates, raw_route);
);
} else {
search_engine_ptr->shortest_path(
rawRoute.segmentEndCoordinates,
rawRoute
);
} }
if (INT_MAX == rawRoute.lengthOfShortestPath) if (INT_MAX == raw_route.lengthOfShortestPath)
{ {
SimpleLogger().Write(logDEBUG) << "Error occurred, single path not found"; SimpleLogger().Write(logDEBUG) << "Error occurred, single path not found";
} }
reply.status = http::Reply::ok; reply.status = http::Reply::ok;
//TODO: Move to member as smart pointer if (!routeParameters.jsonpParameter.empty())
BaseDescriptor<DataFacadeT> * desc; {
if("" != routeParameters.jsonpParameter) {
reply.content.push_back(routeParameters.jsonpParameter); reply.content.push_back(routeParameters.jsonpParameter);
reply.content.push_back("("); reply.content.push_back("(");
} }
DescriptorConfig descriptorConfig; DescriptorConfig descriptor_config;
unsigned descriptorType = 0; unsigned descriptor_type = 0;
if(descriptorTable.find(routeParameters.outputFormat) != descriptorTable.end() ) { if(descriptorTable.find(routeParameters.outputFormat) != descriptorTable.end() ) {
descriptorType = descriptorTable.find(routeParameters.outputFormat)->second; descriptor_type = descriptorTable.find(routeParameters.outputFormat)->second;
} }
descriptorConfig.zoom_level = routeParameters.zoomLevel; descriptor_config.zoom_level = routeParameters.zoomLevel;
descriptorConfig.instructions = routeParameters.printInstructions; descriptor_config.instructions = routeParameters.printInstructions;
descriptorConfig.geometry = routeParameters.geometry; descriptor_config.geometry = routeParameters.geometry;
descriptorConfig.encode_geometry = routeParameters.compression; descriptor_config.encode_geometry = routeParameters.compression;
switch(descriptorType){ boost::shared_ptr<BaseDescriptor<DataFacadeT> > descriptor;
switch(descriptor_type){
case 0: case 0:
desc = new JSONDescriptor<DataFacadeT>(); descriptor = boost::make_shared<JSONDescriptor<DataFacadeT> >();
break; break;
case 1: case 1:
desc = new GPXDescriptor<DataFacadeT>(); descriptor = boost::make_shared<GPXDescriptor<DataFacadeT> >();
break; break;
default: default:
desc = new JSONDescriptor<DataFacadeT>(); descriptor = boost::make_shared<JSONDescriptor<DataFacadeT> >();
break; break;
} }
PhantomNodes phantomNodes; PhantomNodes phantom_nodes;
phantomNodes.source_phantom = rawRoute.segmentEndCoordinates[0].source_phantom; phantom_nodes.source_phantom = raw_route.segmentEndCoordinates[0].source_phantom;
phantomNodes.target_phantom = rawRoute.segmentEndCoordinates[rawRoute.segmentEndCoordinates.size()-1].target_phantom; phantom_nodes.target_phantom = raw_route.segmentEndCoordinates[raw_route.segmentEndCoordinates.size()-1].target_phantom;
desc->SetConfig(descriptorConfig); descriptor->SetConfig(descriptor_config);
desc->Run(rawRoute, phantomNodes, facade, reply); descriptor->Run(raw_route, phantom_nodes, facade, reply);
if("" != routeParameters.jsonpParameter) { if (!routeParameters.jsonpParameter.empty())
{
reply.content.push_back(")\n"); reply.content.push_back(")\n");
} }
reply.headers.resize(3); reply.headers.resize(3);
reply.headers[0].name = "Content-Length"; reply.headers[0].name = "Content-Length";
std::string tmp;
unsigned content_length = 0; unsigned content_length = 0;
BOOST_FOREACH(const std::string & snippet, reply.content) { BOOST_FOREACH(const std::string & snippet, reply.content) {
content_length += snippet.length(); content_length += snippet.length();
} }
intToString(content_length, tmp); std::string tmp_string;
reply.headers[0].value = tmp; intToString(content_length, tmp_string);
switch(descriptorType){ reply.headers[0].value = tmp_string;
switch(descriptor_type){
case 0: case 0:
if( !routeParameters.jsonpParameter.empty() ){ if( !routeParameters.jsonpParameter.empty() ){
reply.headers[1].name = "Content-Type"; reply.headers[1].name = "Content-Type";
@ -223,8 +215,6 @@ public:
} }
break; break;
} }
delete desc;
return; return;
} }
private: private: