the C++ STL vector has a lot of decent properties, but only works when the size of each item is known at run-time.
I would like to have a vector class that features
I've seen this question before! Is there an STL container that stores an array of elements in contiguous memory where the element size is specified at runtime?
Guy wanted an "interleaved vector" (his word) that would hold dynamically sized objects, defined by a map of member types and offsets:
typedef Toffset uint; //byte offset;
typedef Ttype uint; //enum of types
typedef std::pair member;
typedef std::unordered_map memberdefs;
And I came up with a (untested) class to handle that. The full code is in the link, but prototypes are:
class interleaved_vector {
const char* buffer;
size_t count;
size_t size;
std::shared_ptr members;
public:
class dynamic_object {
const char* buffer;
std::shared_ptr members;
friend interleaved_vector;
dynamic_object(const char* buffer_, std::shared_ptr members_);
dynamic_object& operator=(const dynamic_object& b) = delete;
public:
dynamic_object(const dynamic_object& b) ;
template
T get(const std::string& member) const;
template <>
T* get(const std::string& member) const;
void* operator[](const std::string& member) const;
};
interleaved_vector(const char* buffer_, size_t count_, size_t size_, const memberdefs& members_);
dynamic_object get(size_t index) const;
dynamic_object operator[](size_t index) const;
size_t size();
};
As a warning: it does rely on some behavior that I think is undefined, and in general, is a bad idea. Go with a vector of pointers.