Param.hpp 5.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/******************************************************************************
 *
 * 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.
 *
 *
 * A parameter for the DLF (Distributed Logging System)
 *
22
 * @author Castor Dev team, castor-dev@cern.ch
23
24
 *****************************************************************************/

25
#pragma once
26

27
28
#include "castor/dlf/IPAddress.hpp"
#include "castor/dlf/TimeStamp.hpp"
Steven Murray's avatar
Steven Murray committed
29
30
#include "castor/IObject.hpp"

31
#include <shift/dlf_api.h>
32
33
#include <string.h>
#include <stdlib.h>
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

namespace castor {

  namespace dlf {

    /**
     * A parameter for the DLF.
     */
    class Param {

    public:

      /**
       * Constructor for strings
       */
49
      Param(const char* name, std::string value) :
50
        m_deallocate(true) {
51
        m_cParam.name = (char*) name;
52
53
54
55
56
        m_cParam.type = DLF_MSG_PARAM_STR;
        if (!strcmp(name, "TPVID")) {
          m_cParam.type = DLF_MSG_PARAM_TPVID;
        }
        m_cParam.value.par_string = strdup(value.c_str());
57
58
59
60
61
      };

      /**
       * Constructor for C strings
       */
62
      Param(const char* name, const char* value) :
63
        m_deallocate(true) {
64
        m_cParam.name = (char*) name;
65
66
67
68
        m_cParam.type = DLF_MSG_PARAM_STR;
        if (!strcmp(name, "TPVID")) {
          m_cParam.type = DLF_MSG_PARAM_TPVID;
        }
69
        if (0 != value) {
70
          m_cParam.value.par_string = strdup(value);
71
        } else {
72
          m_cParam.value.par_string = 0;
73
        }
74
75
76
77
78
      };

      /**
       * Constructor for uuids
       */
79
      Param(const char* name, Cuuid_t value) :
80
        m_deallocate(false) {
81
        m_cParam.name = (char*) name;
82
        m_cParam.type = DLF_MSG_PARAM_UUID;
83
        m_cParam.value.par_uuid = value;
84
85
      };

86
87
88
89
90
91
92
      /**
       * Constructor for SubRequest uuids
       */
      Param(Cuuid_t value) :
        m_deallocate(false) {
        m_cParam.name = NULL;
        m_cParam.type = DLF_MSG_PARAM_UUID;
93
94
        m_cParam.value.par_uuid = value;
      };
95

96
97
98
      /**
       * Constructor for int
       */
99
      Param(const char* name, const long int value) :
100
        m_deallocate(false) {
101
        m_cParam.name = (char*) name;
102
        m_cParam.type = DLF_MSG_PARAM_INT;
103
        m_cParam.value.par_int = value;
104
105
106
107
108
      };

      /**
       * Constructor for int
       */
109
      Param(const char* name, const long unsigned int value) :
110
        m_deallocate(false) {
111
        m_cParam.name = (char*) name;
112
#if defined __x86_64__
113
114
115
        m_cParam.type = DLF_MSG_PARAM_INT64;
        m_cParam.value.par_u64 = value;
#else
116
        m_cParam.type = DLF_MSG_PARAM_INT;
117
        m_cParam.value.par_int = value;
118
#endif
119
120
121
122
123
      };

      /**
       * Constructor for int
       */
124
      Param(const char* name, const int value) :
125
        m_deallocate(false) {
126
        m_cParam.name = (char*) name;
127
        m_cParam.type = DLF_MSG_PARAM_INT;
128
        m_cParam.value.par_int = value;
129
130
131
132
133
      };

      /**
       * Constructor for int
       */
134
      Param(const char* name, const unsigned int value) :
135
        m_deallocate(false) {
136
        m_cParam.name = (char*) name;
137
        m_cParam.type = DLF_MSG_PARAM_INT;
138
        m_cParam.value.par_int = value;
139
140
141
142
143
      };

      /**
       * Constructor for u_signed64
       */
144
      Param(const char* name, u_signed64 value) :
145
        m_deallocate(false) {
146
        m_cParam.name = (char*) name;
147
        m_cParam.type = DLF_MSG_PARAM_INT64;
148
        m_cParam.value.par_u64 = value;
149
150
151
152
153
      };

      /**
       * Constructor for floats
       */
154
      Param(const char* name, float value) :
155
        m_deallocate(false) {
156
        m_cParam.name = (char*) name;
157
        m_cParam.type = DLF_MSG_PARAM_DOUBLE;
158
        m_cParam.value.par_double = value;
159
160
161
162
163
      };

      /**
       * Constructor for doubles
       */
164
      Param(const char* name, double value) :
165
        m_deallocate(false) {
166
        m_cParam.name = (char*) name;
167
        m_cParam.type = DLF_MSG_PARAM_DOUBLE;
168
        m_cParam.value.par_double = value;
169
170
      };

171
172
173
174
175
176
177
178
179
180
      /**
       * Constructor for Raw parameters
       */
      Param(const char* rawParams) :
        m_deallocate(true) {
        m_cParam.name = (char*)"";
        m_cParam.type = DLF_MSG_PARAM_RAW;
        m_cParam.value.par_string = strdup(rawParams);
      };

181
182
183
      /**
       * Constructor for IPAddress
       */
184
      Param(const char* name, castor::dlf::IPAddress value);
185
186
187
188

      /**
       * Constructor for TimeStamp
       */
189
      Param(const char* name, castor::dlf::TimeStamp value);
190

191
192
193
      /**
       * Constructor for objects
       */
194
      Param(const char* name, castor::IObject* value);
195
196
197
198
199

      /**
       *
       */
      ~Param() {
200
201
        if (m_deallocate && 0 != m_cParam.value.par_string) {
          free(m_cParam.value.par_string);
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
        }
      };

    public:

      /**
       * Gets the corresponding C parameter for the
       * DLF C interface
       */
      dlf_write_param_t cParam() {
        return m_cParam;
      }

    private:

      /// the parameter, in a C structure
      dlf_write_param_t m_cParam;

      /// Whether the param value should be deallocated
      bool m_deallocate;

    };

  } // end of namespace dlf

} // end of namespace castor