factory.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef __DATA_SOURCE_FACTORY_H
  15. #define __DATA_SOURCE_FACTORY_H 1
  16. //#include <boost/noncopyable.hpp>
  17. //#include <boost/shared_ptr.hpp>
  18. //#include <exceptions/exceptions.h>
  19. #include <datasrc/client.h>
  20. #include <exceptions/exceptions.h>
  21. #include <cc/data.h>
  22. namespace isc {
  23. namespace datasrc {
  24. typedef DataSourceClient* ds_creator(isc::data::ConstElementPtr config);
  25. typedef void ds_destructor(DataSourceClient* instance);
  26. class DLHolder {
  27. public:
  28. DLHolder(const std::string& name);
  29. ~DLHolder();
  30. void* getSym(const char* name);
  31. private:
  32. const std::string ds_name_;
  33. void *ds_lib_;
  34. };
  35. class DataSourceClientContainer {
  36. public:
  37. DataSourceClientContainer(const std::string& type,
  38. isc::data::ConstElementPtr config);
  39. ~DataSourceClientContainer();
  40. DataSourceClient& getInstance() { return *instance_; }
  41. private:
  42. DataSourceClient* instance_;
  43. ds_destructor* destructor_;
  44. DLHolder ds_lib_;
  45. };
  46. /// \brief Raised if the given config contains bad data
  47. ///
  48. /// Depending on the datasource type, the configuration may differ (for
  49. /// instance, the sqlite3 datasource needs a database file).
  50. class DataSourceConfigError : public isc::Exception {
  51. public:
  52. DataSourceConfigError(const char* file, size_t line, const char* what) :
  53. isc::Exception(file, line, what) {}
  54. };
  55. /// \brief Create a datasource instance
  56. ///
  57. /// This function is a fixed generator for datasource instances of all types.
  58. ///
  59. /// Currently, the different types are hardcoded in the implementation of this
  60. /// function. However, we plan on making it more flexible, possibly through
  61. /// th
  62. ///
  63. /// \note This function returns a raw pointer. The caller is expected to
  64. /// delete this pointer again. We don't return any specific smart
  65. /// pointer for flexibility. However, we highly advice that the
  66. /// return value of this function is directly put into a shared or
  67. /// scoped pointer.
  68. ///
  69. /// \exception DataSourceConfigError if the given configuration values are
  70. /// bad for the given datasource type
  71. DataSourceClient*
  72. createDataSourceClient(const std::string& type,
  73. isc::data::ConstElementPtr config);
  74. }
  75. }
  76. #endif // DATA_SOURCE_FACTORY_H
  77. // Local Variables:
  78. // mode: c++
  79. // End: