How to encapsulate functions inside a library

I'm working on a project that uses an in-house library, which is also used by other projects. There are some classes and methods in other classes that are not used outside the library itself, is there a design pattern that I could use so those classes and methods are not exposed outside the library? The overall architecture looks something like this (please excuse me for the rough design and lack of detail). NOTES: LibClassB is used in a lot of threads only inside the lib. Library code: LibBaseClassA{ public: static LibBaseClassA* factory(int arg){ if( arg==CONST_VAL1 ){ return new LibSubClassA2(); } return new LibSubClassA1 (); } ~LibBaseClassA(){ LibClassB::getInstance()-remove_observed(this); } virtual int function_a(int direction)=0; int internal_function2(int arg){ do_something(); do_stuff(); } void init_all(int arg1, int arg2); protected: LibBaseClassA(){ LibClassB::getInstance()->add_observed(this); } } LibSubClassA1 : private LibBaseClassA{ public: virtual int function_a(int direction){ init_task_a1(direction); do_task_sca1(); } private: init_task_a1(); do_task_sca1(); } LibSubClassA2 : private LibBaseClassA{ public: virtual int function_a(int direction){ init_task_a2(); do_task_sca2(direction); } private: init_task_a2(); do_task_sca2(); } LibClassB{ public: void transform(LibBaseClassA* pObj){ pObj->internal_function2( this->calculate_somthing() ) } void add_observed(LibBaseClassA* pObj){ m_classA_instances.append(pObj); } void remove_observed(LibBaseClassA* pObj){ m_classA_instances.remove(pObj); } static LibClassB* getInstance(){ static LibClassB instance; return &instance; } private: LibClassB(){ read_hardware_data(); start_thread(&(this->function_thread()); } void function_thread(){ for(obs:m_classA_instances) { transform(obs); } } list m_classA_instances; } Individual projects code: #include int main(int argc, argv**){ list actuators_list; while(!finish){ int option = get_external_input(); if(option>0 && optionfunction_a(option); } } wait(SLEEP_TIME); } }

Feb 12, 2025 - 20:29
 0
How to encapsulate functions inside a library

I'm working on a project that uses an in-house library, which is also used by other projects.

There are some classes and methods in other classes that are not used outside the library itself, is there a design pattern that I could use so those classes and methods are not exposed outside the library?

The overall architecture looks something like this (please excuse me for the rough design and lack of detail). NOTES: LibClassB is used in a lot of threads only inside the lib.

Library code:


LibBaseClassA{
 public:

   static LibBaseClassA* factory(int arg){ 
     if( arg==CONST_VAL1 ){
     return new LibSubClassA2();
     }
   
     return new LibSubClassA1 ();
   }

   ~LibBaseClassA(){
    LibClassB::getInstance()-remove_observed(this);
   }

    virtual int function_a(int direction)=0;
    int internal_function2(int arg){ do_something(); do_stuff(); }
    void init_all(int arg1, int arg2);

  protected:
    LibBaseClassA(){
      LibClassB::getInstance()->add_observed(this);
   }


}

LibSubClassA1 : private LibBaseClassA{
  public:
    virtual int function_a(int direction){ init_task_a1(direction); do_task_sca1(); }

  private:
    init_task_a1(); 
    do_task_sca1();
}

LibSubClassA2 : private LibBaseClassA{
  public:
    virtual int function_a(int direction){ init_task_a2(); do_task_sca2(direction); }

   private:
    init_task_a2(); 
    do_task_sca2();
}

LibClassB{
  
  public:
   void transform(LibBaseClassA*  pObj){
      pObj->internal_function2( this->calculate_somthing()  )
   }
   
   void add_observed(LibBaseClassA*  pObj){
      m_classA_instances.append(pObj);
   }

   void remove_observed(LibBaseClassA*  pObj){
      m_classA_instances.remove(pObj);
   }

   static LibClassB* getInstance(){
      static LibClassB instance;
      return &instance;
   }

  private:
      LibClassB(){
        read_hardware_data();
        start_thread(&(this->function_thread());

      }

   void function_thread(){
     for(obs:m_classA_instances)
     {
       transform(obs);
     }
   }

      list  m_classA_instances;
   

}

Individual projects code:



#include 

int main(int argc, argv**){
   list actuators_list;

  while(!finish){

    int option = get_external_input();
    if(option>0 && option<2 ){
      LibBaseClassA* obj = LibBaseClassA::factory(option);
      actuators_list.append(obj);
    }
    else
    {
       for(obj:actuators_list){
           obj->function_a(option);
       }
    }

    wait(SLEEP_TIME);

  }
}