xref: /haiku/headers/private/storage/NotOwningEntryRef.h (revision 37fedaf8494b34aad811abcc49e79aa32943f880)
1  /*
2   * Copyright 2013, Haiku, Inc. All Rights Reserved.
3   * Distributed under the terms of the MIT License.
4   *
5   * Authors:
6   *		Ingo Weinhold <ingo_weinhold@gmx.de>
7   */
8  #ifndef _NOT_OWNING_ENTRY_REF_H
9  #define _NOT_OWNING_ENTRY_REF_H
10  
11  
12  #include <Entry.h>
13  #include <Node.h>
14  
15  
16  namespace BPrivate {
17  
18  
19  /*!	entry_ref subclass that avoids cloning the entry name.
20  
21  	Therefore initialization is cheaper and cannot fail. It derives from
22  	entry_ref for convenience. However, care must be taken that:
23  	 - the name remains valid while the object is in use,
24  	 - the object isn't passed as an entry_ref to a function that modifies it.
25   */
26  class NotOwningEntryRef : public entry_ref {
27  public:
28  	NotOwningEntryRef()
29  	{
30  	}
31  
32  	NotOwningEntryRef(dev_t device, ino_t directory, const char* name)
33  	{
34  		SetTo(device, directory, name);
35  	}
36  
37  	NotOwningEntryRef(const node_ref& directoryRef, const char* name)
38  	{
39  		SetTo(directoryRef, name);
40  	}
41  
42  	NotOwningEntryRef(const entry_ref& other)
43  	{
44  		*this = other;
45  	}
46  
47  	~NotOwningEntryRef()
48  	{
49  		name = NULL;
50  	}
51  
52  	NotOwningEntryRef& SetTo(dev_t device, ino_t directory, const char* name)
53  	{
54  		this->device = device;
55  		this->directory = directory;
56  		this->name = const_cast<char*>(name);
57  		return *this;
58  	}
59  
60  	NotOwningEntryRef& SetTo(const node_ref& directoryRef, const char* name)
61  	{
62  		return SetTo(directoryRef.device, directoryRef.node, name);
63  	}
64  
65  	node_ref DirectoryNodeRef() const
66  	{
67  		return node_ref(device, directory);
68  	}
69  
70  	NotOwningEntryRef& SetDirectoryNodeRef(const node_ref& directoryRef)
71  	{
72  		device = directoryRef.device;
73  		directory = directoryRef.node;
74  		return *this;
75  	}
76  
77  	NotOwningEntryRef& operator=(const entry_ref& other)
78  	{
79  		return SetTo(other.device, other.directory, other.name);
80  	}
81  };
82  
83  
84  } // namespace BPrivate
85  
86  
87  using ::BPrivate::NotOwningEntryRef;
88  
89  
90  #endif	// _NOT_OWNING_ENTRY_REF_H
91