SOL9 2.0 Class: DnsRecordArrayT

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * DnsRecordArrayT.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
// 2010/1/2
// 2011/1/14 Updated

#pragma once
#include <sol/dns/DnsObject.h>
#include <sol/net/IPv4Address.h>
#include <sol/net/IPv6Address.h>
#include <sol/dns/DnsData.h>
#include <sol/dns/DnsRecordT.h>

namespace SOL {

template <class T> class DnsRecordArrayT :public DnsObject {

private:
    WORD    type;

    size_t    count;

    DnsRecordT<T>* records;

public:
    /**
     * Constructor
     */
    DnsRecordArrayT()
    :type(0),
     count(0),
     records(NULL)
    {

    }

public:
    ~DnsRecordArrayT()
    {
        clear();
    }

public:
    void clear()
    {
        if (this->records) {
            delete [] this->records;
            this->records = NULL;
        }
    }

public:
    WORD getType()
    {
        return this->type;
    }

public:
    size_t getCount()
    {
        return this->count;
    }

public:
    //2011/1/14
    DnsRecordT<T>& getNth(int n) 
    {
        if (this->records) {
            if (n>=0 && n<this->count) {
                return  this->records[n];
            } else {
                throw Exception("DnsDataArrayT::getNth; OutOfIndex");
            }
        } else {
            throw Exception("DnsDataArrayT::getNth; No data.");
        }
    }

public:
    DnsRecordT<T>* alloc(WORD type, size_t count) 
    {
        isValidType(type);

        if (count <1) {
            throw Exception("DnsDataArrayT::alloc; Invalid count");
        }

        this->type = type;
    
        this->count = count;
        this->records = new DnsRecordT<T>[count];

        return this->records;
    }

public:
    bool isValidType(WORD type) 
    {
        bool rc = false;
        if ((type >= DNS_TYPE_A     && type<=DNS_TYPE_OPT) ||
            (type >= DNS_TYPE_ADDRS && type<=DNS_TYPE_ANY)) {
            rc = true;
        } else {
            throw Exception("DnsDataArrayT::isValidType; Invalid type");
        }
        return rc;
    }


public:
    void display()
    {
        if (records && count>0) {
            for (size_t i=0; i<count; i++) {
                records[i].display();
            }
        }
    }
};

}



Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.