Skip to content

Commit

Permalink
add create() in class_list
Browse files Browse the repository at this point in the history
  • Loading branch information
sanuj committed Jul 4, 2016
1 parent 08da61f commit 912ba2c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/interfaces/modular/SGBase.i
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public void readExternal(java.io.ObjectInput in) throws java.io.IOException, jav
#include <shogun/base/Version.h>
#include <shogun/base/Parallel.h>
#include <shogun/base/SGObject.h>
#include <shogun/base/class_list.h>

extern void sg_global_print_message(FILE* target, const char* str);
extern void sg_global_print_warning(FILE* target, const char* str);
Expand Down Expand Up @@ -325,6 +326,7 @@ namespace std {
%include <shogun/io/SGIO.h>
%include <shogun/base/Version.h>
%include <shogun/base/Parallel.h>
%include <shogun/base/class_list.h>

#ifdef SWIGPYTHON
namespace shogun
Expand Down Expand Up @@ -508,3 +510,7 @@ copy_reg._reconstructor=_sg_reconstructor


#endif /* SWIGPYTHON */

using namespace shogun;

%template(createKernel) create<CKernel>;
16 changes: 15 additions & 1 deletion src/shogun/base/class_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define __SG_CLASS_LIST_H__

#include <shogun/lib/config.h>

#include <shogun/base/some.h>
#include <shogun/lib/DataType.h>

namespace shogun {
Expand All @@ -24,6 +24,20 @@ namespace shogun {
*/
CSGObject* new_sgserializable(const char* sgserializable_name,
EPrimitiveType generic);

/** Creates an instance of a class identified by input name
* that is wrapped with a shared pointer like structure @ref Some
*
* @param name name of class
* @param type type for template class
* @return Some object that holds created instance of @ref T
*/
template<class T>
Some<T> create(const char* name,
EPrimitiveType type=PT_NOT_GENERIC)
{
return Some<T>(dynamic_cast<T*>(new_sgserializable(name, type)));
}
}

#endif /* __SG_CLASS_LIST_H__ */
25 changes: 25 additions & 0 deletions tests/unit/base/class_list_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2016 Sanuj Sharma
*/

#include <shogun/base/SGObject.h>
#include <shogun/base/class_list.h>
#include <shogun/kernel/GaussianKernel.h>
#include <gtest/gtest.h>

using namespace shogun;

TEST(class_list, create_gaussian_kernel)
{
const char* class_name = "GaussianKernel";
auto gk = create<CKernel>(class_name);
auto another_gk = some<CGaussianKernel>();

EXPECT_EQ(strcmp(gk->get_name(), class_name), 0);
EXPECT_TRUE(gk->equals(another_gk));
}

0 comments on commit 912ba2c

Please sign in to comment.