Is it reasonable to codify lazy loading as a template class? - c++

I'm wondering if something like
template <typename T>
class LazyLoaded
{
mutable char mem[sizeof T]; //First item in the class to keep alignment issues at bay
const std::function<void (T&)> initializer;
mutable bool loaded;
public:
LazyLoaded() : loaded(false)
{
initializer = [] (T&) {};
}
LazyLoaded(const std::function<void (T&)>& init) : initializer(init), loaded(false)
{
}
T& Get()
{
if (!loaded)
{
new (static_cast<void *>(&mem)) T();
initializer(*static_cast<T*>(&mem));
loaded = true;
}
return *static_cast<T*>(&mem);
}
~LazyLoaded()
{
if (loaded)
{
static_cast<T*>(&mem)->~T();
}
}
};
is possible or makes sense to do. (I think there are issues with this code, but hey, I threw it together in 10 minutes, so....)

It's called boost::optional. This should provide almost all the necessary functionality.

Related

C++ Keep tracks of changes inside a class

Imagine a class representing a mail :
class mail {
string subject;
string content;
date receivedDate;
};
Now what I want to achieve is to know if my mail data is set, and once they're set, which ones where changed. I could go with a combination of std::optional and a std::map like this :
class Mail {
std::optional<string> subject;
std::optional<string> content;
std::optional<date> receivedDate;
enum EField { Subject, Content, ReceivedDate };
typedef std::map<EField, bool> ChangedMap;
ChangedMap changedFields;
public:
Mail(optional<string> subject, ... ) {
// initialize map with fields... hard coded
}
bool HasSubject() const { return subject; }
string GetSubject() const { return subject.get(); }
void SetSubject(const std::string& newSubject) {
subject = newSubject;
changedFields[Subject] = true;
}
void RemoveSubject() {
changedFields[Subject] = HasSubject();
subject.reset();
}
bool IsSubjectChanged() const {
return changedFields[Subject];
}
};
But I really think I am missing something crucial here. Would you see any better way to do it, preferably with less memory usage and no hardcoded values ?
I thought about about inheriting from std::optional but I don't see it as a good thing too.
Thanks
Let's generalize this problem: given a type T, I want a wrapper tracked<T> that keeps track of the history of reads/writes at run-time.
I would approach this problem by using std::tuple and metaprogramming. Firstly, let's define mail in terms of an std::tuple:
class mail
{
private:
std::tuple<string, string, date> _data;
public:
// `variant_type` could be automatically computed from the
// tuple type.
using variant_type = std::variant<string, string, date>;
enum class index
{
subject = 0,
content = 1,
date = 2
};
template <index TIndex>
decltype(auto) access()
{
return std::get<static_cast<std::size_t>(TIndex)>(_data);
}
};
I would then create something like tracked<T> that keeps track of the operations executed on T:
template <typename T>
class tracked
{
private:
using index_type = typename T::index;
using variant_type = typename T::variant_type;
struct write_action
{
variant_type _before;
variant_type _after;
};
struct read_action
{
index_type _index;
};
T _data;
std::vector<std::variant<write_action, read_action>> _history;
public:
template <index TIndex>
const auto& read() const
{
_history.emplace_back(read_action{TIndex});
return _data.access<TIndex>();
}
template <index TIndex, typename T>
void write(T&& new_value) const
{
// Remember previous value.
variant_type _before{_data.access<TIndex>()};
_history.emplace_back(write_action{_before, new_value});
return _data.access<TIndex>() = std::forward<T>(new_value);
}
};
The code above is not completely correct, as you need constructors for the action types, exception handling, move semantics support, and much more. I hope you get the general idea though.

Bypass a template error with a private destructor

In compile time, I've got the following issue, how to make this compile, because conceptually for me it's correct, any suggestions of refactoring are welcome.
I got a compile error because "Search" destructor is private but I won't use delete on a Search pointer since I provided a custom Deleter in the initialization of the base class. I know that the compiler doesn't know that, how to bypass it.
error description :
error C2248: cannot access private member declared in class 'Search'
compiler has generated 'Search::~Search' here
class Search
{
public:
static Search* New(/* */); // using a pool of already allocated objects to avoid expensive allocations
static void Delete(Search*);
private:
Search(/* */) {/* */}
~Search() {/* */}
};
template<class T>
class MyList
{
public:
typedef (*CustomDeleter) (T* pElement);
MyList(CustomDeleter lpfnDeleter = NULL) {};
void Empty()
{
for (/**/)
{
if (m_pList[m_nListLastUsed])
{
if (m_lpfnCustomDeleter == NULL)
delete m_pList[m_nListLastUsed]; // COMPILE ERROR HERE BECAUSE Search destructor is private BUT I won't use that instruction since
// I provided a custom Deletern I know that the compiler doesn't know that, how to bypass it
else
m_lpfnCustomDeleter(m_pList[m_nListLastUsed]);
}
}
}
private:
T** m_pList;
CustomDeleter m_lpfnCustomDeleter; // Pointer to a custom deleter
};
class Query : public MyList<Search>
{
public:
Query() : MyList<Search>(&Search::Delete) // I set a custom deleter since Search hides its destructor : is this the right way ?
{}
~Query()
{
/****/
Empty(); // PROBLEM HERE
/***/
}
};
Make sure that 'm_lpfnCustomDeleter' is never NULL or better nullptr. You can make sure of this by falling back to a default 'deleter' if the user does not provide with any custom deleter.
I would prefer something like below.
#include <iostream>
template <typename PointerType>
struct DefaultDeleter {
void operator()(PointerType* ptr) {
std::cout << "Delete\n";
}
};
struct CustomDeleter {
void operator()(int* ptr) {
std::cout << "Custom int deleter" << std::endl;
}
};
template <typename T, typename Deleter = DefaultDeleter<T>>
class Whatever
{
public:
Whatever() {
std::cout << "Cons\n";
}
void deinit() {
Deleter d;
auto v = new T;
d(v); // Just for the sake of example
}
};
int main() {
Whatever<char> w;
w.deinit();
Whatever<int, CustomDeleter> w2;
w2.deinit();
return 0;
}
Updated :: W/o code refactoring
Assuming w/o c++11
Have this small metaprogram added to your code base.
namespace my {
template <typename T, typename U> struct is_same {
static const bool value = false;
};
template <typename T>
struct is_same<T, T> {
static const bool value = true;
};
template <bool v, typename T = void> struct enable_if;
template <typename T = void> struct<true, T> {
typedef T type;
};
}
Change your Empty function to:
void Empty() {
for (/****/) {
do_delete();
}
}
template <typename =
typename my::enable_if<my::is_same<T, Search>::value>::type>
void do_delete() {
assert (m_lpfnCustomDeleter != NULL);
m_lpfnCustomDeleter(m_pList[m_nListLastUsed]);
}
void do_delete() {
delete m_pList[m_nListLastUsed];
}
If you are using c++11, the you dont have to write the metaprogram under namespace 'my'. Just replace 'my::is_same' and 'my::enable_if' with 'std::is_same' and 'std::enable_if'.
Note:, Have not compiled and tested the above code.
Separate the code doing the deleting from the rest:
if (m_pList[m_nListLastUsed])
{
if (m_lpfnCustomDeleter == NULL)
delete m_pList[m_nListLastUsed]; // COMPILE ERROR HERE BECAUSE Search destructor is private BUT I won't use that instruction since
// I provided a custom Deletern I know that the compiler doesn't know that, how to bypass it
else
m_lpfnCustomDeleter(m_pList[m_nListLastUsed]);
}
Replace the code above by a call to:
custom_delete(m_pList[m_nListLastUsed]);
Then add it as a method of your list class, don't forget to include <type_traits> as well:
std::enabled_if<std::is_destructible<T>::value, void>::type custom_delete(T* ptr) {
/* Note: this isn't pre-2000 anymore, 'lpfn' as a prefix is horrible,
don't use prefixes! */
if (m_lpfnCustomDeleter) {
m_lpfnCustomDeleter(ptr);
} else {
delete ptr;
}
}
std::enabled_if<!std::is_destructible<T>::value, void>::type custom_delete(T* ptr) {
if (!m_lpfnCustomDeleter) {
throw "No custom deleter for a non destructible type!";
}
m_lpfnCustomDeleter(ptr);
}
enabled_if will make it so that the function where it can delete the object directly doesn't exist in your list if the object has a private destructor.
Alternatively, you could pass a structure (or function) acting as a custom deleter as the second template argument of your list with a default value as one that calls the delete operator, then directly call this structure on your pointer, as in Arunmu's anser.

Can I make one C wrapper for any C++ vector that could go in a Extern C

I need one C wrapper for any C++ vector that can be passed to a function that expects some specific type of vector. Like my below C wrapper for OpenCV's BRISK"
void cv_BRISK_generateKernel(BRISK* self, vector_float* radiusList,
vector_int* numberList, float dMax, float dMin, vector_int* indexChange) {
self->generateKernel(*radiusList, *numberList, dMax, dMin, *indexChange);
}
the vector_int* and vector_float* are typedef's as below
typedef vector<int> vector_int;
typedef vector<float> vector_float;
These are the vector wrappers I have so far, they work, but I would like to know if there is a way to make just one wrapper for all the vector types. It would have to go in an Extern C..so it can't be a template. but instead of having the below wrappers I would like to make just one wrapper that can be passed to a function expecting a vector_float*(typedeffor vector<float>) or a vector_KeyPoint*(typedef for vector<KeyPoint>) or a vector_int*(typedef for vector<int>) etc. I know about template classes but I can't use them here because it has to go in an extern C {}
vector_float* std_create_vectorf() {
return new vector<float>;
}
vector_int* std_create_vector() {
return new vector<int>;
}
vector_char* std_create_vectorc() {
return new vector<char>;
}
Here is my idealized wrapper If someone can help me figure out how to make this happen I would appreciate it
vector_any_vec* std_create_vectorany() {
return new vector<anyvector>;
}
How about wrapping this up in an interface using run-time polymorphism? You sacrifice a bit of type safety but it should achieve what you need.
enum stored_type
{
st_int,
st_float,
st_char
};
struct IGeneralVector
{
virtual stored_type get_type() = 0;
virtual void* get_data() = 0;
virtual ~IGeneralVector();
};
class VectorFloatHolder : public IGeneralVector
{
std::vector<float>* data;
public:
VectorFloatHolder(std::vector<float>* in) : data(in)
{}
virtual stored_type get_type() override
{
return st_float;
}
virtual void* get_data() override
{
return reinterpret_cast<void *>(data);
}
virtual ~VectorFloatHolder()
{
delete data;
}
};
IGeneralVector* std_create_vectorf()
{
return new VectorFloatHolder(new std::vector<float>);
}
Update
After reading your comment I have a slightly better idea of what you're trying to achieve. But I'm not sure whether it's possible to do exactly what you want as I'm not sure of any other implementation constraints you have. Here's another way to go this time using type erasure.
class ObjectHolder
{
struct base_type
{
virtual ~base_type() {}
};
template<typename T>
struct object_type : base_type
{
object_type(const T& t) : object(t) {}
T object;
};
std::unique_ptr<base_type> data;
public:
template<typename T>
VectorHolder(T t) : data(new object_type<T>(t))
{
}
template<typename T>
T GetData()
{
object_type<T>* val = static_cast<object_type<T>*>(data.get());
return val->object;
}
};
template<typename T>
ObjectHolder* std_create_vector()
{
return new VectorHolder(new std::vector<T>);
}
int main()
{
ObjectHolder* vh = std_create_vector < std::vector<float>>();
// then later on you can get back the original type via:
std::vector<float>* fp = vh->GetData<std::vector<float>*>();
}

Design Pattern, adding data to a class (3rd party) without modifying it

When I have to extend the behaviour of a class without modifying it, I often use the design pattern visitor. It adds member-like functions without modifying the core of the class it works with.
More or less in the same way, I need to extend a third party class, but mostly with data, not behaviour.
In such cases, I often use a std::map matching the a key MyClass* with a value MyClassExtender. MyClassExtender contains all the additionnal information.
While doing that, I happened to wonder if there are other ways of doing that, maybe more common or more 'best-practice". Should I call this additive class an Extender ?
Is there a name for such a pattern...
Nota Bene: I could have simply aggregated the MyClass* and MyClassExtender in a new class, but I need to access MyClassExtender given a MyClass* really often, so the st::map is really convinient.
Why don't you just subclass the class? Inheritance is the way to extend classes, whether with behavior or state. Unless you just want to associate instances of the class with other data, in which case it's not extending at all, and a std::map is the right answer.
So - create your MyClass object with in the struct with your extension objects:
struct MyClassEx {
MyClassExtension extension;
MyClass object;
};
To make it more robustness for different types - use templates from the example: http://ideone.com/mmfK83
The solution below is inspired by std::shared_ptr/std::make_shared:
template <typename Type>
struct LinkExtension;
template <typename Type>
struct TypeEx {
using Extension = typename LinkExtension<Type>::Type;
alignas(Type) uint8_t objectData[sizeof(Type)];
alignas(Extension) uint8_t extensionData[sizeof(Extension)];
Type* getObject() { return reinterpret_cast<Type*>(objectData); }
const Type* getObject() const { return reinterpret_cast<const Type*>(objectData); }
Extension* getExtension() { return reinterpret_cast<Extension*>(extensionData); }
const Extension* getExtension() const { return reinterpret_cast<const Extension*>(extensionData); }
template <class... Args>
TypeEx(Args&&... args)
{
new (objectData) Type(std::forward<Args>(args)...);
new (extensionData) Extension();
}
~TypeEx()
{
getObject()->~Type();
getExtension()->~Extension();
}
TypeEx(const TypeEx&) = delete;
TypeEx& operator = (const TypeEx&) = delete;
};
And some helper functions:
template <typename Type, class... Args>
Type* createObjectEx(Args&&... args)
{
TypeEx<Type>* retVal = new TypeEx<Type>(std::forward<Args>(args)...);
return retVal->getObject();
}
template <typename Type>
typename LinkExtension<Type>::Type& getObjectEx(Type* obj)
{
static_assert(std::is_standard_layout<TypeEx<Type>>::value, "Oops");
static_assert(offsetof(TypeEx<Type>, objectData) == 0, "Oops");
TypeEx<Type>* retVal = static_cast<TypeEx<Type>*>((void*)obj);
return *(retVal->getExtension());
}
template <typename Type>
const typename LinkExtension<Type>::Type& getObjectEx(const Type* obj)
{
static_assert(std::is_standard_layout<TypeEx<Type>>::value, "Oops");
static_assert(offsetof(TypeEx<Type>, objectData) == 0, "Oops");
const TypeEx<Type>* retVal = static_cast<const TypeEx<Type>*>((const void*)obj);
return *(retVal->getExtension());
}
template <typename Type>
void deleteObjectEx(const Type* obj)
{
const TypeEx<Type>* objectEx = static_cast<const TypeEx<Type>*>((const void*)obj);
delete objectEx;
}
And how to link extension to class:
class MyClass {
public:
virtual ~MyClass() = default;
};
struct MyClassExtension {
int a;
int b;
};
template <>
struct LinkExtension<MyClass> {
using Type = MyClassExtension;
};
And proof it works:
void printExtension(MyClass* object);
int main() {
MyClass* object = createObjectEx<MyClass>();
MyClassExtension& extension = getObjectEx(object);
extension.a = 1;
extension.b = 2;
printExtension(object);
deleteObjectEx(object);
TypeEx<MyClass> objectEx;
objectEx.getExtension()->a = 3;
objectEx.getExtension()->b = 4;
printExtension(objectEx.getObject());
}
void printExtension(MyClass* object)
{
MyClassExtension& extension = getObjectEx(object);
std::cout << extension.a << ' ' << extension.b << std::endl;
}
If your compiler does not support variadic templates, the solution is still possible, but requires more hand work to be complete.

Template type deduction with a non-copyable class

Suppose I have an autolocker class which looks something like this:
template <T>
class autolocker {
public:
autolocker(T *l) : lock(l) {
lock->lock();
}
~autolocker() {
lock->unlock();
}
private:
autolocker(const autolocker&);
autolocker& operator=(const autolocker&);
private:
T *lock;
};
Obviously the goal is to be able to use this autolocker with anything that has a lock/unlock method without resorting to virtual functions.
Currently, it's simple enough to use like this:
autolocker<some_lock_t> lock(&my_lock); // my_lock is of type "some_lock_t"
but it is illegal to do:
autolocker lock(&my_lock); // this would be ideal
Is there anyway to get template type deduction to play nice with this (keep in my autolocker is non-copyable). Or is it just easiest to just specify the type?
Yes you can use the scope-guard technique
struct autolocker_base {
autolocker_base() { }
protected:
// ensure users can't copy-as it
autolocker_base(autolocker_base const&)
{ }
autolocker_base &operator=(autolocker_base const&)
{ return *this; }
};
template <T>
class autolocker : public autolocker_base {
public:
autolocker(T *l) : lock(l) {
lock->lock();
}
autolocker(const autolocker& o)
:autolocker_base(o), lock(o.lock)
{ o.lock = 0; }
~autolocker() {
if(lock)
lock->unlock();
}
private:
autolocker& operator=(const autolocker&);
private:
mutable T *lock;
};
Then write a function creating the autolocker
template<typename T>
autolocker<T> makelocker(T *l) {
return autolocker<T>(l);
}
typedef autolocker_base const& autolocker_t;
You can then write it like this:
autolocker_t lock = makelocker(&my_lock);
Once the const reference goes out of scope, the destructor is called. It doesn't need to be virtual. At least GCC optimizes this quite well.
Sadly, this means you have to make your locker-object copyable since you need to return it from the maker function. But the old object won't try to unlock twice, because its pointer is set to 0 when it's copied, so it's safe.
Obviously you can't get away with autolocker being a template, because you want to use it as a type, and templates must be instantiated in order to obtain types.
But type-erasure might be used to do what you want. You turn the class template into a class and its constructor into a member template. But then you'd have to dynamically allocate an inner implementation object.
Better, store a pointer to a function that performs the unlock and let that function be an instance of a template chosen by the templatized constructor. Something along these lines:
// Comeau compiles this, but I haven't tested it.
class autolocker {
public:
template< typename T >
autolocker(T *l) : lock_(l), unlock_(&unlock<T>) { l->lock(); }
~autolocker() { unlock_(lock_); }
private:
autolocker(const autolocker&);
autolocker& operator=(const autolocker&);
private:
typedef void (*unlocker_func_)(void*);
void *lock_;
unlocker_func_ unlock_;
template <typename T>
static void unlock(void* lock) { ((T*)lock)->unlock(); }
};
I haven't actually tried this and the syntax might be wrong (I'm not sure how to take the address of a specific function template instance), but I think this should be doable in principle. Maybe someone comes along and fixes what I got wrong.
I like this a lot more than the scope guard, which, for some reason, I never really liked at all.
I think jwismar is correct and what you want is not possible with C++. However, a similar (not direct analogue) construct is possible with C++0x, using several new features (rvalues/moving and auto variable type):
#include <iostream>
template <typename T>
class autolocker_impl
{
public:
autolocker_impl(T *l) : lock(l) {
lock->lock();
}
autolocker_impl (autolocker_impl&& that)
: lock (that.lock)
{
that.lock = 0;
}
~autolocker_impl() {
if (lock)
lock->unlock();
}
private:
autolocker_impl(const autolocker_impl&);
autolocker_impl& operator=(const autolocker_impl&);
private:
T *lock;
};
template <typename T>
autolocker_impl <T>
autolocker (T* lock)
{
return autolocker_impl <T> (lock);
}
struct lock_type
{
void lock ()
{ std::cout << "locked\n"; }
void unlock ()
{ std::cout << "unlocked\n"; }
};
int
main ()
{
lock_type l;
auto x = autolocker (&l);
}
autolocker is a class template, not a class. Your "this would be ideal" is showing something that doesn't make sense in C++.