~josejuan-sanchez/esajpip/continue

« back to all changes in this revision

Viewing changes to src/ipc/ipc_object.h

  • Committer: José Juan Sánchez Hernández
  • Date: 2013-10-01 10:01:21 UTC
  • Revision ID: josejuan.sanchez@gmail.com-20131001100121-xfobvkenqie7y0te
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef _IPC_OBJECT_H_
 
2
#define _IPC_OBJECT_H_
 
3
 
 
4
 
 
5
#ifndef _USE_BOOST
 
6
        #include <tr1/memory>
 
7
#else
 
8
        #include <boost/tr1/memory.hpp>
 
9
#endif
 
10
 
 
11
 
 
12
namespace ipc
 
13
{
 
14
 
 
15
  /**
 
16
   * Enumeration of the possible values returned
 
17
   * when a wait operation is performed for an IPC
 
18
   * object.
 
19
   */
 
20
  enum WaitResult
 
21
  {
 
22
    WAIT_OBJECT = 0,    ///< Wait successful (object got)
 
23
    WAIT_TIMEOUT,               ///< Time out
 
24
    WAIT_ERROR                  ///< Error
 
25
  };
 
26
 
 
27
 
 
28
  /**
 
29
   * Class base of all the IPC classes that has the basic
 
30
   * operations (<code>Init</code>, <code>Wait</code> and
 
31
   * <code>Dispose</code>) to be overloaded. It has also
 
32
   * an internal boolean value to set the object status.
 
33
   *
 
34
   * For the IPC objects the Windows IPC philosophy has
 
35
   * been adopted because of its simplicity and flexibility.
 
36
   * Under this philosophy, the main operation that can be
 
37
   * performed to an IPC object is <code>Wait</code>, to wait
 
38
   * for getting the control of the object. Depending on
 
39
   * the type of the IPC object (mutex, event, etc.), the
 
40
   * meaning of "getting" the control of the object can be
 
41
   * different.
 
42
   */
 
43
  class IPCObject
 
44
  {
 
45
  private:
 
46
        /**
 
47
         * Internal status of the object. As it is a private
 
48
         * member, the derived classes must use the methods
 
49
         * <code>Init</code> and <code>Dispose</code> to set
 
50
         * the value of this status.
 
51
         */
 
52
    bool valid;
 
53
 
 
54
  public:
 
55
    /**
 
56
     * Pointer to an IPC object.
 
57
     */
 
58
    typedef std::tr1::shared_ptr<IPCObject> Ptr;
 
59
 
 
60
    /**
 
61
     * Initializes the internal status to <code>false</code>.
 
62
     */
 
63
    IPCObject()
 
64
    {
 
65
      valid = false;
 
66
    }
 
67
 
 
68
    /**
 
69
     * Sets the internal status to <code>true</code>
 
70
     * @return <code>true</code> if successful. If it
 
71
     * is not overloaded, it always returns <code>true
 
72
     * </code>.
 
73
     */
 
74
    virtual bool Init()
 
75
    {
 
76
      valid = true;
 
77
      return true;
 
78
    }
 
79
 
 
80
    /**
 
81
     * Performs a wait operation with the object to get it.
 
82
     * @param time_out Time out (infinite by default).
 
83
     * @return <code>WAIT_OBJECT</code> if successful,
 
84
     * <code>WAIT_TIMEOUT</code> if time out or <code>
 
85
     * WAIT_ERROR</code> is error. If it is not overloaded,
 
86
     * it always returns <code>WAIT_ERROR</code>.
 
87
     */
 
88
    virtual WaitResult Wait(int time_out = -1)
 
89
    {
 
90
      return WAIT_ERROR;
 
91
    }
 
92
 
 
93
    /**
 
94
     * Returns <code>true</code> if the object is valid,
 
95
     * that is, the internal status value is <code>true</code>.
 
96
     */
 
97
    bool IsValid()
 
98
    {
 
99
      return valid;
 
100
    }
 
101
 
 
102
    /**
 
103
     * Release the resources associated to the IPC object and
 
104
     * sets the internal status to <code>false</code>.
 
105
     * @return <code>true</code> if successful. If it is not
 
106
     * overloaded, it always returns <code>true</code>.
 
107
     */
 
108
    virtual bool Dispose()
 
109
    {
 
110
      valid = false;
 
111
      return true;
 
112
    }
 
113
 
 
114
    /**
 
115
     * The desctructor calls the method <code>Dispose</code>.
 
116
     */
 
117
    virtual ~IPCObject()
 
118
    {
 
119
      Dispose();
 
120
    }
 
121
  };
 
122
 
 
123
}
 
124
 
 
125
 
 
126
#endif /* _IPC_OBJECT_H_ */