Path classes and functions

The pni::nexus::Path class

class pni::nexus::Path

Nexus path class.

This class represents a full Nexus path. Such a path describes the position of an object within a Nexus file. In general the path has a form like this

filename://entry:NXentry/instrument:NXinstrument/detector/data@attrname

More specific this can be written as

[filename://]path

Usage

string path_str = .....;
nxpath path = nxpath::from_string(path_str);

A path can either be absolute or relative to a particular object. However, a path with a filename part is always absolute (for obvious reasons).

Public Types

using Element = std::pair<pni::string, pni::string>

object element (groupname:class)

using ElementList = std::list<Element>

a list of subsequent objects

using ElementIterator = ElementList::iterator

iterator over elements

using ConstElementIterator = ElementList::const_iterator

const iterator over elements

Public Functions

Path()

default constructor

Path(const fs::path &file, const ElementList &groups, const pni::string &attr)

constructor

Parameters
  • file: name of the file

  • groups: a list of elements for the object path

  • attr: the optional name of an attribute

Path(const hdf5::Path &path)

constructor

Construct a NeXus path from an HDF5 path. This constructor is deliberately non-explicit. As an HDF5 path does not contain any file or attribute information only the element section of the path will be set.

Parameters
  • path: reference to the original HDF5 path

operator hdf5::Path()

conversion to an HDF5 path

This conversion is only possible if the NeXus path is unique. Otherwise an exception will be thrown.

As an HDF5 path does not contain any information about the file or an attribute this conversion basically copies the names of all object links to the HDF5 path.

Return

new instance of hdf5::Path

Exceptions
  • std::runtime_error: in case of a failure

bool has_filename() const noexcept

true if has filename

Returns true if the path contains a file name.

Return

true if filename exists

bool has_attribute() const noexcept

true if has attribute

Returns true if the path contains an attribute name.

Return

true if path has attribute

fs::path filename() const noexcept

return the filename

void filename(const fs::path &file)

set the filename

std::string attribute() const noexcept

return the attribute name

void attribute(const std::string &attribute_name)

set the attribute name

void push_back(const Element &o)

append element

Append a new element to the end of the Nexus path.

Exceptions
  • value_error: if one tries to push_back a root group to a non-empty path

Parameters
  • o: element to append

void push_front(const Element &o)

prepend element

Append an element to the beginning of the Nexus path.

Parameters
  • o: element to append

void pop_front()

last element from path

void pop_back()

remove first element from path

Element front() const

get last element

Return the last element of the Nexus path.

Return

last element

Element back() const

get first element

Return the first element of the Nexus path

Return

first element

size_t size() const

return number of group entries

ElementIterator begin()

get iterator to first element

ElementIterator end()

get iterator to last+1 element

ConstElementIterator begin() const

get const iterator to first element

ConstElementIterator end() const

get const iterator to last+1 element

Public Static Functions

Path from_string(const std::string &input)

create path from string

This method creates a path from a string passed to it.

Return

path instance

Exceptions
Parameters
  • input: string with path representation

std::string to_string(const Path &p)

create string from path

Returns the string representation of a path.

Return

string representation of the path

Parameters
  • p: reference to path instance

Related

PNINEXUS_EXPORT std::istream &operator>>(std::istream &i, Path &p)

read path from an input stream

Constructing a nexus path from a stream. Analogously to the output operator this is used to read a path from a stream-able source.

nexus::Path p;
std::cin>>p;

Return

reference to the advanced original stream

Parameters
  • i: reference to the input stream

  • p: reference to the path

PNINEXUS_EXPORT bool operator==(const Path::Element &a, const Path::Element &b)

equality operator for path elements

Two path elements are considered equal if their name and, in the case of groups class, are equal.

Return

true if the elements are equal

Parameters
  • a: reference to the element on the left hand-side of the operator

  • b: reference to the element on the right hand-side of the operator

PNINEXUS_EXPORT bool operator!=(const Path::Element &a, const Path::Element &b)

inequality operator for path elements

Return

true if the elements are no equal

Parameters
  • a: reference to the element on the left hand-side of the operator

  • b: reference to the element on the right hand-side of the operator

PNINEXUS_EXPORT bool operator!=(const Path &lhs, const Path &rhs)

inequality operator for two path objects

Compares two path objects and returns true if they are not equal.

Return

true if not equal, false otherwise

Parameters
  • lhs: reference to the path on the left hand-side

  • rhs: reference to the path on the right hand-side

PNINEXUS_EXPORT std::ostream &operator<<(std::ostream &stream, const Path &p)

output operator for a nexus path

Prints a NeXus path to an output stream. One can either use this to write a Nexus path to standard out

nexus::Path p = ....;
std::cout<<p<<std::endl;

or to a string using the stringstream operator

std::stringstream ss;
ss<<p;

Return

reference to the output operator

Parameters
  • stream: reference to the output stream

  • p: reference to the path

NeXus path objects

pni::nexus::PathObject

class pni::nexus::PathObject

type erasure for path addressable objects

This is a very simple type erasure (though not implemented in the classic scheme) for all objects which can be addressed with a NeXus path. The current path implementation allows to address

  • groups

  • datasets

  • and attributes

while a common HDF5 path can only address the first two. Thus we need a special return type for all path operations when dereferencing objects in an HDF5 tree.

Public Functions

PathObject()

default constructor

We need default construction for container types. When a PathObject is default constructed its type is Type::None. In this case no conversion to any other object could be achieved.

PathObject(const hdf5::attribute::Attribute &attribute)

constructor

Build a PathObject from an attribute. In this case type() will return Type::Attribute.

Parameters
  • attribute: reference to the original attribute

PathObject(const hdf5::node::Node &dataset)

constructor

Build a PathObject from a dataset. After construction type() will return Type::Dataset.

Parameters
  • dataset: reference to the original dataset

PathObject(const hdf5::node::Link &link)

constructor

Build a PathObject from a link.

PathObject(const PathObject&) = default

copy constructor

We use the compiler provided default implementation here.

Type type() const noexcept

return type of the currently stored object

Return the type of the currently stored object. If the object was default constructed NONE is returned.

operator hdf5::attribute::Attribute() const

provides implicit conversion to an attribute

Returns the stored object as an HDF5 attribute. If the stored instance is not an HDF5 attribute an exception will be thrown.

Pre

the object stored in the PathObject instance is an instance of hdf5::attribute::Attribute

Exceptions
  • std::runtime_error: in case of a failure

operator hdf5::node::Group() const

implicit conversion to a group

Pre

the object stored in the PathObject instance is an instance of hdf5::node::Group

Exceptions
  • std::runtime_error: in case of a failure

operator hdf5::node::Dataset() const

implicit conversion to a dataset

Pre

the object stored in PathObject instance is an instance of hdf5::node::Dataset.

Exceptions
  • std::runtime_error: in case of a failure

operator hdf5::node::Node() const

implicit conversion to a general HDF5 node type

Provides implicit conversion to an HDF5 node instance. If the object stored is neither a group or a dataset an exception will be thrown.

Pre

the object stored in the PathObject instance is an instance of hdf5::node::Dataset of hdf5::node::Group.

Exceptions
  • std::runtime_error: in case of a failure

pni::nexus::nexus::PathObjectList

class pni::nexus::PathObjectList : public std::list<PathObject>

container for PathObject

Used in algorithms returning several instances of PathObject. Can be used along with STL algorithms to filter particular types.

Public Functions

operator NodeList() const

implicit conversion to a NodeList

Pre

all instances in the PathObjectList must store instances of datasets or groups.

Exceptions
  • std::runtime_error: in case of a failure

operator AttributeList() const

implicit conversion to an AttributeList

Pre

all instances in the PathObjectList must store attributes

Exceptions
  • std::runtime_error: in case of a failure

operator GroupList() const

implicit conversion to a GroupList

Pre

all instances in the PathObjectList must store groups

Exceptions
  • std::runtime_error: in case of a failure

operator DatasetList() const

implicit conversion to a DatasetList

Pre

all instances in the PathObjetList must store datasets

Exceptions
  • std::runtime_error: in case of a failure

Related functions

PNINEXUS_EXPORT bool pni::nexus::is_dataset(const PathObject &object) noexcept

return true if object stores a dataset

PNINEXUS_EXPORT bool pni::nexus::is_group(const PathObject &object) noexcept

return true if object stores a group

PNINEXUS_EXPORT bool pni::nexus::is_attribute(const PathObject &object) noexcept

return true if object stores an attribute

Searching with Paths

PNINEXUS_EXPORT Path pni::nexus::get_path(const hdf5::attribute::Attribute &attribute)

get the NeXus path for an HDF5 attribute

Pre

the attribute must be a valid HDF5 object

Parameters
  • attribute: reference to the attribute for which to obtain the path

PNINEXUS_EXPORT Path pni::nexus::get_path(const hdf5::node::Node &node)

get the NeXus path for an HDF5 node

Pre

the node must be a valid HDF5 object

Parameters
  • node: reference to the HDF5 node

PNINEXUS_EXPORT PathObjectList pni::nexus::get_objects(const hdf5::node::Group &base, const Path &path)

search for objects

Searches recursively below base and return a list of path objects which match path. The function distinguishes two situations

  • if path is a relative path the paths of all child objects of base are taken relative to base

  • if path is an absolute path the full path of each child must match.

There are some important notes when it comes to dealing with links. Under the hood get_objects iterates over links rather than nodes. This allows the function to handle unresolvable links. If a particular link is resolvable it will be converted into the appropriate node type. In the case of an unresolvable link the link itself will be stored.

Return

a list of path objects

Exceptions
  • std::runtime_error: in case of a failure

Parameters
  • base: the base group from which to start the search

  • path: the path which to match