-
-
Save chrisdembia/2085086a6705e1d18fa4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Demonstrate use of ClonePtr to allow deep copying of maps containing | |
// abstract objects. | |
#include "Simbody.h" | |
#include <cstdio> | |
#include <iostream> | |
#include <string> | |
#include <map> | |
using namespace SimTK; | |
using namespace std; | |
class AbstractBase { | |
public: | |
AbstractBase(const string& name) : m_name(name) {} | |
virtual ~AbstractBase() {} | |
virtual AbstractBase* clone() const = 0; | |
virtual std::string getType() const = 0; | |
string getNameAndAddress() const { | |
return m_name + "\tis a " + getType() + "@" | |
+ String((size_t)this, "0x%llx"); | |
} | |
private: | |
string m_name; | |
}; | |
class Foo : public AbstractBase { | |
public: | |
Foo(const string& name) : AbstractBase(name) {} | |
Foo* clone() const override {printf("cloning Foo\n"); return new Foo(*this);} | |
string getType() const override {return "Foo";} | |
}; | |
class Bar : public AbstractBase { | |
public: | |
Bar(const string& name) : AbstractBase(name) {} | |
Bar* clone() const override {printf("cloning Bar\n"); return new Bar(*this);} | |
string getType() const override {return "Bar";} | |
}; | |
using Map=map<string, ClonePtr<AbstractBase>>; | |
int main() { | |
Map stuff; | |
stuff["Foo1"] = new Foo("Foo1"); | |
stuff["Foo2"] = new Foo("Foo2"); | |
stuff["MyBar"] = new Bar("MyBar"); | |
stuff["YourBar"] = new Bar("YourBar"); | |
for (auto& p : stuff) | |
cout << p.first << ": " << p.second->getNameAndAddress() << endl; | |
Map copy(stuff); // "copy" should be a deep copy of "stuff" now. | |
for (auto& p : copy) | |
cout << p.first << ": " << p.second->getNameAndAddress() << endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generic CMakeLists.txt for making a Simbody-using executable. | |
# This shows how to use the provided SimbodyConfig.cmake to locate a Simbody | |
# installation on your machine so you can use it from your own code. | |
# You will most likely want to copy some of these lines into your own | |
# CMakeLists.txt rather than use this one verbatim. | |
cmake_minimum_required(VERSION 2.8) | |
project(cloneptr) | |
# List your source and header files here. | |
set(my_source_files cloneptr.cpp) | |
set(my_header_files ) | |
# This depends on SimbodyConfig.cmake being located somewhere predictable | |
# on your machine. If you have installed it somewhere that CMake won't be | |
# able to guess, you'll need to tell find_package where to look. | |
find_package(Simbody REQUIRED) | |
include_directories(${Simbody_INCLUDE_DIR}) | |
link_directories(${Simbody_LIB_DIR}) | |
add_executable(cloneptr ${my_source_files} ${my_header_files}) | |
target_link_libraries(cloneptr ${Simbody_LIBRARIES}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment