File: totemNPVariantWrapper.h

package info (click to toggle)
totem 2.30.2-6
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 19,672 kB
  • ctags: 6,155
  • sloc: ansic: 36,919; xml: 23,362; sh: 10,556; cpp: 6,230; python: 2,529; makefile: 1,832
file content (113 lines) | stat: -rw-r--r-- 4,870 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright © 2008 Christian Persch
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifndef __TOTEM_NPVARIANT_WRAPPER_H__
#define __TOTEM_NPVARIANT_WRAPPER_H__

#include <assert.h>
#include <string.h>

#include "npapi.h"
#include "npruntime.h"

class totemNPVariantWrapper {

  public:

    totemNPVariantWrapper ()                  : mOwned (false) { VOID_TO_NPVARIANT (mVariant); }
    totemNPVariantWrapper (bool aValue)       : mOwned (false) { BOOLEAN_TO_NPVARIANT (aValue, mVariant); }
    totemNPVariantWrapper (uint32_t aValue)   : mOwned (false) { INT32_TO_NPVARIANT (aValue, mVariant);   }
    totemNPVariantWrapper (double aValue)     : mOwned (false) { DOUBLE_TO_NPVARIANT (aValue, mVariant);  }
    totemNPVariantWrapper (char *aValue)      : mOwned (false) { STRINGZ_TO_NPVARIANT (aValue, mVariant); }
//     totemNPVariantWrapper (NPString *aValue)  : mOwned (false) { STRINGN_TO_NPVARIANT (aValue, mVariant); }
    totemNPVariantWrapper (NPObject *aObject) : mOwned (false) { OBJECT_TO_NPVARIANT (aObject, mVariant); }

    totemNPVariantWrapper (const totemNPVariantWrapper& aOther) : mVariant (aOther.mVariant), mOwned (false) { }

    ~totemNPVariantWrapper () { Clear (); }

    bool IsVoid    () const { return NPVARIANT_IS_VOID (mVariant);    }
    bool IsNull    () const { return NPVARIANT_IS_NULL (mVariant);    }
    bool IsBoolean () const { return NPVARIANT_IS_BOOLEAN (mVariant); }
    bool IsInt32   () const { return NPVARIANT_IS_INT32 (mVariant);   }
    bool IsDouble  () const { return NPVARIANT_IS_DOUBLE (mVariant);  }
    bool IsString  () const { return NPVARIANT_IS_STRING (mVariant);  }
    bool IsObject  () const { return NPVARIANT_IS_OBJECT (mVariant);  }

    bool      GetBoolean  () const { return NPVARIANT_TO_BOOLEAN (mVariant); }
    uint32_t  GetInt32    () const { return NPVARIANT_TO_INT32 (mVariant);   }
    double    GetDouble   () const { return NPVARIANT_TO_DOUBLE (mVariant);  }
    char *    GetString   () const { return (char *) NPVARIANT_TO_STRING (mVariant).UTF8Characters;  }
    uint32_t  GetStringLen() const { return NPVARIANT_TO_STRING (mVariant).UTF8Length; }
    NPString  GetNPString () const { return NPVARIANT_TO_STRING (mVariant);  }
    NPObject* GetObject   () const { return NPVARIANT_TO_OBJECT (mVariant);  }

    void SetVoid    ()                  { Clear (); VOID_TO_NPVARIANT (mVariant);            }
    void SetNull    ()                  { Clear (); NULL_TO_NPVARIANT (mVariant);            }
    void SetBoolean (bool aValue)       { Clear (); BOOLEAN_TO_NPVARIANT (aValue, mVariant); }
    void SetInt32   (uint32_t aValue)   { Clear (); INT32_TO_NPVARIANT (aValue, mVariant);   }
    void SetDouble  (double aValue)     { Clear (); DOUBLE_TO_NPVARIANT (aValue, mVariant);  }
    void SetString  (char *aValue)      { Clear (); STRINGZ_TO_NPVARIANT (aValue, mVariant); }
//     void SetString  (NPString *aValue)  { Clear (); STRINGN_TO_NPVARIANT (aValue, mVariant); }
    void SetObject  (NPObject *aObject) { Clear (); OBJECT_TO_NPVARIANT (aObject, mVariant); }

    operator char*     () { return GetString   (); }
    operator NPString  () { return GetNPString (); }
    operator NPObject* () { return GetObject   (); }

    operator NPVariant*() { return &mVariant; }
    
    class GetterCopies {
      public:
       explicit GetterCopies (totemNPVariantWrapper& aTarget) : mTarget (aTarget) { }
        ~GetterCopies () { }

       operator NPVariant*() { return mTarget.StartAssignment (); }

      private:
        totemNPVariantWrapper& mTarget;
    };

  private:

    totemNPVariantWrapper& operator= (const totemNPVariantWrapper&); // not implemented

    void Clear () {
      if (mOwned) {
        NPN_ReleaseVariantValue (&mVariant);
        mOwned = false;
      } else {
        VOID_TO_NPVARIANT (mVariant);
      }
    }

    NPVariant* StartAssignment () { Clear (); mOwned = true; return &mVariant; }

  protected:
    NPVariant mVariant;
    bool mOwned;
};

inline totemNPVariantWrapper::GetterCopies
getter_Copies (totemNPVariantWrapper &aTarget)
{
  return totemNPVariantWrapper::GetterCopies (aTarget);
}

#endif /* __TOTEM_NPVARIANT_WRAPPER_H__ */