Boost Klassenbibliothek
Utility
boost::noncopyable // Base class for all objects without copy constructor and assignment operator
#include <boost/utility.hpp>
class MyClass : private boost::noncopyable { };
Smart_ptr
boost::scoped_ptr<T> p (new T); // Like noncopyable std::auto_ptr, delete T when p leaves scope
boost::shared_ptr<T> p (new T); // Reference counting: delete T if there are no more references (useful for container values)
boost::intrusive_ptr<T> p (new T); //
boost::weak_ptr<T> p (new T); //
#include <boost/smart_ptr.hpp>
void foo() {
boost::scoped_ptr<MyClass> ptr(new MyClass);
// No delete required.
}
boost::shared_ptr<MyClass> mc (new MyClass);
std::vector<boost::shared_ptr<MyClass> > v;
v.push_back (new MyClass);
Conversion
T t = boost::polymorphic_cast<T>(x) // Like std::dynamic_cast, but throws bad_cast exception for pointers, too
T t = boost::numeric_cast<T>(x); // Convert numbers throws bad_cast exception if x exceed range of T
T t = boost::lexical_cast<T>(x); // Convert any streamable class
Operators
Operator Implementiert Erzeugt
boost::dereferencable<T> operator* operator->
boost::indexable<T> operator+ operator[]
boost::totally_ordered<T>
boost::less_than_comparable<T> operator< >, <=, >=
boost::equality_comparable<T> operator== operator!=
boost::arithmetic<T>
boost::additive<T>
boost::addable<T> operator+= operator+
boost::substractable<T> operator-= operator-
boost::multiplicative<T>
boost::multipliable<T> operator*= operator*
boost::modable<T> operator%= operator%
boost::bitwise<T>
boost::andable<T> operator&= operator&
boost::orable<T> operator!= operator|
boost::xorable<T> operator^= operator^
boost::unit_steppable<T>
boost::incrementable<T> operator++ operator++(int)
boost::decrementable<T> operator-- operator--(int)
boost::shiftable<T>
boost::left_shiftable<T> operator<<= operator<<
boost::right_shiftable<T> operator>>= operator>>
boost::ring_operators<T>
boost::ordered_ring_operators<T>
boost::field_operators<T>
boost::ordered_field_operators<T>
boost::euclidean_ring_operators<T>
boost::ordered_euclidean_ring_operators<T>
#include <boost/operators.hpp>
class MyClass : public boost::totally_ordered<MyClass> { // Barton-Nackman trick aka CRTP
public:
bool operator<(const MyClass & m);
bool operator==(const MyClass & m);
};
Signals
boost::signal
<
Signature, Signatur der aufzurufenden Methode
Combiner, Zusammenfassung der Rückgabewerte (last_value)
Group, Typ für Gruppierung (int)
GroupCompare, Vergleichsmethode für Gruppierung (std::less<int>)
SlotFunction FIXME
>
#include <boost/signals.hpp>
bool my_slot(int);
...
boost::signal<bool (int)> sig;
sig.connect(my_slot);
sig();
Mutex
#include <boost/thread/mutex.hpp>
class MyClass : private boost::mutex {
void foo() {
boost::mutex::scoped_lock scoped_lock(*this);
...
}
};
Tuple
#include <boost/tuple/tuple.hpp>
boost::tuple::tuple<int, std::string, MyClass> t(42, "Hallo, Welt!");
t.get<0>(); // first
t.get<1>(); // second
t.get<2>(); // third
Filesystem
#include <boost/filesystem/path.hpp>
using namespace boost::filesystem fs;
class fs::path {
public:
typedef iterator implementation-defined
typedef bool (*name_check) (const std::string & name);
path ();
path (const std::string & src);
path (const char * src);
path (const std::string & src, name_check checker);
path (const char * src, name_check checker);
path & operator/= (const path & rhs);
path operator/ (const path & rhs) const;
const std::string & string () const;
std::string native_file_string () const;
std::string native_directory_string () const;
path & normalize ();
path root_path () const;
std::string root_name () const;
std::string root_directory () const;
path relative_path () const;
std::string leaf () const;
path branch_path () const;
bool empty () const;
bool is_complete () const;
bool has_root_path () const;
bool has_root_name () const;
bool has_root_directory () const;
bool has_relative_path () const;
bool has_leaf () const;
bool has_branch_path () const;
iterator begin () const;
iterator end () const;
static bool default_name_check_writable ();
static void default_name_check (name_check new_check);
static name_check default_name_check ();
bool operator== (const path & that) const;
bool operator!= (const path & that) const;
bool operator< (const path & that) const;
bool operator<= (const path & that) const;
bool operator> (const path & that) const;
bool operator>= (const path & that) const;
private:
std::vector<std::string> m_name; for exposition only
};
Operationen auf dem Dateisystem.
#include <boost/filesystem/operations.hpp>
bool exists (const path & ph);
bool symbolic_link_exists (const path & ph);
bool is_directory (const path & ph);
bool is_empty (const path & ph);
bool equivalent (const path & ph1, const path & ph2);
boost::intmax_t file_size (const path & ph);
std::time_t last_write_time (const path & ph);
void last_write_time (const path & ph, std::time_t new_time);
bool create_directory (const path & directory_ph);
bool remove (const path & ph);
unsigned long remove_all (const path & ph);
void rename (const path & from_path, const path & to_path);
void copy_file (const path & source_file, const path & target_file);
const path & initial_path ();
path current_path ();
path complete (const path & ph, const path & base = initial_path());
path system_complete (const path & ph);
Literatur
Björn Karlsson: Beyond the C++ Standard Library -- An introduction to Boost, Addison Wesley