Skip to content

Instantly share code, notes, and snippets.

@mrexodia
Created July 17, 2015 21:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrexodia/4e4804f482add1f3b2e5 to your computer and use it in GitHub Desktop.
Save mrexodia/4e4804f482add1f3b2e5 to your computer and use it in GitHub Desktop.
#include "DLL.h"
#include <stdlib.h>
#include <cstring>
DLL_EXPORT void* DataAlloc(size_t size)
{
return malloc(size);
}
DLL_EXPORT void DataFree(void* ptr)
{
if (ptr)
free(ptr);
}
DLL_EXPORT bool GetIntList(ListInfo* list)
{
if (!list)
return false;
int myData[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
list->size = sizeof(myData);
list->count = list->size / sizeof(myData[0]);
list->data = DataAlloc(list->size);
memcpy(list->data, myData, list->size);
return true;
}
DLL_EXPORT bool GetBoolList(ListInfo* list)
{
if (!list)
return false;
bool myData[] = { true, false, true, false, true };
list->size = sizeof(myData);
list->count = list->size / sizeof(myData[0]);
list->data = DataAlloc(list->size);
memcpy(list->data, myData, list->size);
return true;
}
#ifndef _DLL_H
#define _DLL_H
#include "List.h"
#define DLL_EXPORT __declspec(dllexport)
#ifdef __cplusplus
extern "C" {
#endif //__cplusplus
DLL_EXPORT void* DataAlloc(size_t size);
DLL_EXPORT void DataFree(void* ptr);
DLL_EXPORT bool GetIntList(ListInfo* list);
DLL_EXPORT bool GetBoolList(ListInfo* list);
#ifdef __cplusplus
}
#endif //__cplusplus
#endif //_DLL_H
#ifndef _LIST_H
#define _LIST_H
#include <assert.h>
#include <iterator>
typedef struct
{
size_t count;
size_t size;
void* data;
} ListInfo;
#ifdef __cplusplus
/**
\brief A list object.
\tparam Type List contents type.
*/
template<typename Type>
class List
{
public:
typedef void (*DATAFREE)(void* ptr);
/**
\brief List constructor.
\param _freeData (Optional) the free function.
*/
inline List(DATAFREE dataFree = DataFree)
: _dataFree(dataFree)
{
if (!_dataFree) //crash when no free function was supplied
__debugbreak();
memset(&_listInfo, 0, sizeof(_listInfo));
}
/**
\brief List destructor.
*/
inline ~List()
{
cleanup();
}
/**
\brief Cleans up the list, freeing the list data when it is not null.
*/
inline void cleanup()
{
if (_listInfo.data)
{
_dataFree(_listInfo.data);
_listInfo.data = nullptr;
}
}
/**
\brief Gets the ListInfo. Also calls cleanup() to prevent possible memory leaks!
\return A ListInfo* (will never be null).
*/
inline ListInfo* list()
{
cleanup();
return &_listInfo;
}
/**
\brief Gets the ListInfo without calling cleanup().
\return A ListInfo* (will never be null).
*/
inline const ListInfo* clist() const
{
return &_listInfo;
}
/**
\brief Gets the list data.
\return Returns ListInfo->data. Can be null if the list was never initialized.
*/
inline Type* data() const
{
return reinterpret_cast<Type*>(_listInfo.data);
}
/**
\brief Gets the number of elements in the list. This will crash the program if the data is not consistent with the specified template argument.
\return The number of elements in the list.
*/
inline size_t count() const
{
if (_listInfo.size != _listInfo.count * sizeof(Type)) //make sure the user is using the correct type.
__debugbreak();
return _listInfo.count;
}
/**
\brief Array indexer operator. This will crash if you try to access out-of-bounds.
\param index Zero-based index of the item you want to get.
\return Reference to a value at that index.
*/
inline Type & operator[](size_t index) const
{
if (index >= count()) //make sure the out-of-bounds access is caught as soon as possible.
__debugbreak();
return data()[index];
}
private:
ListInfo _listInfo;
DATAFREE _dataFree;
};
#endif //__cplusplus
#endif //_LIST_H
#include <stdio.h>
#include "DLL.h"
#include <vector>
int main()
{
List<int> myInts;
GetIntList(myInts.list());
for (size_t i = 0; i < myInts.count(); i++)
{
printf("%d ", myInts[i]);
}
puts("");
List<bool> myBools;
GetBoolList(myBools.list());
for (size_t i = 0; i < myBools.count();i++)
{
printf("%s ", myBools[i] ? "true" : "false");
}
puts("");
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment