Thread: [Nutshell-devel] Basic class information : Byte Arrays
Status: Planning
Brought to you by:
alexandream
From: Alexandre M. <ale...@gm...> - 2006-08-24 15:58:01
|
As requested by Coacci, posting the description of the basic classes involved in network code. ByteArray is an abstract class, it has a few methods implemented, but cannot be instantiated. what it does is to give a common interface to access things that wraps a byte array (char *). This can be used to aid in the serialization of many data types. ByteArray public: const char& operator[](int index) const; char& operator[](int index); int getCapacity(); void copyFrom(const ByteArray& source, int srcIndex, int dstIndex, int size); private: char& fetchCharacter(int index); // a wrapper to common functionality shared // by both [] operators. ByteArray(char * wrappedData, int capacity); virtual ~ByteArray(); protected: int capacity; char* data; -- With ByteArray as a basis, we can start creating wrappers for many things. There is a class of information that can be wrapped in byte arrays for easy serialization: all those "integer-based" types (char, short, int, unsigned char, unsigned short, unsigned int, void* and fixed-point (which I will explain later)). To aid those in serialization, we use a basic template class that can make them, it is ByteArrayTemplateWrapper. template <class T> ByteArrayTemplateWrapper extends ByteArray as public public: ByteArrayTemplateWrapper(const T wrappedData); ~ByteArrayTemplateWrapper(); const T& getValue(); void setValue(const T&); private: T internalData; Well, what it does: It receives a pointer to an entity of the T Type, and initialize its parent ByteArray with a pointer to it (converted to char*) as data and the sizeof(T) value as capacity. That effectively creates an serialized array of the data described by the object. WARNING: One should not use this template to serialize data that holds pointers or stuff like that when sending through the network, as it obviously would make no sense to the other end of the connection. The ByteArrayTemplateWrapper is realized through a few typedefs, as follows: typedef ByteArrayTemplateWrapper<int> ByteArrayIntWrapper; typedef ByteArrayTemplateWrapper<short> ByteArrayShortWrapper; typedef ByteArrayTemplateWrapper<char> ByteArrayCharWrapper; typedef ByteArrayTemplateWrapper<void*> ByteArrayVoidPtrWrapper; typedef ByteArrayTemplateWrapper<unsigned int> ByteArrayUnsignedIntWrapper; typedef ByteArrayTemplateWrapper<unsigned short> ByteArrayUnsignedShortWrapper; typedef ByteArrayTemplateWrapper<unsigned char> ByteArrayUnsignedCharWrapper; These are commonly used to handle the information packed into a message's body. -- Along with the TemplateWrappers there is also an "Allocated Array Wrapper", that just creates an array of chars from heap space and holds its pointer until it is destructed (be it by scope ending or explicit delete). It follows: AllocByteArray extends ByteArray as public public: AllocByteArray(int capacity); AllocByteArray(char* source, int size); ~AllocByteArray(); And that is pretty much it. The constructors are simple, one just allocates it, the other allocates and copies the data from the source array to the newly allocated one. Well, I just hope this is enough information for you to start the UML class diagrams, Coacci. I have to work a bit now, otherwise my boss will get mad at me (lol). Cheers, Alexandre Moreira. |
From: Alexandre M. <ale...@gm...> - 2006-08-24 17:40:00
|
Answering my own message. damn I hate to forget things. 2006/8/24, Alexandre Moreira <ale...@gm...>: [...] > > ByteArray > public: > const char& operator[](int index) const; > char& operator[](int index); > int getCapacity(); > void copyFrom(const ByteArray& source, int srcIndex, int dstIndex, int size); char * getBackingArray() //returns a pointer to the array being used as back-end. > > private: > char& fetchCharacter(int index); // a wrapper to common functionality shared > // by both [] operators. > ByteArray(char * wrappedData, int capacity); > virtual ~ByteArray(); > protected: > int capacity; > char* data; > -- > [...] |
From: Rodrigo C. <rc...@gm...> - 2006-08-26 15:23:44
|
> > ByteArray is an abstract class, it has a few methods implemented, but > cannot be instantiated. > what it does is to give a common interface to access things that wraps a > byte array (char *). This can be used to aid in the serialization of many > data types. > > ByteArray > public: > const char& operator[](int index) const; > char& operator[](int index); > int getCapacity(); > void copyFrom(const ByteArray& source, int srcIndex, int dstIndex, int > size); > private: > char& fetchCharacter(int index); // a wrapper to common functionality > shared > // by both [] operators. > ByteArray(char * wrappedData, int capacity); > Wait a moment, this constructor is really to be private? OTOH I don't see a default constructor (as this one is private), did you miss this or was intentional? virtual ~ByteArray(); > protected: > int capacity; > char* data; > -- > > --=20 Abra=E7os, Rodrigo |
From: Alexandre M. <ale...@gm...> - 2006-08-28 15:59:36
|
[...] > > ByteArray(char * wrappedData, int capacity); > > > Wait a moment, this constructor is really to be private? > OTOH I don't see a default constructor (as this one is private), did you > miss this or was intentional? > oops! It was a protected constructor, sorry. |