Declare variable with unknown type

Džuris

I am reading .wav files in a c++ program.

The .wav format is such that a samples are either uint8_t or int16_t. Is it possible to declare the variable based on the format of the specific file?

One solution would be:

int16_t sample;
if (bits==8)
{
  uint8_t temp8;
  in.read((char*)&temp8,bits);
  sample = (int16_t) temp8;
}
else
{
  in.read((char*)&sample,bits);
}

and I can use sample from now on. However that doesn't seem very handy and in the 8bit case the variable sample is bigger then it's needed.

I believe this could also be done with union but it still would make the variable take up 2 bytes regardless of anything.

What I would actually want is:

if(bits==8)
  uint8_t sample;
else
  int16_t sample;

But with a scope that exceeds the conditional statement.

Flovdis

You can use an abstract base class and a template class to provide different implementations of sample reader. Using the abstract interface, later code doesn't need to know if it handles 8 bit or 16 bit samples.

#include <fstream>
#include <vector>
#include <exception>

class SampleReader
{
public:
    virtual void readSample( std::ifstream &in ) = 0;
};

template<class T>
class SampleReaderT : public SampleReader
{
public:
    virtual void readSample( std::ifstream &in )
    {
        while (!in.eof()) {
            const T value = readOneValue(in);
            if (in) {
                _sample.push_back();
            } else {
                break;
            }
        }
    }

private:
    T readOneValue( std::ifstream &in )
    {
        T inputUnit;
        in.read((char*)&inputUnit,sizeof(T));
    }

private:
    std::vector<T> _sample;
};

typedef SampleReaderT<int8_t> SampleReader8Bit;
typedef SampleReaderT<int16_t> SampleReader16Bit;

int main(int argc, char** argv)
{
    int sampleBits = 8;

    SampleReader *sampleReader = nullptr;
    if (sampleBits == 8) {
        sampleReader = new SampleReader8Bit();
    } else if (sampleBits == 16) {
        sampleReader = new SampleReader16Bit();
    } else {
        throw std::out_of_range("unknown bit number");
    }

    std::ifstream in("test.dat");
    sampleReader->readSample(in);
}

Note the following things in this example:

  • This is just an example, you certainly need to find a better design for your project.
  • There is a base class with pure virtual methods. This base class is a great help to make your code abstract. You just can use a pointer to SampleReader and don't have to know if this is a 8bit or 16bit sample reader.
  • The shown way to read the values is not platform independent. There are platforms with a different byte order.
  • The error handling is missing in this example. Reading a stream can raise errors at any time.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Declare variable with unknown type

From Dev

Unknown system variable using DECLARE

From Dev

Unknown system variable using DECLARE

From Dev

Declare generic variable type

From Dev

Declare an unknown number of lists in a type alias

From Dev

Declare a variable of a table type in postgres

From Dev

Why declare a variable as a wildcard type

From Dev

Set VBScript Variable with Unknown Type

From Dev

Is it possible to declare the type of the variable in Rust for loops?

From Dev

Is it possible to explicitly declare the type of a local variable in PHP?

From Dev

How to declare a variable named 'type' in Play/Scala?

From Dev

swift declare variable of subtype with reference of super type

From Dev

Declare row type variable in PL/pgSQL

From Dev

Declare Typescript global variable as "module" type

From Dev

Must declare the scalar variable @insertValues on table type

From Dev

php Declare a variable by type held in a string

From Dev

Is it possible to declare the type of the variable in Rust for loops?

From Dev

swift declare variable of subtype with reference of super type

From Dev

Must declare the scalar variable @insertValues on table type

From Dev

Declare a variable type depending on a ternary expression

From Dev

Variable was declare with a never-completed type

From Dev

Is it possible to assign data type to unknown variable at runtime

From Dev

Saving variable of unknown subclass type in switch statement

From Dev

Is it possible to assign data type to unknown variable at runtime

From Dev

Call a method with arguments by a variable if argument type is unknown

From Dev

Why declare a variable type p as new type instead of declare it just as type p?

From Dev

Can I use type of a variable to declare another variable in Java?

From Dev

c++ declare variable by size of type instead of type

From Dev

How do I declare a type variable that extends a Parameterised type?

Related Related

  1. 1

    Declare variable with unknown type

  2. 2

    Unknown system variable using DECLARE

  3. 3

    Unknown system variable using DECLARE

  4. 4

    Declare generic variable type

  5. 5

    Declare an unknown number of lists in a type alias

  6. 6

    Declare a variable of a table type in postgres

  7. 7

    Why declare a variable as a wildcard type

  8. 8

    Set VBScript Variable with Unknown Type

  9. 9

    Is it possible to declare the type of the variable in Rust for loops?

  10. 10

    Is it possible to explicitly declare the type of a local variable in PHP?

  11. 11

    How to declare a variable named 'type' in Play/Scala?

  12. 12

    swift declare variable of subtype with reference of super type

  13. 13

    Declare row type variable in PL/pgSQL

  14. 14

    Declare Typescript global variable as "module" type

  15. 15

    Must declare the scalar variable @insertValues on table type

  16. 16

    php Declare a variable by type held in a string

  17. 17

    Is it possible to declare the type of the variable in Rust for loops?

  18. 18

    swift declare variable of subtype with reference of super type

  19. 19

    Must declare the scalar variable @insertValues on table type

  20. 20

    Declare a variable type depending on a ternary expression

  21. 21

    Variable was declare with a never-completed type

  22. 22

    Is it possible to assign data type to unknown variable at runtime

  23. 23

    Saving variable of unknown subclass type in switch statement

  24. 24

    Is it possible to assign data type to unknown variable at runtime

  25. 25

    Call a method with arguments by a variable if argument type is unknown

  26. 26

    Why declare a variable type p as new type instead of declare it just as type p?

  27. 27

    Can I use type of a variable to declare another variable in Java?

  28. 28

    c++ declare variable by size of type instead of type

  29. 29

    How do I declare a type variable that extends a Parameterised type?

HotTag

Archive