File: ExceptionImpl.h

package info (click to toggle)
libsmbios 2.0.3.dfsg-1.1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 3,768 kB
  • ctags: 2,016
  • sloc: cpp: 14,292; sh: 9,408; xml: 3,820; makefile: 454; ansic: 402; python: 157
file content (271 lines) | stat: -rw-r--r-- 8,897 bytes parent folder | download | duplicates (3)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=c:
/*
 * Copyright (C) 2005 Dell Inc.
 *  by Michael Brown <Michael_E_Brown@dell.com>
 * Licensed under the Open Software License version 2.1
 *
 * Alternatively, 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.
 */


#ifndef EXCEPTIONIMPL_H
#define EXCEPTIONIMPL_H



// compat header should always be first header
#include "smbios/compat.h"

#include <map>
#include <string>
#include <sstream>
#include <exception>

// types.h should be first user-defined header.
#include "smbios/types.h"
#include "smbios/IException.h"

// abi_prefix should be last header included before declarations
#include "smbios/config/abi_prefix.hpp"

#define DEFINE_EXCEPTION_EX( excName, ns, superclass )   \
    class excName : public smbios::Exception< ns :: superclass >  \
    {                                       \
    public:                                 \
        ~excName() throw() {};  \
        excName( const std::string initMessage ) : smbios::Exception< ns :: superclass >(initMessage) {} ;\
        excName( const excName &src ) : smbios::Exception< ns :: superclass >(src) {} ;\
        excName( ) : smbios::Exception< ns :: superclass >() {} ;\
    }

    /*commented out fragment above is valid code but hits a bug in MSVC++ and emits C2437 diagnostic erroneously*/

// Macro for convenient exception throwing
// (includes message, file, line #)
#define THROW(Type, txt) \
    throw Type ( std::string( __FILE__ ## ":Line " ## LIBSMBIOS_STRINGIZE(__LINE__)) + txt)

namespace smbios
{
    // TODO: all this needs to be hidden to preserve ABI.
    template <class S>
    class Exception : public S
    {
    public:
        // destructor
        virtual ~Exception() throw() {};
        // constructors
        Exception( const std::string initMessage ) : S(), messageStr( initMessage ), outputStr("") {};
        Exception( ) : S(), messageStr( "" ), outputStr("")  {};
        Exception( const Exception<S> &source );
        // overloaded assignment
        Exception<S> &operator =( const Exception<S> &source );

        virtual const char *what() const throw() ;
        virtual std::string getParameterString( const std::string &name ) const;
        virtual u32 getParameterNumber( const std::string &name ) const;

        virtual void setMessageString( const std::string &newMsgString );
        virtual void setParameter( const std::string &name, const std::string &value );
        virtual void setParameter( const std::string &name, const u32 value );
    private:
        std::string messageStr;
        mutable std::string outputStr;

        std::map< std::string, std::string > r_ptrStrMap;
        std::map< std::string, u32 > r_ptrNumMap;
    };

    // copy constructor
    template <class S>
    Exception<S>::Exception( const Exception<S> &source )
            : S(), messageStr( source.messageStr ), outputStr("")
    {
        // copy parameters over from source
        std::map< std::string, u32 >::const_iterator iter = source.r_ptrNumMap.begin();
        while ( iter != source.r_ptrNumMap.end() )
        {
            setParameter( iter->first, iter->second );
            ++iter;
        }

        std::map< std::string, std::string >::const_iterator iterStr = source.r_ptrStrMap.begin();
        while ( iterStr != source.r_ptrStrMap.end() )
        {
            setParameter( iterStr->first, iterStr->second );
            ++iterStr;
        }
    }

    // overloaded assignment
    template <class S>
    Exception<S> &Exception<S>::operator =( const Exception<S> &source )
    {
        if (this == &source)
            return *this;
        
        messageStr = source.messageStr;
        outputStr = "";

        // copy parameters over from source
        std::map< std::string, u32 >::const_iterator iter = source.r_ptrNumMap.begin();
        while ( iter != source.r_ptrNumMap.end() )
        {
            setParameter( iter->first, iter->second );
            ++iter;
        }

        std::map< std::string, std::string >::const_iterator iterStr = source.r_ptrStrMap.begin();
        while ( iterStr != source.r_ptrStrMap.end() )
        {
            setParameter( iterStr->first, iterStr->second );
            ++iterStr;
        }

        return *this;
    }

    template <class S>
    const char * Exception<S>::what() const throw()
    {
        outputStr = messageStr;

        size_t strLen = outputStr.length();
        size_t pos = 0;
        while(pos < strLen)
        {
            std::string varName = "";
            size_t replaceLen = 0;
            char varType;
            size_t endVar = 0;
            std::ostringstream rep;

            pos = outputStr.find( "%", pos );
            if( pos >= strLen )  // no more occurences
                break;

            // handle '%' as last character
            if( pos == strLen-1 )
                break;  // this is an error. maybe throw?

            // handle %%
            if( outputStr[pos+1] == '%' )
            {
                outputStr.replace(pos, 2, "%");
                goto next_pos;
            }

            if( outputStr[pos+1] != '(' )
            {
                // somebody lost their mind. Ignore them
                //   Only legal sequences with '%' are: "%%" and "%("
                goto next_pos;  // this is an error. maybe throw?
            }

            endVar = outputStr.find( ")", pos );
            if( endVar >= strLen )
            {
                // again with the crazy people.
                goto next_pos;  // this is an error. maybe throw?
            }

            // handle "%(xXx)" at end with no var type.
            if( endVar == strLen-1 )
                break;  // this is an error. maybe throw?

            varType = outputStr[endVar + 1];

            replaceLen = endVar - pos + 2;
            varName = outputStr.substr( pos+2, replaceLen - 4 );

            // limit vars to 32 chars (limit accidental runaway vars.)
            if( replaceLen > 32 )
                goto next_pos;

            switch( varType )
            {
            case 'i':
                rep <<  getParameterNumber(varName);
                outputStr.replace( pos, replaceLen, rep.str() );
                goto loop_end; // go back to start of while(), lets us recursively substitute
            case 's':
                outputStr.replace( pos, replaceLen, getParameterString(varName));
                goto loop_end; // go back to start of while(), lets us recursively substitute
            }

next_pos:
            ++pos;

loop_end:
            strLen = outputStr.length(); // in case string changed
        }

        return outputStr.c_str();
    }

    template <class S>
    void Exception<S>::setMessageString( const std::string &newStr )
    {
        messageStr = newStr;
    }


    template <class S>
    void Exception<S>::setParameter( const std::string &name, const std::string &value)
    {
        r_ptrStrMap[ name ] = value;
    }

    template <class S>
    void Exception<S>::setParameter( const std::string &name, const u32 value)
    {
        r_ptrNumMap[ name ] = value;
    }


    template <class S>
    u32 Exception<S>::getParameterNumber( const std::string &name ) const
    {
        std::map< std::string, u32 >::const_iterator iterStr = r_ptrNumMap.find(name);
        return iterStr->second;
    }

    template <class S>
    std::string Exception<S>::getParameterString( const std::string &name ) const
    {
        std::map< std::string, std::string >::const_iterator iterStr = r_ptrStrMap.find(name);
        return iterStr->second;
    }



    // some standard exceptions

    /** Raised when some class does not implement part of the public interface
      Used mainly in classes where there are optional parts of the interface
      defined that require extra external functionality, such as XML, for example.
      */
    DEFINE_EXCEPTION_EX( NotImplementedImpl, smbios, NotImplemented );

    /** Used in cases where something that "cannot happen" happens. Raised in
     * instances usually caused by some internal class state becoming
     * corrupted.
     */
    DEFINE_EXCEPTION_EX( InternalErrorImpl, smbios, InternalError );

    /** Used in cases where operating system privleges prevent an action. */
    DEFINE_EXCEPTION_EX( PermissionExceptionImpl, smbios, PermissionException );
}

// always should be last thing in header file
#include "smbios/config/abi_suffix.hpp"

#endif /* EXCEPTIONIMPL_H */