Skip to content
Snippets Groups Projects
Commit e1b937ee authored by Raphael's avatar Raphael
Browse files

remove a commited .orig file

parent 50ffd348
No related branches found
No related tags found
1 merge request!26Rt robot unit v2
/*
* This file is part of ArmarX.
*
* ArmarX is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* ArmarX is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package RobotAPI::RobotUnit
* @author Raphael ( ufdrv at student dot kit dot edu )
* @date 2017
* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
* GNU General Public License
*/
#ifndef _ARMARX_UNIT_RobotAPI_RobotUnit_util_H
#define _ARMARX_UNIT_RobotAPI_RobotUnit_util_H
#include <boost/units/detail/utility.hpp>
#include <string>
namespace armarx
{
template<class T>
std::string getTypeName(const T&, bool withoutNamespaceSpecifier = false)
{
const std::string demangled = boost::units::detail::demangle(typeid(T).name());
if (withoutNamespaceSpecifier)
{
std::size_t substrStart = 0;
std::size_t level = 0;
for (int i = static_cast<int>(demangled.size() - 1); i >= 0; --i)
{
if (!level && demangled.at(i) == ':')
{
substrStart = i + 1;
break;
}
level += (demangled.at(i) == '>') - (demangled.at(i) == '<');
}
return demangled.substr(substrStart);
}
return demangled;
}
}
#include <string>
namespace std
{
inline const std::string& to_string(const std::string& s)
{
return s;
}
inline std::string to_string(std::string&& s)
{
return std::move(s);
}
}
#include <ArmarXCore/core/util/TemplateMetaProgramming.h>
#include <vector>
#include <map>
namespace armarx
{
/**
* @brief This class is pretty much similar to a map.
*
* This class stores keys and values in two vectors and uses a map to map the keys to the index of the corresponding value
* This enables map acces and index access.
* The index of an inserted element never changes (this is different to a map).
* Index access may be required in a high frequency case (e.g. an rt control loop)
*/
template < class KeyT, class ValT,
class KeyContT = std::vector<KeyT>,
class ValContT = std::vector<ValT>,
class IdxT = typename ValContT::size_type,
class MapT = std::map<KeyT, IdxT >>
class KeyValueVector
{
public:
void add(KeyT key, ValT value)
{
if (indices_.find(key) != indices_.end())
{
throw std::invalid_argument {"Already added a value for this key" + describeKey(&key)};
}
indices_.emplace(key, size());
values_.emplace_back(std::move(value));
keys_.emplace_back(std::move(key));
}
IdxT index(const KeyT& k) const
{
return indices_.at(k);
}
ValT& at(IdxT i)
{
return values_.at(i);
}
ValT const& at(IdxT i) const
{
return values_.at(i);
}
ValT& at(const KeyT& k)
{
return at(index(k));
}
ValT const& at(const KeyT& k) const
{
return at(index(k));
}
ValT& at(const KeyT& k, ValT& defaultVal)
{
return has(k) ? at(index(k)) : defaultVal;
}
ValT const& at(const KeyT& k, const ValT& defaultVal) const
{
return has(k) ? at(index(k)) : defaultVal;
}
IdxT size() const
{
return values_.size();
}
ValContT const& values() const
{
return values_;
}
MapT const& indices() const
{
return indices_;
}
KeyContT const& keys() const
{
return keys_;
}
IdxT count(const KeyT& k) const
{
return indices_.count(k);
}
bool has(const KeyT& k) const
{
return indices_.count(k);
}
void clear()
{
indices_.clear();
keys_.clear();
values_.clear();
}
private:
template<class T, class = typename std::enable_if<meta::HasToString<T>::value>::type>
static std::string describeKey(const T* key)
{
return ": " + std::to_string(*key);
}
static std::string describeKey(...)
{
return "";
}
MapT indices_;
KeyContT keys_;
ValContT values_;
};
}
#include <atomic>
namespace armarx
{
template <typename T>
struct AtomicWrapper
{
std::atomic<T> atom;
AtomicWrapper() : atom {} {}
AtomicWrapper(const T& v) : atom {v} {}
AtomicWrapper(const std::atomic<T>& a) : atom {a.load()} {}
AtomicWrapper(const AtomicWrapper& other) : atom {other.atom.load()} {}
AtomicWrapper(AtomicWrapper&&) = default;
AtomicWrapper& operator=(const T& v)
{
atom.store(v);
return *this;
}
AtomicWrapper& operator=(const std::atomic<T>& a)
{
atom.store(a.load());
return *this;
}
AtomicWrapper& operator=(const AtomicWrapper& other)
{
atom.store(other.atom.load());
return *this;
}
AtomicWrapper& operator=(AtomicWrapper &&) = default;
operator T() const
{
return atom.load();
}
};
}
#include <mutex>
#include <thread>
#include <ArmarXCore/core/logging/Logging.h>
namespace armarx
{
template<class MutexT>
struct messaging_unique_lock
{
using mutex_type = MutexT;
messaging_unique_lock(messaging_unique_lock&&) = default;
messaging_unique_lock(MutexT& mtx):
l {mtx, std::defer_lock},
m {&mtx}
{
<<<<<<< HEAD
bool isFree = l.try_lock();
if (isFree)
{
std::cout << "[thread " << std::this_thread::get_id() << "] locking mutex(" << m << ", already locked " << lock_count[m] << " times) (mutex was free) (type "
<< getTypeName(mtx.native_handle()) << ") :\n"
<< (lock_count[m] ? std::string {}: LogSender::CreateBackTrace()) << std::flush;
}
else
{
std::cout << "[thread " << std::this_thread::get_id() << "] locking mutex(" << m << ", already locked " << lock_count[m] << " times) (mutex was not free) (type "
<< getTypeName(mtx.native_handle()) << ") :\n"
<< (lock_count[m] ? std::string {}: LogSender::CreateBackTrace()) << std::flush;
l.lock();
std::cout << "[thread " << std::this_thread::get_id() << "] locking mutex(" << m << ", now locked " << lock_count[m] << " times) (mutex was not free in 10ms)...done" << std::flush;
}
=======
std::cout << "[thread " << std::this_thread::get_id() << "] locking mutex(" << m << ", already locked " << lock_count[m] << " times):\n" << (lock_count[m] ? std::string{}: LogSender::CreateBackTrace()) << std::flush;
l.lock();
>>>>>>> 16039a95e1fda778a127463d88c6c4d36ce40c07
++lock_count[m];
}
~messaging_unique_lock()
{
if (l.owns_lock())
{
--lock_count[m];
std::cout << "[thread " << std::this_thread::get_id() << "] unlocking mutex(" << m << ", still locked " << lock_count[m] << " times)" << std::flush;
}
}
std::unique_lock<MutexT> l;
const MutexT* const m;
static thread_local std::map<const MutexT*, std::size_t> lock_count;
};
template<class MutexT>
thread_local std::map<const MutexT*, std::size_t> messaging_unique_lock<MutexT>::lock_count {};
}
#define TYPEDEF_PTRS_SHARED(T) \
class T; \
using T##Ptr = std::shared_ptr<T>; \
using Const##T##Ptr = std::shared_ptr<const T>
#define TYPEDEF_PTRS_HANDLE(T) \
class T; \
using T##Ptr = IceUtil::Handle<T>
//#include <memory>
//#include <boost/shared_ptr.hpp>
//#include <IceUtil/Handle.h>
//namespace armarx
//{
// template<class T>
// class SharedPtr
// {
// public:
// private:
// struct SharedPtrInterface
// {
// void reset();
// T* get();
// const T* get();
// };
// instance
// };
//}
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment