tahoma2d/toonz/sources/include/tcg/auto.h

200 lines
5.4 KiB
C
Raw Normal View History

2016-05-17 03:04:11 +12:00
#pragma once
2016-03-19 06:57:51 +13:00
#ifndef TCG_AUTO_H
#define TCG_AUTO_H
#include "base.h"
#include "traits.h"
/* \file auto.h
2016-06-15 18:43:10 +12:00
\brief This file contains template classes able to perform special
operations upon
2016-03-19 06:57:51 +13:00
instance destruction.
2016-06-15 18:43:10 +12:00
\details These classes can be useful to enforce block-scoped operations at a
block's
entry point, considering that a block end can be far away, or the
function
2016-03-19 06:57:51 +13:00
could return abruptly at several different points.
*/
2016-06-15 18:43:10 +12:00
namespace tcg {
2016-03-19 06:57:51 +13:00
//*******************************************************************************
// tcg::auto_type definition
//*******************************************************************************
struct _auto_type {
2016-06-15 18:43:10 +12:00
mutable bool m_destruct;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
_auto_type(bool destruct) : m_destruct(destruct) {}
_auto_type(const _auto_type &other) : m_destruct(other.m_destruct) {
other.m_destruct = false;
}
_auto_type &operator=(const _auto_type &other) {
m_destruct = other.m_destruct, other.m_destruct = false;
return *this;
}
2016-03-19 06:57:51 +13:00
};
typedef const _auto_type &auto_type;
//*******************************************************************************
// tcg::auto_func definition
//*******************************************************************************
template <typename Op>
struct auto_zerary : public _auto_type {
2016-06-15 18:43:10 +12:00
Op m_op;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
auto_zerary(bool destruct = true) : _auto_type(destruct) {}
~auto_zerary() {
if (this->m_destruct) m_op();
}
2016-03-19 06:57:51 +13:00
};
//--------------------------------------------------------------------
template <typename Op, typename T = typename function_traits<Op>::arg1_type>
struct auto_unary : public _auto_type {
2016-06-15 18:43:10 +12:00
T m_arg1;
Op m_op;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
auto_unary(bool destruct = true) : _auto_type(destruct) {}
auto_unary(Op op, T arg, bool destruct = true)
: _auto_type(destruct), m_arg1(arg), m_op(op) {}
~auto_unary() {
if (this->m_destruct) m_op(m_arg1);
}
2016-03-19 06:57:51 +13:00
};
//--------------------------------------------------------------------
template <typename Op, typename T1 = typename function_traits<Op>::arg1_type,
2016-06-15 18:43:10 +12:00
typename T2 = typename function_traits<Op>::arg2_type>
2016-03-19 06:57:51 +13:00
struct auto_binary : public _auto_type {
2016-06-15 18:43:10 +12:00
T1 m_arg1;
T2 m_arg2;
Op m_op;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
auto_binary(bool destruct = true) : _auto_type(destruct) {}
auto_binary(Op op, T1 arg1, T2 arg2, bool destruct = true)
: _auto_type(destruct), m_arg1(arg1), m_arg2(arg2), m_op(op) {}
~auto_binary() {
if (this->m_destruct) m_op(m_arg1, m_arg2);
}
2016-03-19 06:57:51 +13:00
};
//*******************************************************************************
// Helper functions
//*******************************************************************************
template <typename Op>
2016-06-15 18:43:10 +12:00
auto_zerary<Op> make_auto(Op op, bool destruct = true) {
return auto_zerary<Op>(op, destruct);
2016-03-19 06:57:51 +13:00
}
template <typename Op, typename T>
2016-06-15 18:43:10 +12:00
auto_unary<Op> make_auto(Op op, T &arg1, bool destruct = true) {
return auto_unary<Op>(op, arg1, destruct);
2016-03-19 06:57:51 +13:00
}
template <typename Op, typename T>
2016-06-15 18:43:10 +12:00
auto_unary<Op> make_auto(Op op, const T &arg1, bool destruct = true) {
return auto_unary<Op>(op, arg1, destruct);
2016-03-19 06:57:51 +13:00
}
template <typename Op, typename T1, typename T2>
2016-06-15 18:43:10 +12:00
auto_binary<Op> make_auto(Op op, T1 &arg1, T2 &arg2, bool destruct = true) {
return auto_binary<Op>(op, arg1, arg2, destruct);
2016-03-19 06:57:51 +13:00
}
template <typename Op, typename T1, typename T2>
2016-06-15 18:43:10 +12:00
auto_binary<Op> make_auto(Op op, const T1 &arg1, T2 &arg2,
bool destruct = true) {
return auto_binary<Op>(op, arg1, arg2, destruct);
2016-03-19 06:57:51 +13:00
}
template <typename Op, typename T1, typename T2>
2016-06-15 18:43:10 +12:00
auto_binary<Op> make_auto(Op op, T1 &arg1, const T2 &arg2,
bool destruct = true) {
return auto_binary<Op>(op, arg1, arg2, destruct);
2016-03-19 06:57:51 +13:00
}
template <typename Op, typename T1, typename T2>
2016-06-15 18:43:10 +12:00
auto_binary<Op> make_auto(Op op, const T1 &arg1, const T2 &arg2,
bool destruct = true) {
return auto_binary<Op>(op, arg1, arg2, destruct);
2016-03-19 06:57:51 +13:00
}
//*******************************************************************************
// tcg::auto_reset definition
//*******************************************************************************
template <typename T, T val>
2016-06-15 18:43:10 +12:00
class auto_reset {
typedef T var_type;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
var_type &m_var;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
auto_reset(var_type &var) : m_var(var) {}
~auto_reset() { m_var = val; }
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
auto_reset(const auto_reset &);
auto_reset &operator=(const auto_reset &);
2016-03-19 06:57:51 +13:00
};
//*******************************************************************************
// tcg::auto_backup definition
//*******************************************************************************
template <typename T>
struct auto_backup {
2016-06-15 18:43:10 +12:00
typedef T var_type;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
var_type m_backup;
var_type *m_original;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
auto_backup() : m_original() {}
auto_backup(var_type &original) : m_original(&original), m_backup(original) {}
auto_backup(var_type *original) : m_original(original) {
if (m_original) m_backup = *m_original;
}
~auto_backup() {
if (m_original) *m_original = m_backup;
}
void reset(T &original) {
m_original = &original;
m_backup = original;
}
void reset(T *original) {
m_original = original;
if (m_original) m_backup = *original;
}
T *release() {
T *original = m_original;
m_original = 0;
return original;
}
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
auto_backup(const auto_backup &);
auto_backup &operator=(const auto_backup &);
2016-03-19 06:57:51 +13:00
};
2016-06-15 18:43:10 +12:00
} // namespace tcg
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
#endif // TCG_AUTO_H