473,382 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Types as tags and RTTI for flow control

I'm trying to work out a design for dynamically determining file types, and
for generating new files of a given type. I'm curious to know what others
think of my current strategy. Is it "so einfach wie möglich machen, aber
nicht einfacher" or "Rube Goldberg"?

The first part of this has to do with a technique used in the C++ Standard
Library, so I suspect the purist will not have any objection. It's the
approach used to build the inheritance hierarchy for iterators. Understand
"Role" to mean what role a file has such as header, source, etc. I have
organized my types as follows:

namespace files {
struct FileRole {};
struct CppRole : FileRole{};
struct CppSrcRole : CppRole {};
struct CppHeaderRole: CppRole {};

// create a functor class that takes some kind of descriptive
// statement about a file, and returns role information in the
// form of a subclass of FileRole.

struct RoleRecognizer {
virtual FileRole* operator(const QFileInfo& fileInfo) const = 0;
};

// There is a problem, however. There are no virtual functions in
// any of the types derived from FileRole. The type information is,
// therefore only useful at compiletime. So I do this:
struct FileRole {virtual ~FileRole (){}};
struct CppRole : FileRole{virtual ~CppRole (){}};
struct CppSrcRole : CppRole {virtual ~CppSrcRole (){}};
struct CppHeaderRole: CppRole {virtual ~CppHeaderRole(){}};
}

I can now sniff the return value from RoleRecognizer::operator(fileInfo); to
determin, at runtime, what kind of file I have. A much simpler approach
would be to use an enum such as

enum Role {
FileRole,
CppRole,
CppSrcRole,
CppHeaderRole
};

And then return values of type Role.

There are problems with this approach. For one, I have never like the open
nature of enums. I can check that the value I pass is declared to be of a
given enum type, but I cannot ensure the value is one of those declared in
the enum definition. Enums are therefore, to some extent nondeterministic.
One great advantage to using enums is that they require very little simple
to declare. There is a disadvantage that comes with that simplicity. I
cannot later add functionality to an enum value such as a conversion
operator to std::string.

IIRC, Stroustrup cautions against using RTTI for flow control. There are
places, such as in 3D graphics, where I have seen RTTI used to great
advantage. I intend to use RTTI for flow control when determining what
kind of object to create for an in-memory representation of a file. The
idea is to subclass RoleRecognizer to implement specific strategies for
resolving file roles. For example *.cpp vs *.cc, or extensionless
filesnames with file type meta information on the first line. Such as:

/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield

Is this a path others have headed down, and found problematic?

--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Mar 2 '06 #1
3 1598
Steven T. Hatton wrote:
I'm trying to work out a design for dynamically determining file types,
and for generating new files of a given type. I'm curious to know what
others think of my current strategy. Is it "so einfach wie möglich machen,
aber nicht einfacher" or "Rube Goldberg"?
Pardon me?
IIRC, Stroustrup cautions against using RTTI for flow control. There are
places, such as in 3D graphics, where I have seen RTTI used to great
advantage. I intend to use RTTI for flow control when determining what
kind of object to create for an in-memory representation of a file. The
idea is to subclass RoleRecognizer to implement specific strategies for
resolving file roles. For example *.cpp vs *.cc, or extensionless
filesnames with file type meta information on the first line. Such as:

/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield

Is this a path others have headed down, and found problematic?


How about using a string instead that contains e.g. the name of your file's
mimetype, like "text/x-c++src"?

Mar 3 '06 #2

Steven T. Hatton wrote:

[]
The first part of this has to do with a technique used in the C++ Standard
Library, so I suspect the purist will not have any objection. It's the
approach used to build the inheritance hierarchy for iterators. Understand
"Role" to mean what role a file has such as header, source, etc. I have
organized my types as follows:

namespace files {
struct FileRole {};
struct CppRole : FileRole{};
struct CppSrcRole : CppRole {};
struct CppHeaderRole: CppRole {};

// create a functor class that takes some kind of descriptive
// statement about a file, and returns role information in the
// form of a subclass of FileRole.

struct RoleRecognizer {
virtual FileRole* operator(const QFileInfo& fileInfo) const = 0;
};

// There is a problem, however. There are no virtual functions in
// any of the types derived from FileRole. The type information is,
// therefore only useful at compiletime. So I do this:
struct FileRole {virtual ~FileRole (){}};
struct CppRole : FileRole{virtual ~CppRole (){}};
struct CppSrcRole : CppRole {virtual ~CppSrcRole (){}};
struct CppHeaderRole: CppRole {virtual ~CppHeaderRole(){}};
}

I can now sniff the return value from RoleRecognizer::operator(fileInfo); to
determin, at runtime, what kind of file I have. A much simpler approach
would be to use an enum such as
[]
IIRC, Stroustrup cautions against using RTTI for flow control. There are
places, such as in 3D graphics, where I have seen RTTI used to great
advantage. I intend to use RTTI for flow control when determining what
kind of object to create for an in-memory representation of a file. The


Just dispatch to a virtual function overriden in derived classes of the
returned object, or use visitor pattern.

Mar 3 '06 #3
Rolf Magnus wrote:
Steven T. Hatton wrote:
I'm trying to work out a design for dynamically determining file types,
and for generating new files of a given type. I'm curious to know what
others think of my current strategy. Is it "so einfach wie möglich
machen, aber nicht einfacher" or "Rube Goldberg"?


Pardon me?


"Man soll die Dinge so einfach wie möglich machen, aber nicht noch
einfacher". See the epigraph of TC++PL, Chapter 24.

http://www.rubegoldberg.com/
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield

Is this a path others have headed down, and found problematic?


How about using a string instead that contains e.g. the name of your
file's mimetype, like "text/x-c++src"?


I'm the victim, not the perpetrator. What I'm trying to do is break the
binding between file type determination mechanisms and the mechanisms that
depend on file types. Mimetypes will serve as the lingua Franka between
components, but I have to get there first.
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Mar 6 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Brett C. | last post by:
My thesis, "Localized Type Inference of Atomic Types in Python", was successfully defended today for my MS in Computer Science at the California Polytechnic State University, San Luis Obispo. With...
9
by: Agoston Bejo | last post by:
Hello there, I would like to know what overheads there are to think of when using RTTI. I understand that enabling RTTI increases the sizes of the classes, but not the objects themselves. This...
9
by: Karel Miklav | last post by:
In lots of places in a programm I need to identify type of received messages, so I create them as virtual classes and use RTTI to find their type later. But these are simple messages, often without...
9
by: Alvin Bruney [MVP] | last post by:
Exceptions must not be used to control program flow. I intend to show that this statement is flawed. In some instances, exceptions may be used to control program flow in ways that can lead to...
2
by: Joey | last post by:
I have a web app that uses forms authentication. The app also has a downloads section, and I need to be able to use <location> tags to control access to the downloadable files there (preferably by...
13
by: anil.rita | last post by:
When the user chooses an AV file to play, based upon the type of file, I want to use the default installed media player to play it. I am wondering if this is a good way - any alternatives,...
2
by: Chameleon | last post by:
I know than dynamic_cast check string name of derived to base class and if one of them match, return the pointer of that object or else zero. I suppose, I dynamic_cast instead of strings, checks...
7
by: Bryan | last post by:
If I have two classes derived from the same base class, how can I check if they are the same type? class Base : CObject { public: Base() {}; ~Base() {}; }
15
by: Madhur | last post by:
Hi All, I would like you help me in creating an array of data types. I am interested in look at the the data type which looks like this Array...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.