From ec6b9f59d40b2c389496f8e6af6bce64944af998 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 10 Aug 2010 11:16:42 +0200 Subject: Add query support --- odb/result.hxx | 188 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 odb/result.hxx (limited to 'odb/result.hxx') diff --git a/odb/result.hxx b/odb/result.hxx new file mode 100644 index 0000000..979988a --- /dev/null +++ b/odb/result.hxx @@ -0,0 +1,188 @@ +// file : odb/result.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_RESULT_HXX +#define ODB_RESULT_HXX + +#include // std::ptrdiff_t, std::size_t +#include // iterator categories + +#include +#include + +namespace odb +{ + template + class result; + + template + class result_impl; + + template + class result_iterator + { + public: + typedef typename object_traits::pointer_type value_type; + typedef value_type& reference; + typedef value_type* pointer; + typedef std::ptrdiff_t difference_type; + typedef std::input_iterator_tag iterator_category; + + public: + result_iterator () + : res_ (0) + { + } + + explicit + result_iterator (result_impl& res) + : res_ (&res) + { + } + + // Input iterator requirements. + // + public: + value_type + operator* () const + { + return res_->current (true); + } + + // Our value_type is already a pointer so return it instead of + // a pointer to it (operator-> will just have to go one deeper + // in the latter case). + // + value_type + operator-> () const + { + return res_->current (false); + } + + result_iterator& + operator++ () + { + res_->next (); + return *this; + } + + result_iterator + operator++ (int) + { + // All non-end iterators for a result object move together. + // + res_->next (); + return *this; + } + + bool + equal (const result_iterator& j) + { + return (res_ ? res_->current (false) : 0) == + (j.res_ ? j.res_->current (false) : 0); + } + + private: + result_impl* res_; + }; + + // Input iterator requirements. + // + template + inline bool + operator== (result_iterator i, result_iterator j) + { + return i.equal (j); + } + + template + inline bool + operator!= (result_iterator i, result_iterator j) + { + return !i.equal (j); + } + + // + // + template + class result_impl: public shared_base + { + public: + virtual + ~result_impl (); + + protected: + friend class result; + friend class result_iterator; + + virtual typename object_traits::pointer_type + current (bool release) = 0; + + virtual void + next () = 0; + }; + + template + class result + { + public: + typedef typename object_traits::pointer_type value_type; + typedef value_type* pointer; + typedef const value_type* const_pointer; + typedef value_type& reference; + typedef const value_type& const_reference; + + typedef result_iterator iterator; + + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + + public: + result (shared_ptr > impl) + : impl_ (impl) + { + } + + /* + result& + operator= (shared_ptr > impl) + { + impl_ = impl; + } + */ + + public: + iterator + begin () + { + return iterator (*impl_); + } + + iterator + end () + { + return iterator (); + } + + public: + operator shared_ptr > () + { + return impl_; + } + + // Copying or assignment of results is not supported. + // + private: + result (const result&); + result& operator= (const result&); + + private: + shared_ptr > impl_; + }; +} + +#include + +#endif // ODB_RESULT_HXX -- cgit v1.1