473,776 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Intrusive list

Hi everybody,

I am creating an intrusive list ('next' and 'prev' pointers are stored
within an object that is in a list). One method of doing that is to inherit
all objects from some class that contains these pointers. However, it is
unacceptable for my problem, because it limits the number of lists the
object can be in to 1. I must have some objects in more than 1 list. Instead
of inheriting, I use aggregation:

class Node { // Node of a list
Node *prev, *next;
}

class SomeObject { // Object that can be in 3 independent lists
Node l1, l2, l3;
}

When I iterate through my list, I get pointer to a Node that is stored
within SomeObject. But I need to get pointer to SomeObject itself. Right now
I'm using nonconforming and ugly way:

#define MEMBER_OFFSET(C lassName, FieldName) int(&(((ClassNa me
*)0)->FieldName))

ListNode *node;
SomeObject *so = reinterpret_cas t<SomeObject *>((char *)node -
MEMBER_OFFSET(S omeObject, l2));

My question is, is there a standard conforming way of doing that? For
example by using pointers to members?

Best regards,
Marcin


Jul 22 '05 #1
9 4056
Marcin Kalicinski wrote:
I am creating an intrusive list ('next' and 'prev' pointers are stored
within an object that is in a list).
Why aren't you using std::list?
One method of doing that is to inherit
all objects from some class that contains these pointers. However, it is
unacceptable for my problem, because it limits the number of lists the
object can be in to 1.
How so? C++ supports multiple inheritance.
I must have some objects in more than 1 list. Instead
of inheriting, I use aggregation:

class Node { // Node of a list
Node *prev, *next;
}

class SomeObject { // Object that can be in 3 independent lists
Node l1, l2, l3;
}

When I iterate through my list, I get pointer to a Node that is stored
within SomeObject. But I need to get pointer to SomeObject itself. Right now
I'm using nonconforming and ugly way:

#define MEMBER_OFFSET(C lassName, FieldName) int(&(((ClassNa me
*)0)->FieldName))

ListNode *node;
SomeObject *so = reinterpret_cas t<SomeObject *>((char *)node -
MEMBER_OFFSET(S omeObject, l2));

My question is, is there a standard conforming way of doing that? For
example by using pointers to members?


I don't know of any portable way to get the address of an object given
the address of an arbitrary sub-object.
Jul 22 '05 #2
Marcin Kalicinski wrote:

My question is, is there a standard conforming way of doing that? For
example by using pointers to members?


No.
But of course you could add a back pointer from each node
class to the object it links.

class Node {
Node *prev, *next;
SomeObject* TheObject;
}

But honestly, I would change the design.
Declare one container as beeing the master and
holding the objects. All other lists just store
pointers to those objects. This way each object
can be referenced by as many lists as you wish.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #3

Uzytkownik "Jeff Schwab" <je******@comca st.net> napisal w wiadomosci
news:Gr******** ************@co mcast.com...
Marcin Kalicinski wrote:

Why aren't you using std::list?


Because it is non-intrusive and I need an intrusive list.
One method of doing that is to inherit
all objects from some class that contains these pointers. However, it is
unacceptable for my problem, because it limits the number of lists the
object can be in to 1.


How so? C++ supports multiple inheritance.


But I cannot inherit multiple times from the same class.

Marcin
Jul 22 '05 #4
"Marcin Kalicinski" <ka****@poczta. onet.pl> wrote in
news:c8******** **@korweta.task .gda.pl:
Hi everybody,

I am creating an intrusive list ('next' and 'prev' pointers are stored
within an object that is in a list). One method of doing that is to
inherit all objects from some class that contains these pointers.
However, it is unacceptable for my problem, because it limits the
number of lists the object can be in to 1. I must have some objects in
more than 1 list. Instead of inheriting, I use aggregation:

class Node { // Node of a list
Node *prev, *next;
}

class SomeObject { // Object that can be in 3 independent lists
Node l1, l2, l3;
}

When I iterate through my list, I get pointer to a Node that is stored
within SomeObject. But I need to get pointer to SomeObject itself.
Right now I'm using nonconforming and ugly way:

#define MEMBER_OFFSET(C lassName, FieldName) int(&(((ClassNa me
*)0)->FieldName))

ListNode *node;
SomeObject *so = reinterpret_cas t<SomeObject *>((char *)node -
MEMBER_OFFSET(S omeObject, l2));

My question is, is there a standard conforming way of doing that? For
example by using pointers to members?


How about going the other way around?
Instead of "embedding" the node pointer in an object, you could use the
aligned storage* trick to hold the memory and place your object there.

* aligned storage as per boost::aligned_ storage, or Alexandrescu's
"Discrimina ted Unions" article.

Very very very oversimplifying :

template <class T>
class Node {
char m_buffer[sizeof(T)];
Node* m_prev;
Node* m_next;
};

You could plant objects directly into node buffers by using placement
new.

As I said, the above is a huge oversimplificat ion. The buffer should be
properly aligned for holding type T (see Alexandrescu's article on
discriminated unions at http://www.moderncppdesign.com ).

Nonetheless, it's a very interesting technique. It can help get rid of
that one extra level of pointer indirection.

--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #5
bartek <sp************ ******@o2.pl> wrote in
news:Xn******** *************** ***********@153 .19.251.200:
How about going the other way around?
Instead of "embedding" the node pointer in an object, you could use the
aligned storage* trick to hold the memory and place your object there.


(...)

Now, looking and what I've posted, I'm finding it difficult to grasp why
the heck I'd want to do it that way... I'm tired I guess... Please
disregard.

--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #6

"Marcin Kalicinski" <ka****@poczta. onet.pl> wrote in message
news:c8******** **@korweta.task .gda.pl...

Uzytkownik "Jeff Schwab" <je******@comca st.net> napisal w wiadomosci
news:Gr******** ************@co mcast.com...
Marcin Kalicinski wrote:

Why aren't you using std::list?


Because it is non-intrusive and I need an intrusive list.
One method of doing that is to inherit
all objects from some class that contains these pointers. However, it is unacceptable for my problem, because it limits the number of lists the
object can be in to 1.


How so? C++ supports multiple inheritance.


But I cannot inherit multiple times from the same class.


You cannot inherit *directly* multiple times from the same class.

class ListBase
{
ListBase* next;
ListBase* prev;
};

template <int ID>
class BaseWrapper : public ListBase
{
};

class MyClass : public BaseWrapper<1>, public BaseWrapper<2>, public
baseWrapper<3>
{
};

MyClass can be on three different lists.

john
Jul 22 '05 #7
"Marcin Kalicinski" <ka****@poczta. onet.pl> wrote in message news:<c8******* ***@korweta.tas k.gda.pl>...
Hi everybody,

I am creating an intrusive list ('next' and 'prev' pointers are stored
within an object that is in a list). One method of doing that is to inherit
all objects from some class that contains these pointers. However, it is
unacceptable for my problem, because it limits the number of lists the
object can be in to 1. I must have some objects in more than 1 list. Instead
of inheriting, I use aggregation:

class Node { // Node of a list
Node *prev, *next;
}

class SomeObject { // Object that can be in 3 independent lists
Node l1, l2, l3;
}

When I iterate through my list, I get pointer to a Node that is stored
within SomeObject. But I need to get pointer to SomeObject itself. Right now
I'm using nonconforming and ugly way:

#define MEMBER_OFFSET(C lassName, FieldName) int(&(((ClassNa me
*)0)->FieldName))

ListNode *node;
SomeObject *so = reinterpret_cas t<SomeObject *>((char *)node -
MEMBER_OFFSET(S omeObject, l2));

My question is, is there a standard conforming way of doing that? For
example by using pointers to members?


You can use multiple inheritance with an extra level of
derivation to avoid ambiguity problems:

template<int N>
class NodeBase:public Node
{
};

class SomeObject:
public NobeBase<1>,
public NobeBase<2>,
public NobeBase<3>
{
...
};

See what I mean? Now, if given a NodeBase<n>* one can portably
downcast to SomeObject like this:

NodeBase<2> *pn2=...;
SomeObject* po=static_cast< SomeObject*>(pn 2);

If instead of a NodeBase<n>* you're provided with a
pointer to Node, then you must know from some other source
which of the inherited Nodes you are referring to:

Node* pn;
//we know we've been passed node #3
SomeObject* po=static_cast< SomeObject*>(
static_cast<Nod eBase<3>*>(pn)) ;

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Jul 22 '05 #8

"Marcin Kalicinski" <ka****@poczta. onet.pl> skrev i en meddelelse
news:c8******** **@korweta.task .gda.pl...
Hi everybody,

I am creating an intrusive list ('next' and 'prev' pointers are stored
within an object that is in a list). One method of doing that is to inherit all objects from some class that contains these pointers. However, it is
unacceptable for my problem, because it limits the number of lists the
object can be in to 1. I must have some objects in more than 1 list. Instead of inheriting, I use aggregation:

class Node { // Node of a list
Node *prev, *next;
}

class SomeObject { // Object that can be in 3 independent lists
Node l1, l2, l3;
}

When I iterate through my list, I get pointer to a Node that is stored
within SomeObject. But I need to get pointer to SomeObject itself. Right now I'm using nonconforming and ugly way:

#define MEMBER_OFFSET(C lassName, FieldName) int(&(((ClassNa me
*)0)->FieldName))

ListNode *node;
SomeObject *so = reinterpret_cas t<SomeObject *>((char *)node -
MEMBER_OFFSET(S omeObject, l2));

My question is, is there a standard conforming way of doing that? For
example by using pointers to members?

Best regards,
Marcin

I believe that using templates and pointer-to members could do what you want
to. A brief sketch - giving a singly linked list:

template <typename t> struct link
{
t * next;
};

template <typename t,link<t>::*lin kmember> class link_iterator
{
link<t> head;
link_iterator operator++() { head = head->next;}
};

struct test
{
link<test> link1;
link<test> link2;
};

link_iterator<t est,&test::link 1> iter1;
link_iterator<t est,&test::link 2> iter2;

Just a sketch, but hoping this helps.

/Peter
Jul 22 '05 #9

"Peter Koch Larsen" <pk*****@mailme .dk> skrev i en meddelelse
news:eM******** **************@ news000.worldon line.dk...

"Marcin Kalicinski" <ka****@poczta. onet.pl> skrev i en meddelelse
news:c8******** **@korweta.task .gda.pl...
Hi everybody,

[snip]
#define MEMBER_OFFSET(C lassName, FieldName) int(&(((ClassNa me
*)0)->FieldName))

ListNode *node;
SomeObject *so = reinterpret_cas t<SomeObject *>((char *)node -
MEMBER_OFFSET(S omeObject, l2));

My question is, is there a standard conforming way of doing that? For
example by using pointers to members?

Best regards,
Marcin

I believe that using templates and pointer-to members could do what you

want to. A brief sketch - giving a singly linked list:

template <typename t> struct link
{
t * next;
};

template <typename t,link<t>::*lin kmember> class link_iterator
{
link<t> head;
link_iterator operator++() { head = head->next;}
};

This was written five minutes past my mental bedtime. A correct approach
would be:

template <typename t,link<t>::*lin kmember> class link_iterator
{
t* head;
link_iterator operator++() { head = head->*linkmember; }
};

/Peter

struct test
{
link<test> link1;
link<test> link2;
};

link_iterator<t est,&test::link 1> iter1;
link_iterator<t est,&test::link 2> iter2;

Just a sketch, but hoping this helps.

/Peter

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
997
by: Charles Hartman | last post by:
On Mar 23, 2005, at 7:10 PM, python-list-request@python.org wrote: > 7. (",) Do You Want To Know For Sure You Are Going To Heaven? Is there no way of filtering this recurring offensive material from the list? Charles Hartman
0
1450
by: Sabyasachi Basu | last post by:
Most C++ object serialization techniques are intrusive, that is they require all serializable classes need to have functions to stream in and stream out their contents. I came across "non-intrusive serialization" while going through the tutorial on Robert Ramey's serialization library for Boost. However, it is not clear to me how to implement serialization without the cooperation of the classes being serialized. Can someone through...
6
3108
by: massimo | last post by:
Hey, I wrote this program which should take the numbers entered and sort them out. It doesn¹t matter what order, if decreasing or increasing. I guess I'm confused in the sorting part. Anyone has any advices?? #include <iostream> using namespace std;
6
5427
by: Pradeep | last post by:
Hi, I am using intrusive pointers for my code but i am not much aquianted with them. I am using an example to illustrate my problem as i can't share the code. I have two clases ( say A and B) B is derived from A( B: public A) now i want to pass the this* pointer of the derived class(B) to a
1
1624
by: Pradeep | last post by:
Hi, I am working with Intrusive pointers in a DLL and I need to set the value of the intrusive pointer to an object to NULL from inside the method of that class. So here's the scenario. I have a class A which has a method del().
10
15131
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy *enemydata; // Holds information about an enemy (in a game) // Its a double linked list node
24
5779
by: Robin Cole | last post by:
I'd like a code review if anyone has the time. The code implements a basic skip list library for generic use. I use the following header for debug macros: /* public.h - Public declarations and macros */ #ifndef PUBLIC #define PUBLIC
4
3604
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; };   struct pcb *pcb_pointer;
2
2923
by: Steven T. Hatton | last post by:
It's my understanding that intrusive pointers are frowned upon. For example this is from the boost::intrusive_ptr documentation: "As a general rule, if it isn't obvious whether intrusive_ptr better fits your needs than shared_ptr, try a shared_ptr-based design first." This tells me that intrusive pointers are faster and leaner than any of the other alternative other than raw pointers. http://www.boost.org/libs/smart_ptr/smarttests.htm ...
0
9628
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9464
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10289
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8952
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7471
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.