Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7014701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:33:21+00:00 2026-05-27T22:33:21+00:00

I am developing a programming interface for several devices by various manufacturers. Most manufacturers

  • 0

I am developing a programming interface for several devices by various manufacturers. Most manufacturers typically produce at least a dozen models. Commands and data are sent to the devices as low-level instructions. The problem is that although no two devices support the same set of instructions and there is considerable overlap between the set of instructions that most devices support.

Because the low-level instructions are quirky I plan to wrap them in intuitively-named class methods, so that I don’t have to look up the docs when writing or reading (or debugging) code. In the first version of my design, all methods will belong to a Device class, whose constructor accepts a single parameter that is an enum indicating the model of a device. For example:

  class Device
  {
  public:
       enum Model{ ABC , KLM , XYZ };
       Device( Model _model );        // ctor

       // Commands (encapsulate low-level instructions)
       inline void do_Foo();           // supported by all models
       inline void do_Bar();           // unsupported by 'KLM'
  };

However, in addition, I would like to prevent the command methods from being called if they are not supported by the model with which the Device was initialized. In fact, I would like to generate a compile time error if do_Bar() for example is called for the device model KLM. I’ve ruled out creating a class for each device model because this would involve creating scores of classes.

Thoughts

I’ve considered using the preprocessor directive #error in order to generate compile-time errors using the current device model as a predicate or precondition, although I’m not sure whether the preprocessor #if.. macros support non-constants, such as my device models. In an ideal world a command method would be tagged with the methods that support and are therefore allowed to call it. Yet, and I hope I’m not asking for too much, I would like this to be done as easily as possible, so that adding support for newer devices is relatively simple and doesn’t involve too many (error-prone) edits.

Afterthought: I realize by design may be flawed because all methods should be callable. I imagine that a subset of of valid commands can still be generated for each device using the STL, although I don’t know which STL paradigm (e.g. traits) applies in this case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:33:21+00:00Added an answer on May 27, 2026 at 10:33 pm

    You can’t do compile time decisions on something which is only known at runtime (like a parameter passed to a compiler).
    Therefore you basically have two options:

    1) throw an exception at runtime, when an unsupported method is called

    inline void do_Bar(){
      if(model == KLM) throw runtime_exception("do_bar unsupported by device");
      ...
    }
    

    2) Create many classes, possibly through templates which contain only the appropriate methods. One way of doing that is the following:

      enum Model{ ABC , KLM , XYZ };
      template<Model M>
      class Device {
      public:       
           Device();        // ctor
           // Commands (encapsulate low-level instructions)
           inline void do_Foo();           // supported by all models
           template<Bool Dummy = true>
           inline typename std::enable_if<Dummy && (M != KLM), void>::type do_Bar(); // unsupported by 'KLM'
      };
    

    The template parameter Dummy is needed, since enable_if depends on SFINAE which will only work it if the method itself is a template method and enable_if depends on the template parameter. Since it is defaulted template parameter it doesn’t need to be explicitely mentioned when calling the method, so

    Device<ABC> d;
    d.do_bar();
    

    will still work (so no change in the interface there).

    I used std::enable_if, which is only availible on C++11, if you don’t have that you need to either use boost::enable_if, or write it yourself (its not that hard).

    The second option has the disadvantage that it is not possible to write code, which doesn’t know the underlying model. On the plus side it allows you to mask slight differences in the offered interfaces through partial specialization (or using enable_if) to get different implementations for different models.

    boost::enable_if is different from std::enable_if in that it takes a type as first parameter instead of a boolean. So one could either use boost::enable_if_c, which works just like std::enable_if or use boost::enable_if in conjunction with boost::integral_constant (which is part of Boost Type Traits, so include boost/type_traits.hpp):

    template<Bool B> typename boost::enable_if<boost::integral_constant<bool, B && (M != KLM)>, void>::type do_bar();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a data driven website and quite a lot of programming logic
I'm developing a programming language for which I want to provide a Range data
I'm programming in objective-C for several iPod devices and I was wondering about something.
What are most commonly faced programming scenarios while developing Web/Enterprise Applications? Note: Include common
I'm currently developing a new language for programming in a continuous environment (compare it
I'm developing an operating system and rather than programming the kernel, I'm designing the
I am new to .NET programming. We are a team of 4 members developing
I recently came across Erlang, the programming language, and I've become interested in developing
Background: I have a Java application that many programming clients interface with. Recently, a
In an iPhone app I'm developing, I grab a set of data from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.