web idl 接口定义语言数据类型与 C++绑定关系(转)

转自:http://blog.csdn.net/perfectpdl/article/details/7098408

Modules

Every IDL module corresponds to a C++ namespace. The name of that namespace is based on module's prefixed name.

IDL

 
  1. module dom {  
  2.  };  

 

C++

 
  1. namespace org {  
  2.    namespace w3c {  
  3.      namespace dom {  

 

IDL

 
  1. [Prefix=::com]  
  2.  module getfirebug {  

C++

 
  1. namespace com {  
  2.    namespace getfirebug {  

Note: Historically each W3C specification introduced its own module name. However, the recent specifications tend to omit the module specification in the IDL definition. At least it seems one module name per spec doesn't make sense any more.

cf. http://lists.w3.org/Archives/Public/public-webapps/2009AprJun/1380.html

Primitive types

 

IDL C++
boolean bool
byte signed char
octet unsigned char
short short
unsigned short unsigned short
long int
unsigned long unsigned int
long long long long
unsigned long long unsigned long long
float float
double double

 

Any

The any type is the union of all other possible types.

IDL

  any

C++

 
  1. class Any {  
  2.   public:  
  3.    Any();  
  4.    Any(const Any& value);  
  5.    template <typename T>  
  6.    Any(const T& x);  
  7.    Any& operator=(const Any& x);  
  8.    template<typename T>  
  9.    Any& operator=(const T& x);  
  10.    template<typename T>  
  11.    operator T() const;  
  12.    // snip  
  13.  };  

Example

IDL

  1. interface CanvasRenderingContext2D {  
  2.    attribute any fillStyle;  
  3.  };  

 
C++
 
  1. class CanvasRenderingContext2D : public Object {  
  2.  public:  
  3.    Any getFillStyle();  
  4.    void setFillStyle(Any fillStyle);  
  5.  };  
  6.   
  7.  canvasRenderingContext2D->setFillStyle("black");  

DOMString

The DOMString type corresponds to the set of all possible sequences of 16 bit unsigned integer code units to be interpreted as UTF-16 encoded strings.

 

IDL C++
DOMString std::u16string

 

Date

The Date type corresponds to a 64 bit unsigned integer value. In C++ DOM API, the DOMTimeStamp type is defined as Date following Chrome's implementation as specified in DOM Level3 Core (*).

 

IDL C++
Date unsigned long long

 

(*) It appears currently Chrome is the only browser that treats the DOMTimeStamp type as the Date object in ECMAScript.

Object

The object type corresponds to the set of all possible object references, plus the special value null, which indicates no object reference.

 

IDL C++
object Object
null 0

 

Interfaces

An interface is a specification of a set of interface members, which are the constants, attributes and operations.

IDL

 
  1. interface CanvasRenderingContext2D {  
  2.  };  

C++

 
  1. class CanvasRenderingContext2D : public Object {  
  2.  };  

Nullable types - T?

A nullable type is an IDL type that can represent an existing type (called the inner type) values, plus an additional value null.

 

IDL C++
inner-type? Nullable<inner-type>

 

C++

 
  1. template <typename T>  
  2.  class Nullable {  
  3.   public:  
  4.    bool hasValue() const;  
  5.    T value() const;  
  6.    Nullable();  
  7.    Nullable(const T& value);  
  8.    Nullable(const Nullable<T>& nullable);  
  9.    Nullable(const Any& any);  
  10.    // snip  
  11.  };  

Example

IDL

 
  1. attribute DOMString? background;  

C++

 
  1. Nullable<std::string> getBackground();  
  2.  void setBackground(Nullable<std::string> background);  

Sequences - sequence<T>

The sequence<T> type is a parameterized type whose values are (possibly zero-length) sequences of values of type T.

 

IDL C++
sequence<T> Sequence<T>

 

C++

 
  1. template <typename T>  
  2.  class Sequence  
  3.  {  
  4.   public:  
  5.    Sequence();  
  6.    Sequence(const T* array, unsigned int size);  
  7.    Sequence(std::initializer_list<T> list);  
  8.    Sequence(const Sequence& value);  
  9.    ~Sequence();  
  10.    T operator[](int index) const;  
  11.    T& operator[](int index);  
  12.    unsigned int getLength() const;  
  13.    // snip  
  14.  };  
IDL
  
  1. typedef stylesheets::StyleSheetList StyleSheetList;  
  2.   
  3.   readonly attribute StyleSheetList styleSheets;  

C++

 
  1. typedef Sequence<StyleSheet*> StyleSheetList;  
  2.   
  3.  StyleSheetList getStyleSheets();  

Constants

Constants can be defined in interfaces and exceptions.

IDL

 
  1. interface MediaError {  
  2.    const unsigned short MEDIA_ERR_ABORTED = 1;  
  3.    const unsigned short MEDIA_ERR_NETWORK = 2;  
  4.    const unsigned short MEDIA_ERR_DECODE = 3;  
  5.    const unsigned short MEDIA_ERR_NONE_SUPPORTED = 4;  
  6.  };  

C++

   
  1. class MediaError : public Object {  
  2.    public:  
  3.      static const unsigned short MEDIA_ERR_ABORTED = 1;  
  4.      static const unsigned short MEDIA_ERR_NETWORK = 2;  
  5.      static const unsigned short MEDIA_ERR_DECODE = 3;  
  6.      static const unsigned short MEDIA_ERR_NONE_SUPPORTED = 4;  
  7.    };  

Operations

Each operation defined in the IDL interface will be mapped to one or more member functions in the C++ interface class.

IDL

 
  1. interface CanvasRenderingContext2D {  
  2.    void fillRect(float x, float y, float w, float h);  
  3.  };  

C++

 
  1. class CanvasRenderingContext2D : public Object {  
  2.   public:  
  3.    void fillRect(float x, float y, float w, float h);  
  4.  };  

Optional argument

If the "optional" keyword appears on an operation argument, it indicates that the operation can be invoked by passing values only for the those arguments appearing before the optional argument in the operation’s argument list.

IDL

  
  1. interface ColorCreator {  
  2.     object createColor(float v1, optional float v2, float v3, optional float alpha);  
  3.   };  

 

C++

 
  1. class ColorCreator {  
  2.  public:  
  3.    Object createColor(float v1);  
  4.    Object createColor(float v1, float v2, float v3);  
  5.    Object createColor(float v1, float v2, float v3, float alpha);  
  6.  };    

Variadic operation

If the final argument uses the "..." terminal, it indicates that the operation is variadic, and can be passed zero or more arguments after the regular arguments. In C++, a varying number of arguments are represent as a Variadic value.

Example

IDL

  1. interface IntegerSet {  
  2.   readonly attribute unsigned long cardinality;  
  3.   
  4.   void union(long... ints);  
  5.   void intersection(long... ints);  
  6. };  


C++

 
  1. class IntegerSet {  
  2.  public:  
  3.    unsigned int getCardinality();  
  4.    void union(Variadic<int> ints = Variadic<int>());  
  5.    void intersection(Variadic<int> ints = Variadic<int>());  
  6.  };  
  7.  template <typename T>  
  8.  class Variadic  
  9.  {  
  10. ublic:  
  11.    Variadic();  
  12.    Variadic(const Any* variadic, size_t length);  
  13.    Variadic(std::initializer_list<T> list);  
  14.    T operator[](int index) const;  
  15.    size_t size() const;  
  16.  };  

Operations with no identifier

While it has been agreed to remove unnamed getters/setters at W3C WebApps WG (*), operations with no identifier are still in use in several W3C specifications. In C++, the default operation name as shown in the following table is used for operations with no identifier.

 

IDL C++
getter getElement
setter setElement
creator createElement
deleter deleteElement
stringifier toString

 

Example

IDL

  
  1. interface CanvasPixelArray {  
  2.     readonly attribute unsigned long length;  
  3.     getter octet (in unsigned long index);  
  4.     setter void (in unsigned long index, in octet value);  
  5.   };  

C++

  
  1. class CanvasPixelArray : public Object {  
  2.    public:  
  3.     // CanvasPixelArray  
  4.     unsigned int getLength();  
  5.     unsigned char getElement(unsigned int index);  
  6.     void setElement(unsigned int index, unsigned char value);  
  7.     // snip  
  8.   };  
  9. (*) http://www.w3.org/2009/11/02-webapps-minutes.html#action07  

Attributes

For each attribute defined on the IDL interface, a getter method is declared. For each attribute defined on the IDL interface that is not read only, a corresponding setter method is also declared.

IDL

  
  1. interface CanvasRenderingContext2D {  
  2.     attribute float globalAlpha;  
  3.   };  

C++

 
  1. class CanvasRenderingContext2D : public Object {  
  2.  public:  
  3.    float getGlobalAlpha();  
  4.    void setGlobalAlpha(float globalAlpha);  
  5.  };  

Note: A getter is prefixed with "get", and the setter is prefixed with "set" following the Web IDL Java binding.

Exceptions

An exception is used to declare a type of exception that can be thrown by implementation.

IDL

 

 
  1. exception DOMException {  
  2.    const unsigned short INDEX_SIZE_ERR = 1;  
  3.    const unsigned short DOMSTRING_SIZE_ERR = 2;  
  4.    unsigned short code;  
  5.  };  

C++

  1. struct DOMException {  
  2.    const unsigned short INDEX_SIZE_ERR = 1;  
  3.    const unsigned short DOMSTRING_SIZE_ERR = 2;  
  4.    unsigned short code;  
  5.  };  

 
Note: The use of exceptions can be turned off with esidl.

Implements statements

An implements statement declares the all objects implementing an interface A (the first name) MUST also implement interface B (the second name). In C++, interface members in the mixin interface B are mixed into the primary interface A.

Example

IDL

 
  1. interface ElementTraversal {  
  2.    readonly attribute Element        firstElementChild;  
  3.    readonly attribute Element        lastElementChild;  
  4.    readonly attribute Element        previousElementSibling;  
  5.    readonly attribute Element        nextElementSibling;  
  6.  };  
  7.   
  8.  Element implements ElementTraversal;  
  9.   
  10.  interface Element : Node {  
  11.    // snip  
  12.  };  

C++

 
  1. class Element : public Node  
  2.  {  
  3.   public:  
  4.    // snip  
  5.    Element getFirstElementChild();  
  6.    Element getLastElementChild();  
  7.    Element getPreviousElementSibling();  
  8.    Element getNextElementSibling();  
  9.    // snip  
cf. http://lists.w3.org/Archives/Public/public-script-coord/2010OctDec/0071.html

Extended attributes

[Constructor][NamedConstructor]

If the [Constructor] extended attribute appears on an interface, the corresponding C++ constructor(s) are defined.

IDL

  
  1. [Constructor,  
  2.    Constructor(HTMLFormElement form)]  
  3.   interface FormData {  
  4.     void append(DOMString name, Blob value);  
  5.     void append(DOMString name, DOMString value);  
  6.   };  

C++

 
  1. class FormData : public Object  
  2.  {  
  3.   public:  
  4.    // FormData  
  5.    void append(std::u16string name, file::Blob value);  
  6.    void append(std::u16string name, std::u16string value);  
  7.   
  8.    // [Constructor]  
  9.    FormData();  
  10.    FormData(html::HTMLFormElement form);  
  11.      
  12.    // snip  
  13.  };  

Note: Unlike ECMAScript, you do not have to use the "new" operator to create an instance of the interface with the [Constructor] extended attribute.

[PutForwards]

If the [PutForwards] extended attribute appears on a read only attribute declaration whose type is an object implementing an interface, it indicates that assigning to the attribute will have specific behavior. Namely, the assignment is “forwarded” to the attribute (specified by the extended attribute argument) on the object that is currently referenced by the attribute being assigned to.

IDL

 
  1. interface Name {  
  2.    attribute DOMString full;  
  3.    attribute DOMString family;  
  4.    attribute DOMString given;  
  5.  };  
  6.   
  7.  interface Person {  
  8.    [PutForwards=full] readonly attribute Name name;  
  9.  };  

 

C++

  1. class Person {  
  2. public:  
  3.   Name* getName();  
  4.   void setName(std::string name);  
  5.   // snip  
  6. };  


[Prefix]

See Modules.

[Supplemental]

cf. http://lists.w3.org/Archives/Public/public-webapps/2009JulSep/0528.html

Example

IDL

 
  1. interface Element : Node {  
  2.    readonly attribute DOMString tagName;  
  3.    // snip  
  4.  };  
  5.   
  6.  [Supplemental] interface Element {  
  7.    ClientRectList getClientRects();  
  8.    ClientRect getBoundingClientRect();  
  9.  };  

C++

 
  1. class Element : public Node {  
  2.   public:  
  3.    std::string getTagName();  
  4.    // snip  
  5.    ClientRectList getClientRects();  
  6.    ClientRect getBoundingClientRect();  
  7.  };  

posted on 2012-07-13 19:10  亭子  阅读(672)  评论(0编辑  收藏  举报

导航