CARMA C++
WorkRequest.h
Go to the documentation of this file.
1 #ifndef CARMA_UTIL_WORKREQUEST_H
2 #define CARMA_UTIL_WORKREQUEST_H
3 
4 
7 
8 #include <algorithm>
9 #include <string>
10 
11 #include "carma/util/WorkResult.h"
12 #include "carma/util/PthreadMutex.h"
13 
14 
15 namespace carma {
16 namespace util {
17 
18 
20 class WorkRequest {
21  public:
22  class Impl;
23 
24  explicit WorkRequest( Impl * );
25 
27  WorkRequest( const WorkRequest & rhs );
28 
30  virtual ~WorkRequest( );
31 
33  WorkRequest & operator=( const WorkRequest & rhs );
34 
38  void swap( WorkRequest & rhs );
39 
46  bool operator<( const WorkRequest & rhs ) const;
47 
48  ::std::string getId( ) const;
49 
50  void service( );
51 
52  private:
53  Impl * impl_;
54 };
55 
56 
57 class WorkRequest::Impl {
58  friend class WorkRequest;
59 
60  public:
61  virtual ~Impl( );
62 
63  protected:
64  Impl( const ::std::string & id,
65  const WorkResult & workResult );
66 
67  ::std::string getId( ) const;
68 
69  private:
70  static Impl * addRef( Impl * impl );
71 
72  static void removeRef( const Impl * impl );
73 
74  void service( );
75 
76  virtual void serviceImpl( ) = 0;
77 
78  void postNormalResult( );
79 
80  void postAbnormalResult( const ::std::string & errorText );
81 
82  const ::std::string id_;
83 
84  mutable util::PthreadMutex refCountGuard_;
85  mutable ::size_t refCount_;
86 
87  WorkResult workResult_;
88  bool resultPosted_;
89 };
90 
91 
92 } // namespace carma::util
93 } // namespace carma
94 
95 
96 inline ::std::string
97 carma::util::WorkRequest::Impl::getId( ) const
98 {
99  // It's okay to use id_ because we know the object should still
100  // exist and the id_ member is well and truly immutable const
101 
102  return id_;
103 }
104 
105 
106 inline
107 carma::util::WorkRequest::WorkRequest( const WorkRequest & rhs ) :
108 impl_( Impl::addRef( rhs.impl_ ) )
109 {
110 }
111 
112 
113 inline void
115 {
116  ::std::swap( impl_, rhs.impl_ );
117 }
118 
119 
122 {
123  WorkRequest temp( rhs );
124 
125  swap( temp );
126 
127  return *this;
128 }
129 
130 
131 inline bool
133 {
134  return (impl_ < rhs.impl_);
135 }
136 
137 
138 inline ::std::string
139 carma::util::WorkRequest::getId( ) const
140 {
141  return impl_->getId();
142 }
143 
144 
145 #endif
void swap(WorkRequest &rhs)
Swap two instances.
Definition: WorkRequest.h:114
Abstract result of servicing a work request.
Definition: WorkResult.h:86
virtual ~WorkRequest()
Destruct an instance.
WorkRequest & operator=(const WorkRequest &rhs)
Assign an instance the value of another.
Definition: WorkRequest.h:121
Interface file for the carma::util::WorkResultSet and carma::util::WorkResult classes.
A simple wrapper class that makes use of ::pthread_mutex_t easier in a C++ world. ...
Definition: PthreadMutex.h:41
An abstract work request.
Definition: WorkRequest.h:20
bool operator<(const WorkRequest &rhs) const
Arbitrary total ordering of WorkRequest instances.
Definition: WorkRequest.h:132