[DEV] add Function

This commit is contained in:
Edouard DUPIN 2017-08-30 21:42:07 +02:00
parent 3cd626eb9d
commit 628146c380
6 changed files with 82 additions and 6 deletions

0
etk/Function.cpp Normal file
View File

35
etk/Function.hpp Normal file
View File

@ -0,0 +1,35 @@
/** @file
* @author Edouard DUPIN
* @copyright 2011, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
#include <etk/types.hpp>
#pragma once
namespace etk {
/**
* @brief Generic Thread interface (OS independent)
*/
template<class ETK_TYPE_FUNCTION>
class Function {
private:
#ifdef __TARGET_OS__Windows
#else
pthread_mutex_t m_thread;
#endif
uint32_t m_uid; //!< unique id of the thread
etk::String m_name; //!< Name of the thread (do not get it on the system ==> more portable)
etk::Function<void()> m_function; //!< Function to call every cycle of the thead running
public:
Thread(etk::Function<void()>&& _call, const std::string& _name);
~Thread();
void join();
bool detach();
void setName(const std::string& _name);
const std::string& setName() const;
uint32_t getIdentifier() const;
};
}

View File

@ -57,6 +57,13 @@ etk::Stream& etk::Stream::operator<< (const uint64_t _data) {
*m_data += etk::toString(_data);
return *this;
}
#if defined(__TARGET_OS__MacOs) \
|| defined(__TARGET_OS__IOs)
etk::Stream& etk::Stream::operator<< (const size_t _data) {
*m_data += etk::toString(_data);
return *this;
}
#endif
etk::Stream& etk::Stream::operator<< (const float _data) {
*m_data += etk::toString(_data);
return *this;

View File

@ -29,6 +29,10 @@ namespace etk {
Stream& operator<< (uint16_t _data);
Stream& operator<< (uint32_t _data);
Stream& operator<< (uint64_t _data);
#if defined(__TARGET_OS__MacOs) \
|| defined(__TARGET_OS__IOs)
Stream& operator<< (size_t _data);
#endif
Stream& operator<< (float _data);
Stream& operator<< (double _data);
const char* c_str() const;

View File

@ -554,7 +554,15 @@ uint64_t etk::String::to<uint64_t>() const {
#endif
return ret;
}
#if defined(__TARGET_OS__MacOs) \
|| defined(__TARGET_OS__IOs)
template <>
size_t etk::String::to<size_t>() const {
int ret = 0;
sscanf(c_str(), "%u", &ret);
return ret;
}
#endif
template <>
bool etk::String::to<bool>() const {
if( compare("true", false) == true
@ -642,6 +650,13 @@ namespace etk {
_variableRet = _value.to<uint64_t>();
return true;
}
#if defined(__TARGET_OS__MacOs) \
|| defined(__TARGET_OS__IOs)
template<> bool from_string<size_t>(size_t& _variableRet, const etk::String& _value) {
_variableRet = _value.to<size_t>();
return true;
}
#endif
template<> bool from_string<float>(float& _variableRet, const etk::String& _value) {
_variableRet = _value.to<float>();
return true;
@ -688,7 +703,14 @@ etk::String etk::toString(const int32_t& _val) {
template<>
etk::String etk::toString(const int64_t& _val) {
char tmpVal[256];
sprintf(tmpVal, "%ld", _val);
#if ( defined(__TARGET_OS__Android) \
|| defined(__TARGET_OS__Windows) \
|| defined(__TARGET_OS__MacOs) \
|| defined(__TARGET_OS__IOs))
sprintf(tmpVal, "%lld", _val);
#else
sprintf(tmpVal, "%ld", _val);
#endif
return tmpVal;
}
template<>
@ -712,17 +734,25 @@ etk::String etk::toString(const uint32_t& _val) {
template<>
etk::String etk::toString(const uint64_t& _val) {
char tmpVal[256];
sprintf(tmpVal, "%lu", _val);
#if ( defined(__TARGET_OS__Android) \
|| defined(__TARGET_OS__Windows) \
|| defined(__TARGET_OS__MacOs) \
|| defined(__TARGET_OS__IOs))
sprintf(tmpVal, "%llu", _val);
#else
sprintf(tmpVal, "%lu", _val);
#endif
return tmpVal;
}
/*
#if defined(__TARGET_OS__MacOs) \
|| defined(__TARGET_OS__IOs)
template<>
etk::String etk::toString(const size_t& _val) {
char tmpVal[256];
sprintf(tmpVal, "%zu", _val);
return tmpVal;
}
*/
#endif
template<>
etk::String etk::toString(const float& _val) {

View File

@ -791,7 +791,7 @@ namespace etk {
if (_start > _stop) {
size_t start = _start;
_start = _stop;
_stop = _start;
_stop = start;
}
for (size_t iii=_start; iii<_stop; ++iii) {
bool swapped = false;