TOP

C++ Representation: Translation Rules

Applies to: ASN.1/C++ v7.3-7.3.1

When an ASN.1 specification is processed by the ASN.1/C++ Compiler, C++ code is created that contains C++ representations of the ASN.1 types in the specification. The rules used to create these representations follow, with examples.

  1. A typedef is generated for each simple type reference that can be represented by a primitive C++ type.
    ASN.1 C++
    Age  ::= INTEGER
    typedef OSS_INT32 Age;
  2. A typedef is generated for each type reference that can be represented by an ASN.1/C++ runtime class.
    ASN.1 C++
    Text ::= IA5String
    typedef OssString Text;
  3. A class is generated for each constructed type (SEQUENCE, SET, CHOICE, SEQUENCE OF, or SET OF) that contains the accessors and mutators required by the type components.
    ASN.1 C++
    Personal ::= SEQUENCE {
        married  BOOLEAN,
        age      INTEGER,
        weight   INTEGER OPTIONAL,
        name     IA5String DEFAULT "No name"
        }
    class OSS_PUBLIC Personal   /* SEQUENCE */
    {
    public:
        void * operator new(size_t size);
        void operator delete(void *ptr);
    
        typedef ossBoolean married;
        typedef OSS_INT32 age;
        typedef OSS_INT32 weight;
        typedef OssString name;
    
        static const name & default_name;
    
        static const name& get_default_name();
    
        Personal();
        Personal(const Personal &);
        Personal(married, age, weight, const name &);
        Personal(married, age);
        ~Personal();
    
        Personal & operator = (const Personal &);
        int operator == (const Personal &) const;
        int operator != (const Personal &) const;
    
        married & get_married();
        married get_married() const;
        void set_married(married);
    
        age & get_age();
        age get_age() const;
        void set_age(age);
    
        weight *get_weight();
        const weight *get_weight() const;
        void set_weight(weight);
        int weight_is_present() const;
        void omit_weight();
    
        name *get_name();
        const name *get_name() const;
        void set_name(const name &);
        int name_is_default() const;
        void set_default_name();
    private:
        OSS_UINT32 bit_mask;
        married married_field;
        age age_field;
        weight weight_field;
        name name_field;
    };
  4. Additional methods to transfer ownership are generated for elements of constructed types that have --<POINTER>-- directives applied. Using these methods, you can access or modify an element without a deep copy of the element's information.
    ASN.1 C++
    Personal ::= SEQUENCE {
        married  BOOLEAN,
        age      INTEGER,
        name     IA5String --<POINTER>--
    }
    class OSS_PUBLIC Personal   /* SEQUENCE */
    {
    public:
        void * operator new(size_t size);
        void operator delete(void *ptr);
    
        typedef ossBoolean married;
        typedef OSS_INT32 age;
        typedef OssString name;
    
        Personal();
        Personal(const Personal &);
        Personal(married, age, const name &);
        Personal(married, age, name *);
        ~Personal();
    
        Personal & operator = (const Personal &);
        int operator == (const Personal &) const;
        int operator != (const Personal &) const;
    
        married & get_married();
        married get_married() const;
        void set_married(married);
    
        age & get_age();
        age get_age() const;
        void set_age(age);
    
        name & get_name();
        const name & get_name() const;
        void set_name(const name &);
        void set_name(name *);
        name *release_name();
    private:
        married married_field;
        age age_field;
        name *name_field;
    };
  5. An additional set of accessor/mutator methods is generated when a SET/SEQUENCE/CHOICE type is extensible and the -relaySafe compiler option is specified. Use these methods to get unknown extension additions (probably from a later protocol version) in unencoded form to edit or delete as needed. If -relaySafe is not specified, the extra methods are not generated.
    ASN.1 C++
    Personal ::= SEQUENCE {
        married   BOOLEAN,
        age      INTEGER,
        ...
    }
    C++ (generated with the -relaySafe compiler option)
    class OSS_PUBLIC Personal   /* SEQUENCE */
    {
    public:
        void * operator new(size_t size);
        void operator delete(void *ptr);
    
        typedef ossBoolean married;
        typedef OSS_INT32 age;
    
        Personal();
        Personal(const Personal &);
        Personal(married, age);
        ~Personal();
    
        Personal & operator = (const Personal &);
        int operator == (const Personal &) const;
        int operator != (const Personal &) const;
    
        married & get_married();
        married get_married() const;
        void set_married(married);
    
        age & get_age();
        age get_age() const;
        void set_age(age);
    
        OssExtensions *get_UnknownExt();
        const OssExtensions *get_UnknownExt() const;
        void set_UnknownExt(const OssExtensions &);
        int UnknownExt_is_present() const;
        void omit_UnknownExt();
    private:
        married married_field;
        age age_field;
        OssExtensions *ossUnknownExt_field;
    };
  6. A handler class is generated for each PDU type that can be used on its representation class to encode, decode, print, and so on.
    ASN.1 C++
    Personal ::= CHOICE {
        married   BOOLEAN,
        age      INTEGER
    } --<PDU>--
    class OSS_PUBLIC Personal  : public OssChoice   /* CHOICE */
    {
        . . .
    };
    
    class OSS_PUBLIC Personal_PDU : public ConcretePDU {
    public:
        Personal_PDU();
        void set_data(Personal &);
        Personal *get_data() const;
        . . .
    protected:
        OssTypeIndex get_index() const;
    };
  7. A universal handler class is generated that can be used on any PDU type defined in the specification to encode, decode, print, and so on.
    ASN.1 C++
    ASN.1 (sample.asn)
    Module DEFINITIONS ::= BEGIN
        A ::= INTEGER
        B ::= IA5String
    END
    /* Representation types */
    
    typedef OSS_INT32 A;
    
    typedef OssString B;
    
    /* Universal PDU class */
    
    class OSS_PUBLIC sample_PDU : public UniversalPDU {
    public:
        sample_PDU();
        void set_A(A &);
        A *get_A() const;
        void set_B(B &);
        B *get_B() const;
        . . .
        };
    
    /* Specific PDU classes */
    
    class OSS_PUBLIC A_PDU : public ConcretePDU {
        . . .
    };
    
    class OSS_PUBLIC B_PDU : public ConcretePDU {
        . . .
    };
  8. A control class is generated for use with specification-defined objects in various operations. See <projectname>_Control for more details.
  9. For each value reference, the ASN.1/C++ Compiler generates an initialized const reference to an initialized C++ object that contains the same value. The object that name references is initialized to "John Smith" in the compiler-generated code.
    ASN.1 C++
    NAME ::= IA5String
    name NAME ::= "John Smith"
    typedef OssString NAME;
        . . .
    extern OSS_PUBLIC const OssString& name;
  10. When the generated value is of any C++ integer or enumerated type, the constant is generated completely in the header. The generated constants can then be used as case labels in a switch statement.
    ASN.1 C++
    INT ::= INTEGER
    value INT ::= 777
    typedef OSS_INT32 INT;
        . . .
    const OSS_INT32 value = 777;

This documentation applies to release 7.3 and later of the OSS® ASN.1 Tools for C++.

Copyright © 2024 OSS Nokalva, Inc. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means electronic, mechanical, photocopying, recording or otherwise, without the prior permission of OSS Nokalva, Inc.
Every distributed copy of the OSS® ASN.1 Tools for C++ is associated with a specific license and related unique license number. That license determines, among other things, what functions of the OSS ASN.1 Tools for C++ are available to you.