Skip to content

Commit d3a8c58

Browse files
author
Clement Champetier
committed
Option: delete Option subclasses
* Option class is not abstract anymore: it represents all possible type of Option (Int, Boolean, Choice...). * OptionType enum manages the different cases of option (Int, Boolean, Choice...). * Option class contains an array of Option: contains sub options in case of Choice or Group. * OptionLoader manages an array of Option (instead of pointers). * OptionChecker doesn't need to include libav / ffmpeg files (thankfully). * Update SConstruct: Options folder doesn't exist anymore.
1 parent 307a9d0 commit d3a8c58

20 files changed

+209
-696
lines changed

app/optionChecker/optionChecker.cpp

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
#include <AvTranscoder/InputFile.hpp>
22
#include <AvTranscoder/DatasStructures/AudioDesc.hpp>
33

4-
#include <AvTranscoder/Options/OptionGroup.hpp>
5-
#include <AvTranscoder/Options/OptionChoice.hpp>
64
#include <AvTranscoder/OptionLoader.hpp>
75

8-
extern "C" {
9-
#ifndef __STDC_CONSTANT_MACROS
10-
#define __STDC_CONSTANT_MACROS
11-
#endif
12-
#include <libavutil/opt.h>
13-
}
14-
156
#include <string>
167
#include <iostream>
178
#include <iomanip>
@@ -32,58 +23,48 @@ int optionChecker( const std::string& inputfilename )
3223
{
3324
std::cout << std::left;
3425
std::cout << "****************************" << std::endl;
35-
std::cout << std::setw(30) << option->getName();
36-
std::cout << ": " << option->getHelp() << std::endl;
37-
std::cout << "Type: " << option->getType() << std::endl;
26+
std::cout << std::setw(30) << option.getName();
27+
std::cout << ": " << option.getHelp() << std::endl;
28+
std::cout << "Type: " << option.getType() << std::endl;
3829

3930
// get default value
40-
int valueInt;
41-
double valueDouble;
42-
std::string valueStr;
43-
bool valueBool;
44-
std::pair<int, int> value2D;
4531

46-
if( option->getType() == "OptionInt" )
32+
if( option.getType() == avtranscoder::TypeInt )
4733
{
48-
std::cout << "DefaultValue: " << option->getDefaultValue( valueInt ) << std::endl;
34+
std::cout << "DefaultValue: " << option.getDefaultValueInt() << std::endl;
4935
}
50-
else if( option->getType() == "OptionBoolean" )
36+
else if( option.getType() == avtranscoder::TypeBool )
5137
{
52-
std::cout << "DefaultValue: " << option->getDefaultValue( valueBool ) << std::endl;
38+
std::cout << "DefaultValue: " << option.getDefaultValueBool() << std::endl;
5339
}
54-
else if( option->getType() == "OptionDouble" )
40+
else if( option.getType() == avtranscoder::TypeDouble )
5541
{
56-
std::cout << "DefaultValue: " << option->getDefaultValue( valueDouble ) << std::endl;
42+
std::cout << "DefaultValue: " << option.getDefaultValueDouble() << std::endl;
5743
}
58-
else if( option->getType() == "OptionRatio" )
44+
else if( option.getType() == avtranscoder::TypeRatio )
5945
{
60-
option->getDefaultValue( value2D );
61-
std::cout << "DefaultValue: " << value2D.first << ", " << value2D.second << std::endl;
46+
std::cout << "DefaultValue: " << option.getDefaultValueRatio().first << ", " << option.getDefaultValueRatio().second << std::endl;
6247
}
63-
else if( option->getType() == "OptionString" )
48+
else if( option.getType() == avtranscoder::TypeString )
6449
{
65-
std::cout << "DefaultValue: " << option->getDefaultValue( valueStr ) << std::endl;
50+
std::cout << "DefaultValue: " << option.getDefaultValueString() << std::endl;
6651
}
67-
else if( option->getType() == "OptionChoice" )
52+
else if( option.getType() == avtranscoder::TypeChoice )
6853
{
69-
std::cout << "DefaultValue: " << option->getDefaultValue( valueDouble ) << std::endl;
70-
71-
avtranscoder::OptionChoice* choice = dynamic_cast<avtranscoder::OptionChoice*>( option );
72-
std::cout << "Nb choices: " << choice->getNbChoices() << std::endl;
73-
std::cout << "Default choice index: " << choice->getDefaultChoiceIndex() << std::endl;
74-
for(size_t i = 0; i < choice->getNbChoices(); ++i )
75-
std::cout << "Choice " << i << ": " << choice->getChoice( i ).first << " // " << choice->getChoice( i ).second << std::endl;
54+
std::cout << "Nb choices: " << option.getNbChilds() << std::endl;
55+
std::cout << "Default choice index: " << option.getDefaultChildIndex() << std::endl;
56+
for(size_t i = 0; i < option.getNbChilds(); ++i )
57+
std::cout << "Choice " << i << ": " <<
58+
option.getChild( i ).getName() << " // " <<
59+
option.getChild( i ).getHelp() << std::endl;
7660
}
77-
else if( option->getType() == "OptionGroup" )
61+
else if( option.getType() == avtranscoder::TypeGroup )
7862
{
79-
std::cout << "DefaultValue: " << option->getDefaultValue( valueInt ) << std::endl;
80-
81-
avtranscoder::OptionGroup* group = dynamic_cast<avtranscoder::OptionGroup*>( option );
82-
std::cout << "Nb choices: " << group->getNbElements() << std::endl;
83-
for(size_t i = 0; i < group->getNbElements(); ++i )
63+
std::cout << "Nb choices: " << option.getNbChilds() << std::endl;
64+
for(size_t i = 0; i < option.getNbChilds(); ++i )
8465
std::cout << "Element " << i << ": " <<
85-
group->getElement( i ).getName() << " // " <<
86-
group->getElement( i ).getDefaultValue( valueBool ) << std::endl;
66+
option.getChild( i ).getName() << " // " <<
67+
option.getChild( i ).getDefaultValueBool() << std::endl;
8768
}
8869
}
8970
}
@@ -109,4 +90,4 @@ int main( int argc, char** argv )
10990
}
11091

11192
std::cout << "end ..." << std::endl;
112-
}
93+
}

src/AvTranscoder/Option.cpp

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,92 @@
11
#include "Option.hpp"
22

3-
#include <stdexcept>
4-
53
namespace avtranscoder
64
{
75

8-
Option::Option( const AVOption& avOption )
9-
: m_name ( avOption.name )
10-
, m_help ( avOption.help ? avOption.help : "" )
11-
, m_unit ( avOption.unit ? avOption.unit : "" )
12-
, m_flags( avOption.flags )
6+
Option::Option( const AVOption& avOption, OptionType type )
7+
: m_avOption( avOption )
8+
, m_type( type )
9+
, m_options()
10+
, m_defaultChildIndex( 0 )
11+
{
12+
13+
}
14+
15+
OptionType Option::getTypeFromAVOption( const char* unit, AVOptionType avType )
16+
{
17+
if( unit && avType == AV_OPT_TYPE_FLAGS )
18+
return TypeGroup;
19+
else if( unit && avType == AV_OPT_TYPE_INT )
20+
return TypeChoice;
21+
else if( unit && avType == AV_OPT_TYPE_CONST )
22+
return TypeChild;
23+
24+
switch( avType )
25+
{
26+
case AV_OPT_TYPE_FLAGS:
27+
{
28+
return TypeBool;
29+
}
30+
case AV_OPT_TYPE_INT:
31+
case AV_OPT_TYPE_INT64:
32+
{
33+
return TypeInt;
34+
}
35+
case AV_OPT_TYPE_DOUBLE:
36+
case AV_OPT_TYPE_FLOAT:
37+
{
38+
return TypeDouble;
39+
}
40+
case AV_OPT_TYPE_STRING:
41+
case AV_OPT_TYPE_BINARY:
42+
{
43+
return TypeString;
44+
}
45+
case AV_OPT_TYPE_RATIONAL:
46+
{
47+
return TypeRatio;
48+
}
49+
default:
50+
{
51+
return TypeUnknown;
52+
}
53+
}
54+
return TypeUnknown;
55+
}
56+
57+
OptionType Option::getType() const
58+
{
59+
return m_type;
60+
}
61+
62+
bool Option::getDefaultValueBool() const
1363
{
64+
return m_avOption.default_val.i64;
1465
}
1566

16-
int Option::getDefaultValue( int& value ) const
67+
int Option::getDefaultValueInt() const
1768
{
18-
throw std::runtime_error( "Wrong access to getDefaultValue (int)." );
69+
return m_avOption.default_val.i64;
1970
}
2071

21-
double Option::getDefaultValue( double& value ) const
72+
double Option::getDefaultValueDouble() const
2273
{
23-
throw std::runtime_error( "Wrong access to getDefaultValue (double)." );
74+
return m_avOption.default_val.dbl;
2475
}
2576

26-
std::string& Option::getDefaultValue( std::string& value ) const
77+
std::string Option::getDefaultValueString() const
2778
{
28-
throw std::runtime_error( "Wrong access to getDefaultValue (char*)." );
79+
return std::string( m_avOption.default_val.str ? m_avOption.default_val.str : "" );
2980
}
3081

31-
bool Option::getDefaultValue( bool& value ) const
82+
std::pair<int, int> Option::getDefaultValueRatio() const
3283
{
33-
throw std::runtime_error( "Wrong access to getDefaultValue (bool)." );
84+
return std::pair<int, int>( m_avOption.default_val.q.num, m_avOption.default_val.q.den );
3485
}
3586

36-
std::pair<int, int>& Option::getDefaultValue( std::pair<int, int>& value ) const
87+
void Option::appendChild( const Option& child )
3788
{
38-
throw std::runtime_error( "Wrong access to getDefaultValue (std::pair<int, int>)." );
89+
m_options.push_back( child );
3990
}
4091

4192
}

src/AvTranscoder/Option.hpp

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,78 @@ extern "C" {
99
}
1010

1111
#include <vector>
12-
#include <bitset>
1312
#include <string>
1413
#include <utility> //pair
1514

1615
namespace avtranscoder
1716
{
1817

18+
enum OptionType {
19+
TypeBool,
20+
TypeInt,
21+
TypeDouble,
22+
TypeString,
23+
TypeRatio,
24+
TypeChoice,
25+
TypeGroup,
26+
27+
TypeChild, // Option which brelongs to Choice or Group
28+
TypeUnknown
29+
};
30+
1931
/**
20-
* @brief Abstract class to wrap AVOption.
21-
* Subclasses implement specific AVOption: int, boolean...
32+
* @brief Wrapper of AVOption.
33+
* Get its type to know what the option is about: Int, Double, Ratio, Choice...
34+
* Parse its array of options to get the potential childs (Choice and Group).
2235
*/
2336
class Option
24-
{
37+
{
38+
public:
39+
static OptionType getTypeFromAVOption( const char* unit, AVOptionType avType );
40+
2541
public:
26-
Option( const AVOption& avOption );
27-
virtual ~Option() {}
28-
29-
virtual std::string getType() const = 0;
30-
31-
virtual int getDefaultValue( int& value ) const;
32-
virtual double getDefaultValue( double& value ) const;
33-
virtual std::string& getDefaultValue( std::string& value ) const;
34-
virtual bool getDefaultValue( bool& value ) const;
35-
virtual std::pair<int, int>& getDefaultValue( std::pair<int, int>& value ) const;
36-
37-
std::string& getName() { return m_name; }
38-
std::string& getHelp() { return m_help; }
39-
std::string& getUnit() { return m_unit; }
40-
41-
bool isEncodingOpt(){ return m_flags & AV_OPT_FLAG_ENCODING_PARAM; }
42-
bool isDecodingOpt(){ return m_flags & AV_OPT_FLAG_DECODING_PARAM; }
43-
bool isAudioOpt(){ return m_flags & AV_OPT_FLAG_AUDIO_PARAM; }
44-
bool isVideoOpt(){ return m_flags & AV_OPT_FLAG_VIDEO_PARAM; }
45-
bool isSubtitleOpt(){ return m_flags & AV_OPT_FLAG_SUBTITLE_PARAM; }
46-
47-
protected:
48-
std::string m_name;
49-
std::string m_help;
50-
std::string m_unit;
51-
int m_flags;
42+
Option( const AVOption& avOption, OptionType type );
43+
~Option() {}
44+
45+
OptionType getType() const;
46+
const std::string getName() { return std::string( m_avOption.name ? m_avOption.name : "" ); }
47+
const std::string getHelp() { return std::string( m_avOption.help ? m_avOption.help : "" ); }
48+
const std::string getUnit() { return std::string( m_avOption.unit ? m_avOption.unit : "" ); }
49+
50+
// flags
51+
int getFlags() { return m_avOption.flags; }
52+
bool isEncodingOpt(){ return m_avOption.flags & AV_OPT_FLAG_ENCODING_PARAM; }
53+
bool isDecodingOpt(){ return m_avOption.flags & AV_OPT_FLAG_DECODING_PARAM; }
54+
bool isAudioOpt(){ return m_avOption.flags & AV_OPT_FLAG_AUDIO_PARAM; }
55+
bool isVideoOpt(){ return m_avOption.flags & AV_OPT_FLAG_VIDEO_PARAM; }
56+
bool isSubtitleOpt(){ return m_avOption.flags & AV_OPT_FLAG_SUBTITLE_PARAM; }
57+
58+
// default value
59+
bool getDefaultValueBool() const;
60+
int getDefaultValueInt() const;
61+
double getDefaultValueDouble() const;
62+
std::string getDefaultValueString() const;
63+
std::pair<int, int> getDefaultValueRatio() const;
64+
65+
// array of childs
66+
bool hasChild() { return ! m_options.empty(); }
67+
std::vector<Option> getChilds() { return m_options; }
68+
Option& getChild( size_t index ) { return m_options.at( index ); }
69+
size_t getNbChilds() { return m_options.size(); }
70+
int getDefaultChildIndex() const { return m_defaultChildIndex; }
71+
void setDefaultChildIndex( size_t index ) { m_defaultChildIndex = index; }
72+
void appendChild( const Option& child );
73+
74+
private:
75+
AVOption m_avOption;
76+
OptionType m_type;
77+
78+
/**
79+
* If the option corresponds to a Choice or a Group, it can contain childs,
80+
* which are also options.
81+
*/
82+
std::vector<Option> m_options;
83+
size_t m_defaultChildIndex;
5284
};
5385

5486
}

0 commit comments

Comments
 (0)