Initial commit
This commit is contained in:
@@ -0,0 +1,530 @@
|
||||
// Copyright 2025-present the zvec project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
namespace zvec {
|
||||
namespace ailego {
|
||||
|
||||
/*! Callback Validator (declaration)
|
||||
*/
|
||||
template <typename TFunc>
|
||||
struct CallbackValidator;
|
||||
|
||||
/*! Callback Validator (function pointer)
|
||||
*/
|
||||
template <typename R, typename... TParams>
|
||||
struct CallbackValidator<R (*)(TParams...)> {
|
||||
enum { Value = true };
|
||||
};
|
||||
|
||||
/*! Callback Validator (function)
|
||||
*/
|
||||
template <typename R, typename... TParams>
|
||||
struct CallbackValidator<R(TParams...)> : CallbackValidator<R (*)(TParams...)> {
|
||||
};
|
||||
|
||||
/*! Callback Validator (member function pointer)
|
||||
*/
|
||||
template <typename T, typename R, typename... TParams>
|
||||
struct CallbackValidator<R (T::*)(TParams...)>
|
||||
: CallbackValidator<R (*)(TParams...)> {};
|
||||
|
||||
/*! Callback Validator (constable member function pointer)
|
||||
*/
|
||||
template <typename T, typename R, typename... TParams>
|
||||
struct CallbackValidator<R (T::*)(TParams...) const>
|
||||
: CallbackValidator<R (*)(TParams...)> {};
|
||||
|
||||
/*! Callback Validator (volatile member function pointer)
|
||||
*/
|
||||
template <typename T, typename R, typename... TParams>
|
||||
struct CallbackValidator<R (T::*)(TParams...) volatile>
|
||||
: CallbackValidator<R (*)(TParams...)> {};
|
||||
|
||||
/*! Callback Validator (constable volatile member function pointer)
|
||||
*/
|
||||
template <typename T, typename R, typename... TParams>
|
||||
struct CallbackValidator<R (T::*)(TParams...) const volatile>
|
||||
: CallbackValidator<R (*)(TParams...)> {};
|
||||
|
||||
/*! Callback Validator
|
||||
*/
|
||||
template <typename TFunc>
|
||||
struct CallbackValidator {
|
||||
protected:
|
||||
using FalseType = long;
|
||||
using TrueType = char;
|
||||
|
||||
//! Check if the class contains operator()
|
||||
template <typename T>
|
||||
static TrueType &Validate(decltype(&T::operator()));
|
||||
|
||||
//! Check if the class contains operator()
|
||||
template <typename T>
|
||||
static FalseType &Validate(...);
|
||||
|
||||
public:
|
||||
enum { Value = (sizeof(Validate<TFunc>(nullptr)) == sizeof(TrueType)) };
|
||||
};
|
||||
|
||||
/*! Callback Validator (left reference)
|
||||
*/
|
||||
template <typename TFunc>
|
||||
struct CallbackValidator<TFunc &> : CallbackValidator<TFunc> {};
|
||||
|
||||
/*! Callback Validator (right reference)
|
||||
*/
|
||||
template <typename TFunc>
|
||||
struct CallbackValidator<TFunc &&> : CallbackValidator<TFunc> {};
|
||||
|
||||
/*! Callback Traits (declaration)
|
||||
*/
|
||||
template <typename TFunc>
|
||||
struct CallbackTraits;
|
||||
|
||||
/*! Callback Traits (function pointer)
|
||||
*/
|
||||
template <typename R, typename... TParams>
|
||||
struct CallbackTraits<R (*)(TParams...)> {
|
||||
using Type = R (*)(TParams...);
|
||||
using ResultType = R;
|
||||
using TupleType = std::tuple<typename std::decay<TParams>::type...>;
|
||||
|
||||
//! Callback Traits Parameter
|
||||
template <size_t N>
|
||||
struct Parameter {
|
||||
using Type = typename std::tuple_element<N, std::tuple<TParams...>>::type;
|
||||
};
|
||||
|
||||
//! Number of parameters
|
||||
enum { Arity = sizeof...(TParams) };
|
||||
};
|
||||
|
||||
/*! Callback Traits (function)
|
||||
*/
|
||||
template <typename R, typename... TParams>
|
||||
struct CallbackTraits<R(TParams...)> : CallbackTraits<R (*)(TParams...)> {
|
||||
using Type = R (*)(TParams...);
|
||||
};
|
||||
|
||||
/*! Callback Traits (member function pointer)
|
||||
*/
|
||||
template <typename T, typename R, typename... TParams>
|
||||
struct CallbackTraits<R (T::*)(TParams...)>
|
||||
: CallbackTraits<R (*)(TParams...)> {
|
||||
using Type = R (T::*)(TParams...);
|
||||
};
|
||||
|
||||
/*! Callback Traits (constable member function pointer)
|
||||
*/
|
||||
template <typename T, typename R, typename... TParams>
|
||||
struct CallbackTraits<R (T::*)(TParams...) const>
|
||||
: CallbackTraits<R (*)(TParams...)> {
|
||||
using Type = R (T::*)(TParams...) const;
|
||||
};
|
||||
|
||||
/*! Callback Traits (volatile member function pointer)
|
||||
*/
|
||||
template <typename T, typename R, typename... TParams>
|
||||
struct CallbackTraits<R (T::*)(TParams...) volatile>
|
||||
: CallbackTraits<R (*)(TParams...)> {
|
||||
using Type = R (T::*)(TParams...) volatile;
|
||||
};
|
||||
|
||||
/*! Callback Traits (constable volatile member function pointer)
|
||||
*/
|
||||
template <typename T, typename R, typename... TParams>
|
||||
struct CallbackTraits<R (T::*)(TParams...) const volatile>
|
||||
: CallbackTraits<R (*)(TParams...)> {
|
||||
using Type = R (T::*)(TParams...) const volatile;
|
||||
};
|
||||
|
||||
/*! Callback Traits
|
||||
*/
|
||||
template <typename TFunc>
|
||||
struct CallbackTraits : CallbackTraits<decltype(&TFunc::operator())> {
|
||||
using Type = TFunc;
|
||||
};
|
||||
|
||||
/*! Callback Traits (left reference)
|
||||
*/
|
||||
template <typename TFunc>
|
||||
struct CallbackTraits<TFunc &> : CallbackTraits<TFunc> {};
|
||||
|
||||
/*! Callback Traits (right reference)
|
||||
*/
|
||||
template <typename TFunc>
|
||||
struct CallbackTraits<TFunc &&> : CallbackTraits<TFunc> {};
|
||||
|
||||
/*! Callback Functor
|
||||
*/
|
||||
template <typename TFunc>
|
||||
struct CallbackFunctor {
|
||||
using Traits = CallbackTraits<TFunc>;
|
||||
using Type = typename Traits::Type;
|
||||
using ResultType = typename Traits::ResultType;
|
||||
using TupleType = typename Traits::TupleType;
|
||||
|
||||
//! Tuple Index Maker
|
||||
template <size_t N, size_t... I>
|
||||
struct TupleIndexMaker : TupleIndexMaker<N - 1, N - 1, I...> {};
|
||||
|
||||
//! Tuple Index
|
||||
template <size_t...>
|
||||
struct TupleIndex {};
|
||||
|
||||
//! Tuple Index Maker (special)
|
||||
template <size_t... I>
|
||||
struct TupleIndexMaker<0, I...> {
|
||||
using Type = TupleIndex<I...>;
|
||||
};
|
||||
|
||||
//! Run the callback function
|
||||
template <size_t... I>
|
||||
static ResultType Run(Type &impl, TupleType &tuple, TupleIndex<I...>) {
|
||||
return (impl)(std::forward<typename Traits::template Parameter<I>::Type>(
|
||||
std::get<I>(tuple))...);
|
||||
}
|
||||
|
||||
//! Run the callback member function
|
||||
template <typename T, size_t... I>
|
||||
static ResultType Run(T *obj, Type &impl, TupleType &tuple,
|
||||
TupleIndex<I...>) {
|
||||
return (obj->*impl)(
|
||||
std::forward<typename Traits::template Parameter<I>::Type>(
|
||||
std::get<I>(tuple))...);
|
||||
}
|
||||
|
||||
//! Run the callback function
|
||||
static ResultType Run(Type &impl, TupleType &tuple) {
|
||||
return Run(impl, tuple, typename TupleIndexMaker<Traits::Arity>::Type());
|
||||
}
|
||||
|
||||
//! Run the callback member function
|
||||
template <typename T>
|
||||
static ResultType Run(T *obj, Type &impl, TupleType &tuple) {
|
||||
return Run(obj, impl, tuple,
|
||||
typename TupleIndexMaker<Traits::Arity>::Type());
|
||||
}
|
||||
};
|
||||
|
||||
/*! Callback Object
|
||||
*/
|
||||
template <typename T>
|
||||
struct CallbackObject {
|
||||
using Type = typename std::remove_reference<T>::type;
|
||||
};
|
||||
|
||||
/*! Callback (declaration)
|
||||
*/
|
||||
template <typename R>
|
||||
class Callback;
|
||||
|
||||
/*! Callback (void)
|
||||
*/
|
||||
template <>
|
||||
class Callback<void> {
|
||||
public:
|
||||
using Pointer = std::shared_ptr<Callback<void>>;
|
||||
|
||||
//! Destructor
|
||||
virtual ~Callback(void) {}
|
||||
|
||||
//! Function call
|
||||
void operator()(void) {
|
||||
this->run();
|
||||
}
|
||||
|
||||
//! Run the callback function
|
||||
virtual void run(void) = 0;
|
||||
|
||||
//! Create callback closure (member function pointer)
|
||||
template <typename T, typename R, typename... TParams, typename... TArgs>
|
||||
static typename Callback<R>::Pointer New(T *obj, R (T::*impl)(TParams...),
|
||||
TArgs &&...args);
|
||||
|
||||
//! Create callback closure (constable member function pointer)
|
||||
template <typename T, typename R, typename... TParams, typename... TArgs>
|
||||
static typename Callback<R>::Pointer New(const T *obj,
|
||||
R (T::*impl)(TParams...) const,
|
||||
TArgs &&...args);
|
||||
|
||||
//! Create callback closure (volatile member function pointer)
|
||||
template <typename T, typename R, typename... TParams, typename... TArgs>
|
||||
static typename Callback<R>::Pointer New(volatile T *obj,
|
||||
R (T::*impl)(TParams...) volatile,
|
||||
TArgs &&...args);
|
||||
|
||||
//! Create callback closure (constable volatile member function pointer)
|
||||
template <typename T, typename R, typename... TParams, typename... TArgs>
|
||||
static typename Callback<R>::Pointer New(const volatile T *obj,
|
||||
R (T::*impl)(TParams...)
|
||||
const volatile,
|
||||
TArgs &&...args);
|
||||
|
||||
//! Create callback closure (function)
|
||||
template <
|
||||
typename TFunc, typename... TArgs,
|
||||
typename = typename std::enable_if<CallbackValidator<TFunc>::Value>::type>
|
||||
static typename Callback<typename CallbackTraits<TFunc>::ResultType>::Pointer
|
||||
New(TFunc &&impl, TArgs &&...args);
|
||||
};
|
||||
|
||||
/*! Callback
|
||||
*/
|
||||
template <typename R>
|
||||
class Callback : public Callback<void> {
|
||||
public:
|
||||
using Pointer = std::shared_ptr<Callback<R>>;
|
||||
using Callback<void>::run;
|
||||
|
||||
//! Function call
|
||||
void operator()(void) {
|
||||
this->run();
|
||||
}
|
||||
|
||||
//! Function call with return
|
||||
void operator()(R *r) {
|
||||
this->run(r);
|
||||
}
|
||||
|
||||
//! Run the callback function
|
||||
virtual void run(R *) = 0;
|
||||
|
||||
protected:
|
||||
//! Constructor
|
||||
Callback(void) {};
|
||||
};
|
||||
|
||||
/*! Callback Implementation
|
||||
*/
|
||||
template <typename T, typename R, typename TFunc>
|
||||
class CallbackImpl : public Callback<R> {
|
||||
public:
|
||||
using Object = CallbackObject<T>;
|
||||
using Functor = CallbackFunctor<TFunc>;
|
||||
|
||||
//! Constructor
|
||||
template <typename... TArgs>
|
||||
CallbackImpl(typename Object::Type *obj, const typename Functor::Type &impl,
|
||||
TArgs &&...args)
|
||||
: obj_(obj), impl_(impl), tuple_(std::forward<TArgs>(args)...) {}
|
||||
|
||||
//! Constructor
|
||||
template <typename... TArgs>
|
||||
CallbackImpl(typename Object::Type *obj, typename Functor::Type &&impl,
|
||||
TArgs &&...args)
|
||||
: obj_(obj),
|
||||
impl_(std::move(impl)),
|
||||
tuple_(std::forward<TArgs>(args)...) {}
|
||||
|
||||
//! Run the callback function
|
||||
void run(void) override {
|
||||
Functor::Run(obj_, impl_, tuple_);
|
||||
}
|
||||
|
||||
//! Run the callback function
|
||||
void run(R *r) override {
|
||||
*r = Functor::Run(obj_, impl_, tuple_);
|
||||
}
|
||||
|
||||
protected:
|
||||
//! Disable them
|
||||
CallbackImpl(void) = delete;
|
||||
CallbackImpl(const CallbackImpl &) = delete;
|
||||
CallbackImpl(CallbackImpl &&) = delete;
|
||||
CallbackImpl &operator=(const CallbackImpl &) = delete;
|
||||
|
||||
private:
|
||||
typename Object::Type *obj_;
|
||||
typename Functor::Type impl_;
|
||||
typename Functor::TupleType tuple_;
|
||||
};
|
||||
|
||||
/*! Callback Implementation
|
||||
*/
|
||||
template <typename T, typename TFunc>
|
||||
class CallbackImpl<T, void, TFunc> : public Callback<void> {
|
||||
public:
|
||||
using Object = CallbackObject<T>;
|
||||
using Functor = CallbackFunctor<TFunc>;
|
||||
|
||||
//! Constructor
|
||||
template <typename... TArgs>
|
||||
CallbackImpl(typename Object::Type *obj, const typename Functor::Type &impl,
|
||||
TArgs &&...args)
|
||||
: obj_(obj), impl_(impl), tuple_(std::forward<TArgs>(args)...) {}
|
||||
|
||||
//! Constructor
|
||||
template <typename... TArgs>
|
||||
CallbackImpl(typename Object::Type *obj, typename Functor::Type &&impl,
|
||||
TArgs &&...args)
|
||||
: obj_(obj),
|
||||
impl_(std::move(impl)),
|
||||
tuple_(std::forward<TArgs>(args)...) {}
|
||||
|
||||
//! Run the callback function
|
||||
void run(void) override {
|
||||
Functor::Run(obj_, impl_, tuple_);
|
||||
}
|
||||
|
||||
protected:
|
||||
//! Disable them
|
||||
CallbackImpl(void) = delete;
|
||||
CallbackImpl(const CallbackImpl &) = delete;
|
||||
CallbackImpl(CallbackImpl &&) = delete;
|
||||
CallbackImpl &operator=(const CallbackImpl &) = delete;
|
||||
|
||||
private:
|
||||
typename Object::Type *obj_;
|
||||
typename Functor::Type impl_;
|
||||
typename Functor::TupleType tuple_;
|
||||
};
|
||||
|
||||
/*! Callback Implementation
|
||||
*/
|
||||
template <typename R, typename TFunc>
|
||||
class CallbackImpl<void, R, TFunc> : public Callback<R> {
|
||||
public:
|
||||
using Functor = CallbackFunctor<TFunc>;
|
||||
|
||||
//! Constructor
|
||||
template <typename... TArgs>
|
||||
CallbackImpl(const typename Functor::Type &impl, TArgs &&...args)
|
||||
: impl_(impl), tuple_(std::forward<TArgs>(args)...) {}
|
||||
|
||||
//! Constructor
|
||||
template <typename... TArgs>
|
||||
CallbackImpl(typename Functor::Type &&impl, TArgs &&...args)
|
||||
: impl_(std::move(impl)), tuple_(std::forward<TArgs>(args)...) {}
|
||||
|
||||
//! Run the callback function
|
||||
void run(void) override {
|
||||
Functor::Run(impl_, tuple_);
|
||||
}
|
||||
|
||||
//! Run the callback function
|
||||
void run(R *r) override {
|
||||
*r = Functor::Run(impl_, tuple_);
|
||||
}
|
||||
|
||||
protected:
|
||||
//! Disable them
|
||||
CallbackImpl(void) = delete;
|
||||
CallbackImpl(const CallbackImpl &) = delete;
|
||||
CallbackImpl(CallbackImpl &&) = delete;
|
||||
CallbackImpl &operator=(const CallbackImpl &) = delete;
|
||||
|
||||
private:
|
||||
typename Functor::Type impl_;
|
||||
typename Functor::TupleType tuple_;
|
||||
};
|
||||
|
||||
/*! Callback Implementation
|
||||
*/
|
||||
template <typename TFunc>
|
||||
class CallbackImpl<void, void, TFunc> : public Callback<void> {
|
||||
public:
|
||||
using Functor = CallbackFunctor<TFunc>;
|
||||
|
||||
//! Constructor
|
||||
template <typename... TArgs>
|
||||
CallbackImpl(const typename Functor::Type &impl, TArgs &&...args)
|
||||
: impl_(impl), tuple_(std::forward<TArgs>(args)...) {}
|
||||
|
||||
//! Constructor
|
||||
template <typename... TArgs>
|
||||
CallbackImpl(typename Functor::Type &&impl, TArgs &&...args)
|
||||
: impl_(std::move(impl)), tuple_(std::forward<TArgs>(args)...) {}
|
||||
|
||||
//! Run the callback function
|
||||
void run(void) override {
|
||||
Functor::Run(impl_, tuple_);
|
||||
}
|
||||
|
||||
protected:
|
||||
//! Disable them
|
||||
CallbackImpl(void) = delete;
|
||||
CallbackImpl(const CallbackImpl &) = delete;
|
||||
CallbackImpl(CallbackImpl &&) = delete;
|
||||
CallbackImpl &operator=(const CallbackImpl &) = delete;
|
||||
|
||||
private:
|
||||
typename Functor::Type impl_;
|
||||
typename Functor::TupleType tuple_;
|
||||
};
|
||||
|
||||
//! Create callback closure (member function pointer)
|
||||
template <typename T, typename R, typename... TParams, typename... TArgs>
|
||||
typename Callback<R>::Pointer Callback<void>::New(T *obj,
|
||||
R (T::*impl)(TParams...),
|
||||
TArgs &&...args) {
|
||||
return std::make_shared<CallbackImpl<T, R, decltype(impl)>>(
|
||||
obj, impl, std::forward<TArgs>(args)...);
|
||||
}
|
||||
|
||||
//! Create callback closure (constable member function pointer)
|
||||
template <typename T, typename R, typename... TParams, typename... TArgs>
|
||||
typename Callback<R>::Pointer Callback<void>::New(const T *obj,
|
||||
R (T::*impl)(TParams...)
|
||||
const,
|
||||
TArgs &&...args) {
|
||||
return std::make_shared<CallbackImpl<const T, R, decltype(impl)>>(
|
||||
obj, impl, std::forward<TArgs>(args)...);
|
||||
}
|
||||
|
||||
//! Create callback closure (volatile member function pointer)
|
||||
template <typename T, typename R, typename... TParams, typename... TArgs>
|
||||
typename Callback<R>::Pointer Callback<void>::New(
|
||||
volatile T *obj, R (T::*impl)(TParams...) volatile, TArgs &&...args) {
|
||||
return std::make_shared<CallbackImpl<volatile T, R, decltype(impl)>>(
|
||||
obj, impl, std::forward<TArgs>(args)...);
|
||||
}
|
||||
|
||||
//! Create callback closure (constable volatile member function pointer)
|
||||
template <typename T, typename R, typename... TParams, typename... TArgs>
|
||||
typename Callback<R>::Pointer Callback<void>::New(const volatile T *obj,
|
||||
R (T::*impl)(TParams...)
|
||||
const volatile,
|
||||
TArgs &&...args) {
|
||||
return std::make_shared<CallbackImpl<const volatile T, R, decltype(impl)>>(
|
||||
obj, impl, std::forward<TArgs>(args)...);
|
||||
}
|
||||
|
||||
//! Create callback closure (function)
|
||||
template <typename TFunc, typename... TArgs, typename>
|
||||
typename Callback<typename CallbackTraits<TFunc>::ResultType>::Pointer
|
||||
Callback<void>::New(TFunc &&impl, TArgs &&...args) {
|
||||
return std::make_shared<CallbackImpl<
|
||||
void, typename CallbackTraits<TFunc>::ResultType, decltype(impl)>>(
|
||||
std::forward<TFunc>(impl), std::forward<TArgs>(args)...);
|
||||
}
|
||||
|
||||
//! Callback Handler
|
||||
template <typename R>
|
||||
using CallbackHandler = typename Callback<R>::Pointer;
|
||||
|
||||
//! Closure
|
||||
using Closure = Callback<void>;
|
||||
|
||||
//! Closure Handler
|
||||
using ClosureHandler = Closure::Pointer;
|
||||
|
||||
} // namespace ailego
|
||||
} // namespace zvec
|
||||
@@ -0,0 +1,24 @@
|
||||
// Copyright 2025-present the zvec project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "scope_guard.h"
|
||||
|
||||
#define AILEGO_DEFER_NAME_(x, y) x##y
|
||||
#define AILEGO_DEFER_NAME(x) AILEGO_DEFER_NAME_(__ailegoDefer_, x)
|
||||
|
||||
//! Defer operator
|
||||
#define AILEGO_DEFER(...) \
|
||||
auto AILEGO_DEFER_NAME(__LINE__) = ailego::ScopeGuard::Make(__VA_ARGS__)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,172 @@
|
||||
// Copyright 2025-present the zvec project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
namespace zvec {
|
||||
namespace ailego {
|
||||
|
||||
/*! Factory
|
||||
*/
|
||||
template <typename TBase>
|
||||
class Factory {
|
||||
public:
|
||||
/*! Factory Register
|
||||
*/
|
||||
template <typename TImpl, typename = typename std::enable_if<
|
||||
std::is_base_of<TBase, TImpl>::value>::type>
|
||||
class Register {
|
||||
public:
|
||||
//! Constructor
|
||||
Register(const char *key) {
|
||||
Factory::Instance()->set(key, [] { return Register::Construct(); });
|
||||
}
|
||||
|
||||
//! Constructor
|
||||
template <typename... TArgs>
|
||||
Register(const char *key, TArgs &&...args) {
|
||||
std::tuple<TArgs...> tuple(std::forward<TArgs>(args)...);
|
||||
|
||||
Factory::Instance()->set(key, [tuple] {
|
||||
return Register::Construct(
|
||||
tuple, typename TupleIndexMaker<sizeof...(TArgs)>::Type());
|
||||
});
|
||||
}
|
||||
|
||||
protected:
|
||||
//! Tuple Index Maker
|
||||
template <size_t N, size_t... I>
|
||||
struct TupleIndexMaker : TupleIndexMaker<N - 1, N - 1, I...> {};
|
||||
|
||||
//! Tuple Index
|
||||
template <size_t...>
|
||||
struct TupleIndex {};
|
||||
|
||||
//! Tuple Index Maker (special)
|
||||
template <size_t... I>
|
||||
struct TupleIndexMaker<0, I...> {
|
||||
typedef TupleIndex<I...> Type;
|
||||
};
|
||||
|
||||
//! Construct a register object
|
||||
template <typename... TArgs, size_t... I>
|
||||
static TImpl *Construct(const std::tuple<TArgs...> &tuple,
|
||||
TupleIndex<I...>) {
|
||||
return new (std::nothrow) TImpl(std::get<I>(tuple)...);
|
||||
}
|
||||
|
||||
//! Construct a register object
|
||||
static TImpl *Construct(void) {
|
||||
return new (std::nothrow) TImpl();
|
||||
}
|
||||
};
|
||||
|
||||
//! Produce an instance (c_ptr)
|
||||
static TBase *Make(const char *key) {
|
||||
return Factory::Instance()->produce(key);
|
||||
}
|
||||
|
||||
//! Produce an instance (shared_ptr)
|
||||
static std::shared_ptr<TBase> MakeShared(const char *key) {
|
||||
return std::shared_ptr<TBase>(Factory::Make(key));
|
||||
}
|
||||
|
||||
//! Produce an instance (unique_ptr)
|
||||
static std::unique_ptr<TBase> MakeUnique(const char *key) {
|
||||
return std::unique_ptr<TBase>(Factory::Make(key));
|
||||
}
|
||||
|
||||
//! Test if the class is exist
|
||||
static bool Has(const char *key) {
|
||||
return Factory::Instance()->has(key);
|
||||
}
|
||||
|
||||
//! Retrieve classes in factory
|
||||
static std::vector<std::string> Classes(void) {
|
||||
return Factory::Instance()->classes();
|
||||
}
|
||||
|
||||
protected:
|
||||
//! Constructor
|
||||
Factory(void) : map_() {}
|
||||
|
||||
//! Retrieve the singleton factory
|
||||
static Factory *Instance(void) {
|
||||
static Factory factory;
|
||||
return (&factory);
|
||||
}
|
||||
|
||||
//! Inserts a new class into map
|
||||
template <typename TFunc>
|
||||
void set(const char *key, TFunc &&func) {
|
||||
map_[key] = std::forward<TFunc>(func);
|
||||
}
|
||||
|
||||
//! Produce an instance
|
||||
TBase *produce(const char *key) {
|
||||
auto iter = map_.find(key);
|
||||
if (iter != map_.end()) {
|
||||
return iter->second();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
//! Test if the class is exist
|
||||
bool has(const char *key) {
|
||||
return (map_.find(key) != map_.end());
|
||||
}
|
||||
|
||||
//! Retrieve classes in factory
|
||||
std::vector<std::string> classes(void) const {
|
||||
std::vector<std::string> vec;
|
||||
for (const auto &it : map_) {
|
||||
vec.push_back(std::string(it.first));
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
private:
|
||||
//! Disable them
|
||||
Factory(const Factory &);
|
||||
Factory(Factory &&);
|
||||
Factory &operator=(const Factory &);
|
||||
|
||||
/*! Key Comparer
|
||||
*/
|
||||
struct KeyComparer {
|
||||
bool operator()(const char *lhs, const char *rhs) const {
|
||||
return (std::strcmp(lhs, rhs) < 0);
|
||||
}
|
||||
};
|
||||
|
||||
//! Don't use variable buffer as key store.
|
||||
//! The key must be use a static buffer to store.
|
||||
std::map<const char *, std::function<TBase *()>, KeyComparer> map_;
|
||||
};
|
||||
|
||||
//! Factory Register
|
||||
#define AILEGO_FACTORY_REGISTER(__NAME__, __BASE__, __IMPL__, ...) \
|
||||
static ailego::Factory<__BASE__>::Register<__IMPL__> \
|
||||
__ailegoFactoryRegister_##__NAME__(#__NAME__, ##__VA_ARGS__)
|
||||
|
||||
} // namespace ailego
|
||||
} // namespace zvec
|
||||
@@ -0,0 +1,177 @@
|
||||
// Copyright 2025-present the zvec project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "closure.h"
|
||||
|
||||
namespace zvec {
|
||||
namespace ailego {
|
||||
|
||||
/*! Scope Guard Implementation
|
||||
*/
|
||||
template <typename T, typename TFunc>
|
||||
class ScopeGuardImpl {
|
||||
public:
|
||||
using Object = CallbackObject<T>;
|
||||
using Functor = CallbackFunctor<TFunc>;
|
||||
|
||||
//! Constructor
|
||||
ScopeGuardImpl(ScopeGuardImpl &&rhs)
|
||||
: obj_(rhs.obj_),
|
||||
impl_(std::move(rhs.impl_)),
|
||||
tuple_(std::move(rhs.tuple_)) {
|
||||
rhs.obj_ = nullptr;
|
||||
}
|
||||
|
||||
//! Constructor
|
||||
template <typename... TArgs>
|
||||
ScopeGuardImpl(typename Object::Type *obj, const typename Functor::Type &impl,
|
||||
TArgs &&...args)
|
||||
: obj_(obj), impl_(impl), tuple_(std::forward<TArgs>(args)...) {}
|
||||
|
||||
//! Constructor
|
||||
template <typename... TArgs>
|
||||
ScopeGuardImpl(typename Object::Type *obj, typename Functor::Type &&impl,
|
||||
TArgs &&...args)
|
||||
: obj_(obj),
|
||||
impl_(std::move(impl)),
|
||||
tuple_(std::forward<TArgs>(args)...) {}
|
||||
|
||||
// Destructor
|
||||
~ScopeGuardImpl(void) {
|
||||
if (obj_) {
|
||||
Functor::Run(obj_, impl_, tuple_);
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
//! Disable them
|
||||
ScopeGuardImpl(void) = delete;
|
||||
ScopeGuardImpl(const ScopeGuardImpl &) = delete;
|
||||
ScopeGuardImpl &operator=(const ScopeGuardImpl &) = delete;
|
||||
|
||||
private:
|
||||
//! Members
|
||||
typename Object::Type *obj_;
|
||||
typename Functor::Type impl_;
|
||||
typename Functor::TupleType tuple_;
|
||||
};
|
||||
|
||||
/*! Scope Guard Implementation (void, TFunc)
|
||||
*/
|
||||
template <typename TFunc>
|
||||
class ScopeGuardImpl<void, TFunc> {
|
||||
public:
|
||||
//! Callback Functor Type
|
||||
using Functor = CallbackFunctor<TFunc>;
|
||||
|
||||
//! Constructor
|
||||
ScopeGuardImpl(ScopeGuardImpl &&rhs)
|
||||
: impl_(std::move(rhs.impl_)),
|
||||
tuple_(std::move(rhs.tuple_)),
|
||||
valid_(rhs.valid_) {
|
||||
rhs.valid_ = false;
|
||||
}
|
||||
|
||||
//! Constructor
|
||||
template <typename... TArgs>
|
||||
ScopeGuardImpl(const typename Functor::Type &impl, TArgs &&...args)
|
||||
: impl_(impl), tuple_(std::forward<TArgs>(args)...), valid_(true) {}
|
||||
|
||||
//! Constructor
|
||||
template <typename... TArgs>
|
||||
ScopeGuardImpl(typename Functor::Type &&impl, TArgs &&...args)
|
||||
: impl_(std::move(impl)),
|
||||
tuple_(std::forward<TArgs>(args)...),
|
||||
valid_(true) {}
|
||||
|
||||
// Destructor
|
||||
~ScopeGuardImpl(void) {
|
||||
if (valid_) {
|
||||
Functor::Run(impl_, tuple_);
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
//! Disable them
|
||||
ScopeGuardImpl(void) = delete;
|
||||
ScopeGuardImpl(const ScopeGuardImpl &) = delete;
|
||||
ScopeGuardImpl &operator=(const ScopeGuardImpl &) = delete;
|
||||
|
||||
private:
|
||||
//! Members
|
||||
typename Functor::Type impl_;
|
||||
typename Functor::TupleType tuple_;
|
||||
bool valid_;
|
||||
};
|
||||
|
||||
/*! Scope Guard
|
||||
*/
|
||||
struct ScopeGuard {
|
||||
//! Make a scope guard object (member function pointer)
|
||||
template <typename T, typename R, typename... TParams, typename... TArgs>
|
||||
static inline auto Make(T *obj, R (T::*impl)(TParams...), TArgs &&...args)
|
||||
-> ScopeGuardImpl<T, typename CallbackTraits<decltype(impl)>::Type> {
|
||||
return ScopeGuardImpl<T, typename CallbackTraits<decltype(impl)>::Type>(
|
||||
obj, impl, std::forward<TArgs>(args)...);
|
||||
}
|
||||
|
||||
//! Make a scope guard object (constable member function pointer)
|
||||
template <typename T, typename R, typename... TParams, typename... TArgs>
|
||||
static inline auto Make(const T *obj, R (T::*impl)(TParams...) const,
|
||||
TArgs &&...args)
|
||||
-> ScopeGuardImpl<const T,
|
||||
typename CallbackTraits<decltype(impl)>::Type> {
|
||||
return ScopeGuardImpl<const T,
|
||||
typename CallbackTraits<decltype(impl)>::Type>(
|
||||
obj, impl, std::forward<TArgs>(args)...);
|
||||
}
|
||||
|
||||
//! Make a scope guard object (volatile member function pointer)
|
||||
template <typename T, typename R, typename... TParams, typename... TArgs>
|
||||
static inline auto Make(volatile T *obj, R (T::*impl)(TParams...) volatile,
|
||||
TArgs &&...args)
|
||||
-> ScopeGuardImpl<volatile T,
|
||||
typename CallbackTraits<decltype(impl)>::Type> {
|
||||
return ScopeGuardImpl<volatile T,
|
||||
typename CallbackTraits<decltype(impl)>::Type>(
|
||||
obj, impl, std::forward<TArgs>(args)...);
|
||||
}
|
||||
|
||||
//! Make a scope guard object (constable volatile member function pointer)
|
||||
template <typename T, typename R, typename... TParams, typename... TArgs>
|
||||
static inline auto Make(const volatile T *obj,
|
||||
R (T::*impl)(TParams...) const volatile,
|
||||
TArgs &&...args)
|
||||
-> ScopeGuardImpl<const volatile T,
|
||||
typename CallbackTraits<decltype(impl)>::Type> {
|
||||
return ScopeGuardImpl<const volatile T,
|
||||
typename CallbackTraits<decltype(impl)>::Type>(
|
||||
obj, impl, std::forward<TArgs>(args)...);
|
||||
}
|
||||
|
||||
//! Make a scope guard object (function)
|
||||
template <
|
||||
typename TFunc, typename... TArgs,
|
||||
typename = typename std::enable_if<CallbackValidator<TFunc>::Value>::type>
|
||||
static inline auto Make(TFunc &&impl, TArgs &&...args)
|
||||
-> ScopeGuardImpl<void, typename CallbackTraits<TFunc>::Type> {
|
||||
return ScopeGuardImpl<void, typename CallbackTraits<TFunc>::Type>(
|
||||
std::forward<TFunc>(impl), std::forward<TArgs>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ailego
|
||||
} // namespace zvec
|
||||
@@ -0,0 +1,51 @@
|
||||
// Copyright 2025-present the zvec project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace zvec {
|
||||
namespace ailego {
|
||||
|
||||
/*! Singleton (C++11)
|
||||
*/
|
||||
template <typename T>
|
||||
class Singleton {
|
||||
public:
|
||||
using ObjectType = typename std::remove_reference<T>::type;
|
||||
|
||||
//! Retrieve instance of object
|
||||
static ObjectType &Instance(void) noexcept(
|
||||
std::is_nothrow_constructible<ObjectType>::value) {
|
||||
// Since it's a static variable, if the class has already been created,
|
||||
// it won't be created again. And it is thread-safe in C++11.
|
||||
static ObjectType obj;
|
||||
return obj;
|
||||
}
|
||||
|
||||
protected:
|
||||
//! Constructor (Allow inheritance)
|
||||
Singleton(void) {}
|
||||
|
||||
private:
|
||||
//! Disable them
|
||||
Singleton(Singleton const &) = delete;
|
||||
Singleton(Singleton &&) = delete;
|
||||
Singleton &operator=(Singleton const &) = delete;
|
||||
Singleton &operator=(Singleton &&) = delete;
|
||||
};
|
||||
|
||||
} // namespace ailego
|
||||
} // namespace zvec
|
||||
Reference in New Issue
Block a user