BaseObject.hpp 3.16 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/******************************************************************************
 *                      BaseObject.hpp
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  CERN
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *
 * Basic object support, including pointer to Services and log support
 *
 * @author Sebastien Ponce
 *****************************************************************************/

26
#pragma once
27
28

// Include Files
29
#include "castor/dlf/Dlf.hpp"
30
#include "castor/exception/Exception.hpp"
31
#include <pthread.h>
32
33
34
35
36
37
38
39
40
41

namespace castor {

  // Forward Declarations
  class Services;

  /**
   * Basic object support, including pointer to Services and log support
   */
  class BaseObject {
42

43
  public:
44
45
46
47
48

    /**
     * constructor
     */
    BaseObject() throw();
49

50
51
52
    /**
     * destructor
     */
53
    virtual ~BaseObject() throw() {};
54

55
    /**
56
     * Static access to the underlying thread-safe Services object
57
58
     */
    static Services* services() throw(castor::exception::Exception);
59

60
61
62
63
64
65
66
    /**
     * Static function to reset (deallocate + set the thread specific pointer
     * to NULL) the thread-specific services. Useful in test environments and
     * for clients which to not run the thread pools (but use the marshallers)
     */
    static void resetServices() throw(castor::exception::Exception);
    
67
68
69
70
    /**
     * Static access to the underlying thread-shared Services object
     */
    static Services* sharedServices() throw(castor::exception::Exception);
71

72
73
    /**
     * Non static access to the underlying Services object
74
     */
75
    Services* svcs() throw(castor::exception::Exception);
76

77
  protected:
78

79
80
81
82
    /**
     * The thread-shared services catalog
     */
    static Services* s_sharedServices;
83

84
  private:
85
    
86
    /**
87
     * A little class that will construct the pthread key at init time
88
     */
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    class pthreadKey {
    public:
      pthreadKey() throw (castor::exception::Exception)  {
        int rc = pthread_key_create(&m_key, NULL);
        if (rc != 0) {
          castor::exception::Exception e(rc);
          e.getMessage() << "Error caught in call to pthread_key_create (from castor::BaseObject::pthreadKey::pthreadKey";
          throw e;
        }
      }
      operator pthread_key_t () const {
        return m_key;
      }
    private:
      pthread_key_t m_key;
    };
105
106

    /**
107
     * The key to thread-specific storage for Services
108
     */
109
110
    static pthreadKey s_servicesKey;
    
111
112
  }; // end of class BaseObject

113
} // end of namespace castor
114