Skip to content

Commit

Permalink
Fix build errors in examples after atomic-bomb refactor of mrpt-base (M…
Browse files Browse the repository at this point in the history
…RPT#651)

* all examples: deps and names clean up
* fix examples build errors (MRPT#643)
* executeCommand()  and Ping() fixed and tested on Windows
* TParameter.h: fix build under MSVC
  • Loading branch information
jlblancoc authored Feb 12, 2018
1 parent a187ef4 commit 7d47a44
Show file tree
Hide file tree
Showing 405 changed files with 5,226 additions and 13,413 deletions.
397 changes: 207 additions & 190 deletions cmakemodules/DefineExamples.cmake

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions libs/bayes/include/mrpt/bayes/CParticleFilterData.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

#include <mrpt/bayes/CProbabilityParticle.h>
#include <mrpt/bayes/CParticleFilterCapable.h>

#include <mrpt/core/exceptions.h>
#include <mrpt/core/bits_math.h>
#include <cmath>
#include <deque>
#include <algorithm>

Expand Down Expand Up @@ -79,7 +81,7 @@ struct CParticleFilterDataImpl : public CParticleFilterCapable
if (out_max_log_w) *out_max_log_w = maxW;

/* Return the max/min ratio: */
return exp(maxW - minW);
return std::exp(maxW - minW);
MRPT_END
}

Expand All @@ -93,12 +95,12 @@ struct CParticleFilterDataImpl : public CParticleFilterCapable
for (typename particle_list_t::const_iterator it =
derived().m_particles.begin();
it != derived().m_particles.end(); ++it)
sumLinearWeights += exp(it->log_w);
sumLinearWeights += std::exp(it->log_w);
/* Compute ESS: */
for (typename particle_list_t::const_iterator it =
derived().m_particles.begin();
it != derived().m_particles.end(); ++it)
cum += mrpt::square(exp(it->log_w) / sumLinearWeights);
cum += mrpt::square(std::exp(it->log_w) / sumLinearWeights);

if (cum == 0)
return 0;
Expand Down
2 changes: 1 addition & 1 deletion libs/comms/include/mrpt/comms/net_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ std::string getLastSocketErrorStr();
*
*/
bool Ping(
const std::string& address, const int& max_attempts,
const std::string& address, const int max_attempts,
std::string* output_str = NULL);

/** @} */ // end grouping
Expand Down
26 changes: 13 additions & 13 deletions libs/comms/src/net_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,34 +494,34 @@ std::string mrpt::comms::net::getLastSocketErrorStr()
#endif
}

MRPT_TODO("Modify ping to run on Windows + Test this");
bool mrpt::comms::net::Ping(
const std::string& address, const int& max_attempts,
const std::string& address, const int max_attempts,
std::string* output_str /*=NULL*/)
{
using namespace std;

// Format a command string
string cmd_str = "ping";

// different "count" argument for Windows and *NIX systems
// different "count" argument for Windows and *NIX systems
#if defined(MRPT_OS_LINUX) || defined(__APPLE__)
string count_arg_str = "-c " + std::to_string(max_attempts);
cmd_str += " -c ";
#else
THROW_EXCEPTION("Ping not implemented on Windows Yet.");

string count_arg_str = "-n " + std::to_string(max_attempts);
cmd_str += " -n ";
#endif
cmd_str += std::to_string(max_attempts);

string redirection_str = "2>&1";
// Address:
cmd_str += " ";
cmd_str += address;

// Join the command subparts
string cmd_full_str = mrpt::format(
"%s %s %s %s", cmd_str.c_str(), count_arg_str.c_str(), address.c_str(),
redirection_str.c_str());
// Redirection:
#if defined(MRPT_OS_LINUX) || defined(__APPLE__)
cmd_str += " 2>&1";
#endif

// Finally exec the command
int code = executeCommand(cmd_full_str, output_str);
int code = executeCommand(cmd_str, output_str);

return (code == 0);
}
2 changes: 1 addition & 1 deletion libs/comms/src/sockets_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

// Reuse code from example:
//#define SOCKET_TEST_VERBOSE
#include "samples/SocketsTest/SocketsTest_impl.cpp"
#include "samples/comms_socket_example/SocketsTest_impl.cpp"

TEST(SocketTests, send_receive_object)
{
Expand Down
2 changes: 1 addition & 1 deletion libs/graphs/include/mrpt/graphs/CDirectedTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class CDirectedTree
: public mrpt::graphs::CDirectedTree<TYPE_EDGES>::Visitor
{
std::stringstream& m_s;
CMyVisitor(std::ostringstream& s) : m_s(s) {}
CMyVisitor(std::stringstream& s) : m_s(s) {}
virtual void OnVisitNode(
const TNodeID parent,
const typename mrpt::graphs::CDirectedTree<
Expand Down
21 changes: 10 additions & 11 deletions libs/io/include/mrpt/io/CFileStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ enum
*/
class CFileStream : public CStream
{
protected:
size_t Read(void* Buffer, size_t Count) override;
size_t Write(const void* Buffer, size_t Count) override;

private:
/** The actual input file stream. */
std::fstream m_f;
Expand All @@ -66,13 +62,16 @@ class CFileStream : public CStream
/** Destructor */
virtual ~CFileStream();

size_t Read(void* Buffer, size_t Count) override;
size_t Write(const void* Buffer, size_t Count) override;

/** Opens the file, returning true on success.
* \param fileName The file to be open in this stream
* \param mode The open mode: can be an or'd conbination of different
* \param fileName The file to be open in this stream
* \param mode The open mode: can be an or'd conbination of different
* values.
* By default the file is opened for open and write and created if not
* By default the file is opened for open and write and created if not
* found.
*/
*/
bool open(
const std::string& fileName, TFileOpenModes mode = fomRead | fomWrite);
/** Closes the file */
Expand All @@ -97,9 +96,9 @@ class CFileStream : public CStream
uint64_t getPositionO();

/** Reads one string line from the file (until a new-line character)
* \return true if a line has been read, false on EOF or error */
* \return true if a line has been read, false on EOF or error */
bool readLine(std::string& str);

}; // End of class def.
} // End of namespace
} // end of namespace
} // namespace io
} // namespace mrpt
13 changes: 6 additions & 7 deletions libs/system/include/mrpt/system/TParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <sstream>
#include <iostream>
#include <stdexcept>
#include <algorithm> // std::max
#include <mrpt/config.h>
#include <mrpt/core/common.h> // Disable MSVC warning 4251 in this class

Expand Down Expand Up @@ -131,15 +132,13 @@ struct TParameters : public std::map<std::string, T>
void getAsString(std::string& s) const
{
size_t maxStrLen = 10;
for (typename BASE::const_iterator it = BASE::begin();
it != BASE::end(); ++it)
maxStrLen = std::max(maxStrLen, it->first.size());
for (const auto & e : *this)
maxStrLen = std::max(maxStrLen, e.first.size());
maxStrLen++;
std::stringstream str;
for (typename BASE::const_iterator it = BASE::begin();
it != BASE::end(); ++it)
str << it->first << std::string(maxStrLen - it->first.size(), ' ')
<< " = " << it->second << std::endl;
for (const auto & e : *this)
str << e.first << std::string(maxStrLen - e.first.size(), ' ')
<< " = " << e.second << std::endl;
s = str.str();
}
};
Expand Down
161 changes: 128 additions & 33 deletions libs/system/src/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,53 +551,43 @@ const std::string& mrpt::system::getMRPTLicense()
return sLicenseText;
}

/*---------------------------------------------------------------
#ifdef _WIN32
std::string winerror2str(const char* errorPlaceName)
{
char str[700];
DWORD e = GetLastError();
FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM, 0, e, 0, str, sizeof(str), NULL);
std::string s;
s = "[";
s += errorPlaceName;
s += "] Error: ";
s += str;
return s;
}
#endif

/*---------------------------------------------------------------
launchProcess
---------------------------------------------------------------*/

bool mrpt::system::launchProcess(const std::string& command)

{
#ifdef _WIN32

STARTUPINFOA SI;

PROCESS_INFORMATION PI;

memset(&SI, 0, sizeof(STARTUPINFOA));

SI.cb = sizeof(STARTUPINFOA);

if (CreateProcessA(
NULL, (LPSTR)command.c_str(), NULL, NULL, true, 0, NULL, NULL, &SI,
&PI))

{
// Wait:

WaitForSingleObject(PI.hProcess, INFINITE);

return true;

} // End of process executed OK

else

{
char str[300];

DWORD e = GetLastError();

FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM, 0, e, 0, str, sizeof(str), NULL);

// ERROR:

std::cerr << "[launchProcess] Couldn't spawn process. Error msg: "
<< str << std::endl;

std::cerr << winerror2str("launchProcess");
return false;
}

Expand Down Expand Up @@ -667,6 +657,7 @@ int mrpt::system::executeCommand(

// Create the stringstream
stringstream sout;
int exit_code = -1;

#ifdef MRPT_OS_LINUX
// Run Popen
Expand All @@ -689,8 +680,117 @@ int mrpt::system::executeCommand(
}

// Close
int exit_code = pclose(in);
exit_code = pclose(in);
#else
try
{
exit_code = -1;

HANDLE g_hChildStd_IN_Rd = NULL;
HANDLE g_hChildStd_IN_Wr = NULL;
HANDLE g_hChildStd_OUT_Rd = NULL;
HANDLE g_hChildStd_OUT_Wr = NULL;

HANDLE g_hInputFile = NULL;
SECURITY_ATTRIBUTES saAttr;
// Set the bInheritHandle flag so pipe handles are inherited.
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
// Create a pipe for the child process's STDOUT.
if (!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0))
throw winerror2str("StdoutRd CreatePipe");

// Ensure the read handle to the pipe for STDOUT is not inherited.

if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
throw winerror2str("Stdout SetHandleInformation");

// Create a pipe for the child process's STDIN.

if (!CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0))
throw winerror2str("Stdin CreatePipe");

// Ensure the write handle to the pipe for STDIN is not inherited.

if (!SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0))
throw winerror2str("Stdin SetHandleInformation");

// Create the child process:
PROCESS_INFORMATION piProcInfo;
STARTUPINFOA siStartInfo;
BOOL bSuccess = FALSE;

// Set up members of the PROCESS_INFORMATION structure.

ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));

// Set up members of the STARTUPINFO structure.
// This structure specifies the STDIN and STDOUT handles for redirection.

ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdError = g_hChildStd_OUT_Wr;
siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
siStartInfo.hStdInput = g_hChildStd_IN_Rd;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;

// Create the child process.
bSuccess = CreateProcessA(NULL,
(LPSTR)command.c_str(), // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION

// If an error occurs, exit the application.
if (!bSuccess)
throw winerror2str("CreateProcess");

// Read from pipe that is the standard output for child process.
DWORD dwRead;
CHAR chBuf[4096];
bSuccess = FALSE;
DWORD exitval = 0;
exit_code = 0;
for (;;)
{
DWORD dwAvailable = 0;
PeekNamedPipe(g_hChildStd_OUT_Rd, NULL, NULL, NULL, &dwAvailable, NULL);
if (dwAvailable)
{
bSuccess = ReadFile(g_hChildStd_OUT_Rd, chBuf, sizeof(chBuf), &dwRead, NULL);
if (!bSuccess || dwRead == 0) break;
sout.write(chBuf, dwRead);
}
else
{
// process ended?
if (GetExitCodeProcess(piProcInfo.hProcess, &exitval))
{
if (exitval != STILL_ACTIVE)
{
exit_code = exitval;
break;
}
}
}
}

// Close handles to the child process and its primary thread.
CloseHandle(piProcInfo.hProcess);
CloseHandle(piProcInfo.hThread);
}
catch (std::string &errStr)
{
std::cerr << errStr;
return 1; // !=0 means error
}
#endif
// set output - if valid pointer given
if (output)
{
Expand All @@ -699,9 +799,4 @@ int mrpt::system::executeCommand(

// Return exit code
return exit_code;
#else
MRPT_TODO("Write popen alternative for Windows")
THROW_EXCEPTION("not implemented for Windows yet!");
#endif

} // end of executeCommand
4 changes: 0 additions & 4 deletions samples/NIDAQ_test/LASER_SCAN_TEST.INI

This file was deleted.

Binary file removed samples/NIDAQ_test/_out_point_cloud.3Dscene
Binary file not shown.
Loading

0 comments on commit 7d47a44

Please sign in to comment.