Skip to content

Commit

Permalink
4 ➡️ 5 ( #268)
Browse files Browse the repository at this point in the history
4 ➡️ 5
  • Loading branch information
mjcarroll authored Nov 19, 2021
2 parents 92abc29 + ee37636 commit 1a456b1
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 13 deletions.
5 changes: 4 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

## Ignition Common 4.x

## Ignition Common 4.X.X (20XX-XX-XX)
## Ignition Common 4.4.0 (2021-10-15)

1. Add support for animation tension
* [Pull request #256](https://github.com/ignitionrobotics/ign-common/pull/256)

## Ignition Common 4.3.0 (2021-09-27)

Expand Down
9 changes: 9 additions & 0 deletions graphics/include/ignition/common/MeshManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ namespace ignition
/// \param[in] the mesh to add.
public: void AddMesh(Mesh *_mesh);

/// \brief Remove a mesh based on a name.
/// \param[in] _name Name of the mesh to remove.
/// \return True if the mesh was removed, false if the mesh with the
/// provided name could not be found.
public: bool RemoveMesh(const std::string &_name);

/// \brief Remove all meshes.
public: void RemoveAll();

/// \brief Get a mesh by name.
/// \param[in] _name the name of the mesh to look for
/// \return the mesh or nullptr if not found
Expand Down
27 changes: 27 additions & 0 deletions graphics/src/MeshManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,33 @@ const Mesh *MeshManager::MeshByName(const std::string &_name) const
return nullptr;
}

//////////////////////////////////////////////////
void MeshManager::RemoveAll()
{
std::lock_guard<std::mutex> lock(this->dataPtr->mutex);
for (auto m : this->dataPtr->meshes)
{
delete m.second;
}
this->dataPtr->meshes.clear();
}

//////////////////////////////////////////////////
bool MeshManager::RemoveMesh(const std::string &_name)
{
std::lock_guard<std::mutex> lock(this->dataPtr->mutex);

auto iter = this->dataPtr->meshes.find(_name);
if (iter != this->dataPtr->meshes.end())
{
delete iter->second;
this->dataPtr->meshes.erase(iter);
return true;
}

return false;
}

//////////////////////////////////////////////////
bool MeshManager::HasMesh(const std::string &_name) const
{
Expand Down
22 changes: 22 additions & 0 deletions graphics/src/MeshManager_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,28 @@ TEST_F(MeshManager, CreateExtrudedPolylineInvalid)
EXPECT_TRUE(!common::MeshManager::Instance()->HasMesh(meshName));
}

/////////////////////////////////////////////////
TEST_F(MeshManager, Remove)
{
auto mgr = common::MeshManager::Instance();

EXPECT_FALSE(mgr->HasMesh("box"));
mgr->CreateBox("box",
ignition::math::Vector3d(1, 1, 1),
ignition::math::Vector2d(0, 0));
EXPECT_TRUE(mgr->HasMesh("box"));

mgr->CreateSphere("sphere", 1.0, 1, 1);
EXPECT_TRUE(mgr->HasMesh("sphere"));

EXPECT_TRUE(mgr->RemoveMesh("box"));
EXPECT_FALSE(mgr->HasMesh("box"));
EXPECT_TRUE(mgr->HasMesh("sphere"));

mgr->RemoveAll();
EXPECT_FALSE(mgr->HasMesh("sphere"));
}

/////////////////////////////////////////////////
int main(int argc, char **argv)
{
Expand Down
14 changes: 13 additions & 1 deletion include/ignition/common/Console.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <iostream>
#include <fstream>
#include <memory>
#include <sstream>
#include <string>

Expand Down Expand Up @@ -183,10 +184,17 @@ namespace ignition
/// \brief Destructor.
public: virtual ~Buffer();

/// \brief Writes _count characters to the string buffer
/// \param[in] _char Input rharacter array.
/// \param[in] _count Number of characters in array.
/// \return The number of characters successfully written.
public: std::streamsize xsputn(
const char *_char, std::streamsize _count) override;

/// \brief Sync the stream (output the string buffer
/// contents).
/// \return Return 0 on success.
public: virtual int sync();
public: int sync() override;

/// \brief Destination type for the messages.
public: LogType type;
Expand All @@ -198,6 +206,10 @@ namespace ignition

/// \brief Level of verbosity
public: int verbosity;

/// \brief Mutex to synchronize writes to the string buffer
/// and the output stream.
public: std::mutex syncMutex;
};

IGN_COMMON_WARN_IGNORE__DLL_INTERFACE_MISSING
Expand Down
43 changes: 34 additions & 9 deletions src/Console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using namespace ignition;
using namespace common;


FileLogger ignition::common::Console::log("");

// On UNIX, these are ANSI-based color codes. On Windows, these are colors from
Expand Down Expand Up @@ -119,14 +120,29 @@ Logger::Buffer::~Buffer()
this->pubsync();
}

/////////////////////////////////////////////////
std::streamsize Logger::Buffer::xsputn(const char *_char,
std::streamsize _count)
{
std::lock_guard<std::mutex> lk(this->syncMutex);
return std::stringbuf::xsputn(_char, _count);
}

/////////////////////////////////////////////////
int Logger::Buffer::sync()
{
std::string outstr = this->str();
std::string outstr;
{
std::lock_guard<std::mutex> lk(this->syncMutex);
outstr = this->str();
}

// Log messages to disk
Console::log << outstr;
Console::log.flush();
{
std::lock_guard<std::mutex> lk(this->syncMutex);
Console::log << outstr;
Console::log.flush();
}

// Output to terminal
if (Console::Verbosity() >= this->verbosity && !outstr.empty())
Expand All @@ -143,7 +159,10 @@ int Logger::Buffer::sync()
if (lastNewLine)
ss << std::endl;

fprintf(outstream, "%s", ss.str().c_str());
{
std::lock_guard<std::mutex> lk(this->syncMutex);
fprintf(outstream, "%s", ss.str().c_str());
}
#else
HANDLE hConsole = CreateFileW(
L"CONOUT$", GENERIC_WRITE|GENERIC_READ, 0, nullptr, OPEN_EXISTING,
Expand All @@ -168,14 +187,20 @@ int Logger::Buffer::sync()
std::ostream &outStream =
this->type == Logger::STDOUT ? std::cout : std::cerr;

if (vtProcessing)
outStream << "\x1b[" << this->color << "m" << outstr << "\x1b[m";
else
outStream << outstr;
{
std::lock_guard<std::mutex> lk(this->syncMutex);
if (vtProcessing)
outStream << "\x1b[" << this->color << "m" << outstr << "\x1b[m";
else
outStream << outstr;
}
#endif
}

this->str("");
{
std::lock_guard<std::mutex> lk(this->syncMutex);
this->str("");
}
return 0;
}

Expand Down
21 changes: 19 additions & 2 deletions src/Util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,36 @@ std::string ignition::common::systemTimeIso()
return timeToIso(IGN_SYSTEM_TIME());
}

// Taken from gtest.cc
static bool PortableLocaltime(time_t seconds, struct tm* out) {
#if defined(_MSC_VER)
return localtime_s(out, &seconds) == 0;
#elif defined(__MINGW32__) || defined(__MINGW64__)
// MINGW <time.h> provides neither localtime_r nor localtime_s, but uses
// Windows' localtime(), which has a thread-local tm buffer.
struct tm* tm_ptr = localtime(&seconds); // NOLINT
if (tm_ptr == nullptr) return false;
*out = *tm_ptr;
return true;
#else
return localtime_r(&seconds, out) != nullptr;
#endif
}

/////////////////////////////////////////////////
std::string ignition::common::timeToIso(
const std::chrono::time_point<std::chrono::system_clock> &_time)
{
char isoStr[25];

auto epoch = _time.time_since_epoch();
auto sec = std::chrono::duration_cast<std::chrono::seconds>(epoch).count();
auto nano = std::chrono::duration_cast<std::chrono::nanoseconds>(
epoch).count() - sec * IGN_SEC_TO_NANO;

time_t tmSec = static_cast<time_t>(sec);
std::strftime(isoStr, sizeof(isoStr), "%FT%T", std::localtime(&tmSec));
struct tm localTime;
PortableLocaltime(tmSec, &localTime);
std::strftime(isoStr, sizeof(isoStr), "%FT%T", &localTime);

return std::string(isoStr) + "." + std::to_string(nano);
}
Expand Down
Loading

0 comments on commit 1a456b1

Please sign in to comment.