Allow building as monolithic app (no plugins) for easier developement.
authorYann Dirson <ydirson@altern.org>
Sun, 20 Apr 2008 19:35:15 +0000 (20 21:35 +0200)
committerYann Dirson <ydirson@altern.org>
Sun, 20 Apr 2008 19:35:15 +0000 (20 21:35 +0200)
src/CMakeLists.txt
src/core/variantloader.h
src/variantfactories/builtinvariants.cpp [new file with mode: 0644]
src/variantfactories/builtinvariants.h [new file with mode: 0644]
src/variantfactories/kdepluginvariants.h
src/variantfactory.cpp
src/variants/CMakeLists.txt
src/variants/chess/CMakeLists.txt
src/variants/chess5x5/CMakeLists.txt
src/variants/crazyhouse/CMakeLists.txt
src/variants/dummy.cpp [new file with mode: 0644]

index 8e3ffa3..cda4964 100644 (file)
@@ -1,3 +1,12 @@
+if(MONOLITH)
+  set(BUILTINVARIANTS_SRC variantfactories/builtinvariants.cpp)
+  set(TAGUA_BUILTIN_VARIANTS taguavariants)
+  add_definitions(-DTAGUA_MONOLITH)
+else(MONOLITH)
+  set(BUILTINVARIANTS_SRC )
+  set(TAGUA_BUILTIN_VARIANTS )
+endif(MONOLITH)
+
 set(tagua_SRC
   controllers/editgame.cpp
   controllers/abstract.cpp
@@ -24,6 +33,7 @@ set(tagua_SRC
   entities/icsentity.cpp
   
   variantfactories/kdepluginvariants.cpp
+  ${BUILTINVARIANTS_SRC}
 
   actioncollection.cpp
   agentgroup.cpp
@@ -142,6 +152,8 @@ if(DEBUG_BUILD)
   set(TAGUA_TARGET taguaprivate)
   
   kde4_add_library(taguaprivate SHARED ${tagua_SRC})
+  target_link_libraries(taguaprivate
+    ${TAGUA_BUILTIN_VARIANTS})
   kde4_add_executable(tagua main.cpp)
 else(DEBUG_BUILD)
   set(TAGUA_TARGET tagua)
index 2f0eb59..9b3fc0f 100644 (file)
@@ -24,6 +24,9 @@ public:
     */
   virtual QStringList all() const = 0;
 
+  /**
+    * \return a pointer on the component repository for variant.
+    */
   virtual Repository* getRepository(const QString& variant) = 0;
 };
 
diff --git a/src/variantfactories/builtinvariants.cpp b/src/variantfactories/builtinvariants.cpp
new file mode 100644 (file)
index 0000000..67a7a3c
--- /dev/null
@@ -0,0 +1,72 @@
+#include <QString>
+#include <core/repository.h>
+#include <variant.h>
+#include "builtinvariants.h"
+
+extern "C" Repository* taguachess_initrepo(IVariantLoader*);
+extern "C" Repository* taguachess5x5_initrepo(IVariantLoader*);
+extern "C" Repository* taguacrazyhouse_initrepo(IVariantLoader*);
+
+// FIXME: a KDE plugin willing to reuse a builtin one crashes
+
+/**
+  * Hardcoded definition of builtin variants.
+  */
+static const struct builtin {
+  const char* name;
+  Repository* (*init_func)(IVariantLoader*);
+  const char* theme_proxy;
+} builtins[] = {
+  { "Chess", taguachess_initrepo, "Chess" },
+  { "Chess 5x5", taguachess5x5_initrepo, "Chess" },
+  { "Crazyhouse", taguacrazyhouse_initrepo, "Chess" },
+};
+
+static const struct builtin* const get_builtin(QString name) {
+  for (unsigned i=0; i < sizeof(builtins)/sizeof(builtins[0]); i++)
+    if (name.compare(builtins[i].name, Qt::CaseInsensitive) == 0)
+      return builtins+i;
+  return NULL;
+}
+
+BuiltinVariants::BuiltinVariants() {
+  for (unsigned i=0; i < sizeof(builtins)/sizeof(builtins[0]); i++)
+    m_allvariants << builtins[i].name;
+}
+
+BuiltinVariants& BuiltinVariants::self() {
+  static BuiltinVariants inst;
+  return inst;
+}
+
+Variant* BuiltinVariants::create(const QString& name) {
+  Repository* repo = getRepository(name);
+  if (repo) {
+    const struct builtin* const builtin = get_builtin(name);
+    if (!builtin)
+      kFatal() << "builtin not properly registered" << name;
+    return new Variant(name, repo, builtin->theme_proxy);
+  }
+  return NULL;
+}
+
+QStringList BuiltinVariants::all() const {
+  return m_allvariants;
+}
+
+Repository* BuiltinVariants::getRepository(const QString& variant) {
+  if (m_variants.contains(variant))
+    return m_variants[variant];
+
+  Repository* repo = NULL;
+  const struct builtin* const builtin = get_builtin(variant);
+  if (builtin) {
+    repo = builtin->init_func(this);
+    if (repo)
+      m_variants[builtin->name] = repo;
+    else
+      kError() << "could not load builtin variant" << variant;
+  }
+
+  return repo;
+}
diff --git a/src/variantfactories/builtinvariants.h b/src/variantfactories/builtinvariants.h
new file mode 100644 (file)
index 0000000..7df3fc8
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+  Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
+  Copyright (c) 2008 Yann Dirson <ydirson@altern.org>
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+*/
+
+#ifndef BUILTINVARIANTS_H
+#define BUILTINVARIANTS_H
+
+#include <core/variantloader.h>
+#include <QStringList>
+
+class Repository;
+class Variant;
+
+class BuiltinVariants: public IVariantLoader {
+  QMap<QString,Repository*> m_variants;
+  QStringList m_allvariants;
+public:
+  BuiltinVariants();
+  Variant* create(const QString& name);
+  virtual QStringList all() const;
+  virtual Repository* getRepository(const QString& variant);
+
+  /**
+    * \return the singleton IVariantLoader instance
+    */
+  static BuiltinVariants& self();
+};
+
+#endif // BUILTINVARIANTS_H
index 1a21d6a..7e037e3 100644 (file)
@@ -29,13 +29,12 @@ class KDEPluginVariants : public IVariantLoader {
 public:
   Variant* create(const QString& name);
   virtual QStringList all() const;
+  virtual Repository* getRepository(const QString& variant);
   
   /**
     * \return the singleton IVariantLoader instance
     */
   static KDEPluginVariants& self();
-  
-  virtual Repository* getRepository(const QString& variant);
 };
 
 #endif // KDE_PLUGIN_VARIANTS_H
index 444b6a3..cc25554 100644 (file)
@@ -15,6 +15,9 @@
 #include "foreach.h"
 #include "variant.h"
 #include "variantfactories/kdepluginvariants.h"
+#ifdef TAGUA_MONOLITH
+# include "variantfactories/builtinvariants.h"
+#endif
 
 void load_plugins();
 
@@ -26,16 +29,33 @@ VariantFactory& VariantFactory::self() {
 }
 
 Variant* VariantFactory::create(const QString& name) {
-  Variant* v = KDEPluginVariants::self().create(name);
+  Variant* v = NULL;
+#ifdef TAGUA_MONOLITH
+  if (v == NULL)
+    v = BuiltinVariants::self().create(name);
+#endif
+  if (v == NULL)
+    v = KDEPluginVariants::self().create(name);
+  if (v == NULL)
+    kFatal() << "failed to create variant" << name;
   return v;
 }
 
 QStringList VariantFactory::all() const {
   QStringList l = KDEPluginVariants::self().all();
+#ifdef TAGUA_MONOLITH
+  l << BuiltinVariants::self().all();
+#endif
   return l;
 }
 
 Repository* VariantFactory::getRepository(const QString& variant) {
-  Repository* r = KDEPluginVariants::self().getRepository(variant);
+  Repository* r = NULL;
+#ifdef TAGUA_MONOLITH
+  if (r == NULL)
+    r = BuiltinVariants::self().getRepository(variant);
+#endif
+  if (r == NULL)
+    r = KDEPluginVariants::self().getRepository(variant);
   return r;
 }
index f45132e..8ac4eaf 100644 (file)
@@ -2,3 +2,12 @@ add_subdirectory(chess)
 add_subdirectory(chess5x5)
 add_subdirectory(crazyhouse)
 
+if(MONOLITH)
+kde4_add_library(taguavariants SHARED dummy.cpp)
+target_link_libraries(taguavariants
+  taguachess
+  taguachess5x5
+  taguacrazyhouse
+)
+install(TARGETS taguavariants DESTINATION ${LIB_INSTALL_DIR})
+endif(MONOLITH)
index 6fc9485..76caaae 100644 (file)
@@ -20,7 +20,11 @@ set(taguachess_SRCS
 
 include_directories(${CMAKE_SOURCE_DIR}/src/)
 
+if(MONOLITH)
+kde4_add_library(taguachess SHARED ${taguachess_SRCS})
+else(MONOLITH)
 kde4_add_plugin(taguachess ${taguachess_SRCS})
+endif(MONOLITH)
 
 target_link_libraries(taguachess
   taguacore
index 54f1231..adff377 100644 (file)
@@ -5,7 +5,11 @@ set(taguachess5x5_SRCS
 # we don't need taguachess includes!
 include_directories(${CMAKE_SOURCE_DIR}/src/)
 
+if(MONOLITH)
+kde4_add_library(taguachess5x5 SHARED ${taguachess5x5_SRCS})
+else(MONOLITH)
 kde4_add_plugin(taguachess5x5 ${taguachess5x5_SRCS})
+endif(MONOLITH)
 
 # we don't need to link to taguachess!
 target_link_libraries(taguachess5x5
index 02a891b..bf8528f 100644 (file)
@@ -10,7 +10,11 @@ set(taguacrazyhouse_SRCS
 # we don't need taguachess includes!
 include_directories(${CMAKE_SOURCE_DIR}/src/)
 
+if(MONOLITH)
+kde4_add_library(taguacrazyhouse SHARED ${taguacrazyhouse_SRCS})
+else(MONOLITH)
 kde4_add_plugin(taguacrazyhouse ${taguacrazyhouse_SRCS})
+endif(MONOLITH)
 
 # we don't need to link to taguachess!
 target_link_libraries(taguacrazyhouse
diff --git a/src/variants/dummy.cpp b/src/variants/dummy.cpp
new file mode 100644 (file)
index 0000000..e69de29