Just some refactoring

This commit is contained in:
Eduardo Bart
2012-07-29 07:32:44 -03:00
parent 29a4d467af
commit 73769c62e4
47 changed files with 252 additions and 142 deletions

View File

@@ -0,0 +1,86 @@
/*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef STDEXT_ATTRIBSTORAGE_H
#define STDEXT_ATTRIBSTORAGE_H
#include "types.h"
#include <tuple>
#include <boost/any.hpp>
// disable memory alignment
#pragma pack(push,1)
// this class was designed to use less memory as possible
namespace stdext {
class attrib_storage {
public:
~attrib_storage() { if(m_attribs) delete[] m_attribs; }
template<typename T>
void set(uint8 id, T value) {
bool done = false;
for(int i=0;i<m_size;++i) {
if(std::get<0>(m_attribs[i]) == id) {
std::get<1>(m_attribs[i]) = value;
done = true;
break;
}
}
if(!done) {
auto attribs = new std::tuple<uint8, boost::any>[m_size+1];
if(m_size > 0) {
for(int i=0;i<m_size;++i)
attribs[i] = m_attribs[i];
delete[] m_attribs;
}
m_attribs = attribs;
m_attribs[m_size++] = std::make_tuple(id, value);
}
}
template<typename T>
T get(uint8 id) const {
for(int i=0;i<m_size;++i)
if(std::get<0>(m_attribs[i]) == id)
return boost::any_cast<T>(std::get<1>(m_attribs[i]));
return T();
}
bool has(uint8 id) const {
for(int i=0;i<m_size;++i)
if(std::get<0>(m_attribs[i]) == id)
return true;
return false;
}
private:
std::tuple<uint8, boost::any>* m_attribs = nullptr;
uint8 m_size = 0;
};
// restore memory alignment
#pragma pack(pop)
}
#endif

View File

@@ -0,0 +1,40 @@
/*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef BOOLEAN_H
#define BOOLEAN_H
namespace stdext {
template<bool def>
struct boolean {
boolean() : v(def) { }
operator bool &() { return v; }
operator bool const &() const { return v; }
bool& operator=(const bool& o) { v = o; return v; }
private:
bool v;
};
}
#endif

View File

@@ -27,8 +27,8 @@
// clang is supported
#undef _GLIBCXX_USE_FLOAT128
#elif defined(__GNUC__)
#if !(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
#error "Sorry, you need gcc 4.6 or greater to compile."
#if !(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
#error "Sorry, you need gcc 4.7 or greater to compile."
#endif
#else
#error "Compiler not supported."

View File

@@ -0,0 +1,80 @@
/*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef STDEXT_SHARED_OBJECT_H
#define STDEXT_SHARED_OBJECT_H
#include <boost/checked_delete.hpp>
#include <boost/intrusive_ptr.hpp>
namespace stdext {
template<typename T>
using shared_object_ptr = boost::intrusive_ptr<T>;
class shared_object
{
public:
shared_object() : m_refs(0) { }
virtual ~shared_object() { }
void add_ref() { ++m_refs; assert(m_refs != 0xffffffff); }
void dec_ref() {
if(--m_refs == 0)
boost::checked_delete(this);
}
bool is_unique_ref() { return m_refs == 1; }
unsigned long ref_count() { return m_refs; }
template<typename T>
shared_object_ptr<T> self_cast() { return shared_object_ptr<T>(static_cast<T*>(this)); }
template<typename T>
shared_object_ptr<T> dynamic_self_cast() { return shared_object_ptr<T>(dynamic_cast<T*>(this)); }
private:
unsigned int m_refs;
};
template<class T, typename... Args>
shared_object_ptr<T> make_shared_object(Args... args) { return shared_object_ptr<T>(new T(args...)); }
}
namespace std {
template<typename T>
struct hash<boost::intrusive_ptr<T>> : public __hash_base<size_t, boost::intrusive_ptr<T>> {
size_t operator()(const boost::intrusive_ptr<T>& p) const noexcept { return std::hash<T*>()(p.get()); }
};
}
template<typename T>
struct remove_const_ref {
typedef typename std::remove_const<typename std::remove_reference<T>::type>::type type;
};
template<typename T>
void intrusive_ptr_add_ref(T* p) { (static_cast<stdext::shared_object*>(p))->add_ref(); }
template<typename T>
void intrusive_ptr_release(T* p) { (static_cast<stdext::shared_object*>(p))->dec_ref(); }
#endif

View File

@@ -33,5 +33,7 @@
#include "dumper.h"
#include "time.h"
#include "shared_object.h"
#include "attrib_storage.h"
#include "boolean.h"
#endif

View File

@@ -35,6 +35,17 @@ inline ticks_t micros() { return std::chrono::duration_cast<std::chrono::microse
inline void millisleep(uint32 ms) { usleep(ms * 1000); };
inline void microsleep(uint32 us) { usleep(us); };
struct timer {
public:
timer() { restart(); }
float elapsed_seconds() { return (stdext::micros() - m_start)/1000000.0; }
ticks_t elapsed_millis() { return (stdext::micros() - m_start)/1000; }
ticks_t elapsed_micros() { return stdext::micros() - m_start; }
void restart() { m_start = stdext::micros(); }
private:
ticks_t m_start;
};
}
#endif

View File

@@ -39,6 +39,19 @@ typedef int32_t int32;
typedef int16_t int16;
typedef int8_t int8;
typedef unsigned char fast_uchar;
typedef unsigned long fast_ushort;
typedef unsigned long fast_uint;
typedef unsigned long fast_ulong;
typedef uint_fast64_t fast_uint64;
typedef uint_fast32_t fast_uint32;
typedef uint_fast16_t fast_uint16;
typedef uint_fast8_t fast_uint8;
typedef int_fast64_t fast_int64;
typedef int_fast32_t fast_int32;
typedef int_fast16_t fast_int16;
typedef int_fast8_t fast_int8;
typedef int64_t ticks_t;
#endif