0

I wrote the following traits that defines some policy classes:

  namespace memory
  {
    struct on_RAM
    {
      template<typename T>
      using pop_sizes_type = PopulationSizeOnRAMImplementation<T>;

      template<typename T>
      using flow_type = FlowOnRAMImplementation<T>;
    };

    struct on_disk
    {
      template<typename T>
      using pop_sizes_type = PopulationSizeOnDiskImplementation<T>;

      template<typename T>
      using flow_type = FlowOnDiskImplementation<T>;
    };
  } // end namespace memory

I use these traits to configure the behavior of a simulation class: MyClass<memory::on_RAM> or MyClass<memory::on_disk>

Eveything works fine, but I would like users of the resulting programs to be able to pick on or the other alternative without having to modify the source code.

I guess I can ask them to overwrite a default implementation by giving an option to CMake, that could give that to the compiler, that could make some compile-time choice. But I have no idea how to technically implement that. Any suggestion?

WaterFox
  • 850
  • 6
  • 18
  • 2
    `I use these traits to configure the behavior of a simulation class: MyClass or MyClass` `I would like users of my library/programs to be able to pick on or the other` So users will write `MyClass` or `MyClass`, they already can do that, there's nothing for you to do. – KamilCuk Nov 22 '21 at 21:42
  • @KamilCuk What you propose is what I have been doing for a while for my own use and would work for the template library users, but unfortunately, this is not a solution for non-programmers users that would be interested only in ready-to-use programs. I am looking for a solution that would be external to the C++ code so they don't have to mess with it. I edited the question to make it a bit clearer. – WaterFox Nov 22 '21 at 22:00
  • 1
    Actually, there is a little CMake specific in the problem. E.g. you could use preprocessor conditionals (`#ifdef`) in your code for select the class. [That question](https://stackoverflow.com/questions/8564337/how-to-define-a-c-preprocessor-macro-through-the-command-line-with-cmake) tells you how to pass compiler definition in CMake. – Tsyvarev Nov 22 '21 at 22:04
  • @Tsyvarev Wonderful! This is exactly what I was looking for :) Thank you very much! – WaterFox Nov 22 '21 at 22:06
  • 1
    `this is not a solution for non-programmers users that would be interested only in ready-to-use programs` I do believe, that "non-programmer users" will have bigger problems learning two langauges - cmake and c++ - then just C++. Template specialization (`std::vector`) are really basic C++ concept. `so they don't have to mess with it` they don't have to "mess" with _your_ code, they will write `MyClass` in _their_ code. `I am looking for a solution that would be external to the C++` You would have to explain more what kind of solution are you searching for. – KamilCuk Nov 22 '21 at 22:26
  • @KamilCuk You are right I mis-presented the problem. I work in biological sciences, and some people don't know how to open a terminal: what is fine because the core of their job is something unrelated like wetlab or catching butterflies. So I want to simplify things for them :3 So it's OK if they only have to type `cmake -DCMAKE_ON_RAM`, but I lose them if they have to modify l.36 of `library/src/core/context.h`. And I don't want to lose potential users lol they is already only a bunch of them on the whole planet ;) Tsyvarev pointed to the solution I was looking for! But thank you :D – WaterFox Nov 23 '21 at 03:36

0 Answers0