powerd++  0.4.4
Public Member Functions | Private Attributes | List of all members
sys::ctl::Sysctl< MibDepth > Class Template Reference

Represents a sysctl MIB address. More...

#include <sysctl.hpp>

Public Member Functions

template<typename... Tail>
constexpr Sysctl (mib_t const head, Tail const ... tail) noexcept
 Initialise the MIB address directly. More...
 
size_t size () const
 The size of the sysctl. More...
 
void get (void *const buf, size_t const bufsize) const
 Update the given buffer with a value retrieved from the sysctl. More...
 
template<typename T >
void get (T &value) const
 Update the given value with a value retreived from the sysctl. More...
 
template<typename T >
std::unique_ptr< T[]> get () const
 Retrieve an array from the sysctl address. More...
 
void set (void const *const buf, size_t const bufsize)
 Update the the sysctl value with the given buffer. More...
 
template<typename T >
void set (T const &value)
 Update the the sysctl value with the given value. More...
 

Private Attributes

mib_t mib [MibDepth]
 Stores the MIB address.
 

Detailed Description

template<size_t MibDepth>
class sys::ctl::Sysctl< MibDepth >

Represents a sysctl MIB address.

It offers set() and get() methods to access these sysctls.

There are two ways of initialising a Sysctl instance, by symbolic name or by directly using the MIB address. The latter one only makes sense for sysctls with a fixed address, known at compile time, e.g. Sysctl<2>{CTL_HW, HW_NCPU} for "hw.ncpu". Check /usr/include/sys/sysctl.h for predefined MIBs.

For all other sysctls, symbolic names must be used. E.g. Sysctl<0>{"dev.cpu.0.freq"}. Creating a Sysctl from a symbolic name may throw.

Suitable deduction guides usually allow omitting the template arguments, i.e. Sysctl{CTL_HW, HW_NCPU} and Sysctl{"dev.cpu.0.freq"} implicitly use the correct template argument.

Template Parameters
MibDepthThe MIB level, e.g. "hw.ncpu" is two levels deep

Constructor & Destructor Documentation

◆ Sysctl()

template<size_t MibDepth>
template<typename... Tail>
constexpr sys::ctl::Sysctl< MibDepth >::Sysctl ( mib_t const  head,
Tail const ...  tail 
)
inlineconstexprnoexcept

Initialise the MIB address directly.

Some important sysctl values have a fixed address that can be initialised at compile time with a noexcept guarantee.

Spliting the MIB address into head and tail makes sure that Sysctl(char *) does not match the template and is instead implicitly cast to invoke Sysctl(char const *).

Template Parameters
TailThe types of the trailing MIB address values (must be mib_t)
Parameters
head,tailThe mib

Member Function Documentation

◆ get() [1/3]

template<size_t MibDepth>
template<typename T >
std::unique_ptr<T[]> sys::ctl::Sysctl< MibDepth >::get ( ) const
inline

Retrieve an array from the sysctl address.

This is useful to retrieve variable length sysctls, like characer strings.

Template Parameters
TThe type stored in the array
Returns
And array of T with the right length to store the whole sysctl value
Exceptions
sys::sc_error<error>May throw if the size of the sysctl increases after the length was queried

◆ get() [2/3]

template<size_t MibDepth>
template<typename T >
void sys::ctl::Sysctl< MibDepth >::get ( T &  value) const
inline

Update the given value with a value retreived from the sysctl.

Template Parameters
TThe type store the sysctl value in
Parameters
valueA reference to the target value
Exceptions
sys::sc_error<error>Throws if value retrieval fails or is incomplete, e.g. because the value does not fit into the target type

◆ get() [3/3]

template<size_t MibDepth>
void sys::ctl::Sysctl< MibDepth >::get ( void *const  buf,
size_t const  bufsize 
) const
inline

Update the given buffer with a value retrieved from the sysctl.

Parameters
buf,bufsizeThe target buffer and its size
Exceptions
sys::sc_error<error>Throws if value retrieval fails or is incomplete, e.g. because the value does not fit into the target buffer

◆ set() [1/2]

template<size_t MibDepth>
template<typename T >
void sys::ctl::Sysctl< MibDepth >::set ( T const &  value)
inline

Update the the sysctl value with the given value.

Template Parameters
TThe value type
Parameters
valueThe value to set the sysctl to

◆ set() [2/2]

template<size_t MibDepth>
void sys::ctl::Sysctl< MibDepth >::set ( void const *const  buf,
size_t const  bufsize 
)
inline

Update the the sysctl value with the given buffer.

Parameters
buf,bufsizeThe source buffer
Exceptions
sys::sc_error<error>If the source buffer cannot be stored in the sysctl

◆ size()

template<size_t MibDepth>
size_t sys::ctl::Sysctl< MibDepth >::size ( ) const
inline

The size of the sysctl.

Returns
The size in characters

The documentation for this class was generated from the following file: